def test_run_0(self): # load traj farray = pt.load(filename="./data/tz2.truncoct.nc", top="./data/tz2.truncoct.parm7")[:2] fold = farray.copy() act = allactions.Action_Image() ptrajin = """ center :2-11 image center familiar com :6 """ # create 'strip' action stripact = allactions.Action_Strip() # creat datasetlist to hold distance data dsetlist = CpptrajDatasetList() dflist = DataFileList() # creat ActionList to hold actions alist = ActionList() top = farray.top # add two actions: Action_Strip and Action_Distance alist.add(allactions.Action_Center(), ArgList(":2-11"), top=top) alist.add(allactions.Action_Image(), ArgList("center familiar com :6"), top=top) # do checking alist.check_topology(top) farray2 = Trajectory() frame0 = Frame() # testing how fast to do the actions # loop all frames # use iterator to make faster loop # don't use "for i in range(farray.n_frames)" for frame in farray: # perform actions for each frame # we make a copy since we want to keep orginal Frame frame0 = frame.copy() alist.compute(frame0) # we need to keep the modified frame in farray2 farray2.append(frame0) # make sure that Action_Strip does its job in stripping assert farray2.n_frames == farray.n_frames fsaved = pt.iterload(cpptraj_test_dir + "/Test_Image/image4.crd.save", "data/tz2.truncoct.parm7") assert fsaved.n_frames == 2
def test_matrix(self): import numpy as np from pytraj import ArgList from pytraj import matrix as ma from pytraj.externals.six import iteritems matrix_test_dir = cpptraj_test_dir + "/Test_Matrix/" top_file = matrix_test_dir + "/1rrb_vac.prmtop" crd_file = matrix_test_dir + "/1rrb_vac.mdcrd" traj = pt.iterload(crd_file, top_file) with tempfolder(): state = pt.load_cpptraj_state(all_commands, traj) state.run() state_byres = pt.load_batch(traj, byres_cm) state_byres.run() byres_matlist = [] # no byres keyword for idx, line in enumerate(command_list): arg = ArgList(line) # get function act_key = arg.get_string_key("matrix") slist = arg.get_string_key('out').split(".") mask = arg.get_next_mask() fname = ".".join((slist[0], slist[-1], slist[1])) # get correct name func = ma.__dict__[act_key] # get command command = line.split(act_key)[1] matout = func(traj, command, dtype='ndarray') # cpptraj output has only 3 digits after decimal if 'byres' not in command: aa_eq(matout.flatten(), state.data[idx + 1].values) else: # save data for asserting later byres_matlist.append(matout) byres_arr = np.array(byres_matlist, dtype='f8') # only take byres datasets saved_matbyres = state_byres.data[[1, 3]].values aa_eq(byres_arr, saved_matbyres)
def load_cpptraj_output(txt, dtype=None): """load output from cpptraj Parameters ---------- txt : str cpptraj's trajin dtype : str, return data type Returns ------- if dtype is 'state', return CpptrajState if dtype is 'ndarray', return ndarray and so on """ from pytraj.core.c_core import _load_batch from pytraj.datasetlist import DatasetList from pytraj import ArgList commands = list(filter(lambda x: x, txt.split("\n"))) for idx, line in enumerate(commands): if 'parm' in line: arglist = ArgList(line) # use absolute path fname = os.path.abspath(arglist.get_string_key('parm')) commands[idx] = " ".join(('parm', fname)) if 'trajin' in line: arglist = ArgList(line) # use absolute path relative_fname = arglist.get_string_key('trajin') the_rest_of_line = ' '.join(line.split(relative_fname)[1:]) fname = os.path.abspath(relative_fname) commands[idx] = " ".join(('trajin', fname, the_rest_of_line)) txt = "\n".join([line for line in commands]) state = _load_batch(txt, traj=None) state.run() if dtype == 'state': out = state else: out = DatasetList(state.datasetlist) return out