-
Notifications
You must be signed in to change notification settings - Fork 81
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
MultiStateReporter variable pos/vel save frequency #712
Open
richardjgowers
wants to merge
10
commits into
choderalab:main
Choose a base branch
from
OpenFreeEnergy:multistatereporter_variable_pos_frequency
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4805553
allow MultiStateReporter to write positions and velocities at a diffe…
richardjgowers 59ca4d5
MultiStateReporter use 0 for do not write
richardjgowers 7a3bd40
Merge branch 'main' into multistatereporter_variable_pos_frequency
mikemhenry d8f80b9
Merge branch 'main' into multistatereporter_variable_pos_frequency
mikemhenry 8fba23f
WIP of multistatereporter tests
richardjgowers e0555c5
test for variable position saving
richardjgowers 2d88a33
more tests for smaller nc files
richardjgowers 9a24479
catch IndexError when file had no position/velocity data ever stored
richardjgowers 981fe1b
Merge branch 'main' into multistatereporter_variable_pos_frequency
mikemhenry 542f341
Merge branch 'main' into multistatereporter_variable_pos_frequency
mikemhenry 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,15 +269,20 @@ class TestReporter(object): | |
|
||
@staticmethod | ||
@contextlib.contextmanager | ||
def temporary_reporter(checkpoint_interval=1, checkpoint_storage=None, analysis_particle_indices=()): | ||
def temporary_reporter(checkpoint_interval=1, checkpoint_storage=None, | ||
position_interval=1, velocity_interval=1, | ||
analysis_particle_indices=()): | ||
"""Create and initialize a reporter in a temporary directory.""" | ||
with temporary_directory() as tmp_dir_path: | ||
storage_file = os.path.join(tmp_dir_path, 'temp_dir/test_storage.nc') | ||
assert not os.path.isfile(storage_file) | ||
reporter = MultiStateReporter(storage=storage_file, open_mode='w', | ||
checkpoint_interval=checkpoint_interval, | ||
checkpoint_storage=checkpoint_storage, | ||
analysis_particle_indices=analysis_particle_indices) | ||
analysis_particle_indices=analysis_particle_indices, | ||
position_interval=position_interval, | ||
velocity_interval=velocity_interval, | ||
) | ||
assert reporter.storage_exists(skip_size=True) | ||
yield reporter | ||
|
||
|
@@ -414,6 +419,124 @@ def test_write_sampler_states(self): | |
assert np.allclose(analysis_state.box_vectors / unit.nanometer, | ||
checkpoint_state.box_vectors / unit.nanometer) | ||
|
||
def test_writer_sampler_states_pos_interval(self): | ||
""" write positions and velocities every other frame""" | ||
analysis_particles = (1, 2) | ||
with self.temporary_reporter(analysis_particle_indices=analysis_particles, | ||
position_interval=2, velocity_interval=2, | ||
checkpoint_interval=2) as reporter: | ||
# Create sampler states. | ||
alanine_test = testsystems.AlanineDipeptideVacuum() | ||
positions = alanine_test.positions | ||
sampler_states = [mmtools.states.SamplerState(positions=positions) | ||
for _ in range(2)] | ||
|
||
# Check that after writing and reading, states are identical. | ||
for iteration in range(3): | ||
reporter.write_sampler_states(sampler_states, iteration=iteration) | ||
reporter.write_last_iteration(iteration) | ||
|
||
# Check first frame | ||
restored_sampler_states = reporter.read_sampler_states(iteration=0) | ||
for state, restored_state in zip(sampler_states, restored_sampler_states): | ||
assert np.allclose(state.positions, restored_state.positions) | ||
# By default stored velocities are zeros if not present in origin sampler_state | ||
assert np.allclose(np.zeros(state.positions.shape), restored_state.velocities) | ||
assert np.allclose(state.box_vectors / unit.nanometer, restored_state.box_vectors / unit.nanometer) | ||
# Second frame should not have positions or velocities | ||
restored_sampler_states = reporter.read_sampler_states(iteration=1, analysis_particles_only=True) | ||
for state, restored_state in zip(sampler_states, restored_sampler_states): | ||
# missing values are returned as numpy masked array | ||
# so we check that these arrays are all masked | ||
assert restored_state.positions._value.mask.all() | ||
assert restored_state.velocities._value.mask.all() | ||
assert restored_state.box_vectors is None # not periodic | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is something I need to double check, I'm a little confused why the checkpoint has a box and future frames don't, so this is still WIP till I figure that out |
||
|
||
restored_sampler_states = reporter.read_sampler_states(iteration=2, analysis_particles_only=True) | ||
for state, restored_state in zip(sampler_states, restored_sampler_states): | ||
assert np.allclose(state.positions[analysis_particles, :], restored_state.positions) | ||
# By default stored velocities are zeros if not present in origin sampler_state | ||
assert np.allclose(np.zeros((2, 3)), restored_state.velocities) | ||
assert np.allclose(state.box_vectors / unit.nanometer, restored_state.box_vectors / unit.nanometer) | ||
|
||
def test_write_sampler_states_no_vel(self): | ||
"""do not write velocities to trajectory file""" | ||
analysis_particles = (1, 2) | ||
with self.temporary_reporter(analysis_particle_indices=analysis_particles, | ||
position_interval=1, velocity_interval=0, | ||
checkpoint_interval=2) as reporter: | ||
# Create sampler states. | ||
alanine_test = testsystems.AlanineDipeptideVacuum() | ||
positions = alanine_test.positions | ||
sampler_states = [mmtools.states.SamplerState(positions=positions) | ||
for _ in range(2)] | ||
|
||
# Check that after writing and reading, states are identical. | ||
for iteration in range(3): | ||
reporter.write_sampler_states(sampler_states, iteration=iteration) | ||
reporter.write_last_iteration(iteration) | ||
|
||
# Check first frame | ||
restored_sampler_states = reporter.read_sampler_states(iteration=0, analysis_particles_only=True) | ||
for state, restored_state in zip(sampler_states, restored_sampler_states): | ||
# missing values are returned as numpy masked array | ||
# so we check that these arrays are all masked | ||
assert np.allclose(state.positions[analysis_particles, :], restored_state.positions) | ||
assert restored_state.velocities._value.mask.all() | ||
assert restored_state.box_vectors is None # not periodic | ||
|
||
# Second frame should not have positions or velocities | ||
restored_sampler_states = reporter.read_sampler_states(iteration=1, analysis_particles_only=True) | ||
for state, restored_state in zip(sampler_states, restored_sampler_states): | ||
assert np.allclose(state.positions[analysis_particles, :], restored_state.positions) | ||
assert restored_state.velocities._value.mask.all() | ||
assert restored_state.box_vectors is None # not periodic | ||
|
||
restored_sampler_states = reporter.read_sampler_states(iteration=2, analysis_particles_only=True) | ||
for state, restored_state in zip(sampler_states, restored_sampler_states): | ||
assert np.allclose(state.positions[analysis_particles, :], restored_state.positions) | ||
assert restored_state.velocities._value.mask.all() | ||
assert restored_state.box_vectors is None # not periodic | ||
|
||
def test_write_sampler_states_no_pos(self): | ||
"""do not write positions or velocities to trajectory file""" | ||
analysis_particles = (1, 2) | ||
with self.temporary_reporter(analysis_particle_indices=analysis_particles, | ||
position_interval=0, velocity_interval=0, | ||
checkpoint_interval=2) as reporter: | ||
# Create sampler states. | ||
alanine_test = testsystems.AlanineDipeptideVacuum() | ||
positions = alanine_test.positions | ||
sampler_states = [mmtools.states.SamplerState(positions=positions) | ||
for _ in range(2)] | ||
|
||
# Check that after writing and reading, states are identical. | ||
for iteration in range(3): | ||
reporter.write_sampler_states(sampler_states, iteration=iteration) | ||
reporter.write_last_iteration(iteration) | ||
|
||
# Check first frame | ||
restored_sampler_states = reporter.read_sampler_states(iteration=0, analysis_particles_only=True) | ||
for state, restored_state in zip(sampler_states, restored_sampler_states): | ||
# missing values are returned as numpy masked array | ||
# so we check that these arrays are all masked | ||
assert restored_state.positions._value.mask.all() | ||
assert restored_state.velocities._value.mask.all() | ||
assert restored_state.box_vectors is None # not periodic | ||
|
||
# Second frame should not have positions or velocities | ||
restored_sampler_states = reporter.read_sampler_states(iteration=1, analysis_particles_only=True) | ||
for state, restored_state in zip(sampler_states, restored_sampler_states): | ||
assert restored_state.positions._value.mask.all() | ||
assert restored_state.velocities._value.mask.all() | ||
assert restored_state.box_vectors is None # not periodic | ||
|
||
restored_sampler_states = reporter.read_sampler_states(iteration=2, analysis_particles_only=True) | ||
for state, restored_state in zip(sampler_states, restored_sampler_states): | ||
assert restored_state.positions._value.mask.all() | ||
assert restored_state.velocities._value.mask.all() | ||
assert restored_state.box_vectors is None # not periodic | ||
|
||
def test_analysis_particle_mismatch(self): | ||
"""Test that previously stored analysis particles is higher priority.""" | ||
blank_analysis_particles = () | ||
|
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.
this is something that I'm not 100% sure of currently. netCDF will return a numpy masked array when you access data that isn't present (here we're saving vels every other frame, so accessing velocities here has no data). I hadn't ever encountered these, so maybe it's not the best thing to return? (or maybe it is if it's the netCDF normal return).