Skip to content

Commit

Permalink
added test from pymbar issue 419
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemhenry committed Sep 26, 2024
1 parent 4916e4b commit 7d3dc30
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion openmmtools/tests/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import shutil
import sys
import tempfile
import time
from io import StringIO

import numpy as np
import yaml

import pytest
import requests

try:
import openmm
Expand Down Expand Up @@ -1861,7 +1863,7 @@ def test_analysis_opens_without_checkpoint(self):
del reporter
self.REPORTER(storage_path, checkpoint_storage=cp_file_mod, open_mode="r")

@pytest.mark.flaky(reruns=3)
@pytest.mark.skipif(sys.platform == "darwin", reason="seg faults on osx sometimes")
def test_storage_reporter_and_string(self):
"""Test that creating a MultiState by storage string and reporter is the same"""
thermodynamic_states, sampler_states, unsampled_states = copy.deepcopy(
Expand Down Expand Up @@ -2612,6 +2614,42 @@ def test_resume_velocities_from_legacy_storage(self):
state.velocities.value_in_unit_system(unit.md_unit_system) != 0
), "At least some velocity in sampler state from new checkpoint is expected to different from zero."

@pytest.fixture
def download_nc_file(tmpdir):
FILE_URL = "https://github.com/user-attachments/files/17156868/ala-thr.zip"
MAX_RETRIES = 3
RETRY_DELAY = 2 # Delay between retries (in seconds)
file_name = os.path.join(tmpdir, "ala-thr.nc")
retries = 0
while retries < MAX_RETRIES:
try:
# Send GET request to download the file
response = requests.get(FILE_URL, timeout=20) # Timeout to avoid hanging
response.raise_for_status() # Raise HTTPError for bad responses (4xx/5xx)
with open(file_name, "wb") as f:
f.write(response.content)
# File downloaded successfully, break out of retry loop
break

except (requests.exceptions.RequestException, requests.exceptions.HTTPError) as e:
retries += 1
if retries >= MAX_RETRIES:
pytest.fail(f"Failed to download file after {MAX_RETRIES} retries: {e}")
else:
print(f"Retrying download... ({retries}/{MAX_RETRIES})")
time.sleep(RETRY_DELAY) # Wait before retrying
yield file_name


def test_pymbar_issue_419(download_nc_file):
from openmmtools.multistate import MultiStateReporter, MultiStateSamplerAnalyzer

n_iterations = 1000
reporter_file = download_nc_file
reporter = MultiStateReporter(reporter_file)
analyzer = MultiStateSamplerAnalyzer(reporter, max_n_iterations=n_iterations)
f_ij, df_ij = analyzer.get_free_energy()


# ==============================================================================
# MAIN AND TESTS
Expand Down

0 comments on commit 7d3dc30

Please sign in to comment.