Skip to content
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

Add unit tests for replica_exchange_EE.py, the utils module and a part of the analysis module #37

Merged
merged 43 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
721e320
Enhanced the code coverage for the function set_params
wehs7661 Mar 25, 2024
206fc11
Added test relevant to the MDP parameter 'pull'.
wehs7661 Mar 25, 2024
06070b5
Enhanced the code coverage of replica_exchange_EE.py
wehs7661 Mar 25, 2024
fbcab8d
Added a test using mpi4py
wehs7661 Mar 25, 2024
4a591cb
Added the YAML parameter working_dir
wehs7661 Mar 25, 2024
81c146a
Removed the YAML parameter working_dir and added lines to parse worki…
wehs7661 Mar 25, 2024
060c608
Minor tweaks for the working directory
wehs7661 Mar 25, 2024
2a76ea5
Added a unit test for _run_grompp
wehs7661 Mar 25, 2024
ab675f4
Some minor tweaks
wehs7661 Mar 25, 2024
8592b3e
Fixed a bug in test_mpi_func.py
wehs7661 Mar 25, 2024
80c6d57
Changed the image in config.yml for CircleCI
wehs7661 Mar 25, 2024
d57ba99
Modified .circleci/config.yml
wehs7661 Mar 25, 2024
b1876dc
Fixed a bug in the unit test of test_mpi_func.py
wehs7661 Mar 26, 2024
946d0c2
Modified test_run_grompp and .circleci/config.yml
wehs7661 Mar 26, 2024
7e2fb7d
Added a test for compare_MDPs
wehs7661 Mar 26, 2024
194235c
Add unit tests for utils.py and tweaked utils.py
wehs7661 Mar 26, 2024
60daa80
Added test_clustering.py
wehs7661 Mar 26, 2024
6d10c6d
Added tests for traj2transmtx and plot_rep_trajs
wehs7661 Mar 26, 2024
b557919
Some attempts of using assert_plt_calls to test multiple calls
wehs7661 Mar 28, 2024
3c7d2df
Tweaked stitch_time_series_for_sim to check continuity of trajectorie…
wehs7661 Mar 28, 2024
b399004
Fixed a minor bug in stitch_time_series_for_sim
wehs7661 Mar 28, 2024
8f88afa
Minor tweaks
wehs7661 Mar 28, 2024
bac786e
Tweaked stitch_time_series
wehs7661 Mar 28, 2024
fa205ac
Added a test for plot_state_trajs and developed save_and_exclude; Som…
wehs7661 Mar 28, 2024
fcb8787
Added a test for plot_state_hist
wehs7661 Mar 30, 2024
4610175
Added a test for calc_hist_rmse and plot_transit_time
wehs7661 Mar 31, 2024
460f3e8
Some intermediate work for test_plot_g_vecs
wehs7661 Apr 3, 2024
58488f6
Merge branch 'master' of https://github.com/wehs7661/ensemble_md into…
wehs7661 Apr 3, 2024
e549178
Modified calc_spectral_gap to allow uncertainty estimation; Added syn…
wehs7661 Apr 3, 2024
322af8e
Added synthesize_data.py to ensemble_md.analysis
wehs7661 Apr 3, 2024
cf3821d
Removed synthesize_transmtx from analyze_matrix.py; Simplified synthe…
wehs7661 Apr 3, 2024
9d291e9
Modified calc_spectral_gap and added calc_t_relax
wehs7661 Apr 3, 2024
904dd7b
Fixed the linting error; Modified calc_spectral_gap
wehs7661 Apr 3, 2024
f683e57
Merge pull request #39 from wehs7661/spectral_gap_err
wehs7661 Apr 3, 2024
7d6320a
Refined some previously written tests
wehs7661 Apr 6, 2024
dad30e3
Added example dhdl files for unit tests
wehs7661 Apr 8, 2024
8376dcd
Added new data for unit tests; Tweaked stitch_time_series and added i…
wehs7661 Apr 8, 2024
83fed44
Tweaked stitch_time_series_for_sim; Added unit tests for stitch_time_…
wehs7661 Apr 8, 2024
91806a8
Added an example run_REXEE_log.txt and a unit test for get_swaps
wehs7661 Apr 8, 2024
42fa84a
Added a unit test for plot_swaps
wehs7661 Apr 8, 2024
f18c1ad
Tweaked get_g_evolution and get_dg_evolution and added unit tests for…
wehs7661 Apr 9, 2024
0765a20
Added a unit test for plot_dg_evolution
wehs7661 Apr 9, 2024
0f9952b
Tweaked get_delta_w_updates and added a unit test for it
wehs7661 Apr 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ jobs:
name: Run unit tests
command: |
source $HOME/pkgs/bin/GMXRC
pip3 install pytest
pip3 install pytest
pip3 install pytest-mpi
pip3 install pytest-cov
pytest -vv --disable-pytest-warnings --cov=ensemble_md --cov-report=xml --color=yes ensemble_md/tests/
# COVERAGE_FILE=.coverage_1 pytest -vv --disable-pytest-warnings --cov=ensemble_md --cov-report=xml --color=yes ensemble_md/tests/
# COVERAGE_FILE=.coverage_2 mpirun -np 4 pytest -vv --disable-pytest-warnings --cov=ensemble_md --cov-report=xml --color=yes ensemble_md/tests/test_mpi_func.py --with-mpi
# coverage combine .coverage_*

- run:
name: CodeCov
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pip-delete-this-directory.txt
htmlcov/
.tox/
.coverage
.coverage_*
.coverage.*
.cache
.pytest_cache
Expand Down
10 changes: 5 additions & 5 deletions docs/simulations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,20 @@ include parameters for data analysis here.
:code:`/usr/local/gromacs/bin/gmx`, the path returned by the command :code:`which gmx`) should be used.
Note that REXEE only works with MPI-enabled GROMACS.

3.2. Input files
----------------
3.2. Input settings
-------------------

- :code:`gro`: (Required)
The input system configuration in the form of GRO file(s) used to initiate the REXEE simulation. If only one GRO file is specified,
The path of the input system configuration in the form of GRO file(s) used to initiate the REXEE simulation. If only one GRO file is specified,
it will be used to initiate all the replicas. If multiple GRO files are specified (using the YAML syntax),
the number of GRO files has to be the same as the number of replicas.
- :code:`top`: (Required)
The input system topology in the form of TOP file(s) used to initiate the REXEE simulation. If only one TOP file is specified,
The path of the input system topology in the form of TOP file(s) used to initiate the REXEE simulation. If only one TOP file is specified,
it will be used to initiate all the replicas. If multiple TOP files are specified (using the YAML syntax),
the number of TOP files has to be the same as the number of replicas. In the case where multiple TOP and GRO files are specified,
the i-th TOP file corresponds to the i-th GRO file.
- :code:`mdp`: (Required)
The input MDP file used to initiate the REXEE simulation. Specifically, this input MDP file will serve as a template for
The path of the input MDP file used to initiate the REXEE simulation. Specifically, this input MDP file will serve as a template for
customizing MDP files for all replicas. Therefore, the MDP template must have the whole range of :math:`λ` values.
and the corresponding weights (in fixed-weight simulations). This holds for REXEE simulations for multiple serial mutations as well.
For example, in an REXEE simulation that mutates methane to ethane in one replica and ethane to propane in the other replica, if
Expand Down
71 changes: 65 additions & 6 deletions ensemble_md/analysis/analyze_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from matplotlib import cm
from ensemble_md.utils.exceptions import ParseError
from ensemble_md.utils.exceptions import ParameterError
from ensemble_md.analysis import synthesize_data


def calc_transmtx(log_file, expanded_ensemble=True):
Expand Down Expand Up @@ -122,23 +123,30 @@ def calc_equil_prob(trans_mtx):
return equil_prob


def calc_spectral_gap(trans_mtx, atol=1e-8):
def calc_spectral_gap(trans_mtx, atol=1e-8, n_bootstrap=50, bootstrap_seed=None):
"""
Calculates the spectral gap of the input transition matrix.
Calculates the spectral gap of the input transition matrix and estimates its
uncertainty using the bootstrap method.

Parameters
----------
trans_mtx : np.ndarray
The input state transition matrix
The input transition matrix
atol: float
The absolute tolerance for checking the sum of columns and rows.
n_bootstrap: int
The number of bootstrap iterations for uncertainty estimation.
bootstrap_seed: int
The seed for the random number generator for the bootstrap method.

Returns
-------
spectral_gap : float
The spectral gap of the input transitio n matrix
The spectral gap of the input transitio n matrix.
spectral_gap_err : float
The estimated uncertainty of the spectral gap.
eig_vals : list
The list of eigenvalues
The list of eigenvalues.
"""
check_row = sum([np.isclose(np.sum(trans_mtx[i]), 1, atol=atol) for i in range(len(trans_mtx))])
check_col = sum([np.isclose(np.sum(trans_mtx[:, i]), 1, atol=atol) for i in range(len(trans_mtx))])
Expand All @@ -159,7 +167,58 @@ def calc_spectral_gap(trans_mtx, atol=1e-8):

spectral_gap = np.abs(eig_vals[0]) - np.abs(eig_vals[1])

return spectral_gap, eig_vals
# Estimate the uncertainty of the spectral gap
spectral_gap_list = []
n_performed = 0
while n_performed < n_bootstrap:
mtx_boot = synthesize_data.synthesize_transmtx(trans_mtx, seed=bootstrap_seed)[0]
check_row_boot = sum([np.isclose(np.sum(mtx_boot[i]), 1, atol=atol) for i in range(len(mtx_boot))])
check_col_boot = sum([np.isclose(np.sum(mtx_boot[:, i]), 1, atol=atol) for i in range(len(mtx_boot))])
if check_row_boot == len(mtx_boot):
eig_vals_boot, _ = np.linalg.eig(mtx_boot.T)
elif check_col_boot == len(mtx_boot):
eig_vals_boot, _ = np.linalg.eig(mtx_boot)
else:
continue

n_performed += 1
eig_vals_boot = np.sort(eig_vals_boot)[::-1]
spectral_gap_list.append(np.abs(eig_vals_boot[0]) - np.abs(eig_vals_boot[1]))

spectral_gap_err = np.std(spectral_gap_list, ddof=1)

return spectral_gap, spectral_gap_err, eig_vals


def calc_t_relax(spectral_gap, exchange_period, spectral_gap_err=None):
"""
Calculates the relaxation time given the spectral gap of a transition matrix of interest.
By defintion, the relaxation time is equal to the exchange period divided by the spectral gap.

Parameters
----------
spectral_gap: float
The input spectral gap.
exchange_period : float
The exchange period of the simulation in ps.
spectral_gap_err : float
The uncertainty of the spectral gap, which is used to calculate the uncertainty of the relaxation time using
error propagation.

Returns
-------
t_relax : float
The relaxation time in ps.
t_relax_err : float
The uncertainty of the relaxation time in ps.
"""
t_relax = exchange_period / spectral_gap
t_relax_err = None

if spectral_gap_err is not None:
t_relax_err = exchange_period * spectral_gap_err / spectral_gap ** 2 # error propagation

return t_relax, t_relax_err


def split_transmtx(trans_mtx, n_sim, n_sub):
Expand Down
Loading
Loading