pytraj.FrameIterator

pytraj.trajectory.frameiter.iterframe(traj, *args, **kwd)

create frame iterator with given indices, mask or some iter_options

Examples

>>> import pytraj as pt
>>> traj = pt.datafiles.load_tz2_ortho()
>>> for frame in pt.iterframe(traj, 0, 8, 2): pass
>>> for frame in pt.iterframe(traj, 4, mask='@CA'): print(frame)
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>

# create frame iterator for given indices >>> for frame in pt.iterframe(traj, frame_indices=[0, 7, 3]): print(frame) <Frame with 5293 atoms> <Frame with 5293 atoms> <Frame with 5293 atoms>

>>> fi = pt.iterframe(traj)
>>> # iterframe its self
>>> fi = pt.iterframe(fi)
pytraj.trajectory.frameiter.iterchunk(traj, *args, **kwd)

iterate traj by chunk

Parameters:

traj : TrajectoryIterator

chunksize : int

the number of frames in each chunk

start : int, default 0

start frame to iterate

start : int, default -1 (last frame)

stop frame

autoimage : bool, default False

if True, do autoimage for chunk

Examples

>>> import pytraj as pt
>>> traj = pt.datafiles.load_tz2_ortho()
>>> for frame in pt.iterchunk(traj, 4): pass
>>> for frame in pt.iterchunk(traj, chunksize=4, start=2): pass
>>> for frame in pt.iterchunk(traj, chunksize=4, start=2, stop=9): pass
>>> for frame in pt.iterchunk(traj, chunksize=4, autoimage=True): pass
class pytraj.trajectory.frameiter.FrameIterator(fi_generator, original_top=None, new_top=None, start=0, stop=-1, step=1, mask='', autoimage=False, rmsfit=None, n_frames=None, copy=True, frame_indices=None)

Bases: object

create this class to hold all iterating information. This class is for internal use.

Parameters:

top : new Topology

original_top : original Topology

start, stop, step : int

mask : str or None, default None (all atoms)

only take atom with given mask

frame_indices : iterable, default: None

if frame_indices is not None: ignore (start, stop, step)

autoimage : bool, default: False

if autoimage, perform autoimage

rmsfit : int or a tuple, default False

if rmsfit, perform rms fit to reference. If rmsfit is an integer, perform rms fit to indicated frame for all atoms. If rmsfit is a tuple, perform rmsfit to given frame with given mask. if both autoimage and rmsfit are specified, do autoimage first.

n_frames : total number of frame. read-only

copy : bool, defaul: True

if True, always make a copy of Frame when iterating.

Notes

if ‘autoimage’ and ‘rmsfit’, reference frame is also autoimaged

Examples

>>> # short cut:
>>> # create FrameIterator with start=0, stop=8, step=2
>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')
>>> fi = traj(0, 8, 2)
>>> # perform radgyr calculation with FrameIterator
>>> pt.radgyr(traj(0, 8, 2))
array([ 18.91114428,  18.84969884,  18.8568644 ,  18.9430491 ])
>>> # create FrameIterator with start, stop, step = 0, 8, 2
>>> # autoimage=False, rmsfit=False
>>> fi = traj.iterframe(0, 8, 2)
>>> # create FrameIterator with start, stop, step = 2, 8, 1
>>> # autoimage=False, rmsfit=False
>>> fi = traj.iterframe(2, 8)
>>> # create FrameIterator with start, stop, step = 2, 8, 1
>>> # autoimage=False, rmsfit=False, mask='@CA'
>>> fi = traj.iterframe(2, 8, mask='@CA')
>>> # create FrameIterator with start, stop, step = 2, 8, 1
>>> # autoimage=True, rmsfit=False, mask='@CA'
>>> for frame in traj.iterframe(2, 8, autoimage=True, mask='@CA'): print(frame)
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
>>> # with rmsfit
>>> for frame in traj.iterframe(2, 8, autoimage=True, rmsfit=0): pass
>>> for frame in traj.iterframe(2, 8, autoimage=True, rmsfit=(1, '!@H=')): pass
>>> # rmsfit
>>> fi = traj.iterframe(2, 8, rmsfit=(0, '@CA'))
>>> fi.n_frames
6
>>> fi = traj.iterframe(2, 8, mask='@1,2,3,4,5')
>>> fi.n_atoms
5
>>> # make copy of each Frame
>>> fi = traj.iterframe(2, 8, mask='@1,2,3,4,5', copy=True)
>>> fi = traj.iterframe(2, 8, rmsfit=3)
>>> fi = traj.iterframe(2, 8, mask='@1,2,3,4,5')
>>> # explit use copy=True to give different Frame with list
>>> fi = traj.iterframe(2, 8)
>>> fi.copy = True
>>> pt.radgyr(list(fi), top=traj.top)
array([ 18.84969884,  18.90449256,  18.8568644 ,  18.88917208,
        18.9430491 ,  18.88878079])

Attributes

n_atoms
n_frames

Methods

save([filename, overwrite, options]) save to different file format.
n_atoms
n_frames
save(filename='', overwrite=False, options='', *args, **kwd)

save to different file format.

Notes

FrameIterator will be exhausted since this is an iterator.

Examples

>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')
>>> fi = traj(2, 8, 2, mask='@CA')
>>> fi.save('output/test.nc', overwrite=True)
>>> # short version
>>> traj(2, 8, 2, mask='@CA').save('output/test.nc', overwrite=True)