-
Notifications
You must be signed in to change notification settings - Fork 1
/
readwrite.py
250 lines (208 loc) · 6.49 KB
/
readwrite.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# ------------------------------------------------------------
# Functions to quickly read/write pyemma objects
#
# Written by Giovanni Pinamonti
# SISSA, Trieste, Italy, 2016
#
# ------------------------------------------------------------
import numpy as np
import cPickle as pickle
###TICA write
def write_tica_eval(name,tica_obj):
fh=open(name+'_tica_eval.dat','w')
fh.write("# TICA with lagtime="+str(tica_obj.lag)+" steps\n")
for eval in tica_obj.eigenvalues:
fh.write("%e\n" %(eval))
fh.close()
def write_tica_evec(name,tica_obj):
fh=open(name+'_tica_evec.dat','w')
fh.write("# TICA with lagtime="+str(tica_obj.lag)+" steps\n")
for evec in tica_obj.eigenvectors:
for x in evec:
fh.write("%e " %(x))
fh.write("\n")
fh.close()
def write_tica_trajs(name,IC):
itrj=0
for tictrj in IC:
fh=open(name+'_tica_traj'+str(itrj)+'.dat','w')
# fh.write("# TICA with lagtime="+str(tica_obj.lag)+" steps\n")
for frame in tictrj:
for x in frame:
fh.write(str(x)+" ")
fh.write("\n")
fh.close()
itrj+=1
###TICA read
def read_tica_eval(name):
### to read eigenvalues use this
tica_eval=[]
for line in open(name+'_tica_eval.dat','r'):
if line[0]=='#':
continue
tica_eval.append(np.array(float(line.split()[0])))
return np.array(tica_eval)
def read_tica_evec(name):
### to read eigenvectors use this
tica_evec=[]
for line in open(name+'_tica_evec.dat','r'):
if line[0]=='#':
continue
tica_evec.append(np.array([float(x) for x in line.split()]))
return np.array(tica_evec)
def read_tica_trajs(name,ntraj):
### to read TIC-trajectories use this
NTRAJ=ntraj
IC=[]
for itrj in range(NTRAJ):
tica_trj=[]
for line in open(name+'_tica_traj'+str(itrj)+'.dat','r'):
if line[0]=='#':
continue
tica_trj.append(np.array([float(x) for x in line.split()]))
tica_trj=np.array(tica_trj)
IC.append(tica_trj)
return IC
#######################################
############ CLUSTERING ###############
#######################################
def write_cl_dtrajs(name,dtrajs):
itrj=0
for trj in dtrajs:
fh=open(name+'_cl_dtraj'+str(itrj)+'.dat','w')
for x in trj:
fh.write(str(x)+"\n")
fh.close()
itrj+=1
def write_cl_centers(name,cl):
fh=open(name+'_cl_centers.dat','w')
fh.write("# Clustering details: "+cl.describe()+"\n")
for cc in cl.clustercenters:
for x in cc:
fh.write(str(x)+" ")
fh.write("\n")
fh.close()
import cPickle as pickle
def write_cl_indexes(name,cl):
fh=open(name+'_cl_indexes.idx','w')
pickle.dump(cl.index_clusters,fh,-1)
fh.close()
def read_cl_indexes(name):
fh=open(name+'_cl_indexes.idx','r')
cl_indexes=pickle.load(fh)
fh.close()
return cl_indexes
def read_cl_dtrajs(name,ntraj):
### to read discrete trajectories use this
NTRAJ=ntraj
dtrajs=[]
for itrj in range(NTRAJ):
trj=[]
for line in open(name+'_cl_dtraj'+str(itrj)+'.dat','r'):
if line[0]=='#':
continue
trj.append(int(line))
trj=np.array(trj)
dtrajs.append(trj)
return dtrajs
def read_cl_centers(name):
### to read cluster centers use this
cl_centers=[]
for line in open(name+'_cl_centers.dat','r'):
if line[0]=='#':
continue
cl_centers.append(np.array([float(x) for x in line.split()]))
return np.array(cl_centers)
######################################
############ M.S.M. ###############
######################################
def write_msm_tmat(name,M):
fh=open(name+'_msm_tmat.dat','w')
fh.write("# MSM with a lagtime of "+str(M.lag)+" steps\n")
for line in M.transition_matrix:
for x in line:
fh.write(str(x)+" ")
fh.write("\n")
fh.close()
def read_msm_tmat(name):
### to read msm's transition matrix
T=[]
for line in open(name+'_msm_tmat.dat','r'):
if line[0]=='#':
continue
T.append(np.array([float(x) for x in line.split()]))
T=np.array(T)
if T.shape[0]!=T.shape[1]:
print "WARNING: T is not q square matrix!"
return T
##########################################333
##############3 timescales!!!!!!1 #########33
#####################3333#################333
def write_msm_its(name,its):
fh=open(name+'_msm_its.dat','w')
for lag,ts in zip(its.lags,its.timescales):
fh.write(str(lag)+' ')
for x in ts:
fh.write(str(x)+' ')
fh.write('\n')
fh.close()
def write_hmm_its(name,hmits):
fh=open(name+'_hmm_its.dat','w')
for lag,ts in zip(hmits.lags,hmits.timescales):
#print lag,ts
fh.write(str(lag)+' ')
for x in ts:
fh.write(str(x)+' ')
fh.write('\n')
fh.close()
def read_msm_its(name):
lagtimes=[]
timescales=[]
for line in open(name+'_msm_its.dat','r'):
if line[0]=='#':
continue
lag=int(line.split()[0])
ts=[float(x) for x in line.split()[1:]]
lagtimes.append(lag)
timescales.append(np.array(ts))
return np.array(lagtimes),np.array(timescales)
def read_hmm_its(name):
lagtimes=[]
timescales=[]
for line in open(name+'_hmm_its.dat','r'):
if line[0]=='#':
continue
lag=int(line.split()[0])
ts=[float(x) for x in line.split()[1:]]
lagtimes.append(lag)
timescales.append(np.array(ts))
return np.array(lagtimes),np.array(timescales)
def write_its_errors(name,its):
fh=open(name+'_msm_its_errors.dat','w')
fh.write('# lag, avg, std')
for lag,ts,er in zip(its.lags,its.sample_mean,its.sample_std):
fh.write(str(lag)+' ')
for x in ts:
fh.write(str(x)+' ')
for x in er:
fh.write(str(x)+' ')
fh.write('\n')
fh.close()
def write_hmm(name,HiddenMM):
fh=open(name+'.hmm','wb')
pickle.dump(HiddenMM,fh,-1)
fh.close()
def read_hmm(name):
return pickle.load(open(name+'.hmm','rb'))
def write_msm(name,M):
fh=open(name+'.msm','wb')
pickle.dump(M,fh,-1)
fh.close()
def read_msm(name):
return pickle.load(open(name+'.msm','rb'))
def write_dpa_cl(name,cl_dpa):
fh=open(name+'.dpc','wb')
pickle.dump(cl_dpa,fh,-1)
fh.close()
def read_dpa_cl(name):
return pickle.load(open(name+'.dpc','rb'))