diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 0b59544..5b23339 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -1,42 +1,65 @@ -# How to contribute +# Contributing to CodeEntropy -We welcome contributions from external contributors, and this document -describes how to merge code changes into this CodeEntropy. +Thank you for your interest in contributing to **CodeEntropy**! + +We’re excited to collaborate with developers, researchers, and community members to make CodeEntropy better for everyone. + +This guide explains how to set up your environment, make changes, and submit them for review. Whether you’re fixing a bug, improving documentation, or adding new features, every contribution makes a difference. + +--- ## Getting Started -* Make sure you have a [GitHub account](https://github.com/signup/free). -* [Fork](https://help.github.com/articles/fork-a-repo/) this repository on GitHub. -* On your local machine, - [clone](https://help.github.com/articles/cloning-a-repository/) your fork of - the repository. - -## Making Changes - -* Add some really awesome code to your local fork. It's usually a [good - idea](http://blog.jasonmeridth.com/posts/do-not-issue-pull-requests-from-your-master-branch/) - to make changes on a - [branch](https://help.github.com/articles/creating-and-deleting-branches-within-your-repository/) - with the branch name relating to the feature you are going to add. -* When you are ready for others to examine and comment on your new feature, - navigate to your fork of CodeEntropy on GitHub and open a [pull - request](https://help.github.com/articles/using-pull-requests/) (PR). Note that - after you launch a PR from one of your fork's branches, all - subsequent commits to that branch will be added to the open pull request - automatically. Each commit added to the PR will be validated for - mergability, compilation and test suite compliance; the results of these tests - will be visible on the PR page. -* If you're providing a new feature, you must add test cases and documentation. -* When the code is ready to go, make sure you run the test suite using pytest. -* When you're ready to be considered for merging, check the "Ready to go" - box on the PR page to let the CodeEntropy devs know that the changes are complete. - The code will not be merged until this box is checked, the continuous - integration returns checkmarks, - and multiple core developers give "Approved" reviews. - -# Additional Resources - -* [General GitHub documentation](https://help.github.com/) -* [PR best practices](http://codeinthehole.com/writing/pull-requests-and-other-good-practices-for-teams-using-github/) -* [A guide to contributing to software packages](http://www.contribution-guide.org) -* [Thinkful PR example](http://www.thinkful.com/learn/github-pull-request-tutorial/#Time-to-Submit-Your-First-PR) +Before contributing, please review the [Developer Guide](https://codeentropy.readthedocs.io/en/latest/developer_guide.html). +It covers CodeEntropy’s architecture, setup instructions, and contribution workflow. + +If you’re new to the project, we also recommend: +- Reading the [README](../README.md) for an overview and installation details. +- Checking open [issues](https://github.com/CCPBioSim/CodeEntropy/issues) labeled **good first issue** to find beginner-friendly tasks. + +--- + +## Submitting a Pull Request (PR) + +When you’re ready to submit your work: + +1. **Push your branch** to GitHub. +2. **Open a [pull request](https://help.github.com/articles/using-pull-requests/)** against the `main` branch. +3. **Fill out the PR template**, including: + - A concise summary of what your PR does + - A list of all changes introduced + - Details on how these changes affect the repository (features, tests, documentation, etc.) +4. **Verify before submission**: + - All tests pass + - Pre-commit checks succeed + - Documentation is updated where applicable +5. **Review process**: + - Your PR will be reviewed by the core development team. + - At least **one approval** is required before merging. + +We aim to provide constructive feedback quickly and appreciate your patience during the review process. + +--- + +## Reporting Issues + +Found a bug or have a feature request? + +1. **Open a new issue** on GitHub. +2. Provide a **clear and descriptive title**. +3. Include: + - Steps to reproduce the issue (if applicable) + - Expected vs. actual behavior + - Relevant logs, screenshots, or input files + +Well-documented issues help us address problems faster and keep CodeEntropy stable and robust. + +--- + +## Additional Resources + +- [GitHub Docs](https://help.github.com/) + +--- + +Thank you for helping improve **CodeEntropy**, your contributions make open source stronger! diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index d18357f..ef622a5 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -48,8 +48,16 @@ jobs: - name: checkout uses: actions/checkout@v5.0.0 - - name: Change version in repo - run: sed -i "s/__version__ =.*/__version__ = \"${{ github.event.inputs.version }}\"/g" CodeEntropy/__init__.py + - name: Change version in repo and CITATION.cff + run: | + # Update Python package version + sed -i "s/__version__ =.*/__version__ = \"${{ github.event.inputs.version }}\"/g" CodeEntropy/__init__.py + + # Update CITATION.cff version and date-released + if [ -f CITATION.cff ]; then + sed -i -E "s/^(version:\s*).*/\1${{ github.event.inputs.version }}/" CITATION.cff + sed -i -E "s/^(date-released:\s*).*/\1'$(date -u +%F)'/" CITATION.cff + fi - name: send PR id: pr_id @@ -61,6 +69,7 @@ jobs: body: | Update version - Update the __init__.py with new release + - Update CITATION.cff version & date-released - Auto-generated by [CI] committer: version-updater author: version-updater @@ -104,7 +113,7 @@ jobs: steps: - name: create release - uses: softprops/action-gh-release@v2.4.1 + uses: softprops/action-gh-release@v2.4.2 with: name: v${{ github.event.inputs.version }} generate_release_notes: true diff --git a/CITATION.cff b/CITATION.cff index cd54050..1f50a2c 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -56,6 +56,7 @@ authors: family-names: Swift email: harry.swift@stfc.ac.uk affiliation: 'STFC, Scientific Computing' + orcid: 'https://orcid.org/0009-0007-3323-753X' repository-code: 'https://github.com/CCPBioSim/CodeEntropy' url: 'https://ccpbiosim.github.io/CodeEntropy/' abstract: >- @@ -75,5 +76,5 @@ keywords: - biomolecular simulations - protein flexibility license: MIT -version: 0.3.6 -date-released: '2022-07-06' +version: 1.0.3 +date-released: '2025-09-29' diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 79c3538..729da91 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -2,76 +2,127 @@ ## Our Pledge -In the interest of fostering an open and welcoming environment, we as -contributors and maintainers pledge to making participation in our project and -our community a harassment-free experience for everyone, regardless of age, -body size, disability, ethnicity, gender identity and expression, level of -experience, nationality, personal appearance, race, religion, or sexual -identity and orientation. +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. ## Our Standards -Examples of behavior that contributes to creating a positive environment include: +Examples of behavior that contributes to a positive environment for our +community include: -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community -Examples of unacceptable behavior by participants include: +Examples of unacceptable behavior include: -* The use of sexualized language or imagery and unwelcome sexual attention or advances -* Trolling, insulting/derogatory comments, and personal or political attacks +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks * Public or private harassment -* Publishing others' private information, such as a physical or electronic address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a professional setting - -## Our Responsibilities +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting -Project maintainers are responsible for clarifying the standards of acceptable -behavior and are expected to take appropriate and fair corrective action in -response to any instances of unacceptable behavior. +## Enforcement Responsibilities -Project maintainers have the right and responsibility to remove, edit, or -reject comments, commits, code, wiki edits, issues, and other contributions -that are not aligned to this Code of Conduct, or to ban temporarily or -permanently any contributor for other behaviors that they deem inappropriate, -threatening, offensive, or harmful. +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. -Moreover, project maintainers will strive to offer feedback and advice to -ensure quality and consistency of contributions to the code. Contributions -from outside the group of project maintainers are strongly welcomed but the -final decision as to whether commits are merged into the codebase rests with -the team of project maintainers. +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. ## Scope -This Code of Conduct applies both within project spaces and in public spaces -when an individual is representing the project or its community. Examples of -representing a project or community include using an official project e-mail -address, posting via an official social media account, or acting as an -appointed representative at an online or offline event. Representation of a -project may be further defined and clarified by project maintainers. +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported by contacting the project team at 'lpchungaa@gmail.com'. The project team will -review and investigate all complaints, and will respond in a way that it deems -appropriate to the circumstances. The project team is obligated to maintain -confidentiality with regard to the reporter of an incident. Further details of -specific enforcement policies may be posted separately. +reported to the community leaders responsible for enforcement at +ccpbiosim@stfc.ac.uk. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning -Project maintainers who do not follow or enforce the Code of Conduct in good -faith may face temporary or permanent repercussions as determined by other -members of the project's leadership. +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], -version 1.4, available at -[http://contributor-covenant.org/version/1/4][version] +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org -[homepage]: http://contributor-covenant.org -[version]: http://contributor-covenant.org/version/1/4/ +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. \ No newline at end of file diff --git a/CodeEntropy/__init__.py b/CodeEntropy/__init__.py index 34402c3..06e3df1 100644 --- a/CodeEntropy/__init__.py +++ b/CodeEntropy/__init__.py @@ -1,9 +1,11 @@ """ CodeEntropy -CodeEntropy tool with POSEIDON code integrated to form a complete and generally -applicable set of tools for computing entropy of macromolecular systems from the -forces sampled in a MD simulation. +CodeEntropy is a Python package for computing the configurational entropy of +macromolecular systems using forces sampled from molecular dynamics (MD) simulations. +It implements the multiscale cell correlation method to provide accurate and efficient +entropy estimates, supporting a wide range of applications in molecular simulation +and statistical mechanics. """ __version__ = "1.0.3" diff --git a/CodeEntropy/config/arg_config_manager.py b/CodeEntropy/config/arg_config_manager.py index dab98dc..799b950 100644 --- a/CodeEntropy/config/arg_config_manager.py +++ b/CodeEntropy/config/arg_config_manager.py @@ -12,7 +12,22 @@ "top_traj_file": { "type": str, "nargs": "+", - "help": "Path to Structure/topology file followed by Trajectory file(s)", + "help": "Path to structure/topology file followed by trajectory file", + }, + "force_file": { + "type": str, + "default": None, + "help": "Optional path to force file if forces are not in trajectory file", + }, + "file_format": { + "type": str, + "default": None, + "help": "String for file format as recognised by MDAnalysis", + }, + "kcal_force_units": { + "type": bool, + "default": False, + "help": "Set this to True if you have a separate force file with nonSI units.", }, "selection_string": { "type": str, diff --git a/CodeEntropy/entropy.py b/CodeEntropy/entropy.py index fa75111..75acfe5 100644 --- a/CodeEntropy/entropy.py +++ b/CodeEntropy/entropy.py @@ -86,7 +86,7 @@ def execute(self): ) reduced_atom, number_molecules, levels, groups = self._initialize_molecules() - + logger.debug(f"Universe 3: {reduced_atom}") water_atoms = self._universe.select_atoms("water") water_resids = set(res.resid for res in water_atoms.residues) diff --git a/CodeEntropy/run.py b/CodeEntropy/run.py index 38f3039..71b55ad 100644 --- a/CodeEntropy/run.py +++ b/CodeEntropy/run.py @@ -247,8 +247,43 @@ def run_entropy_workflow(self): # Load MDAnalysis Universe tprfile = args.top_traj_file[0] trrfile = args.top_traj_file[1:] + fileformat = args.file_format logger.debug(f"Loading Universe with {tprfile} and {trrfile}") - u = mda.Universe(tprfile, trrfile) + u = mda.Universe(tprfile, trrfile, format=fileformat) + + # If forces are in separate file merge them with the + # coordinates from the trajectory file + forcefile = args.force_file + if forcefile is not None: + logger.debug(f"Loading Universe with {forcefile}") + u_force = mda.Universe(tprfile, forcefile, format=fileformat) + select_atom = u.select_atoms("all") + select_atom_force = u_force.select_atoms("all") + + coordinates = ( + AnalysisFromFunction( + lambda ag: ag.positions.copy(), select_atom + ) + .run() + .results["timeseries"] + ) + forces = ( + AnalysisFromFunction( + lambda ag: ag.positions.copy(), select_atom_force + ) + .run() + .results["timeseries"] + ) + + if args.kcal_force_units: + # Convert from kcal to kJ + forces *= 4.184 + + logger.debug("Merging forces with coordinates universe.") + new_universe = mda.Merge(select_atom) + new_universe.load_new(coordinates, forces=forces) + + u = new_universe self._config_manager.input_parameters_validation(u, args) @@ -311,15 +346,8 @@ def new_U_select_frame(self, u, start=None, end=None, step=1): .run() .results["timeseries"][start:end:step] ) - dimensions = ( - AnalysisFromFunction(lambda ag: ag.dimensions.copy(), select_atom) - .run() - .results["timeseries"][start:end:step] - ) u2 = mda.Merge(select_atom) - u2.load_new( - coordinates, format=MemoryReader, forces=forces, dimensions=dimensions - ) + u2.load_new(coordinates, format=MemoryReader, forces=forces) logger.debug(f"MDAnalysis.Universe - reduced universe: {u2}") return u2 @@ -351,15 +379,8 @@ def new_U_select_atom(self, u, select_string="all"): .run() .results["timeseries"] ) - dimensions = ( - AnalysisFromFunction(lambda ag: ag.dimensions.copy(), select_atom) - .run() - .results["timeseries"] - ) u2 = mda.Merge(select_atom) - u2.load_new( - coordinates, format=MemoryReader, forces=forces, dimensions=dimensions - ) + u2.load_new(coordinates, format=MemoryReader, forces=forces) logger.debug(f"MDAnalysis.Universe - reduced universe: {u2}") return u2 diff --git a/docs/config.yaml b/docs/config.yaml index 44fbb3e..3f6fd8f 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -2,6 +2,8 @@ run1: top_traj_file: ["1AKI_prod.tpr", "1AKI_prod.trr"] + force_file: + file_formate: selection_string: 'all' start: 0 end: 500 diff --git a/docs/faq.rst b/docs/faq.rst index aec29b1..a119a27 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -4,7 +4,15 @@ Frequently asked questions Why do I get a ``WARNING`` about invalid eigenvalues? ----------------------------------------------------- -Insufficient sampling might introduce noise and cause matrix elements to deviate to values that would not reflect the uncorrelated nature of force-force covariance of distantly positioned residues.Try increasing the sampling time. This is especially true at the residue level. +Insufficient sampling might introduce noise and cause matrix elements to deviate to values that would not reflect the uncorrelated nature of force-force covariance of distantly positioned residues. +Try increasing the sampling time. +This is especially true at the residue level. For example in a lysozyme system, the residue level contains the largest force and torque covariance matrices because at this level we have the largest number of beads (which is equal to the number of residues in a protein) compared to the molecule level (3 beads) and united-atom level (~10 beads per amino acid). +What do I do if there is an error from MDAnalysis not recognising the file type? +------------------------------------------------------------------------------- + +Use the file_format option. +The MDAnalysis documentation has a list of acceptable formats: https://userguide.mdanalysis.org/1.1.1/formats/index.html#id1. +The first column of their table gives you the string you need (case sensitive). diff --git a/docs/getting_started.rst b/docs/getting_started.rst index 6218cff..7fe9071 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -81,10 +81,18 @@ The top_traj_file argument is necessary to identify your simulation data, the ot - Description - Default - Type - * - ``--top_traj_file`` - - Path to Structure/topology file followed by Trajectory file(s). Any MDAnalysis readable files should work (for example ``GROMACS TPR and TRR`` or ``AMBER PRMTOP and NETCDF``). You will need to output the **coordinates** and **forces** to the **same file** . + * - ``--top_traj_file`` + - Path to Structure/topology file followed by Trajectory file. Any MDAnalysis readable files should work (for example ``GROMACS TPR and TRR`` or ``AMBER PRMTOP and NETCDF``). - Required, no default value - list of ``str`` + * - ``--force_file`` + - Path to a file with forces. This option should be used if the forces are not in the same file as the coordinates. It is expected that the force file has the same number of atoms and frames as the trajectory file. Any MDAnalysis readable files should work (for example ``AMBER NETCDF`` or ``LAMMPS DCD``). + - None + - ``str`` + * - ``--file_format`` + - Use to tell MDAnalysis the format if the trajectory or force file does not have the standard extension recognised by MDAnalysis. + - None + - ``str`` * - ``--selection_string`` - Selection string for CodeEntropy such as protein or resid, refer to ``MDAnalysis.select_atoms`` for more information. - ``"all"``: select all atom in trajectory diff --git a/tests/test_CodeEntropy/test_run.py b/tests/test_CodeEntropy/test_run.py index 54def3e..42a6232 100644 --- a/tests/test_CodeEntropy/test_run.py +++ b/tests/test_CodeEntropy/test_run.py @@ -308,6 +308,8 @@ def test_run_entropy_workflow(self): run_manager._config_manager.load_config.return_value = { "test_run": { "top_traj_file": ["/path/to/tpr", "/path/to/trr"], + "force_file": None, + "file_format": None, "selection_string": "all", "output_file": "output.json", "verbose": True, @@ -335,6 +337,8 @@ def test_run_entropy_workflow(self): mock_args.output_file = "output.json" mock_args.verbose = True mock_args.top_traj_file = ["/path/to/tpr", "/path/to/trr"] + mock_args.force_file = None + mock_args.file_format = None mock_args.selection_string = "all" parser = run_manager._config_manager.setup_argparse.return_value parser.parse_known_args.return_value = (mock_args, []) @@ -351,7 +355,9 @@ def test_run_entropy_workflow(self): run_manager.run_entropy_workflow() - mock_universe.assert_called_once_with("/path/to/tpr", ["/path/to/trr"]) + mock_universe.assert_called_once_with( + "/path/to/tpr", ["/path/to/trr"], format=None + ) mock_entropy_manager.execute.assert_called_once() def test_run_configuration_warning(self): @@ -507,6 +513,183 @@ def test_run_entropy_workflow_missing_selection_string(self): with self.assertRaisesRegex(ValueError, "Missing 'selection_string' argument."): run_manager.run_entropy_workflow() + @patch("CodeEntropy.run.EntropyManager") + @patch("CodeEntropy.run.GroupMolecules") + @patch("CodeEntropy.run.LevelManager") + @patch("CodeEntropy.run.AnalysisFromFunction") + @patch("CodeEntropy.run.mda.Merge") + @patch("CodeEntropy.run.mda.Universe") + def test_merges_forcefile_correctly( + self, MockUniverse, MockMerge, MockAnalysis, *_mocks + ): + """ + Ensure that coordinates and forces are merged correctly + when a force file is provided. + """ + run_manager = RunManager("mock/job001") + mock_logger = MagicMock() + run_manager._logging_config = MagicMock( + setup_logging=MagicMock(return_value=mock_logger) + ) + run_manager._config_manager = MagicMock() + run_manager._data_logger = MagicMock() + run_manager.show_splash = MagicMock() + + args = MagicMock( + top_traj_file=["topol.tpr", "traj.xtc"], + force_file="forces.xtc", + file_format="xtc", + selection_string="all", + verbose=False, + output_file="output.json", + kcal_force_units=False, + ) + run_manager._config_manager.load_config.return_value = {"run1": {}} + parser = run_manager._config_manager.setup_argparse.return_value + parser.parse_known_args.return_value = (args, []) + run_manager._config_manager.merge_configs.return_value = args + run_manager._config_manager.input_parameters_validation = MagicMock() + + mock_u, mock_u_force = MagicMock(), MagicMock() + MockUniverse.side_effect = [mock_u, mock_u_force] + mock_atoms, mock_atoms_force = MagicMock(), MagicMock() + mock_u.select_atoms.return_value = mock_atoms + mock_u_force.select_atoms.return_value = mock_atoms_force + + coords, forces = np.random.rand(2, 3, 3), np.random.rand(2, 3, 3) + MockAnalysis.side_effect = [ + MagicMock( + run=MagicMock(return_value=MagicMock(results={"timeseries": coords})) + ), + MagicMock( + run=MagicMock(return_value=MagicMock(results={"timeseries": forces})) + ), + ] + mock_merge = MagicMock() + MockMerge.return_value = mock_merge + + run_manager.run_entropy_workflow() + + MockUniverse.assert_any_call("topol.tpr", ["traj.xtc"], format="xtc") + MockUniverse.assert_any_call("topol.tpr", "forces.xtc", format="xtc") + MockMerge.assert_called_once_with(mock_atoms) + mock_merge.load_new.assert_called_once_with(coords, forces=forces) + self.assertEqual(mock_logger.debug.call_count, 3) + + @patch("CodeEntropy.run.EntropyManager") + @patch("CodeEntropy.run.GroupMolecules") + @patch("CodeEntropy.run.LevelManager") + @patch("CodeEntropy.run.AnalysisFromFunction") + @patch("CodeEntropy.run.mda.Merge") + @patch("CodeEntropy.run.mda.Universe") + def test_converts_kcal_to_kj(self, MockUniverse, MockMerge, MockAnalysis, *_mocks): + """ + Ensure that forces are scaled by 4.184 when kcal_force_units=True. + """ + run_manager = RunManager("mock/job001") + mock_logger = MagicMock() + run_manager._logging_config = MagicMock( + setup_logging=MagicMock(return_value=mock_logger) + ) + run_manager._config_manager = MagicMock() + run_manager._data_logger = MagicMock() + run_manager.show_splash = MagicMock() + + args = MagicMock( + top_traj_file=["topol.tpr", "traj.xtc"], + force_file="forces.xtc", + file_format="xtc", + selection_string="all", + verbose=False, + output_file="output.json", + kcal_force_units=True, + ) + run_manager._config_manager.load_config.return_value = {"run1": {}} + parser = run_manager._config_manager.setup_argparse.return_value + parser.parse_known_args.return_value = (args, []) + run_manager._config_manager.merge_configs.return_value = args + run_manager._config_manager.input_parameters_validation = MagicMock() + + mock_u, mock_u_force = MagicMock(), MagicMock() + MockUniverse.side_effect = [mock_u, mock_u_force] + mock_atoms, mock_atoms_force = MagicMock(), MagicMock() + mock_u.select_atoms.return_value = mock_atoms + mock_u_force.select_atoms.return_value = mock_atoms_force + + coords = np.random.rand(2, 3, 3) + forces = np.random.rand(2, 3, 3) + forces_orig = forces.copy() + MockAnalysis.side_effect = [ + MagicMock( + run=MagicMock(return_value=MagicMock(results={"timeseries": coords})) + ), + MagicMock( + run=MagicMock(return_value=MagicMock(results={"timeseries": forces})) + ), + ] + mock_merge = MagicMock() + MockMerge.return_value = mock_merge + + run_manager.run_entropy_workflow() + _, kwargs = mock_merge.load_new.call_args + np.testing.assert_allclose(kwargs["forces"], forces_orig * 4.184) + + @patch("CodeEntropy.run.EntropyManager") + @patch("CodeEntropy.run.GroupMolecules") + @patch("CodeEntropy.run.LevelManager") + @patch("CodeEntropy.run.AnalysisFromFunction") + @patch("CodeEntropy.run.mda.Merge") + @patch("CodeEntropy.run.mda.Universe") + def test_logs_debug_messages(self, MockUniverse, MockMerge, MockAnalysis, *_mocks): + """ + Ensure that loading and merging steps produce debug logs. + """ + run_manager = RunManager("mock/job001") + mock_logger = MagicMock() + run_manager._logging_config = MagicMock( + setup_logging=MagicMock(return_value=mock_logger) + ) + run_manager._config_manager = MagicMock() + run_manager._data_logger = MagicMock() + run_manager.show_splash = MagicMock() + + args = MagicMock( + top_traj_file=["topol.tpr", "traj.xtc"], + force_file="forces.xtc", + file_format="xtc", + selection_string="all", + verbose=False, + output_file="output.json", + kcal_force_units=False, + ) + run_manager._config_manager.load_config.return_value = {"run1": {}} + parser = run_manager._config_manager.setup_argparse.return_value + parser.parse_known_args.return_value = (args, []) + run_manager._config_manager.merge_configs.return_value = args + run_manager._config_manager.input_parameters_validation = MagicMock() + + mock_u, mock_u_force = MagicMock(), MagicMock() + MockUniverse.side_effect = [mock_u, mock_u_force] + mock_atoms, mock_atoms_force = MagicMock(), MagicMock() + mock_u.select_atoms.return_value = mock_atoms + mock_u_force.select_atoms.return_value = mock_atoms_force + + arr = np.random.rand(2, 3, 3) + MockAnalysis.side_effect = [ + MagicMock( + run=MagicMock(return_value=MagicMock(results={"timeseries": arr})) + ), + MagicMock( + run=MagicMock(return_value=MagicMock(results={"timeseries": arr})) + ), + ] + MockMerge.return_value = MagicMock() + + run_manager.run_entropy_workflow() + debug_msgs = [c[0][0] for c in mock_logger.debug.call_args_list] + self.assertTrue(any("forces.xtc" in m for m in debug_msgs)) + self.assertTrue(any("Merging forces" in m for m in debug_msgs)) + @patch("CodeEntropy.run.AnalysisFromFunction") @patch("CodeEntropy.run.mda.Merge") def test_new_U_select_frame(self, MockMerge, MockAnalysisFromFunction): @@ -522,7 +705,6 @@ def test_new_U_select_frame(self, MockMerge, MockAnalysisFromFunction): # Mock AnalysisFromFunction results for coordinates, forces, and dimensions coords = np.random.rand(10, 100, 3) forces = np.random.rand(10, 100, 3) - dims = np.random.rand(10, 3) mock_coords_analysis = MagicMock() mock_coords_analysis.run.return_value.results = {"timeseries": coords} @@ -530,14 +712,10 @@ def test_new_U_select_frame(self, MockMerge, MockAnalysisFromFunction): mock_forces_analysis = MagicMock() mock_forces_analysis.run.return_value.results = {"timeseries": forces} - mock_dims_analysis = MagicMock() - mock_dims_analysis.run.return_value.results = {"timeseries": dims} - # Set the side effects for the three AnalysisFromFunction calls MockAnalysisFromFunction.side_effect = [ mock_coords_analysis, mock_forces_analysis, - mock_dims_analysis, ] # Mock the merge operation @@ -557,7 +735,6 @@ def test_new_U_select_frame(self, MockMerge, MockAnalysisFromFunction): # Assert that the arrays are passed correctly np.testing.assert_array_equal(args[0], coords) np.testing.assert_array_equal(kwargs["forces"], forces) - np.testing.assert_array_equal(kwargs["dimensions"], dims) # Check if format was included in the kwargs self.assertIn("format", kwargs) @@ -576,7 +753,6 @@ def test_new_U_select_atom(self, MockMerge, MockAnalysisFromFunction): # Mock AnalysisFromFunction results for coordinates, forces, and dimensions coords = np.random.rand(10, 100, 3) forces = np.random.rand(10, 100, 3) - dims = np.random.rand(10, 3) mock_coords_analysis = MagicMock() mock_coords_analysis.run.return_value.results = {"timeseries": coords} @@ -584,14 +760,10 @@ def test_new_U_select_atom(self, MockMerge, MockAnalysisFromFunction): mock_forces_analysis = MagicMock() mock_forces_analysis.run.return_value.results = {"timeseries": forces} - mock_dims_analysis = MagicMock() - mock_dims_analysis.run.return_value.results = {"timeseries": dims} - # Set the side effects for the three AnalysisFromFunction calls MockAnalysisFromFunction.side_effect = [ mock_coords_analysis, mock_forces_analysis, - mock_dims_analysis, ] # Mock the merge operation @@ -613,7 +785,6 @@ def test_new_U_select_atom(self, MockMerge, MockAnalysisFromFunction): # Assert that the arrays are passed correctly np.testing.assert_array_equal(args[0], coords) np.testing.assert_array_equal(kwargs["forces"], forces) - np.testing.assert_array_equal(kwargs["dimensions"], dims) # Check if format was included in the kwargs self.assertIn("format", kwargs)