Principal component analysisΒΆ

try pytraj online:

http://mybinder.org/badge.svg

load trajectory

In [1]:
import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)
# load pytraj
import pytraj as pt

# load trajectory to memory
traj = pt.load('tz2.nc', 'tz2.parm7')

Get the data

In [2]:
# compute pca
data = pt.pca(traj, mask='!@H=', n_vecs=2)

print('projection values of each frame to first mode = {} \n'.format(data[0][0]))
print('projection values of each frame to second mode = {} \n'.format(data[0][1]))
print('eigvenvalues of first two modes', data[1][0])
print("")
print('eigvenvectors of first two modes: \n', data[1][1])
projection values of each frame to first mode = [  4.93425131  13.80002308  20.61605835 ..., -57.92280579 -61.25728607
 -52.85142136] 

projection values of each frame to second mode = [  4.03333616  -6.9132452  -14.53991318 ...,  -6.757936     2.1086719
  -3.60922861] 

eigvenvalues of first two modes [ 1399.36472919   240.42342439]

eigvenvectors of first two modes: 
 [[ 0.00251526  0.0405033  -0.0084492  ...,  0.04534061 -0.09515649
  -0.0475896 ]
 [ 0.0313375   0.12882763 -0.02338202 ..., -0.00380469 -0.04104856
  -0.04230317]]

plot

In [3]:
%matplotlib inline
%config InlineBackend.figure_format = 'retina'  # high resolution
import matplotlib
matplotlib.rcParams['savefig.dpi'] = 2 * matplotlib.rcParams['savefig.dpi'] # larger image

projection_data = data[0]
from matplotlib import pyplot as plt

plt.scatter(projection_data[0], projection_data[1], marker='o', c=range(traj.n_frames), alpha=0.5)
plt.xlabel('PC1')
plt.ylabel('PC2')
cbar = plt.colorbar()
cbar.set_label('frame #')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/haichit/miniconda3/lib/python3.5/site-packages/matplotlib/rcsetup.py in validate_dpi(s)
    206     try:
--> 207         return float(s)
    208     except ValueError:

ValueError: could not convert string to float: 'figurefigure'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/Users/haichit/miniconda3/lib/python3.5/site-packages/matplotlib/__init__.py in __setitem__(self, key, val)
    925             try:
--> 926                 cval = self.validate[key](val)
    927             except ValueError as ve:

/Users/haichit/miniconda3/lib/python3.5/site-packages/matplotlib/rcsetup.py in validate_dpi(s)
    209         raise ValueError('"%s" is not string "figure" or'
--> 210             ' could not convert "%s" to float' % (s, s))
    211 

ValueError: "figurefigure" is not string "figure" or could not convert "figurefigure" to float

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-3-64a38d59058e> in <module>()
      2 get_ipython().magic("config InlineBackend.figure_format = 'retina'  # high resolution")
      3 import matplotlib
----> 4 matplotlib.rcParams['savefig.dpi'] = 2 * matplotlib.rcParams['savefig.dpi'] # larger image
      5 
      6 projection_data = data[0]

/Users/haichit/miniconda3/lib/python3.5/site-packages/matplotlib/__init__.py in __setitem__(self, key, val)
    926                 cval = self.validate[key](val)
    927             except ValueError as ve:
--> 928                 raise ValueError("Key %s: %s" % (key, str(ve)))
    929             dict.__setitem__(self, key, cval)
    930         except KeyError:

ValueError: Key savefig.dpi: "figurefigure" is not string "figure" or could not convert "figurefigure" to float

(pca.ipynb; pca_evaluated.ipynb; pca.py)