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

Ttc fix new #159

Merged
merged 3 commits into from
Sep 25, 2024
Merged

Ttc fix new #159

merged 3 commits into from
Sep 25, 2024

Conversation

Frix-x
Copy link
Owner

@Frix-x Frix-x commented Sep 25, 2024

Summary by Sourcery

Enhance the MeasurementsManager to support chunking of measurement data, improving memory management and data handling for large datasets. Update related modules to utilize the new chunking feature and ensure proper cleanup of temporary files. Document new configuration options in the README.

Enhancements:

  • Introduce chunking mechanism in MeasurementsManager to handle large datasets by saving measurements to temporary files when a specified chunk size is exceeded.
  • Add a method to wait for data transfers to complete, ensuring that all measurement data is written to disk before proceeding.
  • Implement cleanup of temporary files and directories in the destructor of MeasurementsManager to prevent leftover files.
  • Update the initialization of MeasurementsManager across various modules to accept a chunk size parameter, improving memory management during data processing.

Documentation:

  • Update README.md to include new configuration options for measurements_chunk_size and dpi, providing guidance on managing memory usage and graph resolution.

with all the logic to load them back and reconstruct the .stdata file
Copy link
Contributor

sourcery-ai bot commented Sep 25, 2024

Reviewer's Guide by Sourcery

This pull request implements a significant change to the MeasurementsManager class in the Shake&Tune project. The main goal is to improve memory management by introducing a chunking mechanism for storing measurements. This change allows the system to handle larger datasets more efficiently, particularly for vibration measurements.

File-Level Changes

Change Details Files
Implement chunking mechanism in MeasurementsManager
  • Add chunk_size parameter to MeasurementsManager constructor
  • Implement _save_chunk method to write measurements to temporary files
  • Add _reassemble_chunks method to combine chunk files into final output
  • Modify add_measurement method to trigger chunk saving when threshold is reached
  • Update get_measurements method to load data from chunk files
  • Implement cleanup mechanism in del method
shaketune/helpers/accelerometer.py
Update ShakeTuneConfig to include chunk size parameter
  • Add measurements_chunk_size configuration option
  • Update ShakeTuneConfig constructor to include chunk_size parameter
  • Set default chunk size to 15 measurements
shaketune/shaketune.py
shaketune/shaketune_config.py
README.md
Modify command implementations to use new MeasurementsManager
  • Update MeasurementsManager initialization in various commands
  • Add wait_for_data_transfers calls after measurements in commands
shaketune/commands/create_vibrations_profile.py
shaketune/commands/axes_shaper_calibration.py
shaketune/commands/axes_map_calibration.py
shaketune/commands/compare_belts_responses.py
shaketune/commands/excitate_axis_at_freq.py
Update graph creators to use new MeasurementsManager
  • Modify MeasurementsManager initialization in graph creator scripts
shaketune/graph_creators/axes_map_graph_creator.py
shaketune/graph_creators/belts_graph_creator.py
shaketune/graph_creators/shaper_graph_creator.py
shaketune/graph_creators/static_graph_creator.py
shaketune/graph_creators/vibrations_graph_creator.py

Sequence Diagram

sequenceDiagram
    participant C as Command
    participant MM as MeasurementsManager
    participant FS as FileSystem
    C->>MM: add_measurement()
    MM->>MM: Check if chunk size reached
    alt Chunk size reached
        MM->>FS: _save_chunk()
        MM->>MM: clear_measurements(keep_last=True)
    end
    C->>MM: save_stdata()
    MM->>FS: _reassemble_chunks()
    FS-->>MM: Load chunk files
    MM->>FS: Save final .stdata file
    MM->>FS: Remove chunk files
Loading

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

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 @Frix-x - I've reviewed your changes and they look great!

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

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 to tell me if it was helpful.

)

self._write_process = None
def __del__(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Improve error handling in cleanup process

The current error handling in the cleanup process is very broad. Consider adding more specific exception handling and logging to help diagnose issues that might occur during cleanup. This could help with debugging and ensuring resources are properly released.

Suggested change
def __del__(self):
def __del__(self):
try:
if self._temp_dir.exists():
shutil.rmtree(self._temp_dir)
except Exception as e:
logging.error(f"Error during cleanup: {e}")
finally:
if hasattr(self, '_write_process') and self._write_process:
self._write_process.terminate()

Comment on lines +35 to +40
# measurements_chunk_size: 15
# The number of measurements to keep in RAM before writing them to disk. This is useful
# to avoid running out of memory when processing large datasets like for the vibrations
# measurements. If you get Timer Too Close errors, try reducing this value (minimum is 2).
# dpi: 300
# The resolution of the generated graphs. This usually doesn't need to be changed.
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (documentation): Consider adding an introductory line for these new configuration options.

It might be helpful to add a brief introductory sentence before listing these new options, explaining that they are additional, advanced configuration settings. This could provide more context for users.

Suggested change
# measurements_chunk_size: 15
# The number of measurements to keep in RAM before writing them to disk. This is useful
# to avoid running out of memory when processing large datasets like for the vibrations
# measurements. If you get Timer Too Close errors, try reducing this value (minimum is 2).
# dpi: 300
# The resolution of the generated graphs. This usually doesn't need to be changed.
# Advanced Configuration Options:
# The following settings provide additional control over Shake&Tune's behavior.
# measurements_chunk_size: 15
# The number of measurements to keep in RAM before writing them to disk. This is useful
# to avoid running out of memory when processing large datasets like for the vibrations
# measurements. If you get Timer Too Close errors, try reducing this value (minimum is 2).
# dpi: 300
# The resolution of the generated graphs. This usually doesn't need to be changed.

Comment on lines 47 to 50
if keep_last:
self.measurements = [self.measurements[-1]]
else:
self.measurements = []
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (code-quality): Replace if statement with if expression (assign-if-exp)

Suggested change
if keep_last:
self.measurements = [self.measurements[-1]]
else:
self.measurements = []
self.measurements = [self.measurements[-1]] if keep_last else []

except Exception:
pass # Ignore errors as it's not critical
try:
all_measurements = []
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (code-quality): Extract code out into method (extract-method)

@Frix-x Frix-x merged commit 7e2ff9d into ttc-fix Sep 25, 2024
3 checks passed
@Frix-x Frix-x deleted the ttc-fix-new branch September 25, 2024 10:09
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