|
@@ -2,10 +2,10 @@ import numpy, array #,rpy2
|
2
|
2
|
from matplotlib import pyplot as plt
|
3
|
3
|
import numpy as np
|
4
|
4
|
from scipy.optimize import least_squares
|
5
|
|
-from rpy2.robjects.packages import importr
|
6
|
5
|
|
7
|
|
-import rpy2.robjects as robjects
|
8
|
|
-import rpy2.robjects.numpy2ri
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
9
|
9
|
|
10
|
10
|
|
11
|
11
|
from numpy.fft import fft, fftfreq
|
|
@@ -52,215 +52,6 @@ def peakPicker(data, omega, dt):
|
52
|
52
|
indices
|
53
|
53
|
|
54
|
54
|
|
55
|
|
-
|
56
|
|
-
|
57
|
|
-def regressCurve(peaks,times,sigma2=1,intercept=True):
|
58
|
|
-
|
59
|
|
-
|
60
|
|
-
|
61
|
|
- b1 = 0
|
62
|
|
- b2 = 0
|
63
|
|
- rT2 = 0.3
|
64
|
|
- r = robjects.r
|
65
|
|
-
|
66
|
|
-
|
67
|
|
- robjects.globalenv['b1'] = b1
|
68
|
|
- robjects.globalenv['b2'] = b2
|
69
|
|
- robjects.globalenv['rT2'] = rT2
|
70
|
|
- robjects.globalenv['sigma2'] = sigma2
|
71
|
|
- value = robjects.FloatVector(peaks)
|
72
|
|
- times = robjects.FloatVector(numpy.array(times))
|
73
|
|
-
|
74
|
|
-
|
75
|
|
-
|
76
|
|
-
|
77
|
|
-
|
78
|
|
-
|
79
|
|
-
|
80
|
|
-
|
81
|
|
-
|
82
|
|
-
|
83
|
|
-
|
84
|
|
-
|
85
|
|
-
|
86
|
|
- if (intercept):
|
87
|
|
- my_list = robjects.r('list(b1=50, b2=1e2, rT2=0.03)')
|
88
|
|
- my_lower = robjects.r('list(b1=0, b2=0, rT2=.005)')
|
89
|
|
- my_upper = robjects.r('list(b1=20000, b2=2000, rT2=.700)')
|
90
|
|
- else:
|
91
|
|
- my_list = robjects.r('list(b2=1e2, rT2=0.3)')
|
92
|
|
- my_lower = robjects.r('list(b2=0, rT2=.005)')
|
93
|
|
- my_upper = robjects.r('list(b2=2000, rT2=.700)')
|
94
|
|
-
|
95
|
|
- my_cont = robjects.r('nls.control(maxiter=1000, warnOnly=TRUE, printEval=FALSE)')
|
96
|
|
-
|
97
|
|
-
|
98
|
|
- if (intercept):
|
99
|
|
-
|
100
|
|
- fmla = robjects.Formula('value ~ b1 + b2*exp(-times/rT2)')
|
101
|
|
-
|
102
|
|
- else:
|
103
|
|
- fmla = robjects.Formula('value ~ b2*exp(-times/rT2)')
|
104
|
|
-
|
105
|
|
- env = fmla.getenvironment()
|
106
|
|
- env['value'] = value
|
107
|
|
- env['times'] = times
|
108
|
|
-
|
109
|
|
-
|
110
|
|
- my_weights = robjects.r('rep(1,length(value))')
|
111
|
|
- for ii in range(len(my_weights)):
|
112
|
|
- my_weights[ii] *= peaks[ii]/sigma2
|
113
|
|
- Error = False
|
114
|
|
-
|
115
|
|
- if (sigma2 != 1):
|
116
|
|
- print("SIGMA 2")
|
117
|
|
-
|
118
|
|
-
|
119
|
|
- fit = robjects.r.tryCatch(robjects.r.nls(fmla,start=my_list,control=my_cont))
|
120
|
|
-
|
121
|
|
- else:
|
122
|
|
- try:
|
123
|
|
- fit = robjects.r.tryCatch(robjects.r.nls(fmla,start=my_list,control=my_cont,algorithm="port"))
|
124
|
|
- except:
|
125
|
|
- print("regression issue pass")
|
126
|
|
- Error = True
|
127
|
|
-
|
128
|
|
- if not Error:
|
129
|
|
-
|
130
|
|
- report = r.summary(fit)
|
131
|
|
- b1 = 0
|
132
|
|
- b2 = 0
|
133
|
|
- rT2 = 1
|
134
|
|
- if (intercept):
|
135
|
|
- if not Error:
|
136
|
|
- b1 = r['$'](report,'par')[0]
|
137
|
|
- b2 = r['$'](report,'par')[1]
|
138
|
|
- rT2 = r['$'](report,'par')[2]
|
139
|
|
-
|
140
|
|
-
|
141
|
|
-
|
142
|
|
-
|
143
|
|
-
|
144
|
|
- else:
|
145
|
|
- print("ERROR DETECTED, regressed values set to default")
|
146
|
|
- b1 = 1e1
|
147
|
|
- b2 = 1e-2
|
148
|
|
- rT2 = 1e-2
|
149
|
|
-
|
150
|
|
-
|
151
|
|
-
|
152
|
|
- return [b1,b2,rT2]
|
153
|
|
- else:
|
154
|
|
- if not Error:
|
155
|
|
- rT2 = r['$'](report,'par')[1]
|
156
|
|
- b2 = r['$'](report,'par')[0]
|
157
|
|
- else:
|
158
|
|
- print("ERROR DETECTED, regressed values set to default")
|
159
|
|
- return [b2, rT2]
|
160
|
|
-
|
161
|
|
-
|
162
|
|
-
|
163
|
|
-def regressCurve2(peaks,times,sigma2=[None],intercept=True):
|
164
|
|
-
|
165
|
|
- if sigma2[0] != None:
|
166
|
|
- my_weights = robjects.FloatVector( sigma2 )
|
167
|
|
-
|
168
|
|
-
|
169
|
|
-
|
170
|
|
- b1 = 0
|
171
|
|
- b2 = 0
|
172
|
|
- bb2 = 0
|
173
|
|
- rT2 = 0.3
|
174
|
|
- rrT2 = 1.3
|
175
|
|
- r = robjects.r
|
176
|
|
-
|
177
|
|
-
|
178
|
|
- robjects.globalenv['b1'] = b1
|
179
|
|
- robjects.globalenv['b2'] = b2
|
180
|
|
- robjects.globalenv['rT2'] = rT2
|
181
|
|
-
|
182
|
|
- robjects.globalenv['bb2'] = b2
|
183
|
|
- robjects.globalenv['rrT2'] = rT2
|
184
|
|
-
|
185
|
|
-
|
186
|
|
- value = robjects.FloatVector(peaks)
|
187
|
|
- times = robjects.FloatVector(numpy.array(times))
|
188
|
|
-
|
189
|
|
-
|
190
|
|
- if (intercept):
|
191
|
|
- my_list = robjects.r('list(b1=.50, b2=1e2, rT2=0.03, bb2=1e1, rrT2=1.3)')
|
192
|
|
- my_lower = robjects.r('list(b1=0, b2=0, rT2=.005, bb2=0, rrT2=.005 )')
|
193
|
|
- my_upper = robjects.r('list(b1=2000, b2=2000, rT2=.700, bb2=2000, rrT2=1.3 )')
|
194
|
|
- else:
|
195
|
|
- my_list = robjects.r('list(b2=.5, rT2=0.3, bb2=.5, rrT2=1.3)')
|
196
|
|
- my_lower = robjects.r('list(b2=0, rT2=.005, bb2=0, rrT2=.005)')
|
197
|
|
- my_upper = robjects.r('list(b2=1, rT2=2.6, bb2=1, rrT2=2.6)')
|
198
|
|
-
|
199
|
|
- my_cont = robjects.r('nls.control(maxiter=1000, warnOnly=TRUE, printEval=FALSE)')
|
200
|
|
-
|
201
|
|
-
|
202
|
|
- if (intercept):
|
203
|
|
-
|
204
|
|
- fmla = robjects.Formula('value ~ b1 + b2*exp(-times/rT2) + bb2*exp(-times/rrT2)')
|
205
|
|
-
|
206
|
|
- else:
|
207
|
|
- fmla = robjects.Formula('value ~ b2*exp(-times/rT2) + bb2*exp(-times/rrT2)')
|
208
|
|
-
|
209
|
|
- env = fmla.getenvironment()
|
210
|
|
- env['value'] = value
|
211
|
|
- env['times'] = times
|
212
|
|
-
|
213
|
|
-
|
214
|
|
- Error = False
|
215
|
|
-
|
216
|
|
- if (sigma2[0] != None):
|
217
|
|
-
|
218
|
|
-
|
219
|
|
-
|
220
|
|
- fit = robjects.r.tryCatch(robjects.r.nls(fmla,start=my_list,control=my_cont,algorithm='port',weights=my_weights,lower=my_lower,upper=my_upper))
|
221
|
|
-
|
222
|
|
- else:
|
223
|
|
- try:
|
224
|
|
- fit = robjects.r.tryCatch(robjects.r.nls(fmla,start=my_list,control=my_cont,algorithm="port"))
|
225
|
|
- except:
|
226
|
|
- print("regression issue pass")
|
227
|
|
- Error = True
|
228
|
|
-
|
229
|
|
- if not Error:
|
230
|
|
-
|
231
|
|
- report = r.summary(fit)
|
232
|
|
- b1 = 0
|
233
|
|
- b2 = 0
|
234
|
|
- rT2 = 1
|
235
|
|
- if (intercept):
|
236
|
|
- if not Error:
|
237
|
|
- b1 = r['$'](report,'par')[0]
|
238
|
|
- b2 = r['$'](report,'par')[1]
|
239
|
|
- rT2 = r['$'](report,'par')[2]
|
240
|
|
-
|
241
|
|
-
|
242
|
|
-
|
243
|
|
-
|
244
|
|
-
|
245
|
|
- else:
|
246
|
|
- print("ERROR DETECTED, regressed values set to default")
|
247
|
|
- b1 = 1e1
|
248
|
|
- b2 = 1e-2
|
249
|
|
- rT2 = 1e-2
|
250
|
|
-
|
251
|
|
-
|
252
|
|
-
|
253
|
|
- return [b1,b2,rT2, bb2, rrT2]
|
254
|
|
- else:
|
255
|
|
- if not Error:
|
256
|
|
- rT2 = r['$'](report,'par')[1]
|
257
|
|
- b2 = r['$'](report,'par')[0]
|
258
|
|
- rrT2 = r['$'](report,'par')[3]
|
259
|
|
- bb2 = r['$'](report,'par')[2]
|
260
|
|
- else:
|
261
|
|
- print("ERROR DETECTED, regressed values set to default")
|
262
|
|
- return [b2, rT2, bb2, rrT2]
|
263
|
|
-
|
264
|
55
|
def fun(x, t, y):
|
265
|
56
|
""" Cost function for regression, single exponential, no DC term
|
266
|
57
|
x[0] = A0
|
|
@@ -322,343 +113,6 @@ def quadratureDetect2(X, Y, tt, x0="None"):
|
322
|
113
|
|
323
|
114
|
|
324
|
115
|
|
325
|
|
-def quadratureDetect(X, Y, tt, CorrectFreq=False, BiExp=False, CorrectDC=False):
|
326
|
|
-
|
327
|
|
- r = robjects.r
|
328
|
|
-
|
329
|
|
- if CorrectDC:
|
330
|
|
- robjects.r('''
|
331
|
|
- Xc1 <- function(E01, df, tt, phi, T2_1, DC) {
|
332
|
|
- DC + E01*cos(2*pi*df*tt + phi) * exp(-tt/T2_1)
|
333
|
|
- }
|
334
|
|
-
|
335
|
|
- Yc1 <- function(E01, df, tt, phi, T2_1, DC) {
|
336
|
|
- DC - E01*sin(2*pi*df*tt + phi) * exp(-tt/T2_1)
|
337
|
|
- }
|
338
|
|
- ''')
|
339
|
|
- else:
|
340
|
|
- robjects.r('''
|
341
|
|
- Xc1 <- function(E01, df, tt, phi, T2_1) {
|
342
|
|
- E01*cos(2*pi*df*tt + phi) * exp(-tt/T2_1)
|
343
|
|
- }
|
344
|
|
-
|
345
|
|
- Yc1 <- function(E01, df, tt, phi, T2_1) {
|
346
|
|
- -E01*sin(2*pi*df*tt + phi) * exp(-tt/T2_1)
|
347
|
|
- }
|
348
|
|
- ''')
|
349
|
|
-
|
350
|
|
-
|
351
|
|
- if CorrectDC:
|
352
|
|
- robjects.r('''
|
353
|
|
- Xc2 <- function(E01, E02, df, tt, phi, T2_1, T2_2, DC) {
|
354
|
|
- DC + E01*cos(2*pi*df*tt + phi) * exp(-tt/T2_1) +
|
355
|
|
- DC + E02*cos(2*pi*df*tt + phi) * exp(-tt/T2_2)
|
356
|
|
- }
|
357
|
|
-
|
358
|
|
- Yc2 <- function(E01, E02, df, tt, phi, T2_1, T2_2, DC) {
|
359
|
|
- DC - E01*sin(2*pi*df*tt + phi) * exp(-tt/T2_1) +
|
360
|
|
- DC - E02*sin(2*pi*df*tt + phi) * exp(-tt/T2_2)
|
361
|
|
- }
|
362
|
|
- ''')
|
363
|
|
- else:
|
364
|
|
- robjects.r('''
|
365
|
|
- Xc2 <- function(E01, E02, df, tt, phi, T2_1, T2_2) {
|
366
|
|
- E01*cos(2*pi*df*tt + phi) * exp(-tt/T2_1) +
|
367
|
|
- E02*cos(2*pi*df*tt + phi) * exp(-tt/T2_2)
|
368
|
|
- }
|
369
|
|
-
|
370
|
|
- Yc2 <- function(E01, E02, df, tt, phi, T2_1, T2_2) {
|
371
|
|
- -E01*sin(2*pi*df*tt + phi) * exp(-tt/T2_1) +
|
372
|
|
- -E02*sin(2*pi*df*tt + phi) * exp(-tt/T2_2)
|
373
|
|
- }
|
374
|
|
- ''')
|
375
|
|
-
|
376
|
|
-
|
377
|
|
- Zero = robjects.FloatVector(numpy.zeros(len(X)))
|
378
|
|
-
|
379
|
|
-
|
380
|
|
- E01 = 0.
|
381
|
|
- E02 = 0.
|
382
|
|
- df = 0.
|
383
|
|
- phi = 0.
|
384
|
|
- T2_1 = 0.
|
385
|
|
- T2_2 = 0.
|
386
|
|
- DC = 0.
|
387
|
|
- robjects.globalenv['DC'] = DC
|
388
|
|
- robjects.globalenv['E01'] = E01
|
389
|
|
- robjects.globalenv['E02'] = E02
|
390
|
|
- robjects.globalenv['df'] = df
|
391
|
|
- robjects.globalenv['phi'] = phi
|
392
|
|
- robjects.globalenv['T2_1'] = T2_1
|
393
|
|
- robjects.globalenv['T2_2'] = T2_2
|
394
|
|
- XY = robjects.FloatVector(numpy.concatenate((X,Y)))
|
395
|
|
-
|
396
|
|
-
|
397
|
|
- tt = robjects.FloatVector(numpy.array(tt))
|
398
|
|
- X = robjects.FloatVector(numpy.array(X))
|
399
|
|
- Y = robjects.FloatVector(numpy.array(Y))
|
400
|
|
- Zero = robjects.FloatVector(numpy.array(Zero))
|
401
|
|
-
|
402
|
|
-
|
403
|
|
-
|
404
|
|
- if BiExp:
|
405
|
|
- if CorrectDC:
|
406
|
|
- fmla = robjects.Formula('XY ~ c(Xc2( E01, E02, df, tt, phi, T2_1, T2_2, DC ), Yc2( E01, E02, df, tt, phi, T2_1, T2_2, DC ))')
|
407
|
|
- if CorrectFreq:
|
408
|
|
- start = robjects.r('list(E01=.100, E02=.01, df=0, phi=0. , T2_1=.100, T2_2=.01, DC=0.0)')
|
409
|
|
- lower = robjects.r('list(E01=1e-6, E02=1e-6, df=-50, phi=-3.14 , T2_1=.001, T2_2=.001, DC=0.0)')
|
410
|
|
- upper = robjects.r('list(E01=1.00, E02=1.0, df=50, phi=3.14 , T2_1=.800, T2_2=.8, DC=0.5)')
|
411
|
|
- else:
|
412
|
|
- start = robjects.r('list(E01=.100, E02=.01, phi=0.9 , T2_1=.100, T2_2=.01, DC=0.0)')
|
413
|
|
- lower = robjects.r('list(E01=1e-6, E02=1e-6, phi=-3.14 , T2_1=.001, T2_2=.001, DC=0.0)')
|
414
|
|
- upper = robjects.r('list(E01=1.00, E02=1.0, phi=3.14 , T2_1=.800, T2_2=.8, DC=0.5)')
|
415
|
|
- else:
|
416
|
|
- fmla = robjects.Formula('XY ~ c(Xc2( E01, E02, df, tt, phi, T2_1, T2_2 ), Yc2( E01, E02, df, tt, phi, T2_1, T2_2))')
|
417
|
|
- if CorrectFreq:
|
418
|
|
- start = robjects.r('list(E01=.100, E02=.01, df=0, phi=0. , T2_1=.100, T2_2=.01)')
|
419
|
|
- lower = robjects.r('list(E01=1e-6, E02=1e-6, df=-50, phi=-3.14 , T2_1=.001, T2_2=.001)')
|
420
|
|
- upper = robjects.r('list(E01=1.00, E02=1.0, df=50, phi=3.14 , T2_1=.800, T2_2=.8)')
|
421
|
|
- else:
|
422
|
|
- start = robjects.r('list(E01=.100, E02=.01, phi=0.9 , T2_1=.100, T2_2=.01)')
|
423
|
|
- lower = robjects.r('list(E01=1e-6, E02=1e-6, phi=-3.14 , T2_1=.001, T2_2=.001)')
|
424
|
|
- upper = robjects.r('list(E01=1.00, E02=1.0, phi=3.14 , T2_1=.800, T2_2=.8)')
|
425
|
|
- else:
|
426
|
|
- if CorrectDC:
|
427
|
|
- fmla = robjects.Formula('XY ~ c(Xc1( E01, df, tt, phi, T2_1, DC), Yc1( E01, df, tt, phi, T2_1,DC))')
|
428
|
|
- if CorrectFreq:
|
429
|
|
- start = robjects.r('list(E01=.100, df=0 , phi=0. , T2_1=.100, DC=0.0)')
|
430
|
|
- lower = robjects.r('list(E01=1e-6, df=-50., phi=-3.14, T2_1=.001, DC=0.0)')
|
431
|
|
- upper = robjects.r('list(E01=1.00, df=50. , phi=3.14 , T2_1=.800, DC=0.5)')
|
432
|
|
- else:
|
433
|
|
- start = robjects.r('list(E01=.100, phi= 0. , T2_1=.100, DC=0.0)')
|
434
|
|
- lower = robjects.r('list(E01=1e-6, phi=-3.13, T2_1=.001, DC=0.0)')
|
435
|
|
- upper = robjects.r('list(E01=1.00, phi= 3.13, T2_1=.800, DC=0.5)')
|
436
|
|
- else:
|
437
|
|
- fmla = robjects.Formula('XY ~ c(Xc1( E01, df, tt, phi, T2_1), Yc1( E01, df, tt, phi, T2_1))')
|
438
|
|
- if CorrectFreq:
|
439
|
|
- start = robjects.r('list(E01=.100, df=0 , phi=0. , T2_1=.100)')
|
440
|
|
- lower = robjects.r('list(E01=1e-6, df=-50. , phi=-3.14 , T2_1=.001)')
|
441
|
|
- upper = robjects.r('list(E01=1.00, df=50. , phi=3.14 , T2_1=.800)')
|
442
|
|
- else:
|
443
|
|
- start = robjects.r('list(E01=.100, phi= 0. , T2_1=.100)')
|
444
|
|
- lower = robjects.r('list(E01=1e-6, phi=-3.13, T2_1=.001)')
|
445
|
|
- upper = robjects.r('list(E01=1.00, phi= 3.13, T2_1=.800)')
|
446
|
|
-
|
447
|
|
- env = fmla.getenvironment()
|
448
|
|
- env['Zero'] = Zero
|
449
|
|
- env['X'] = X
|
450
|
|
- env['Y'] = Y
|
451
|
|
- env['XY'] = XY
|
452
|
|
- env['tt'] = tt
|
453
|
|
-
|
454
|
|
- cont = robjects.r('nls.control(maxiter=10000, warnOnly=TRUE, printEval=FALSE)')
|
455
|
|
-
|
456
|
|
- fit = robjects.r.tryCatch(robjects.r.nls(fmla, start=start, control=cont, lower=lower, upper=upper, algorithm='port'))
|
457
|
|
-
|
458
|
|
- report = r.summary(fit)
|
459
|
|
-
|
460
|
|
- conv = r['$'](fit,'convergence')[0]
|
461
|
|
-
|
462
|
|
-
|
463
|
|
-
|
464
|
|
- print ("Conv", r['$'](fit,'convergence'))
|
465
|
|
- print (report)
|
466
|
|
-
|
467
|
|
- if BiExp:
|
468
|
|
- if CorrectFreq:
|
469
|
|
- E0 = r['$'](report,'par')[0]
|
470
|
|
- E0 += r['$'](report,'par')[1]
|
471
|
|
- df = r['$'](report,'par')[2]
|
472
|
|
- phi = r['$'](report,'par')[3]
|
473
|
|
- T2 = r['$'](report,'par')[4]
|
474
|
|
- else:
|
475
|
|
- E0 = r['$'](report,'par')[0]
|
476
|
|
- E0 += r['$'](report,'par')[1]
|
477
|
|
- phi = r['$'](report,'par')[2]
|
478
|
|
- T2 = r['$'](report,'par')[3]
|
479
|
|
- else:
|
480
|
|
- if CorrectFreq:
|
481
|
|
- E0 = r['$'](report,'par')[0]
|
482
|
|
- df = r['$'](report,'par')[1]
|
483
|
|
- phi = r['$'](report,'par')[2]
|
484
|
|
- T2 = r['$'](report,'par')[3]
|
485
|
|
- else:
|
486
|
|
- E0 = r['$'](report,'par')[0]
|
487
|
|
- phi = r['$'](report,'par')[1]
|
488
|
|
- T2 = r['$'](report,'par')[2]
|
489
|
|
-
|
490
|
|
-
|
491
|
|
-
|
492
|
|
- return conv, E0,df,phi,T2
|
493
|
|
-
|
494
|
|
-
|
495
|
|
-
|
496
|
|
-
|
497
|
|
-def regressSpec(w, wL, X):
|
498
|
|
-
|
499
|
|
-
|
500
|
|
- s = -1j*w
|
501
|
|
-
|
502
|
|
-
|
503
|
|
-
|
504
|
|
- a = 0
|
505
|
|
- rT2 = 0.1
|
506
|
|
- r = robjects.r
|
507
|
|
-
|
508
|
|
-
|
509
|
|
- robjects.globalenv['a'] = a
|
510
|
|
- robjects.globalenv['rT2'] = rT2
|
511
|
|
- robjects.globalenv['wL'] = wL
|
512
|
|
- robjects.globalenv['nb'] = 0
|
513
|
|
-
|
514
|
|
- s = robjects.ComplexVector(numpy.array(s))
|
515
|
|
- XX = robjects.ComplexVector(X)
|
516
|
|
- Xr = robjects.FloatVector(numpy.real(X))
|
517
|
|
- Xi = robjects.FloatVector(numpy.imag(X))
|
518
|
|
- Xa = robjects.FloatVector(numpy.abs(X))
|
519
|
|
- Xri = robjects.FloatVector(numpy.concatenate((Xr,Xi)))
|
520
|
|
-
|
521
|
|
-
|
522
|
|
- my_lower = robjects.r('list(a=.001, rT2=.001)')
|
523
|
|
-
|
524
|
|
- my_upper = robjects.r('list(a=1.5, rT2=.300)')
|
525
|
|
-
|
526
|
|
-
|
527
|
|
- my_list = robjects.r('list(a=.2, rT2=0.03)')
|
528
|
|
- my_cont = robjects.r('nls.control(maxiter=5000, warnOnly=TRUE, printEval=FALSE)')
|
529
|
|
-
|
530
|
|
-
|
531
|
|
-
|
532
|
|
-
|
533
|
|
-
|
534
|
|
- fmla = robjects.Formula('Xa ~ abs(a*(wL) / (wL^2 + (s+1/rT2)^2 ))')
|
535
|
|
-
|
536
|
|
- env = fmla.getenvironment()
|
537
|
|
- env['s'] = s
|
538
|
|
- env['Xr'] = Xr
|
539
|
|
- env['Xa'] = Xa
|
540
|
|
- env['Xi'] = Xi
|
541
|
|
- env['Xri'] = Xri
|
542
|
|
- env['XX'] = XX
|
543
|
|
-
|
544
|
|
-
|
545
|
|
- fit = robjects.r.tryCatch(robjects.r.nls(fmla, start=my_list, control=my_cont, lower=my_lower, upper=my_upper, algorithm='port'))
|
546
|
|
- report = r.summary(fit)
|
547
|
|
-
|
548
|
|
-
|
549
|
|
-
|
550
|
|
- a = r['$'](report,'par')[0]
|
551
|
|
- rT2 = r['$'](report,'par')[1]
|
552
|
|
- nb = r['$'](report,'par')[2]
|
553
|
|
-
|
554
|
|
- return a, rT2, nb
|
555
|
|
-
|
556
|
|
-
|
557
|
|
-
|
558
|
|
-def regressSpecComplex(w, wL, X):
|
559
|
|
-
|
560
|
|
-
|
561
|
|
- s = -1j*w
|
562
|
|
-
|
563
|
|
-
|
564
|
|
-
|
565
|
|
- a = 1
|
566
|
|
- rT2 = 0.1
|
567
|
|
- r = robjects.r
|
568
|
|
- phi2 = 0
|
569
|
|
- wL2 = wL
|
570
|
|
-
|
571
|
|
-
|
572
|
|
- robjects.globalenv['a'] = a
|
573
|
|
- robjects.globalenv['rT2'] = rT2
|
574
|
|
- robjects.globalenv['wL'] = wL
|
575
|
|
- robjects.globalenv['wL2'] = 0
|
576
|
|
- robjects.globalenv['nb'] = 0
|
577
|
|
- robjects.globalenv['phi2'] = phi2
|
578
|
|
-
|
579
|
|
- s = robjects.ComplexVector(numpy.array(s))
|
580
|
|
- XX = robjects.ComplexVector(X)
|
581
|
|
- Xr = robjects.FloatVector(numpy.real(X))
|
582
|
|
- Xi = robjects.FloatVector(numpy.imag(X))
|
583
|
|
- Xa = robjects.FloatVector(numpy.abs(X))
|
584
|
|
- Xri = robjects.FloatVector(numpy.concatenate((X.real,X.imag)))
|
585
|
|
-
|
586
|
|
- robjects.r('''
|
587
|
|
- source('kernel.r')
|
588
|
|
- ''')
|
589
|
|
-
|
590
|
|
-
|
591
|
|
-
|
592
|
|
-
|
593
|
|
-
|
594
|
|
-
|
595
|
|
- my_lower = robjects.r('list(a=.001, rT2=.001, phi2=-3.14, wL2=wL-5)')
|
596
|
|
-
|
597
|
|
- my_upper = robjects.r('list(a=3.5, rT2=.300, phi2=3.14, wL2=wL+5)')
|
598
|
|
-
|
599
|
|
-
|
600
|
|
- my_list = robjects.r('list(a=.2, rT2=0.03, phi2=0, wL2=wL)')
|
601
|
|
- my_cont = robjects.r('nls.control(maxiter=5000, warnOnly=TRUE, printEval=FALSE)')
|
602
|
|
-
|
603
|
|
-
|
604
|
|
-
|
605
|
|
-
|
606
|
|
-
|
607
|
|
-
|
608
|
|
-
|
609
|
|
- fmla = robjects.Formula('Xri ~ c(Kwr(a, phi2, s, rT2, wL2), Kwi(a, phi2, s, rT2, wL2) ) ')
|
610
|
|
-
|
611
|
|
-
|
612
|
|
-
|
613
|
|
-
|
614
|
|
-
|
615
|
|
-
|
616
|
|
-
|
617
|
|
-
|
618
|
|
-
|
619
|
|
-
|
620
|
|
-
|
621
|
|
-
|
622
|
|
-
|
623
|
|
-
|
624
|
|
-
|
625
|
|
-
|
626
|
|
- env = fmla.getenvironment()
|
627
|
|
- env['s'] = s
|
628
|
|
- env['Xr'] = Xr
|
629
|
|
- env['Xa'] = Xa
|
630
|
|
- env['Xi'] = Xi
|
631
|
|
- env['Xri'] = Xri
|
632
|
|
- env['XX'] = XX
|
633
|
|
-
|
634
|
|
- fit = robjects.r.tryCatch(robjects.r.nls(fmla,start=my_list, control=my_cont))
|
635
|
|
-
|
636
|
|
-
|
637
|
|
-
|
638
|
|
-
|
639
|
|
-
|
640
|
|
-
|
641
|
|
-
|
642
|
|
- report = r.summary(fit)
|
643
|
|
-
|
644
|
|
-
|
645
|
|
-
|
646
|
|
-
|
647
|
|
-
|
648
|
|
-
|
649
|
|
-
|
650
|
|
-
|
651
|
|
-
|
652
|
|
-
|
653
|
|
- a = r['$'](report,'par')[0]
|
654
|
|
- rT2 = r['$'](report,'par')[1]
|
655
|
|
- nb = r['$'](report,'par')[2]
|
656
|
|
-
|
657
|
|
- print ("Python wL2", r['$'](report,'par')[3] )
|
658
|
|
- print ("Python zeta", r['$'](report,'par')[2] )
|
659
|
|
-
|
660
|
|
- return a, rT2, nb
|
661
|
|
-
|
662
|
116
|
|
663
|
117
|
|
664
|
118
|
|