Skip to content

Commit

Permalink
PCA CC
Browse files Browse the repository at this point in the history
  • Loading branch information
alsinmr committed Jan 28, 2024
1 parent b844b85 commit eba0575
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
10 changes: 8 additions & 2 deletions PCA/PCAclean.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from copy import copy,deepcopy
from ..misc.tools import linear_ex
from .PCAmovies import PCAmovies
from .PCAsubs import PCA_Ct,PCA_S2,PCAvecs,PCA2Data,Weighting
from .PCAsubs import PCA_Ct,PCA_S2,PCAvecs,PCA2Data,Weighting,Impulse



Expand Down Expand Up @@ -82,6 +82,12 @@ def Weighting(self):
self._Weighting=Weighting(self)
return self._Weighting

@property
def Impulse(self):
if self._Impulse is None:
self._Impulse=Impulse(self)
return self._Impulse

#%% Misc.
def clear(self):
"""
Expand All @@ -93,7 +99,7 @@ def clear(self):
"""
keys=['_pos','_covar','_sel1index','_sel2index','_lambda','_PC','_pcamp','_mean',
'_S2','_Ct','_Vecs','_Data','_Movie','_Weighting']
'_S2','_Ct','_Vecs','_Data','_Movie','_Weighting','_Impulse']
for k in keys:setattr(self,k,None)
return self

Expand Down
64 changes: 64 additions & 0 deletions PCA/PCAsubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ def S2m(self):
self._S2m=S20m[1:]/S20m[:-1]
return self._S2m[self.pca.Ct.tc_rev_index]



@property
def Am(self):
"""
Expand All @@ -472,6 +474,54 @@ def Am(self):
self._Am=S20m[:-1]-S20m[1:]
return self._Am[self.pca.Ct.tc_rev_index]

def S20mCC(self,q):
"""
Calculates the contribution to the total order parameter arising from
the mth principal component, noting that we number according to sorting
of the correlation times (so zero is the fastest PC, and -1 is the
slowest PC)
Note this is the product of all S2 up to mode m
Returns
-------
2D array (n PCs x n bonds)
"""


pca=self.pca
d20m=self.d2_0m

PC=pca.PCxyz[:,:,pca.Ct.tc_index]
Lambda=pca.Lambda[pca.Ct.tc_index]



X=np.zeros([pca.PC.shape[1],pca.nbonds])
for k in range(3):
for j in range(3):
P=(pca.mean[pca.sel1index[q],k]-pca.mean[pca.sel2index[q],k])*\
(pca.mean[pca.sel1index,j]-pca.mean[pca.sel2index,j])


a=Lambda*(PC[k,pca.sel1index[q]]-PC[k,pca.sel2index[q]])*\
(PC[j,pca.sel1index]-PC[j,pca.sel2index])

b=(P+np.cumsum(a.T,axis=0))/np.sqrt(d20m[:,q]*d20m.T).T

# b=P/d20m[0]

X+=(b**2)
S20m=-1/2+3/2*X
#Cleanup: make the S20m sorted
while np.any(S20m[:-1]<S20m[1:]):
i=S20m[:-1]<S20m[1:]
S20m[:-1][i]=S20m[1:][i]+1e-7


return S20m

def plotS2_direct_v_prod(self,ax=None):
"""
Make a plot of S2 comparing the direct calculation and the product of
Expand Down Expand Up @@ -668,6 +718,20 @@ def bond(self,index:int,rho_index:int=None,PCAfit=None,frac:float=0.75):
out=out[i]

return out

#%% Impulse response: A last attemp
class Impulse():
def __init__(self,pca):
self.pca=pca
self._PCamp=None

@property
def PCamp(self):
if self._PCamp is None:
l2=self.pca.PCamp.shape[1]//2
i=np.argmax(self.pca.PCamp[:,:l2],axis=-1)
self._PCamp=np.array([a[i0:i0+l2] for a,i0 in zip(self.pca.PCamp,i)])
return self._PCamp


#%% Port various correlation functions to data objects
Expand Down
8 changes: 8 additions & 0 deletions PCA/testPCA.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@
pca.select_bond('15N')
pca.select_atoms('name N C CA')




ired=pyDR.md2iRED(select).iRED2data()
ired.detect.r_no_opt(10)
noopt=ired.fit()
noopt.detect.r_auto(6)
fit=noopt.fit().modes2bonds()

0 comments on commit eba0575

Please sign in to comment.