Skip to content

Conversation

@harryswift01
Copy link
Contributor

Summary

This PR introduces a major refactor of the CodeEntropy project, transitioning the codebase to a modular, object-oriented architecture. Alongside the structural updates, I've expanded and reorganized the test suite, pull in a key bug fix in trajectory slicing, and pulled in upstream changes to stay aligned with recent work in main.


Changes

Migrated CodeEntropy to an Object-Oriented Design

  • Replaced the legacy procedural structure with a modular object-oriented design.
  • Introduced core orchestration and domain logic classes:
    • RunManager for handling the entire run lifecycle.
    • LevelManager for managing energy levels and states.
    • EntropyManager with specialized subclasses: VibrationalEntropy, ConformationalEntropy, and OrientationalEntropy.
  • Introduced encapsulation using private attributes and @property decorators.
  • Adopted dependency injection for configuration and logging to decouple components and enable easier unit testing.
  • Removed all legacy function-based modules under CodeEntropy/calculations/.

Refactored EntropyManager.execute() for Readability

  • Split the large execute() method into smaller, focused helper functions.
  • Added descriptive docstrings throughout to improve documentation.
  • No change in logic, the changes made are purely structural.

Restructured and Expanded Tests

  • Updated test organisation to match the new OOP structure.
  • Combined entropy-related tests into a single file with separate test classes.
  • Renamed test_EntropyFunctions to test_CodeEntropy to better match project structure.
  • Adjusted all test imports and references to the new class/module structure.

Added Tests for main.py Execution Logic

  • Added tests that simulate both successful and failed runs using the new orchestration flow.
  • Removed outdated tests that no longer apply to the updated architecture.

Enhanced Coverage for data_logger.py

  • Verified correct initialization of logging.
  • Added tests for data result tracking and JSON output.
  • Checked that log_tables are formatted correctly.

Strengthened Tests for arg_config_manager.py

  • Verified logging switches to DEBUG when --verbose is passed.
  • Ensured invalid argument types are properly caught and raise helpful errors.

Fixed Off-by-One Error in Trajectory Slicing

  • Corrected a bug that could result in the wrong trajectory frames being selected.
  • Pulled in from main branch commit 986c314.

Synced with PR #87 for Config Manager Updates


Impact

  • Clean, Modular Codebase: Much easier to maintain, test, and build on.
  • Improved Reliability: Better test coverage and defensive programming reduce risk of bugs.
  • Consistency Across the Project: Better naming and folder structure bring clarity and coherence.
  • Bug Fixes: Addresses key known issues, including trajectory slicing logic.
  • Ready for the Future: This sets the foundation for future improvements and feature development.

- Replaced legacy procedural modules with a modular, object-oriented structure
- Introduced core classes: `RunManager`, `LevelManager`, `EntropyManager` with subclasses of `VibrationalEntropy`, `ConformationalEntropy`, `OrientationalEntropy`
- Implemented dependency injection for improved testability and modularity
- Encapsulated configuration, logging, and data management into dedicated components
- Added `@property` accessors and private attributes for better encapsulation
- Removed legacy function-based modules under `CodeEntropy/calculations/`
- Added new entry point via `main.py` using `RunManager` orchestration
- Imports now reflect the new file, class and function locations
- Renamed test file names to follow the same structure as the project directory file names
- Moved all of the current entropy tests into a single file seperated in classes by the type of entropy
- Broke down `EntropyManager.execute()` into smaller private methods for readability and maintainability
- Added docstrings to new helper methods and existing class methods
- No changes to core logic; behavior remains the same
- Added a test to verify that the logging level is set to DEBUG when the verbose flag is provided
- Added a test to ensure that invalid argument types raise appropriate errors in the argument parsing logic
- Added a test to verify that the `init` function is correctly set up
- Added a test to ensure that results are added correctly
- Added a test to ensure the dataframes are saved to `JSON` files correctly
- Added a test to ensure the `log_tables` are formatted as expected
…in consistency with naming conventions across the codebase
- Test case to simulate a successful and unsuccessful runs
- Removal of existing test cases as they are not fit for purpose with the new OOP structure
@harryswift01 harryswift01 added this to the WP7 - Refactor milestone May 9, 2025
@harryswift01 harryswift01 self-assigned this May 9, 2025
@harryswift01 harryswift01 linked an issue May 9, 2025 that may be closed by this pull request
9 tasks
- Testing the log directory creation
- Testing the logger instantiation and return value
- Testing that the expected log files are created
- Testing that the logger is able to dynacmically change
- Testing the MDAnalysis and command logger configuration
@jimboid jimboid marked this pull request as ready for review May 12, 2025 09:30
- Additional tests to cover the creation of the folders
 - Test to ensure a job folder is created when the directory is empty
 - Test to ensure the correct job folder is created if there are existing folders
 - Test to ensure job folder is created correctly when non job folders are present
 - Test to ensure job folder is created with mixed job folders and other folders
 - Test to ensure job folders with the wrong suffix are ignored
@harryswift01 harryswift01 changed the title 85 oop refactor Object-oriented programming Refactor for CodeEntropy May 12, 2025
@harryswift01 harryswift01 requested a review from jimboid May 12, 2025 09:57
Copy link
Member

@jimboid jimboid left a comment

Choose a reason for hiding this comment

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

A solid refactor with tests for the vast majority of the new code and its flow. There remains some areas such as levels, entropy and run modules with low test coverage, but these will be dealt with later as they require more specialist testing.

The code looks to be have all of the right parts in the correct places, happy to approve this.

I did raise in the review the possibility of structuring the levels like the entropy class as a series of subclasses with a base for common code to avoid the checking for which level is being dealt with, but this is should not block this PR, mainly for us to discuss later as there may be reasons why this is not simple to do.

Well done on getting this out of the door Harry!

@skfegan
Copy link
Member

skfegan commented May 12, 2025

One detail - in the doc strings in entropy.py. For results_df it should be """Returns the dataframe containing entropy results at all levels.""" and for residue_results it should be """Returns the dataframe containing united-atom level results for each residue."""

@harryswift01 harryswift01 merged commit 33de6c9 into main May 12, 2025
6 checks passed
@harryswift01 harryswift01 deleted the 85-oop-refactor branch May 12, 2025 12:27
@jimboid jimboid modified the milestones: WP7 - Refactor, 1.0.0 release Jun 10, 2025
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.

Refactor architecture to follow object-oriented class structure

4 participants