Skip to content

Commit

Permalink
Handle classes without save_results
Browse files Browse the repository at this point in the history
If an analysis class have no save_results function we simply dump a 
pickel file with the name of the class.
  • Loading branch information
PicoCentauri committed Jan 29, 2021
1 parent 4574eb2 commit 58646cd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
31 changes: 14 additions & 17 deletions cli_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import importlib
import inspect
import re
import pickle
import sys
import warnings
from collections import defaultdict
Expand Down Expand Up @@ -516,23 +517,19 @@ def main(
analysis_kwargs.pop("func")

ac = analysis_callable(**analysis_kwargs)
results = ac.run(start=startframe,
stop=stopframe,
step=step,
verbose=verbose)

# prototype lines to test functionality TO REMOVE
print(analysis_kwargs)
sys.exit("Analysis complete. exiting...")
# extract results?
# here the same, how are the results collected?
# for RMSD we need to access the 'rmsd' attribute after .run() method.
# do we need to alter (add) a common interface on all the MDAnalysis
# interfaces. This would imply a intervention on the MDA code itself.
# we can definitively create a common method on all classes that links
# to the classes specific method/attribute where the results are stored.

save_results_to_some_file(results)
ac.run(start=startframe,
stop=stopframe,
step=step,
verbose=verbose)

try:
ac.save_results()
except AttributeError:
fname = analysis_callable.__name__ + ".pickle"
warnings.warn("No specific saving function."
"Pickling results into `{}`.".format(fname))
with open(fname, "wb") as f:
pickle.dump(ac, f)


def maincli(ap):
Expand Down
26 changes: 25 additions & 1 deletion tests/test_cli_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import pytest
import sys

from MDAnalysis.analysis.rdf import InterRDF
from MDAnalysis.tests.datafiles import PSF, DCD

# Workaround since we have no real module
sys.path.append("..")

from cli_main import convert_str_time
from cli_main import convert_str_time, main


@pytest.mark.parametrize('x, frame',
Expand All @@ -32,3 +35,24 @@ def test_convert_str_time_dt():
def test_convert_str_time_raise():
with pytest.raises(ValueError):
convert_str_time('0.1', dt=1)


class TestMain(object):

@pytest.fixture()
def kwargs(self):
kwargs = {}
kwargs["begin"] = "0"
kwargs["end"] = "1"
kwargs["dt"] = "1"
kwargs["verbose"] = False
kwargs["g1"] = "all"
kwargs["g2"] = "all"
kwargs["func"] = None
return kwargs

def test_pickling(self, kwargs, tmpdir):
with tmpdir.as_cwd():
with pytest.warns(UserWarning):
main(PSF, DCD, analysis_callable=InterRDF, **kwargs)
open("{}.pickle".format(InterRDF.__name__))

0 comments on commit 58646cd

Please sign in to comment.