-
Notifications
You must be signed in to change notification settings - Fork 2
/
dlsAnalyzer.py
398 lines (256 loc) · 12 KB
/
dlsAnalyzer.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import numpy as np
import pandas as pd
from helpers import *
from loadDLSdataHelpers import *
class dls_experiment:
"""
Class for the analysis of dynamic light scattering data
This class was written by Osvaldo Burastero
No warranty whatsoever
If you have questions please contact me:
Last time updated - Feb 2023
"""
def __init__(self):
# Experimental conditions
self.temperature = 293 # in K (20 degrees celsius)
self.viscosity = 8.9e-4 # pascal-second, default value of water
self.refractiveIndex = 1.33 # default value of water
# Experimental setup
self.lambda0, self.scatteringAngle = None, None # in nm & degrees
self.scans, self.reads = 1,1
self.nMeasurements = None
# Experimental results
self.autocorrelation, self.time = None, None
# Metadata (to be filled with pandas dataframes)
self.sampleInfo, self.sampleInfoRelevant = None, None
# Derived quantity from the experimental setup
self.q = None
# Derived quantity from the results
self.g1 = None
# Parameters required for fitting
self.s_space, self.ds, self.hrs, self.weights = None, None, None, None
# Parameters required for fitting (L curve criteria)
self.alphaVec, self.optimalAlpha, self.alphaOptIdx = None, None, None
# Fitted data
self.betaGuess, self.contributionsGuess = None, None
self.curvesResidualNorm, self.curvesPenaltyNorm = None, None
self.residualsG1 = None
# Predicted data
self.autocorrelationPredicted = None
return None
def loadWyatFile(self,file):
"""
Load Wyat plate reader output file
1st column - Time in microseconds
2nd to End columns - Autocorrelation
Headers - Sample names chosen by the user
"""
self.time, self.autocorrelationOriginal, sampleNames = readWyatFile(file)
self.sampleInfo = pd.DataFrame({"conditions":sampleNames,"read":1,"scan":1,"include":True})
self.lambda0 = 817 # in nm
self.scatteringAngle = 150 / 180 * np.pi # radians
return None
def setAutocorrelationData(self):
"""
Using the information from self.sampleInfo, which can be modified by the user,
subset the autocorrelation data and retrieve the samples metadata we want to analyze
"""
# Retrieve only the relevant column
self.autocorrelation = self.autocorrelationOriginal[:,self.sampleInfo.include]
# Retrieve only the relevant rows
self.sampleInfoRelevant = self.sampleInfo[self.sampleInfo.include]
# Set the total number of samples to be analyzed
self.nMeasurements = self.autocorrelation.shape[1]
return None
def getQ(self):
"""
Compute the Bragg wave vector
"""
self.q = get_q(self.lambda0,self.refractiveIndex,self.scatteringAngle)
return None
def createFittingS_space(self,lowHr,highHr,n):
"""
Create the s (inverse of gamma decay rate) space that will be used for the fitting
The limits are given by the minimum and maximum desired hydrodynamic radius (in nanometers)
Run after getQ()!
"""
n = int(n) # Convert n to integer type for the np.logspace function
sUpLimitHigh = s_inverse_decay_rate(
diffusion_from_hydrodynamic_radius(highHr/1e9,self.temperature,self.viscosity), self.q)
sUpLimitLow = s_inverse_decay_rate(
diffusion_from_hydrodynamic_radius(lowHr/1e9,self.temperature,self.viscosity), self.q)
# Sequence in linear space! 10.0**start to 10**stop
self.s_space = np.logspace(np.log10(sUpLimitLow),np.log10(sUpLimitHigh), n)
self.ds = diffusion_from_inverse_decay_rate(self.s_space,self.q)
self.hrs = hydrodynamic_radius(self.ds ,self.temperature,self.viscosity)*1e9 # In nanometers
return None
def getBetaEstimate(self):
"""
Fit a polynomial of degree 2 to the first 5 microseconds of data
"""
self.betaGuess = get_beta_prior(self.autocorrelation,self.time)
return None
def getG1correlation(self):
"""
Calculate the first order autocorrelation function g1
"""
g1 = [g1_from_g2(self.autocorrelation[:,i],self.betaGuess[i]) for i in range(self.nMeasurements)]
self.g1 = np.column_stack((g1))
return None
def getInitialEstimates(self,alpha=0.1,timeLimit=1e8):
"""
Obtain initial estimates for the relative contributions
Run after createFittingS_space() !
timeLimit should be given in microseconds! Default time is 100 seconds (all the autocorrelation curve).
alpha can be one value (same for all curves) or a list of values (one value per curve)
"""
selectedTimes = self.time < (timeLimit / 1e6)
# Return the fitted contributions and residuals of the first order autocorrelation function
self.contributionsGuess, self.residualsG1, _ = get_contributios_prior(
self.g1[selectedTimes,:],self.time[selectedTimes],self.s_space,self.betaGuess,alpha)
return None
def getInitialEstimatesManyAlpha(self,
alphaVec=(5**np.arange(-6,2,0.1,dtype=float))**2,timeLimit=1e8):
"""
Apply the Tikhonov Philips regularisation for a given set of different values of alpha
Useful to get afterwards the optimal alpha according to the L-curve criteria
Result:
We add curvesResidualNorm, curvesPenaltyNorm & alphaVec to the class object
curvesResidualNorm contains the norm of the fidelity term
curvesPenaltyNorm contains the norm of the penalization term
alphaVec contains the explored values of alpha
"""
selectedTimes = self.time < (timeLimit / 1e6)
curvesResidualNorm, curvesPenaltyNorm = [],[]
self.alphaVec = alphaVec
# Iterate over the vector with different values of alpha
for alpha in alphaVec:
_ , residualNorm, penaltyNorm = get_contributios_prior(
self.g1[selectedTimes,:],self.time[selectedTimes],
self.s_space,self.betaGuess,alpha)
curvesResidualNorm.append(residualNorm) # List (one element per alpha) of lists (one element per curve)
curvesPenaltyNorm.append(penaltyNorm) # List (one element per alpha) of lists (one element per curve)
self.curvesResidualNorm = np.array(curvesResidualNorm) # One row per alpha, one column per curve
self.curvesPenaltyNorm = np.array(curvesPenaltyNorm) # One row per alpha, one column per curve
return None
def getOptimalAlphaLcurve(self):
"""
Apply the triangle method to find the corner of the L-curve criteria en return the 'optimal' alpha for each curve
"""
alphaOptIdx = []
# Iterate over the curves
for idx in range(self.curvesResidualNorm.shape[1]):
alphaOptIdx.append(find_Lcurve_corner(self.curvesResidualNorm[:,idx],self.curvesPenaltyNorm[:,idx]))
self.alphaOptIdx = alphaOptIdx
return None
def getInitialEstimatesOptimalAlphaLcurve(self,timeLimit=1e8):
"""
Use the 'optimal' alpha selected using the L-curve corner criteria and the triangle method
to estimate the distribution of (inverse) decay rates
"""
self.optimalAlpha = [self.alphaVec[idx] for idx in self.alphaOptIdx]
self.getInitialEstimates(self.optimalAlpha,timeLimit)
return None
def predictAutocorrelationCurves(self):
# Create list to store the predicted autocorrelation data
self.autocorrelationPredicted = []
for idx in range(self.nMeasurements):
betaEst = self.betaGuess[idx]
contEst = self.contributionsGuess[idx]
# check that we estimated the contributions!
if len(contEst) > 1:
autocorrelationPredicted = g2_finite_aproximation(1 / self.s_space,self.time,betaEst,contEst)
self.autocorrelationPredicted.append(np.array(autocorrelationPredicted))
else:
# In the case we couldn't fit anything!
self.autocorrelationPredicted.append(np.array(0))
self.autocorrelationPredicted = np.column_stack((self.autocorrelationPredicted))
return None
def getWeights(self):
"""
Compare the fitted and experimental autocorrelation curve to get the residuals
and assign weights to each point
Caution: Not used in the Raynals online tool!
"""
residuals = np.subtract(self.autocorrelation,self.autocorrelationPredicted)
weights = 1 / np.abs(residuals)
self.weights = weights / weights.max(axis=0)
return None
def getWeightedInitialEstimates(self,alpha=0.15,timeLimit=1e8):
"""
Call after fitting the g2 correlation curves
that is, after running self.predictAutocorrelationCurves()
Caution: Not used in the Raynals online tool!
"""
if self.weights is None:
self.getWeights()
selectedTimes = self.time < (timeLimit / 1e6)
# Return the fitted contributions and residuals of the first order autocorrelation function
self.contributionsGuess, self.residualsG1 = get_contributios_prior(
self.g1[selectedTimes,:],self.time[selectedTimes],self.s_space,self.betaGuess,alpha,self.weights)
return None
class dlsAnalyzer:
"""
Useful to work with many different dls experiments
"""
def __init__(self):
"""
Create dictionary where each key value pair corresponds to
one DLS experiment, e.g.
"""
self.experimentsOri = {}
self.experimentsModif = {}
self.experimentNames = []
return None
def loadExperiment(self,file,name):
"""
Append one experiment to experimentsOri
"""
if name in self.experimentNames:
return "Experiment name already selected!"
try:
self.experimentsOri[name] = dls_experiment()
self.experimentsOri[name].loadWyatFile(file)
self.experimentNames.append(name)
return "Data loaded successfully!!!"
except:
pass
return "Data could not be loaded"
def deleteExperiment(self,name):
self.experimentNames.remove(name)
del self.experimentsOri[name]
try:
del self.experimentsModif[name]
except:
pass
return None
def setExperimentProperties(self,experimentName,variable,value):
"""
experimentName must be in self.experimentNames
variable can be 'replicates', 'reads', or 'scans'
value is a number
"""
setattr(self.experimentsOri[experimentName], variable, value)
return None
def getExperimentProperties(self,variable):
"""
variable can be 'replicates', 'reads', or 'scans'
"""
return [getattr(self.experimentsOri[experimentName], variable) for experimentName in self.experimentNames]
if __name__ == "__main__":
dls = dlsAnalyzer()
l = dls.loadExperiment("test.csv","test")
d = dls.experimentsOri["test"]
d.lambda0 = 817
d.scatteringAngle = 150 / 180 * np.pi
d.getQ()
d.createFittingS_space(0.09,1e6,200)
d.setAutocorrelationData()
d.getBetaEstimate()
d.getG1correlation()
d.getInitialEstimates()
d.getInitialEstimatesManyAlpha()
d.getOptimalAlphaLcurve()
d.getInitialEstimatesOptimalAlphaLcurve()
d.getInitialEstimatesManyAlpha()