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

Refactoring of the graph creators #171

Merged
merged 11 commits into from
Nov 24, 2024
Merged

Refactoring of the graph creators #171

merged 11 commits into from
Nov 24, 2024

Conversation

Frix-x
Copy link
Owner

@Frix-x Frix-x commented Nov 23, 2024

This is a big code change in order to:

  1. Implement a Factory Design pattern to be cleaner and ease the addition of future new creators (if needed)
  2. Split the computation from the plotting functions. This also allowed to reduce a lot of code duplication in the plotting functions
  3. Factorized the Klipper commands a bit
  4. Re-organized the figures and graphs to make them more aligned together

Copy link

sourcery-ai bot commented Nov 23, 2024

Reviewer's Guide by Sourcery

This pull request implements a major refactoring of the graph creators in the Shake&Tune project, focusing on three main aspects: introducing a Factory pattern for graph creators, separating computation from plotting logic, and improving code organization. The changes make the codebase more maintainable and extensible while reducing code duplication.

Class diagram for GraphCreator and its subclasses

classDiagram
    class GraphCreator {
        +ShakeTuneConfig config
        +String graph_date
        +String version
        +String type
        +Path folder
        +Plotter plotter
        +String custom_filepath
        +void _save_figure(Figure fig, MeasurementsManager measurements_manager, Optional~String~ axis_label)
        +String get_type()
        +void override_output_target(String filepath)
        +void create_graph(MeasurementsManager measurements_manager)
        +void clean_old_files(int keep_results)
    }
    class VibrationsGraphCreator {
        +Optional~String~ kinematics
        +Optional~float~ accel
        +Optional~List~MotorsConfigParser~ motors
        +void configure(String kinematics, float accel, MotorsConfigParser motor_config_parser)
        +void create_graph(MeasurementsManager measurements_manager)
    }
    class ShaperGraphCreator {
        +Optional~float~ max_smoothing
        +Optional~float~ scv
        +Optional~float~ accel_per_hz
        +void configure(float max_smoothing, float scv, float accel_per_hz)
        +void create_graph(MeasurementsManager measurements_manager)
    }
    class StaticGraphCreator {
        +Optional~float~ freq
        +Optional~float~ duration
        +Optional~float~ accel_per_hz
        +void configure(float freq, float duration, float accel_per_hz)
        +void create_graph(MeasurementsManager measurements_manager)
    }
    GraphCreator <|-- VibrationsGraphCreator
    GraphCreator <|-- ShaperGraphCreator
    GraphCreator <|-- StaticGraphCreator
    GraphCreator : +registry
    GraphCreator : +register(String graph_type)
    GraphCreator : +void _save_figure(Figure fig, MeasurementsManager measurements_manager, Optional~String~ axis_label)
    GraphCreator : +String get_type()
    GraphCreator : +void override_output_target(String filepath)
    GraphCreator : +void create_graph(MeasurementsManager measurements_manager)
    GraphCreator : +void clean_old_files(int keep_results)
Loading

Class diagram for VibrationGraphComputation and related classes

classDiagram
    class VibrationGraphComputation {
        +List~Measurement~ measurements
        +String kinematics
        +float accel
        +float max_freq
        +String st_version
        +List~String~ motors
        +compute()
        +_compute_motor_profiles()
        +_compute_dir_speed_spectrogram()
        +_compute_angle_powers()
        +_compute_speed_powers()
        +_filter_and_split_ranges()
        +_compute_symmetry_analysis()
        +_extract_angle_and_speed()
    }
    class Measurement {
        +String name
        +List~float~ samples
    }
    VibrationGraphComputation o-- Measurement
    VibrationGraphComputation : +List~Measurement~ measurements
    VibrationGraphComputation : +String kinematics
    VibrationGraphComputation : +float accel
    VibrationGraphComputation : +float max_freq
    VibrationGraphComputation : +String st_version
    VibrationGraphComputation : +List~String~ motors
    VibrationGraphComputation : +compute()
    VibrationGraphComputation : +_compute_motor_profiles()
    VibrationGraphComputation : +_compute_dir_speed_spectrogram()
    VibrationGraphComputation : +_compute_angle_powers()
    VibrationGraphComputation : +_compute_speed_powers()
    VibrationGraphComputation : +_filter_and_split_ranges()
    VibrationGraphComputation : +_compute_symmetry_analysis()
    VibrationGraphComputation : +_extract_angle_and_speed()
Loading

Class diagram for ShaperGraphComputation and related classes

classDiagram
    class ShaperGraphComputation {
        +List~Measurement~ measurements
        +Optional~float~ accel_per_hz
        +float scv
        +Optional~float~ max_smoothing
        +float max_freq
        +String st_version
        +compute()
        +_calibrate_shaper()
    }
    class Measurement {
        +String name
        +List~float~ samples
    }
    ShaperGraphComputation o-- Measurement
    ShaperGraphComputation : +List~Measurement~ measurements
    ShaperGraphComputation : +Optional~float~ accel_per_hz
    ShaperGraphComputation : +float scv
    ShaperGraphComputation : +Optional~float~ max_smoothing
    ShaperGraphComputation : +float max_freq
    ShaperGraphComputation : +String st_version
    ShaperGraphComputation : +compute()
    ShaperGraphComputation : +_calibrate_shaper()
Loading

Class diagram for StaticGraphComputation and related classes

classDiagram
    class StaticGraphComputation {
        +List~Measurement~ measurements
        +float freq
        +float duration
        +float max_freq
        +Optional~float~ accel_per_hz
        +String st_version
        +compute()
    }
    class Measurement {
        +String name
        +List~float~ samples
    }
    StaticGraphComputation o-- Measurement
    StaticGraphComputation : +List~Measurement~ measurements
    StaticGraphComputation : +float freq
    StaticGraphComputation : +float duration
    StaticGraphComputation : +float max_freq
    StaticGraphComputation : +Optional~float~ accel_per_hz
    StaticGraphComputation : +String st_version
    StaticGraphComputation : +compute()
Loading

File-Level Changes

Change Details Files
Implemented a Factory pattern for graph creators
  • Added a GraphCreatorFactory class to handle graph creator instantiation
  • Introduced a registry mechanism for graph creators using class decorators
  • Refactored graph creator initialization to use the factory pattern
shaketune/graph_creators/graph_creator.py
shaketune/graph_creators/graph_creator_factory.py
Separated computation from plotting logic
  • Created dedicated computation classes for each graph type
  • Moved all plotting logic into a new Plotter class
  • Implemented reusable plotting utilities and helper methods
  • Added support for customizable plot configurations
shaketune/graph_creators/plotter.py
shaketune/graph_creators/vibrations_graph_creator.py
shaketune/graph_creators/shaper_graph_creator.py
shaketune/graph_creators/belts_graph_creator.py
shaketune/graph_creators/axes_map_graph_creator.py
shaketune/graph_creators/static_graph_creator.py
Added CLI support and improved configuration
  • Created a new CLI module for command-line interface
  • Added support for custom output paths and DPI settings
  • Implemented flexible configuration handling
  • Added environment variable support for CLI mode
shaketune/cli.py
shaketune/shaketune_config.py
shaketune/shaketune.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

Copy link

@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 @Frix-x - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider breaking down some of the longer plotting methods in the Plotter class in a future PR to improve readability and maintainability
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 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.

shaketune/cli.py Show resolved Hide resolved
README.md Show resolved Hide resolved
shaketune/graph_creators/plotter.py Show resolved Hide resolved
shaketune/cli.py Outdated Show resolved Hide resolved
shaketune/graph_creators/__init__.py Outdated Show resolved Hide resolved
shaketune/graph_creators/static_graph_creator.py Outdated Show resolved Hide resolved
@Frix-x Frix-x merged commit 56087cf into develop Nov 24, 2024
9 checks passed
@Frix-x Frix-x deleted the refact-graphcreators branch November 24, 2024 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant