try pytraj
online:
seperate module, only use stdlib If want to use external package, import it inside the function
This module stores all useful functions that does not fit to anywhere else.
pytraj.tools.
as_2darray
(traj_or_xyz)¶reshape traj.xyz to 2d array, shape=(n_frames, n_atoms * 3)
Notes
if traj
is mutable, this method return a view of its coordinates.
Examples
>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')
>>> traj.xyz.shape
(10, 5293, 3)
>>> as_2darray(traj).shape
(10, 15879)
>>> as_2darray(traj.xyz).shape
(10, 15879)
pytraj.tools.
as_3darray
(xyz)¶reshape xyz to 3d array, shape=(n_frames, n_atoms, 3)
Examples
>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')
>>> traj.xyz.shape
(10, 5293, 3)
>>> xyz_2d = as_2darray(traj)
>>> xyz_2d.shape
(10, 15879)
>>> as_3darray(xyz_2d).shape
(10, 5293, 3)
>>> as_3darray(traj.xyz)
Traceback (most recent call last):
...
ValueError: ndim must be 2
pytraj.tools.
block_average
(self, n_chunk)¶average by chunk
Examples
>>> block_average(range(30), 3)
array([ 4.5, 14.5, 24.5])
pytraj.tools.
compose
(*funcs)¶Notes: copied from pandas (added pytraj’s example) see license in pytraj/license/
Compose 2 or more callables
Examples
>>> import pytraj as pt
>>> from pytraj.testing import get_fn
>>> func = compose(pt.calc_radgyr, pt.iterload)
>>> fname, tname = get_fn('tz2')
>>> func(fname, tname)
array([ 18.91114428, 18.93654996, 18.84969884, 18.90449256,
18.8568644 , 18.88917208, 18.9430491 , 18.88878079,
18.91669565, 18.87069722])
pytraj.tools.
concat_dict
(iterables)¶concat dict
Examples
>>> dict_0 = {'x' : [1, 2, 3,]}
>>> dict_1 = {'x' : [4, 5]}
>>> concat_dict((dict_0, dict_1))
OrderedDict([('x', array([1, 2, 3, 4, 5]))])
pytraj.tools.
dict_to_ndarray
(dict_of_array)¶convert OrderedDict to numpy array
Examples
>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')
>>> dslist = pt.multidihedral(traj, dhtypes='phi psi', resrange='2', dtype='dict')
>>> list(dslist.keys())
['phi:2', 'psi:2']
>>> dict_to_ndarray(dslist)
array([[-128.72617304, -109.44321317, -130.93278259, ..., -146.70146067,
-121.58263643, -112.74485175],
[ 150.11249102, 142.52303293, 131.11609265, ..., 123.44883266,
141.18992429, 120.03168126]])
pytraj.tools.
dir_
(obj)¶return a list of obj’s attributes with no private method
pytraj.tools.
estimate_size
(n_frames, n_atoms, dtype='f8')¶return MB
>>> import pytraj as pt
>>> traj = pt.datafiles.load_tz2_ortho()
>>> estimate_size(traj.n_frames, traj.n_atoms, 'f8')
1.2114715576171875
>>> estimate_size(traj.n_frames, traj.n_atoms, 'f4')
0.6057357788085938
pytraj.tools.
find_lib
(libname)¶return a list of all library files
pytraj.tools.
flatten
(x)¶Returns a single, flat list which contains all elements retrieved from the sequence and all recursively contained sub-sequences (iterables).
Notes
from: http://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks
Examples
>>> [1, 2, [3,4], (5,6)]
[1, 2, [3, 4], (5, 6)]
>>> flatten([[[1,2,3], (42,None)], [4,5], [6], 7, (8,9,10)])
[1, 2, 3, 42, None, 4, 5, 6, 7, 8, 9, 10]
pytraj.tools.
grep_key
(self, key)¶grep key
Examples
>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')
>>> dslist = pt.calc_multidihedral(traj, dtype='dataset')
>>> pt.tools.grep_key(dslist, 'psi').values[0]
array([ 176.6155643 , 166.82129574, 168.79510009, 167.42561927,
151.18334989, 134.17610997, 160.99207908, 165.1126967 ,
147.94332109, 145.42901383])
pytraj.tools.
groupby
(key, seq)¶Examples
>>> names = ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank']
>>> groupby(len, names)
{3: ['Bob', 'Dan'], 5: ['Alice', 'Edith', 'Frank'], 7: ['Charlie']}
pytraj.tools.
iteritems
(d, **kw)¶Return an iterator over the (key, value) pairs of a dictionary.
Examples
>>> for k, v in iteritems({'x': 3, 'y': 4}): print(k, v) :
x 3
y 4
pytraj.tools.
make_fake_topology
(n_atoms)¶make fake Topology, just for writing xyz array to supported formats (netcdf, dcd, trr, ...)
>>> import pytraj as pt
>>> top = pt.tools.make_fake_topology(100)
>>> top.n_atoms
100
>>> isinstance(top, pt.Topology)
True
>>> import numpy as np
>>> xyz = np.random.rand(10*100*3).reshape(10, 100, 3)
>>> traj0 = pt.Trajectory(xyz=xyz, top=top)
>>> pt.write_traj('output/test.nc', traj0, overwrite=True)
>>> traj = pt.iterload('output/test.nc', top=top)
>>> traj.n_atoms
100
pytraj.tools.
mean_and_error
(a1, a2)¶calculate mean and error from two 1D array-like
Examples
>>> import pytraj as pt
>>> a0 = [2, 4, 6]
>>> a1 = [3, 5, 7]
>>> mean_and_error(a0, a1)
(4.5, 0.5)
pytraj.tools.
merge_coordinates
(iterables)¶merge_coordinates from frames
Examples
>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')
>>> merge_coordinates(traj(0, 3))
array([[ 15.55458927, 28.54844856, 17.18908691],
[ 16.20579147, 29.07935524, 17.74959946],
[ 14.95065975, 29.27651787, 16.83513069],
...,
[ 34.09399796, 7.88915873, 15.6500845 ],
[ 34.4160347 , 8.53098011, 15.01716137],
[ 34.29132462, 8.27471733, 16.50368881]])
pytraj.tools.
merge_frame_from_trajs
(trajlist)¶Examples
>>> import numpy as np
>>> import pytraj as pt
>>> traj0 = pt.load_sample_data('tz2')[:3]
>>> traj1 = pt.load_sample_data('tz2')[3:6]
>>> traj2 = pt.load_sample_data('tz2')[6:9]
>>> print(traj0.n_atoms, traj1.n_atoms, traj2.n_atoms)
5293 5293 5293
>>> for frame in pt.tools.merge_frame_from_trajs((traj0, traj1, traj2)): print(frame)
<Frame with 15879 atoms>
<Frame with 15879 atoms>
<Frame with 15879 atoms>
pytraj.tools.
merge_frames
(iterables)¶merge from frames to a single Frame. Order matters. Examples ——– >>> import pytraj as pt >>> traj = pt.load_sample_data(‘tz2’) >>> traj[0] <Frame with 5293 atoms> >>> merge_frames(traj(0, 3)) <Frame with 15879 atoms>
pytraj.tools.
merge_trajs
(traj1, traj2, start_new_mol=True, n_frames=None)¶Notes
Code might be changed
Examples
>>> import pytraj as pt
>>> import numpy as np
>>> traj1 = pt.load_sample_data('ala3')[:1]
>>> traj2 = pt.load_sample_data('tz2')[:1]
>>> traj3 = merge_trajs(traj1, traj2)
>>> # from frame_iter for saving memory
>>> traj3 = merge_trajs((traj1(0, 10, 2), traj1.top), (traj2(100, 110, 2), traj2.top), n_frames=6)
>>> # raise error if not having the same n_frames
>>> traj4 = pt.load_sample_data('tz2')[:]
>>> traj4.n_frames
10
>>> traj1.n_frames
1
>>> merge_trajs(traj1, traj4)
Traceback (most recent call last):
...
ValueError: must have the same n_frames
pytraj.tools.
moving_average
(data, n)¶moving average
Notes
from stackoverflow
Examples
>>> moving_average([1, 2, 3, 4, 6], 2)
array([ 0.5, 1.5, 2.5, 3.5, 5. ])
pytraj.tools.
n_grams
(a, n)¶Parameters: | a : sequence n : number of elements asarray : bool, default False
|
---|
Notes
adapted from: http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html
Examples
>>> list(n_grams([2, 3, 4 ,5], 2))
[(2, 3), (3, 4), (4, 5)]
pytraj.tools.
read_gaussian_output
(filename=None, top=None)¶return a pytraj.trajectory.Trajectory object
Parameters: | fname : str, filename top : {str, Topology}, optional, default None
|
---|
pytraj.tools.
read_to_array
(fname)¶read text from file to numpy array
pytraj.tools.
rmsd
(a1, a2, flatten=True)¶rmsd for two array with the same shape
Parameters: | a1, a2: np.ndarray flatten : bool, default True
|
---|
Notes
This method is different from pytraj.rmsd
Examples
>>> import pytraj as pt
>>> t0 = pt.load_sample_data('ala3')
>>> t1 = t0[:]
>>> t1.xyz += 1.
>>> rmsd(t0.xyz, t1.xyz)
1.0
pytraj.tools.
rmsd_1darray
(a1, a2)¶rmsd of a1 and a2
Examples
>>> a0 = [1, 3, 4]
>>> a1 = [1.4, 3.5, 4.2]
>>> rmsd_1darray(a0, a1)
0.3872983346207417
>>> rmsd_1darray(a0, [3, 4, 5, 7, 8])
Traceback (most recent call last):
...
ValueError: must have the same shape
pytraj.tools.
split
(data, n_chunks)¶split self.data to n_chunks
Notes
same as numpy.array_split
Examples
>>> for data in split(range(30), 3): print(data)
[0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
pytraj.tools.
split_and_write_traj
(self, n_chunks=None, root_name='trajx', ext='nc', *args, **kwd)¶Examples
>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')
>>> split_and_write_traj(traj, n_chunks=3, root_name='output/trajx', overwrite=True)
pytraj.tools.
split_traj_by_residues
(traj, start=0, stop=-1, step=1)¶return a generator
Examples
>>> import pytraj as pt
>>> traj = pt.datafiles.load_rna()
>>> g = pt.tools.split_traj_by_residues(traj)
>>> t0 = next(g)
>>> print(t0.top.n_residues)
1