Weird things in pytraj

Explicit copy

In [1]: import pytraj as pt

In [2]: traj = pt.iterload('tz2.nc', 'tz2.parm7')

In [3]: xyz = [frame.xyz for frame in traj]

# coords of 1st and last frames are the same and are [0.0, ...]
In [4]: print(xyz[0][0])
[ 0.    0.    1.89]

In [5]: print(xyz[-1][0])
[ 0.    0.    1.89]

# need to explicitly copy
In [6]: xyz = [frame.xyz.copy() for frame in traj]

# coords of 1st and last frames are the same
In [7]: print(xyz[0][0])
[-1.889  9.159  7.569]

In [8]: print(xyz[-1][0])
[ 4.466  8.801  1.89 ]

Why does this happen? When iterating pytraj.TrajectoryIteatory (by pytraj.iterload), pytraj create a Frame object behaving like a buffer. When getting to next snapshot, new coordinates with box will replace the old ones. When reaching final snapshot, the Frame is free and all coordinates are set to 0. We intend to design this behaviour since data copying is expensive. If you want to keep the coordinates, please make sure to ‘copy’.

Or you can use pytraj.get_coordinates

In [9]: xyz = pt.get_coordinates(traj, frame_indices=[0, 5, 6])