-
Notifications
You must be signed in to change notification settings - Fork 55
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
fix #216: angle parameters are not being read from topology file #264
Merged
orbeckst
merged 23 commits into
Becksteinlab:main
from
jandom:jandom/fix/issue-261-angle-parameters-not-read-from-topology-file
Nov 9, 2023
Merged
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
607f448
fix #216: angle parameters are not being read from topology file
jandom 6d66b8f
run black
jandom ff3dbea
refactored heavily to support all angle types
jandom 03de6f6
update top.py
jandom 882c352
fix another bug in top.py
jandom 8093e06
catch more corner-cases
jandom be30949
found some latent bug or a typo?
jandom 52c4aff
update top.py to fix an UB problem
jandom 9404601
everything is 'param' singular
jandom a2e0ea4
fixing tests
jandom 15828f4
let's make this sane
jandom 33ba327
compatibility with python2.7
jandom 28264d2
missing deps
jandom ba5a243
a new dep
jandom d77feb2
update top.py
jandom d6e4ebd
removing typing
jandom 17da063
more python 2.7 compatibility
jandom 1f3d996
Merge branch 'main' into jandom/fix/issue-261-angle-parameters-not-re…
orbeckst 4713719
remove aenum
jandom 3e9eb2e
update CHANGES
jandom 3ad892b
move to pytest.MonkeyPatch
jandom 5f79a8c
Merge branch 'main' into jandom/fix/issue-261-angle-parameters-not-re…
orbeckst 6664d77
update CHANGES
orbeckst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import gromacs as gmx | ||
from unittest.mock import patch, mock_open | ||
import random | ||
import pytest | ||
from gromacs.fileformats import blocks | ||
|
||
|
||
def generate_topology_line(func_type: blocks.AngleFunctionType) -> str: | ||
# Generates random parameters for the different function types | ||
def random_params(num) -> list[float]: | ||
return [random.uniform(0.0, 100.0) for _ in range(num)] | ||
|
||
# Define the pattern for each function type | ||
patterns = { | ||
blocks.AngleFunctionType.HARMONIC: "{:5d}{:5d}{:5d} 1{:10.4f}{:10.4f}", | ||
blocks.AngleFunctionType.G96_ANGLE: "{:5d}{:5d}{:5d} 2{:10.4f}{:10.4f}", | ||
blocks.AngleFunctionType.CROSS_BOND_BOND: "{:5d}{:5d}{:5d} 3{:10.4f}{:10.4f}{:10.4f}", | ||
blocks.AngleFunctionType.CROSS_BOND_ANGLE: "{:5d}{:5d}{:5d} 4{:10.4f}{:10.4f}{:10.4f}{:10.4f}", | ||
blocks.AngleFunctionType.UREY_BRADLEY: "{:5d}{:5d}{:5d} 5{:10.4f}{:10.4f}{:10.4f}{:10.4f}", | ||
blocks.AngleFunctionType.QUARTIC_ANGLE: "{:5d}{:5d}{:5d} 6{:10.4f}{:10.4f}{:10.4f}{:10.4f}{:10.4f}{:10.4f}", | ||
blocks.AngleFunctionType.TABULATED_ANGLE: "{:5d}{:5d}{:5d} 8{:10.4f}{:10.4f}", | ||
blocks.AngleFunctionType.LINEAR_ANGLE: "{:5d}{:5d}{:5d} 9{:10.4f}{:10.4f}", | ||
blocks.AngleFunctionType.RESTRICTED_BENDING: "{:5d}{:5d}{:5d} 10{:10.4f}{:10.4f}", | ||
} | ||
|
||
atoms = [1, 2, 3] | ||
params = random_params(func_type.num_params) | ||
line = patterns[func_type].format(*atoms, *params) | ||
return line | ||
|
||
|
||
def create_topology_data(func_type: blocks.AngleFunctionType) -> str: | ||
""" | ||
https://manual.gromacs.org/current/reference-manual/topologies/topology-file-formats.html#tab-topfile2 | ||
""" | ||
line = generate_topology_line(func_type) | ||
return f""" | ||
[ moleculetype ] | ||
; Name nrexcl | ||
Example 3 | ||
|
||
[ atoms ] | ||
; nr type resnr residue atom cgnr charge mass typeB cha | ||
; residue 1 ASN rtp ASN q +1.0 | ||
1 NH3 1 ASN N 1 -0.3 14.007 ; qtot -0.3 | ||
2 HC 1 ASN H1 2 0.33 1.008 ; qtot 0.03 | ||
3 HC 1 ASN H2 3 0.33 1.008 ; qtot 0.36 | ||
|
||
[ angles ] | ||
; ai aj ak funct c0 c1 c2 c3 | ||
{line} | ||
|
||
[ molecules ] | ||
; Compound #mols | ||
Example 1 | ||
""" | ||
|
||
|
||
@pytest.mark.parametrize("func_type", blocks.AngleFunctionType) | ||
def test_angles(func_type: blocks.AngleFunctionType): | ||
with patch( | ||
"builtins.open", mock_open(read_data=create_topology_data(func_type)) | ||
) as mock: | ||
topol = gmx.fileformats.top.TOP("topol.top") | ||
[molecule] = topol.molecules | ||
[angle] = molecule.angles | ||
assert len(angle.gromacs["param"].keys()) == func_type.num_params |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use pytest's mocking support https://docs.pytest.org/en/7.1.x/how-to/monkeypatch.html so that it stays within pytest? Alternatively, we could install pytest-mock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, what's the motivation here – is it just consistency? I really, really don't enjoy how monkeypatch solves the problem. unittest is mock is basically a built-in and I see it used more frequently.
Happy to add more tests – that's always a good idea – but i will need some direction as to what to cover? My tests already covered all the possible fu values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consistency to stay within one testing framework
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test coverage itself looks decent enough