-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: ♻️ Cleanup code into seperate functions #1183
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -410,44 +410,82 @@ class CIReport: | |
def __init__( | ||
self, | ||
ci: Dict[str, List[Tuple[float, float]]], | ||
with_offset: Optional[bool] = True, | ||
ndigits: Optional[int] = 5, | ||
with_offset: bool = True, | ||
ndigits: int = 5, | ||
best_tol: float = 1.0e-2, | ||
): | ||
"""Initialize the Report object. | ||
|
||
Args: | ||
ci (Dict[str, List[Tuple[float, float]]]): The confidence intervals for | ||
the parameters. | ||
with_offset (bool, optional): Whether to include an offset in the report. | ||
with_offset (bool): Whether to include an offset in the report. | ||
Defaults to True. | ||
ndigits (int, optional): The number of digits to round the report values to. | ||
ndigits (int): The number of digits to round the report values to. | ||
Defaults to 5. | ||
best_tol (float): The tolerance for the best value. | ||
Defaults to 1.0e-2. | ||
""" | ||
self.ci = ci | ||
self.with_offset = with_offset | ||
self.ndigits = ndigits | ||
self.best_tol = best_tol | ||
|
||
self.df = pd.DataFrame() | ||
|
||
def convp(self, x: Tuple[float, float], bound_type: str) -> str: | ||
"""Convert the confidence interval to a string.""" | ||
return "BEST" if abs(x[0]) < 1.0e-2 else f"{x[0] * 100:.2f}% - {bound_type}" | ||
"""Convert the confidence interval to a string. | ||
|
||
Args: | ||
x (Tuple[float, float]): The confidence interval. | ||
bound_type (str): The type of the bound. | ||
|
||
Returns: | ||
str: The confidence interval as a string. | ||
""" | ||
return ( | ||
"BEST" if abs(x[0]) < self.best_tol else f"{x[0] * 100:.2f}% - {bound_type}" | ||
) | ||
|
||
def calculate_offset(self, row: List[Tuple[float, float]]) -> float: | ||
"""Calculate the offset for a row. | ||
|
||
Args: | ||
row (List[Tuple[float, float]]): The row to calculate the offset for. | ||
|
||
Returns: | ||
float: The offset for the row. | ||
""" | ||
offset = 0.0 | ||
if self.with_offset: | ||
for cval, val in row: | ||
if abs(cval) < (self.best_tol or 0.0): | ||
offset = val | ||
return offset | ||
|
||
def create_report_row( | ||
self, name: str, row: List[Tuple[float, float]], offset: float | ||
) -> None: | ||
"""Create a row for the report. | ||
|
||
Args: | ||
name (str): The name of the row. | ||
row (List[Tuple[float, float]]): The row to create the report for. | ||
offset (float): The offset for the row. | ||
""" | ||
for i, (cval, val) in enumerate(row): | ||
sval = val if cval < self.best_tol else val - offset | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (llm): The condition |
||
bound_type = "LOWER" if i < len(row) / 2 else "UPPER" | ||
self.report.setdefault(self.convp((cval, val), bound_type), {})[name] = sval | ||
|
||
def __call__(self) -> None: | ||
"""Generate the Confidence report as a table.""" | ||
report: Dict[str, Dict[str, float]] = {} | ||
|
||
self.report: Dict[str, Dict[str, float]] = {} | ||
for name, row in self.ci.items(): | ||
offset = 0.0 | ||
if self.with_offset: | ||
for cval, val in row: | ||
if abs(cval) < 1.0e-2: | ||
offset = val | ||
for i, (cval, val) in enumerate(row): | ||
sval = val if cval < 1.0e-2 else val - offset | ||
bound_type = "LOWER" if i < len(row) / 2 else "UPPER" | ||
report.setdefault(self.convp((cval, val), bound_type), {})[name] = sval | ||
self.df = pd.DataFrame(report) | ||
self.tabulate(df=self.df) | ||
offset = self.calculate_offset(row) | ||
self.create_report_row(name, row, offset) | ||
|
||
self.tabulate(df=pd.DataFrame(self.report)) | ||
|
||
def tabulate(self, df: pd.DataFrame) -> None: | ||
"""Print the Confidence report as a table.""" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (llm): Using
self.best_tol
in theconvp
method to determine the 'BEST' label dynamically is a significant improvement. It makes the method more adaptable to different precision requirements. However, ensure that the documentation or comments clearly explain the impact ofbest_tol
on the 'BEST' label determination to avoid any confusion for future maintainers or users.