Skip to content
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 report and test #1182

Closed
wants to merge 2 commits into from
Closed

refactor: 🌱 cleanup report and test #1182

wants to merge 2 commits into from

Conversation

Anselmoo
Copy link
Owner

Also add best_tol parameter to CIReport class

All PR-Submissions:


  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open
    Pull Requests for the same
    update/change?

New ✨✨ Feature-Submissions:


  • Does your submission pass tests?
  • Have you lint your code locally prior to submission? Fixed:
  • This PR is for a new feature, not a bug fix.

Changes to ⚙️ Core-Features:


  • Have you added an explanation of what your changes do and why you'd like
    us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully run tests with your changes locally?

@Anselmoo Anselmoo enabled auto-merge (squash) February 18, 2024 11:49
@github-actions github-actions bot added testing Improve testing & coverage maintenance Maintenance & Security dependencies Pull requests that update a dependency file python Pull requests that update Python code root labels Feb 18, 2024
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Type: Enhancement

PR Summary: This pull request introduces a new class, CIReport, to generate and print a report of confidence intervals for a given set of parameters. It refactors the existing reporting functionality by removing direct calls to the tabulate function and instead utilizing a more structured approach to generate the report. Additionally, it introduces a best_tol parameter to the CIReport class to specify the tolerance for determining the best value.

Decision: Comment

📝 Type: 'Enhancement' - not supported yet.
  • Sourcery currently only approves 'Typo fix' PRs.
✅ Issue addressed: this change correctly addresses the issue or implements the desired feature.
No details provided.
📝 Complexity: the changes are too large or complex for Sourcery to approve.
  • Unsupported files: the diff contains files that Sourcery does not currently support during reviews.

General suggestions:

  • Consider consolidating the documentation for class constructors to avoid redundancy and ensure consistency across the codebase.
  • Review the handling of default values and conditional logic within methods to ensure clarity and maintainability.
  • Evaluate the coupling between the new CIReport class and existing printing functionality to enhance flexibility and adaptability for future changes.
spectrafit/report.py: The code change introduces unnecessary complexity through additional methods and parameters, impacting maintainability.

While the introduction of best_tol and the refactoring into methods like calculate_offset and create_report_row aim to enhance modularity and readability, it inadvertently increases the overall complexity of the code. This complexity comes from additional parameters, increased cognitive load due to jumping between methods to follow the logic, and more conditional checks that could potentially introduce subtle bugs.

A more streamlined approach could be to minimize the number of methods and keep the logic more localized within the __call__ method. This would reduce the cognitive load and make the code easier to follow and maintain. Here's a simplified version that incorporates best_tol while aiming to keep the code concise:

class Report:
    def __init__(self, ci, with_offset=True, ndigits=5, best_tol=1.0e-2):
        self.ci = ci
        self.with_offset = with_offset
        self.ndigits = ndigits
        self.best_tol = best_tol
        self.df = pd.DataFrame()

    def __call__(self):
        report = {}
        for name, row in self.ci.items():
            offset = 0.0
            if self.with_offset:
                for cval, val in row:
                    if abs(cval) < self.best_tol:
                        offset = val
                        break  # Assuming we only need the first match

            for i, (cval, val) in enumerate(row):
                sval = val if cval < self.best_tol else val - offset
                bound_type = "LOWER" if i < len(row) / 2 else "UPPER"
                conv_key = "BEST" if abs(cval) < self.best_tol else f"{cval * 100:.2f}% - {bound_type}"
                report.setdefault(conv_key, {})[name] = sval

        self.df = pd.DataFrame(report)
        self.tabulate(self.df)

    def tabulate(self, df):
        PrintingResults.print_tabulate_df(df=df, floatfmt=f".{self.ndigits}f")

This version aims to balance between modularity and simplicity, making the code easier to understand and maintain.

Thanks for using Sourcery. We offer it for free for open source projects and would be very grateful if you could help us grow. If you like it, would you consider sharing Sourcery on your favourite social media? ✨

Share Sourcery

Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

Defaults to 5.
"""

def __init__(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): The CIReport class constructor's documentation repeats the argument descriptions from the class docstring. Consider removing these from the constructor docstring to avoid redundancy and potential discrepancies in future updates.

float: The offset for the row.
"""
offset = 0.0
if self.with_offset:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): The check for self.with_offset inside calculate_offset method implies that if with_offset is False, the offset will always be 0.0. This behavior is correct but consider if there's a more explicit way to handle the offset calculation to improve readability and maintainability.


self.tabulate(df=pd.DataFrame(self.report))

def tabulate(self, df: pd.DataFrame) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): The tabulate method in CIReport class is tightly coupled with the PrintingResults.print_tabulate_df static method. Consider if there's a way to make this method more flexible or generalized, in case the printing or formatting requirements change in the future.

Copy link

codecov bot commented Feb 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c8d0b27) 100.00% compared to head (c0f7c82) 100.00%.

❗ Current head c0f7c82 differs from pull request most recent head f839fff. Consider uploading reports for the commit f839fff to get more accurate results

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1182   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           44        44           
  Lines         4462      4467    +5     
=========================================
+ Hits          4462      4467    +5     
Files Coverage Δ
spectrafit/report.py 100.00% <100.00%> (ø)
spectrafit/test/test_report.py 100.00% <100.00%> (ø)

Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@Anselmoo Anselmoo closed this Feb 18, 2024
auto-merge was automatically disabled February 18, 2024 11:58

Pull request was closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file maintenance Maintenance & Security python Pull requests that update Python code root testing Improve testing & coverage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant