def test_pipe(self):
        traj = pt.iterload("data/tz2.ortho.nc", "data/tz2.ortho.parm7")

        # from TrajectoryIterator
        fi = pt.pipe(traj, ['autoimage', 'rms'])
        xyz = np.array([frame.xyz.copy() for frame in fi])
        t0 = traj[:].autoimage().superpose()
        aa_eq(xyz, t0.xyz)

        # from FrameIterator
        fi = pt.pipe(traj(), ['autoimage', 'rms'])
        xyz = np.array([frame.xyz.copy() for frame in fi])
        t0 = traj[:].autoimage().superpose()
        aa_eq(xyz, t0.xyz)

        # from FrameIterator with indices
        fi = pt.pipe(traj(0, 8, 2), ['autoimage', 'rms'])
        xyz = np.array([frame.xyz.copy() for frame in fi])
        t0 = traj[:8:2].autoimage().superpose()
        aa_eq(xyz, t0.xyz)

        # from TrajectoryIterator, cpptraj's command style
        fi = pt.pipe(traj, '''
        autoimage
        rms''')
        xyz = np.array([frame.xyz.copy() for frame in fi])
        t0 = traj[:].autoimage().superpose()
        aa_eq(xyz, t0.xyz)
Exemple #2
0
    def test_pipe(self):
        traj = pt.iterload(fn("tz2.ortho.nc"), fn("tz2.ortho.parm7"))

        # from TrajectoryIterator
        fi = pt.pipe(traj, ['autoimage', 'rms'])
        xyz = np.array([frame.xyz.copy() for frame in fi])
        t0 = traj[:].autoimage().superpose()
        aa_eq(xyz, t0.xyz)

        # from FrameIterator
        fi = pt.pipe(traj(), ['autoimage', 'rms'])
        xyz = np.array([frame.xyz.copy() for frame in fi])
        t0 = traj[:].autoimage().superpose()
        aa_eq(xyz, t0.xyz)

        # from FrameIterator with indices
        fi = pt.pipe(traj(0, 8, 2), ['autoimage', 'rms'])
        xyz = np.array([frame.xyz.copy() for frame in fi])
        t0 = traj[:8:2].autoimage().superpose()
        aa_eq(xyz, t0.xyz)

        # from TrajectoryIterator, cpptraj's command style
        fi = pt.pipe(traj, '''
        autoimage
        rms''')
        xyz = np.array([frame.xyz.copy() for frame in fi])
        t0 = traj[:].autoimage().superpose()
        aa_eq(xyz, t0.xyz)
Exemple #3
0
    def test_pieline(self):
        traj = pt.iterload(fn('tz2.nc'), fn('tz2.parm7'))

        fi = pt.pipe(traj, ['autoimage', ])
        aa_eq(pt.get_coordinates(fi), traj[:].autoimage().xyz)

        fi = pt.pipe(traj, ['autoimage', ], frame_indices=[3, 5])
        aa_eq(pt.get_coordinates(fi), traj[[3, 5]].autoimage().xyz)
Exemple #4
0
    def test_pieline(self):
        traj = pt.iterload("./data/tz2.nc", "./data/tz2.parm7")

        fi = pt.pipe(traj, ['autoimage', ])
        aa_eq(pt.get_coordinates(fi), traj[:].autoimage().xyz)

        fi = pt.pipe(traj, ['autoimage', ], frame_indices=[3, 5])
        aa_eq(pt.get_coordinates(fi), traj[[3, 5]].autoimage().xyz)
Exemple #5
0
 def test_from_iterables(self):
     '''test_from_iterables, tests are ind its doc. Test raise here
     '''
     traj = pt.datafiles.load_tz2_ortho()
     fi = pt.pipe(traj, ['autoimage'])
     # does not have Topology info
     self.assertRaises(ValueError, lambda: pt.Trajectory.from_iterable(fi))
 def test_from_iterables(self):
     '''test_from_iterables, tests are ind its doc. Test raise here
     '''
     traj = pt.datafiles.load_tz2_ortho()
     fi = pt.pipe(traj, ['autoimage'])
     # does not have Topology info
     self.assertRaises(ValueError, lambda: pt.Trajectory.from_iterable(fi))
Exemple #7
0
    def test_save_traj_from_file(self):
        traj = pt.iterload("./data/Tc5b.x", "./data/Tc5b.top")[:5]
        pt.write_traj(filename="./output/test_0.binpos",
                      traj=traj,
                      top="./data/Tc5b.top",
                      overwrite=True)

        savedtraj = pt.iterload("./output/test_0.binpos", traj.top)
        assert savedtraj.n_frames == traj.n_frames

        # write_xyz
        pt.write_traj("./output/test_0.nc",
                      traj.xyz,
                      top="./data/Tc5b.top",
                      overwrite=True)
        aa_eq(pt.iterload('output/test_0.nc', traj.top).xyz, traj.xyz)

        # write single Frame
        pt.write_traj("./output/test_0.nc",
                      traj[0],
                      top=traj.top,
                      overwrite=True)
        aa_eq(pt.iterload('output/test_0.nc', traj.top).xyz, traj[0].xyz)

        # raise if traj is None
        self.assertRaises(
            ValueError,
            lambda: pt.write_traj("./output/test_0.nc", None, overwrite=True))

        # raise if _top is None
        fi = pt.pipe(traj, [
            'autoimage',
        ])
        self.assertRaises(
            ValueError, lambda: pt.write_traj(
                "./output/test_0.nc", traj=fi, overwrite=True))

        # raise if Frame with frame_indices
        self.assertRaises(
            ValueError, lambda: pt.write_traj("./output/test_0.nc",
                                              traj[0],
                                              top="./data/Tc5b.top",
                                              frame_indices=[3, 2],
                                              overwrite=True))

        # raise if Frame with no Topology
        self.assertRaises(
            ValueError, lambda: pt.write_traj(
                "./output/test_0.nc", traj[0], overwrite=True))

        # test if xyz is not c-contiguous
        # pytraj will autoconvert to c-contiguous
        xyz = np.asfortranarray(traj.xyz)
        # make sure no ValueError or TypeError is raised
        pt.write_traj('output/xyz.nc', xyz, top=traj.top, overwrite=True)
Exemple #8
0
def worker_by_actlist(rank,
                      n_cores=2,
                      traj=None,
                      lines=None,
                      dtype='dict',
                      ref=None,
                      kwd=None):
    '''worker for cpptraj commands (string)
    '''
    # need to make a copy if lines since python's list is dangerous
    # it's easy to mess up with mutable list
    # do not use lines.copy() since this is not available in py2.7
    # Note: dtype is a dummy argument, it is always 'dict'
    if lines is None:
        lines = []
    frame_indices = kwd.pop('frame_indices', None)

    new_lines, need_ref = check_valid_command(lines)

    if frame_indices is None:
        my_iter = traj._split_iterators(n_cores, rank=rank)
    else:
        my_iter = traj.iterframe(
            frame_indices=np.array_split(frame_indices, n_cores)[rank])

    if ref is not None:
        if isinstance(ref, Frame):
            reflist = [
                ref,
            ]
        else:
            # list/tuplex
            reflist = ref
    else:
        reflist = [
            traj[0],
        ] if need_ref else []

    dslist = CpptrajDatasetList()

    if reflist:
        for ref_ in reflist:
            ref_dset = dslist.add('reference')
            ref_dset.top = traj.top
            ref_dset.add_frame(ref_)

    # create Frame generator
    fi = pipe(my_iter, commands=new_lines, dslist=dslist)

    # just iterate Frame to trigger calculation.
    for _ in fi:
        pass

    # remove ref
    return (rank, dslist[len(reflist):].to_dict())
Exemple #9
0
def worker_by_actlist(rank,
                      n_cores=2,
                      traj=None,
                      lines=None,
                      dtype='dict',
                      ref=None,
                      kwd=None):
    '''worker for cpptraj commands (string)
    '''
    # need to make a copy if lines since python's list is dangerous
    # it's easy to mess up with mutable list
    # do not use lines.copy() since this is not available in py2.7
    # Note: dtype is a dummy argument, it is always 'dict'
    if lines is None:
        lines = []
    frame_indices = kwd.pop('frame_indices', None)

    new_lines, need_ref = check_valid_command(lines)

    if frame_indices is None:
        my_iter = traj._split_iterators(n_cores, rank=rank)
    else:
        my_iter = traj.iterframe(
            frame_indices=np.array_split(frame_indices, n_cores)[rank])

    if ref is not None:
        if isinstance(ref, Frame):
            reflist = [ref, ]
        else:
            # list/tuplex
            reflist = ref
    else:
        reflist = [traj[0],] if need_ref else []

    dslist = CpptrajDatasetList()

    if reflist:
        for ref_ in reflist:
            ref_dset = dslist.add('reference')
            ref_dset.top = traj.top
            ref_dset.add_frame(ref_)

    # create Frame generator
    fi = pipe(my_iter, commands=new_lines, dslist=dslist)

    # just iterate Frame to trigger calculation.
    for _ in fi:
        pass

    # remove ref
    return (rank, dslist[len(reflist):].to_dict())
Exemple #10
0
    def test_reference(self):
        traj = pt.iterload(fn("tz2.ortho.nc"), fn("tz2.ortho.parm7"))

        # store reference
        dslist = CpptrajDatasetList()
        ref = dslist.add('reference')
        ref.top = traj.top
        ref.append(traj[3])

        fi = pt.pipe(traj, ['autoimage', 'rms refindex 0 @CA'], dslist=dslist)
        xyz = np.array([frame.xyz.copy() for frame in fi])
        t0 = (traj[:].autoimage().superpose(ref=traj[3], mask='@CA'))
        aa_eq(xyz, t0.xyz)

        t1 = traj[:].autoimage()
        aa_eq(pt.rmsd(t1, ref=traj[3], mask='@CA'), dslist[-1].values)
Exemple #11
0
def test_save_traj_from_file():
    traj = pt.iterload(tc5b_trajin, tc5b_top)[:5]
    with tempfolder():
        pt.write_traj(filename="test_0.binpos",
                      traj=traj,
                      top=tc5b_top,
                      overwrite=True)

        savedtraj = pt.iterload("test_0.binpos", traj.top)
        assert savedtraj.n_frames == traj.n_frames

        # write_xyz
        pt.write_traj("test_0.nc", traj.xyz, top=tc5b_top, overwrite=True)
        aa_eq(pt.iterload('test_0.nc', traj.top).xyz, traj.xyz)

        # write single Frame
        pt.write_traj("test_0.nc", traj[0], top=traj.top, overwrite=True)
        aa_eq(pt.iterload('test_0.nc', traj.top).xyz, traj[0].xyz)

        # raise if traj is None
        with pytest.raises(ValueError):
            pt.write_traj("test_0.nc", None, overwrite=True)

        # raise if _top is None
        fi = pt.pipe(traj, [
            'autoimage',
        ])
        with pytest.raises(ValueError):
            pt.write_traj("test_0.nc", traj=fi, overwrite=True)

        # raise if Frame with frame_indices:
        with pytest.raises(ValueError):
            pt.write_traj("test_0.nc",
                          traj[0],
                          top=tc5b_top,
                          frame_indices=[3, 2],
                          overwrite=True)

        # raise if Frame with no Topology
        with pytest.raises(ValueError):
            pt.write_traj("test_0.nc", traj[0], overwrite=True)

        # test if xyz is not c-contiguous
        # pytraj will autoconvert to c-contiguous
        xyz = np.asfortranarray(traj.xyz)
        # make sure no ValueError or TypeError is raised
        pt.write_traj('xyz.nc', xyz, top=traj.top, overwrite=True)
Exemple #12
0
    def test_get_coordinates_trajecotoryiterator(self):
        '''immutable pytraj.TrajectoryIterator
        '''
        traj = self.traj_tz2_ortho.copy()

        # all coordinates
        xyz = pt.get_coordinates(traj)
        aa_eq(traj.xyz, xyz)

        # given frames
        xyz = pt.get_coordinates(traj, frame_indices=[0, 5])
        aa_eq(traj[[0, 5]].xyz, xyz)

        # given frames, autoimage=True
        xyz = pt.get_coordinates(traj, frame_indices=[0, 5], autoimage=True)
        aa_eq(traj[[0, 5]].autoimage().xyz, xyz)

        # given frames, autoimage=True, rmsfit=ref
        ref = traj[-3]
        pt.autoimage(ref, top=traj.top)
        xyz = pt.get_coordinates(traj,
                                 frame_indices=[0, 5],
                                 autoimage=True,
                                 rmsfit=ref)
        aa_eq(traj[[0, 5]].autoimage().superpose(ref=ref).xyz, xyz)

        xyz = pt.get_coordinates(traj,
                                 frame_indices=range(3),
                                 autoimage=True,
                                 rmsfit=ref)

        # with mask
        xyz = pt.get_coordinates(traj, mask='@CA')
        aa_eq(xyz, traj['@CA'].xyz)

        # slow
        fi = pt.pipe(traj, ['autoimage'])
        aa_eq(pt.get_coordinates(fi), traj[:].autoimage().xyz)

        # raise
        self.assertRaises(
            ValueError,
            lambda: pt.get_coordinates(traj(), frame_indices=[0, 2]))
Exemple #13
0
    def test_reference(self):
        traj = pt.iterload("data/tz2.ortho.nc", "data/tz2.ortho.parm7")

        # store reference
        dslist = CpptrajDatasetList()
        ref = dslist.add_new('reference')
        ref.top = traj.top
        ref.append(traj[3])

        fi = pt.pipe(traj,
                     ['autoimage', 'rms refindex 0 @CA'],
                     dslist=dslist)
        xyz = np.array([frame.xyz.copy() for frame in fi])
        t0 = (traj[:]
              .autoimage()
              .superpose(ref=traj[3], mask='@CA'))
        print('ok')
        aa_eq(xyz, t0.xyz)

        t1 = traj[:].autoimage()
        aa_eq(pt.rmsd(t1, ref=traj[3], mask='@CA'), dslist[-1].values)