-
Notifications
You must be signed in to change notification settings - Fork 0
/
met_hop_vs_lib.py
309 lines (252 loc) · 10.1 KB
/
met_hop_vs_lib.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 12 16:14:28 2022
@author: albertsmith
"""
import pyDR
import numpy as np
from pyDR.Fitting.fit import model_free
from pyDR.misc.tools import linear_ex
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
frames=pyDR.Project('Projects/proj_md')
sub=frames['HETs_MET_4pw_SegB']['.+Met']
#%% Function to calculate librational amplitude from S2
""" We assume libration is dominated by small amplitude rotation around a fixed
angle of 109.49 degrees. Then, we calculate order parameters as a function of
a Gaussian distribution of rotation angles
"""
class S2_2_libA():
def __init__(self):
"""
Calculates the amplitude (standard deviation) of librational motion
assuming a restricted wobbling-on-a-cone model with opening angle of
109.5 degrees, where angles are distributed according to a Normal
distribution
Parameters
----------
S2 : float, array
Order parameter(s) to calculate distribution for.
Returns
-------
float, array
Standard deviation of Gaussian distribution.
"""
sigma0=np.atleast_2d(np.linspace(1,360,360))
theta=np.atleast_2d(np.linspace(-180,180,201)).T
P2=1/27*(32*np.cos(theta*np.pi/180)**2+8*np.cos(theta*np.pi/180)-13)
prob=np.exp(-(theta**2)@(1/(2*sigma0**2)))
prob[:,0]=0
prob[100,0]=1
prob/=prob.sum(0)
self.S2=(prob*P2).sum(0)
self.sigma=sigma0.squeeze()/np.sqrt(2)
def __call__(self,S2):
return linear_ex(self.S2,self.sigma,S2)
def sigma2S2(self,sigma):
# if np.any(np.array(sigma)>360):
# print('Warning: S2 only valid for values less than 360')
return linear_ex(self.sigma,self.S2,sigma)
S2_2_libA=S2_2_libA() #Initialize an instance to use as a function ("callable")
"Routine for fitting simulated methyl hopping/methyl libration data to a linear model"
class Z2lib():
def __init__(self):
"""
Initially finds a linear relationship between the log-correlation time (z)
of methyl hopping to the amplitude (sigma), and then uses this relationship
to convert between z (hopping) and sigma (libration)
Returns
-------
None.
"""
self.subproj=sub
A=model_free(sub['MetLib'][0],nz=1,fixz=-14)[1][0]
sigma=S2_2_libA(1-A)
z=model_free(sub['MetHop'][0],nz=1,
fixA=[1-sub['MetHop'][0].R[:,7]])[0][0]
M=np.concatenate(([np.ones(z.shape)],[z])).T
index=np.ones(z.shape,dtype=bool)
thresh=2
for _ in range(2):
self.b=np.linalg.lstsq(M[index],np.atleast_2d(1/(sigma[index]**2)).T)[0]
self.error=self.b[0]+self.b[1]*z-1/(sigma**2)
self.stdev=np.sqrt((self.error[index]**2).sum()/(index.sum()-2))
if not(np.any(np.abs(self.error[index])>thresh*self.stdev)):
break
index=np.abs(self.error)<thresh*self.stdev
self.index=index
self.A=A
self.S2=1-A
self.sigma=sigma
self.z=z
self.sens=None
self.rho=None
self.Amet=8/9
@property
def S2met(self):
return 1-self.Amet
def z2lib(self,z):
"""
Finds sigma of the libration as a function of the log-correlation time of
methyl hopping
Parameters
----------
z : float, array
Log10 of the methyl hopping correlation time.
Returns
-------
None.
"""
x=self.b[0]+self.b[1]*z
index=x<=0
x[index]=1e-12
sigma=x**(-1/2)
sigma[sigma>S2_2_libA(0)]=S2_2_libA(0) #restrict so that it doesn't go over S2=0
return sigma
def z2S2(self,z):
"""
Estimates the order parameter for methyl libration (S2) from the
log-correlation time (z).
Parameters
----------
z : list-like
List of log-correlation times used to calculate S2.
Returns
-------
S2
Order parameters for librational motion.
"""
return S2_2_libA.sigma2S2(self.z2lib(z))
def plot(self):
"""
Plots the fit of correlation time of hopping to order parameter of
librational motion. Shown as two plots, one with log-correlation time
vs. S2, and the other with log-correlation time vs. 1/sigma^2, the
latter being the linear plot. Data points excluded from the linear
fit are shown in red.
Returns
-------
None.
"""
fig=plt.figure()
ax=[fig.add_subplot(1,2,k+1) for k in range(2)]
ax[0].scatter(self.z[self.index],self.sigma[self.index])
ni=np.logical_not(self.index)
ax[0].scatter(self.z[ni],self.sigma[ni],color='red',marker='x')
z=np.sort(self.z)
ax[0].plot(z,self.z2lib(z),color='black')
# ax[0].set_xlabel(r'log$_{10}$($\tau_c$/s)')
ax[0].set_xticks(np.linspace(-11,-10.2,5))
ax[0].set_xticklabels([None if k%2 else '{:.0f} ps'.format(10**v*1e12) for k,v in enumerate(ax[0].get_xticks())])
ax[0].set_xlabel(r'$\tau_c$')
ax[0].set_ylabel(r'$\sigma$ / $^\circ$')
ax[1].scatter(self.z[self.index],1/self.sigma[self.index]**2)
ax[1].scatter(self.z[ni],1/self.sigma[ni]**2,color='red',marker='x')
ax[1].plot(z,self.b[0]+self.b[1]*z,color='black')
ax[1].set_xticks(np.linspace(-11,-10.2,5))
ax[1].set_xticklabels([None if k%2 else '{:.0f} ps'.format(10**v*1e12) for k,v in enumerate(ax[0].get_xticks())])
ax[1].set_xlabel(r'$\tau_c$')
ax[1].set_ylabel(r'$1/\sigma^2 / (^\circ)^{-2}$')
pos=ax[1].get_xlim()[0]+np.diff(ax[1].get_xlim())*.1,ax[1].get_ylim()[1]-np.diff(ax[1].get_ylim())*.1
ax[1].text(*pos,r'$1/\sigma^2$ = '+f'{self.b[1][0]:.4f}'+r'$*\log{\tau_c}$ + '+f'{self.b[0][0]:.3f}')
fig.set_size_inches([8.5,4])
fig.tight_layout()
def plot_z_v_S2(self):
"""
Plots the fit of correlation time of hopping to order parameter of
librational motion. Shown as two plots, one with log-correlation time
vs. S2, and the other with log-correlation time vs. 1/sigma^2, the
latter being the linear plot. Data points excluded from the linear
fit are shown in red.
Returns
-------
None.
"""
fig=plt.figure()
ax=[fig.add_subplot(1,2,k+1) for k in range(2)]
ax[0].scatter(self.z[self.index],self.S2[self.index])
ni=np.logical_not(self.index)
ax[0].scatter(self.z[ni],self.S2[ni],color='red',marker='x')
z=np.sort(self.z)
ax[0].plot(z,self.z2S2(z),color='black')
# ax[0].set_xlabel(r'log$_{10}$($\tau_c$/s)')
ax[0].set_xticks(np.linspace(-11,-10.2,5))
ax[0].set_xticklabels([None if k%2 else '{:.0f} ps'.format(10**v*1e12) for k,v in enumerate(ax[0].get_xticks())])
ax[0].set_xlabel(r'$\tau_c$')
ax[0].set_ylabel(r'$S^2$')
ax[1].scatter(self.z[self.index],1/self.sigma[self.index]**2)
ax[1].scatter(self.z[ni],1/self.sigma[ni]**2,color='red',marker='x')
ax[1].plot(z,self.b[0]+self.b[1]*z,color='black')
ax[1].set_xticks(np.linspace(-11,-10.2,5))
ax[1].set_xticklabels([None if k%2 else '{:.0f} ps'.format(10**v*1e12) for k,v in enumerate(ax[0].get_xticks())])
ax[1].set_xlabel(r'$\tau_c$')
ax[1].set_ylabel(r'$1/\sigma^2 / (^\circ)^{-2}$')
fig.set_size_inches([8.5,4])
fig.tight_layout()
def plot_MFvDet(self):
proj=self.subproj._parent
proj.current_plot=1
self.subproj['MetLib'].plot(style='bar')
proj['ModelFreeFit']['MetLib'].plot()
for a in proj.plot_obj.ax[:-1]:
a.set_ylim([0,self.subproj['MetLib'].R[:,:-1].max()*1.1])
proj.current_plot=2
self.subproj['MetHop'].plot(style='bar')
proj['ModelFreeFit']['MetHop'].plot()
for a in proj.plot_obj.ax[:-1]:
a.set_ylim([0,self.subproj['MetHop'].R[:,:-1].max()*1.1])
def fit2z_sigma(self,data,n:int=2):
"""
Uses the first n detectors of a data object and fits these to a model
with a correlation time of hopping (z) and amplitude of libration (sigma),
assuming these two parameters are dependent.
Parameters
----------
data : pyDR data object
Data object to fit.
n : int, optional
Number of detectors to use. The default is 2.
Returns
-------
None.
"""
self.fit_setup(data.sens)
z=list()
z0=list()
for rho in data.R[:,:n]:
i=np.argmin(((self.rho[:n].T-rho)**2).sum(1))
def fun(z):
rhoc=linear_ex(self.sens.z,self.rho[:n],z).squeeze()
return (rhoc-rho)
out=leastsq(fun,self.sens.z[i])
z.append(out[0][0])
i=np.argmin(((self.sens.rhoz[:n].T-rho)**2).sum(1))
def fun(z):
rhoc=linear_ex(self.sens.z,self.sens.rhoz[:n]*self.Amet,z).squeeze()
return (rhoc-rho)
out=leastsq(fun,self.sens.z[i])
z0.append(out[0][0])
z,z0=np.array(z),np.array(z0)
sigma=self.z2lib(z)
return z,sigma,z0
def fit_setup(self,sens):
"""
Calculates a matrix of detector responses as a function of the methyl
hopping correlation time (z)
Parameters
----------
sens : sens object
pyDR sensitivity object.
Returns
-------
None.
"""
if self.sens is not None and sens==self.sens:return
z=sens.z
# sigma=self.z2lib(z)
# S2=S2_2_libA.sigma2S2(sigma)
S2=self.z2S2(z)
self.rho=sens.rhoz*(self.Amet-1+S2)+\
np.atleast_2d(sens.rhoz[:,0])[email protected]_2d((1-S2))
self.sens=sens