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: ♻️ Refactor DataFramePlot methods to improve clarity #1639

Merged
merged 2 commits into from
Oct 14, 2024

Conversation

Anselmoo
Copy link
Owner

@Anselmoo Anselmoo commented Oct 13, 2024

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?

Summary by Sourcery

Refactor the DataFramePlot class to enhance code clarity by breaking down the plot_2dataframes method into smaller, more manageable methods, and improve documentation for better understanding of the class's functionality.

Enhancements:

  • Refactor the DataFramePlot class to improve code clarity and maintainability by splitting the plot_2dataframes method into smaller, more focused methods.
  • Improve the docstrings for methods in the DataFramePlot class to provide clearer descriptions of their functionality and arguments.
  • Standardize the terminology in the DataFramePlot class by changing 'dataframe' to 'data frame' in comments and docstrings.

Copy link

semanticdiff-com bot commented Oct 13, 2024

Review changes with SemanticDiff.

Analyzed 1 of 1 files.

Overall, the semantic diff is 17% smaller than the GitHub diff.

Filename Status
✔️ spectrafit/plugins/notebook.py 16.59% smaller

Copy link
Contributor

sourcery-ai bot commented Oct 13, 2024

Reviewer's Guide by Sourcery

This pull request refactors the DataFramePlot class in the spectrafit/plugins/notebook.py file to improve code clarity and maintainability. The changes include breaking down large methods into smaller, more focused ones, improving naming conventions, and enhancing code readability.

Class diagram for refactored DataFramePlot methods

classDiagram
    class DataFramePlot {
        +plot_2dataframes(args_plot: PlotAPI, df_1: pd.DataFrame, df_2: Optional[pd.DataFrame])
        +plot_dataframe(args_plot: PlotAPI, df: pd.DataFrame)
        +plot_global_fit(args_plot: PlotAPI, df: pd.DataFrame)
        +plot_metric(args_plot: PlotAPI, df_metric: pd.DataFrame, bar_criteria: Union[str, List[str]], line_criteria: Union[str, List[str]])
        +update_layout_axes(fig: Figure, args_plot: PlotAPI, height: int)
        +title_text(name: str, unit: Optional[str]) str
        +get_minor(args_plot: PlotAPI) Dict[str, Union[str, bool]]
        -_plot_single_dataframe(args_plot: PlotAPI, df: pd.DataFrame) Figure
        -_plot_two_dataframes(args_plot: PlotAPI, df_1: pd.DataFrame, df_2: pd.DataFrame) Figure
        -_create_residual_plot(df: pd.DataFrame, args_plot: PlotAPI) Figure
        -_create_fit_plot(df: pd.DataFrame, args_plot: PlotAPI) Figure
        -_update_plot_layout(fig: Figure, args_plot: PlotAPI, df_2_provided: bool)
    }
Loading

File-Level Changes

Change Details Files
Refactored the plot_2dataframes method
  • Split the method into smaller, more focused methods
  • Created _plot_single_dataframe and _plot_two_dataframes methods
  • Introduced _create_residual_plot and _create_fit_plot helper methods
  • Added _update_plot_layout method for common layout updates
spectrafit/plugins/notebook.py
Improved code readability and consistency
  • Updated docstrings for better clarity
  • Renamed 'dataframe' to 'data frame' in comments and docstrings
  • Simplified complex dictionary comprehensions
  • Replaced dict() constructor with dictionary literals
spectrafit/plugins/notebook.py
Enhanced the plot_global_fit method
  • Simplified the loop for creating subplots
  • Improved variable naming for better understanding
spectrafit/plugins/notebook.py
Optimized the plot_metric method
  • Separated the creation of bar and line plots
  • Improved trace addition to the figure
spectrafit/plugins/notebook.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions bot added the python Pull requests that update Python code label Oct 13, 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.

Hey @Anselmoo - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

color_discrete_sequence=[args_plot.color.residual],
)

def _create_fit_plot(self, df: pd.DataFrame, args_plot: PlotAPI) -> Figure:
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Extract color and line dash mapping logic

The color and line dash mapping logic in this method could be extracted into a separate method. This would reduce code duplication and make it easier to maintain and update these mappings in the future.

def _create_fit_plot(self, df: pd.DataFrame, args_plot: PlotAPI) -> Figure:
    """Create the fit plot."""
    color_dash_mapping = self._get_color_dash_mapping(args_plot)
    return self._plot_fit(df, args_plot, color_dash_mapping)

def _get_color_dash_mapping(self, args_plot: PlotAPI) -> dict:
    """Get color and line dash mapping."""
    return {
        'color': args_plot.color,
        'dash': args_plot.dash
    }

Copy link

codecov bot commented Oct 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (9b112a4) to head (b8402d1).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1639   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           44        44           
  Lines         4468      4492   +24     
=========================================
+ Hits          4468      4492   +24     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
spectrafit/plugins/notebook.py 100.00% <100.00%> (ø)

Copy link

@Anselmoo Anselmoo merged commit f5555e1 into main Oct 14, 2024
49 checks passed
@Anselmoo Anselmoo deleted the feature/notebook-refactor branch October 14, 2024 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Pull requests that update Python code size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant