|
| 1 | +# MDPOW: base.py |
| 2 | +# 2022 Cade Duckworth |
| 3 | + |
| 4 | +""" |
| 5 | +:mod:`mdpow.workflows.base` --- Automated workflow base functions |
| 6 | +================================================================= |
| 7 | +
|
| 8 | +To analyze multiple MDPOW projects, provide :func:`project_paths` |
| 9 | +with the top-level directory containing all MDPOW projects' simulation data |
| 10 | +to obtain a :class:`pandas.DataFrame` containing the project information |
| 11 | +and paths. Then, :func:`automated_project_analysis` takes as input the |
| 12 | +aforementioned :class:`pandas.DataFrame` and runs the specified |
| 13 | +:class:`~mdpow.analysis.ensemble.EnsembleAnalysis` for all MDPOW projects |
| 14 | +under the top-level directory provided to :func:`project_paths`. |
| 15 | +
|
| 16 | +.. seealso:: :mod:`~mdpow.workflows.registry` |
| 17 | +
|
| 18 | +.. autofunction:: project_paths |
| 19 | +.. autofunction:: automated_project_analysis |
| 20 | +
|
| 21 | +""" |
| 22 | + |
| 23 | +import os |
| 24 | +import re |
| 25 | +import pandas as pd |
| 26 | + |
| 27 | +from mdpow.workflows import registry |
| 28 | + |
| 29 | +import logging |
| 30 | + |
| 31 | +logger = logging.getLogger('mdpow.workflows.base') |
| 32 | + |
| 33 | +def project_paths(parent_directory=None, csv=None, csv_save_dir=None): |
| 34 | + """Takes a top directory containing MDPOW projects and determines |
| 35 | + the molname, resname, and path, of each MDPOW project within. |
| 36 | +
|
| 37 | + Optionally takes a .csv file containing `molname`, `resname`, and |
| 38 | + `paths`, in that order. |
| 39 | +
|
| 40 | + :keywords: |
| 41 | +
|
| 42 | + *parent_directory* |
| 43 | + the path for the location of the top directory |
| 44 | + under which the subdirectories of MDPOW simulation |
| 45 | + data exist, additionally creates a 'project_paths.csv' file |
| 46 | + for user manipulation of metadata and for future reference |
| 47 | +
|
| 48 | + *csv* |
| 49 | + .csv file containing the molecule names, resnames, |
| 50 | + and paths, in that order, for the MDPOW simulation |
| 51 | + data to be iterated over must contain header of the |
| 52 | + form: `molecule,resname,path` |
| 53 | +
|
| 54 | + *csv_save_dir* |
| 55 | + optionally provided directory to save .csv file, otherwise, |
| 56 | + data will be saved in current working directory |
| 57 | +
|
| 58 | + :returns: |
| 59 | +
|
| 60 | + *project_paths* |
| 61 | + :class:`pandas.DataFrame` containing MDPOW project metadata |
| 62 | +
|
| 63 | + .. rubric:: Example |
| 64 | + |
| 65 | + Typical Workflow:: |
| 66 | +
|
| 67 | + project_paths = project_paths(parent_directory='/foo/bar/MDPOW_projects') |
| 68 | + automated_project_analysis(project_paths) |
| 69 | +
|
| 70 | + or:: |
| 71 | +
|
| 72 | + project_paths = project_paths(csv='/foo/bar/MDPOW.csv') |
| 73 | + automated_project_analysis(project_paths) |
| 74 | +
|
| 75 | + """ |
| 76 | + |
| 77 | + if parent_directory is not None: |
| 78 | + |
| 79 | + locations = [] |
| 80 | + |
| 81 | + reg_compile = re.compile('FEP') |
| 82 | + for dirpath, dirnames, filenames in os.walk(parent_directory): |
| 83 | + result = [dirpath.strip() for dirname in dirnames if reg_compile.match(dirname)] |
| 84 | + if result: |
| 85 | + locations.append(result[0]) |
| 86 | + |
| 87 | + resnames = [] |
| 88 | + |
| 89 | + for loc in locations: |
| 90 | + res_temp = loc.strip().split('/') |
| 91 | + resnames.append(res_temp[-1]) |
| 92 | + |
| 93 | + project_paths = pd.DataFrame( |
| 94 | + { |
| 95 | + 'molecule': resnames, |
| 96 | + 'resname': resnames, |
| 97 | + 'path': locations |
| 98 | + } |
| 99 | + ) |
| 100 | + if csv_save_dir is not None: |
| 101 | + project_paths.to_csv(f'{csv_save_dir}/project_paths.csv', index=False) |
| 102 | + logger.info(f'project_paths saved under {csv_save_dir}') |
| 103 | + else: |
| 104 | + current_directory = os.getcwd() |
| 105 | + project_paths.to_csv('project_paths.csv', index=False) |
| 106 | + logger.info(f'project_paths saved under {current_directory}') |
| 107 | + |
| 108 | + elif csv is not None: |
| 109 | + locations = pd.read_csv(csv) |
| 110 | + project_paths = locations.sort_values(by=['molecule', 'resname', 'path']).reset_index(drop=True) |
| 111 | + |
| 112 | + return project_paths |
| 113 | + |
| 114 | +def automated_project_analysis(project_paths, ensemble_analysis, **kwargs): |
| 115 | + """Takes a :class:`pandas.DataFrame` created by :func:`~mdpow.workflows.base.project_paths` |
| 116 | + and iteratively runs the specified :class:`~mdpow.analysis.ensemble.EnsembleAnalysis` |
| 117 | + for each of the projects by running the associated automated workflow |
| 118 | + in each project directory returned by :func:`~mdpow.workflows.base.project_paths`. |
| 119 | +
|
| 120 | + Compatibility with more automated analyses in development. |
| 121 | +
|
| 122 | + :keywords: |
| 123 | +
|
| 124 | + *project_paths* |
| 125 | + :class:`pandas.DataFrame` that provides paths to MDPOW projects |
| 126 | +
|
| 127 | + *ensemble_analysis* |
| 128 | + name of the :class:`~mdpow.analysis.ensemble.EnsembleAnalysis` |
| 129 | + that corresponds to the desired automated workflow module |
| 130 | +
|
| 131 | + *kwargs* |
| 132 | + keyword arguments for the supported automated workflows, |
| 133 | + see the :mod:`~mdpow.workflows.registry` for all available |
| 134 | + workflows and their call signatures |
| 135 | +
|
| 136 | + .. rubric:: Example |
| 137 | +
|
| 138 | + A typical workflow is the automated dihedral analysis from |
| 139 | + :mod:`mdpow.workflows.dihedrals`, which applies the *ensemble analysis* |
| 140 | + :class:`~mdpow.analysis.dihedral.DihedralAnalysis` to each project. |
| 141 | + The :data:`~mdpow.workflows.registry.registry` contains this automated |
| 142 | + workflow under the key *"DihedralAnalysis"* and so the automated execution |
| 143 | + for all `project_paths` (obtained via :func:`project_paths`) is performed by |
| 144 | + passing the specific key to :func:`automated_project_analysis`:: |
| 145 | +
|
| 146 | + project_paths = project_paths(parent_directory='/foo/bar/MDPOW_projects') |
| 147 | + automated_project_analysis(project_paths, ensemble_analysis='DihedralAnalysis', **kwargs) |
| 148 | +
|
| 149 | + """ |
| 150 | + |
| 151 | + for row in project_paths.itertuples(): |
| 152 | + molname = row.molecule |
| 153 | + resname = row.resname |
| 154 | + dirname = row.path |
| 155 | + |
| 156 | + logger.info(f'starting {molname}') |
| 157 | + |
| 158 | + try: |
| 159 | + registry.registry[ensemble_analysis](dirname=dirname, resname=resname, molname=molname, **kwargs) |
| 160 | + |
| 161 | + logger.info(f'{molname} completed') |
| 162 | + |
| 163 | + except KeyError as err: |
| 164 | + msg = (f"Invalid ensemble_analysis {err}. An EnsembleAnalysis type that corresponds " |
| 165 | + "to an existing automated workflow module must be input as a kwarg. " |
| 166 | + "ex: ensemble_analysis='DihedralAnalysis'") |
| 167 | + logger.error(f'{err} is an invalid selection') |
| 168 | + |
| 169 | + raise KeyError(msg) |
| 170 | + |
| 171 | + except TypeError as err: |
| 172 | + msg = (f"Invalid ensemble_analysis {ensemble_analysis}. An EnsembleAnalysis type that " |
| 173 | + "corresponds to an existing automated workflow module must be input as a kwarg. " |
| 174 | + "ex: ensemble_analysis='DihedralAnalysis'") |
| 175 | + logger.error(f'workflow module for {ensemble_analysis} does not exist yet') |
| 176 | + |
| 177 | + raise TypeError(msg) |
| 178 | + |
| 179 | + logger.info('all analyses completed') |
| 180 | + return |
0 commit comments