|
@@ -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
|
+#from rpy2.robjects.packages import importr
|
|
7
|
+#import rpy2.robjects as robjects
|
|
8
|
+#import rpy2.robjects.numpy2ri
|
9
|
9
|
|
10
|
10
|
#import notch
|
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
|
|
-# Regress for T2 using rpy2 interface
|
57
|
|
-def regressCurve(peaks,times,sigma2=1,intercept=True):
|
58
|
|
-
|
59
|
|
- # TODO, if regression fails, it might be because there is no exponential
|
60
|
|
- # term, maybe do a second regression then on a linear model.
|
61
|
|
- b1 = 0 # Bias
|
62
|
|
- b2 = 0 # Linear
|
63
|
|
- rT2 = 0.3 # T2 regressed
|
64
|
|
- r = robjects.r
|
65
|
|
-
|
66
|
|
- # Variable shared between R and Python
|
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
|
|
-# my_weights = robjects.RVector(value/sigma2)
|
75
|
|
-# robjects.globalenv['my_weigts'] = my_weights
|
76
|
|
-
|
77
|
|
-# if sigma2 != 0:
|
78
|
|
-# print "weighting"
|
79
|
|
-# tw = numpy.array(peaks)/sigma2
|
80
|
|
-# my_weights = robjects.RVector( tw/numpy.max(tw) )
|
81
|
|
-# else:
|
82
|
|
-# my_weights = robjects.RVector(numpy.ones(len(peaks)))
|
83
|
|
-
|
84
|
|
-# robjects.globalenv['my_weights'] = my_weights
|
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
|
|
- #fmla = robjects.RFormula('value ~ b1 + exp(-times/rT2)')
|
100
|
|
- fmla = robjects.Formula('value ~ b1 + b2*exp(-times/rT2)')
|
101
|
|
- #fmla = robjects.RFormula('value ~ b1 + b2*times + exp(-times/rT2)')
|
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
|
|
- # ugly, but I get errors with everything else I've tried
|
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
|
|
- #fit = robjects.r.nls(fmla,start=my_list,control=my_cont,weights=my_weights)
|
115
|
|
- if (sigma2 != 1):
|
116
|
|
- print("SIGMA 2")
|
117
|
|
- #fit = robjects.r.tryCatch(robjects.r.suppressWarnings(robjects.r.nls(fmla,start=my_list,control=my_cont,algorithm="port", \
|
118
|
|
- # weights=my_weights)), 'silent=TRUE')
|
119
|
|
- fit = robjects.r.tryCatch(robjects.r.nls(fmla,start=my_list,control=my_cont))#, \
|
120
|
|
- # weights=my_weights))
|
121
|
|
- else:
|
122
|
|
- try:
|
123
|
|
- fit = robjects.r.tryCatch(robjects.r.nls(fmla,start=my_list,control=my_cont,algorithm="port"))#,lower=my_lower,upper=my_upper))
|
124
|
|
- except:
|
125
|
|
- print("regression issue pass")
|
126
|
|
- Error = True
|
127
|
|
- # If failure fall back on zero regression values
|
128
|
|
- if not Error:
|
129
|
|
- #Error = fit[3][0]
|
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
|
|
- #print report
|
140
|
|
- #print r['$'](report,'convergence')
|
141
|
|
- #print r['convergence'] #(report,'convergence')
|
142
|
|
- #print r['$'](report,'par')[13]
|
143
|
|
- #print r['$'](report,'par')[14]
|
144
|
|
- else:
|
145
|
|
- print("ERROR DETECTED, regressed values set to default")
|
146
|
|
- b1 = 1e1
|
147
|
|
- b2 = 1e-2
|
148
|
|
- rT2 = 1e-2
|
149
|
|
- #print r['$'](report,'par')[0]
|
150
|
|
- #print r['$'](report,'par')[1]
|
151
|
|
- #print r['$'](report,'par')[2]
|
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
|
|
-# Regress for T2 using rpy2 interface
|
163
|
|
-def regressCurve2(peaks,times,sigma2=[None],intercept=True):
|
164
|
|
-
|
165
|
|
- if sigma2[0] != None:
|
166
|
|
- my_weights = robjects.FloatVector( sigma2 )
|
167
|
|
-
|
168
|
|
- # TODO, if regression fails, it might be because there is no exponential
|
169
|
|
- # term, maybe do a second regression then on a linear model.
|
170
|
|
- b1 = 0 # Bias
|
171
|
|
- b2 = 0 # Linear
|
172
|
|
- bb2 = 0 # Linear
|
173
|
|
- rT2 = 0.3 # T2 regressed
|
174
|
|
- rrT2 = 1.3 # T2 regressed
|
175
|
|
- r = robjects.r
|
176
|
|
-
|
177
|
|
- # Variable shared between R and Python
|
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
|
|
- #robjects.globalenv['sigma2'] = sigma2
|
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
|
|
- #fmla = robjects.RFormula('value ~ b1 + exp(-times/rT2)')
|
204
|
|
- fmla = robjects.Formula('value ~ b1 + b2*exp(-times/rT2) + bb2*exp(-times/rrT2)')
|
205
|
|
- #fmla = robjects.RFormula('value ~ b1 + b2*times + exp(-times/rT2)')
|
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
|
|
- # ugly, but I get errors with everything else I've tried
|
214
|
|
- Error = False
|
215
|
|
- #fit = robjects.r.nls(fmla,start=my_list,control=my_cont,weights=my_weights)
|
216
|
|
- if (sigma2[0] != None):
|
217
|
|
- #print("SIGMA 2")
|
218
|
|
- #fit = robjects.r.tryCatch(robjects.r.suppressWarnings(robjects.r.nls(fmla,start=my_list,control=my_cont,algorithm="port", \
|
219
|
|
- # weights=my_weights)), 'silent=TRUE')
|
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
|
|
- # weights=my_weights))
|
222
|
|
- else:
|
223
|
|
- try:
|
224
|
|
- fit = robjects.r.tryCatch(robjects.r.nls(fmla,start=my_list,control=my_cont,algorithm="port"))#,lower=my_lower,upper=my_upper))
|
225
|
|
- except:
|
226
|
|
- print("regression issue pass")
|
227
|
|
- Error = True
|
228
|
|
- # If failure fall back on zero regression values
|
229
|
|
- if not Error:
|
230
|
|
- #Error = fit[3][0]
|
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
|
|
- #print report
|
241
|
|
- #print r['$'](report,'convergence')
|
242
|
|
- #print r['convergence'] #(report,'convergence')
|
243
|
|
- #print r['$'](report,'par')[13]
|
244
|
|
- #print r['$'](report,'par')[14]
|
245
|
|
- else:
|
246
|
|
- print("ERROR DETECTED, regressed values set to default")
|
247
|
|
- b1 = 1e1
|
248
|
|
- b2 = 1e-2
|
249
|
|
- rT2 = 1e-2
|
250
|
|
- #print r['$'](report,'par')[0]
|
251
|
|
- #print r['$'](report,'par')[1]
|
252
|
|
- #print r['$'](report,'par')[2]
|
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
|
#return conv, E0,df,phi,T2
|
323
|
114
|
#return res_lsq.success, x[0], 0, x[1], x[2]
|
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
|
|
- # bi-exponential
|
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
|
|
- # Make 0 vector
|
377
|
|
- Zero = robjects.FloatVector(numpy.zeros(len(X)))
|
378
|
|
-
|
379
|
|
- # Fitted Parameters
|
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
|
|
- # Arrays
|
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
|
|
- #fit = robjects.r.tryCatch(robjects.r.nls(fmla, start=start, control=cont)) #, \
|
458
|
|
- report = r.summary(fit)
|
459
|
|
-
|
460
|
|
- conv = r['$'](fit,'convergence')[0]
|
461
|
|
- #if conv:
|
462
|
|
- # print (report)
|
463
|
|
- # print ("conv", conv)
|
464
|
|
- print ("Conv", r['$'](fit,'convergence')) # T2
|
465
|
|
- print (report)
|
466
|
|
-
|
467
|
|
- if BiExp:
|
468
|
|
- if CorrectFreq:
|
469
|
|
- E0 = r['$'](report,'par')[0] # E01
|
470
|
|
- E0 += r['$'](report,'par')[1] # E02
|
471
|
|
- df = r['$'](report,'par')[2] # offset
|
472
|
|
- phi = r['$'](report,'par')[3] # phase
|
473
|
|
- T2 = r['$'](report,'par')[4] # T2
|
474
|
|
- else:
|
475
|
|
- E0 = r['$'](report,'par')[0] # E01
|
476
|
|
- E0 += r['$'](report,'par')[1] # E02
|
477
|
|
- phi = r['$'](report,'par')[2] # phase
|
478
|
|
- T2 = r['$'](report,'par')[3] # T2
|
479
|
|
- else:
|
480
|
|
- if CorrectFreq:
|
481
|
|
- E0 = r['$'](report,'par')[0] # E01
|
482
|
|
- df = r['$'](report,'par')[1] # offset
|
483
|
|
- phi = r['$'](report,'par')[2] # phase
|
484
|
|
- T2 = r['$'](report,'par')[3] # T2
|
485
|
|
- else:
|
486
|
|
- E0 = r['$'](report,'par')[0] # E01
|
487
|
|
- phi = r['$'](report,'par')[1] # phase
|
488
|
|
- T2 = r['$'](report,'par')[2] # T2
|
489
|
|
- #phi = 0.907655876627
|
490
|
|
- #phi = 0
|
491
|
|
- #print ("df", df)# = 0
|
492
|
|
- return conv, E0,df,phi,T2
|
493
|
|
-
|
494
|
|
-
|
495
|
|
-#################################################
|
496
|
|
-# Regress for T2 using rpy2 interface
|
497
|
|
-def regressSpec(w, wL, X): #,sigma2=1,intercept=True):
|
498
|
|
-
|
499
|
|
- # compute s
|
500
|
|
- s = -1j*w
|
501
|
|
-
|
502
|
|
- # TODO, if regression fails, it might be because there is no exponential
|
503
|
|
- # term, maybe do a second regression then on a linear model.
|
504
|
|
- a = 0 # Linear
|
505
|
|
- rT2 = 0.1 # T2 regressed
|
506
|
|
- r = robjects.r
|
507
|
|
-
|
508
|
|
- # Variable shared between R and Python
|
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
|
|
- #my_lower = robjects.r('list(a=.001, rT2=.001, nb=.0001)')
|
522
|
|
- my_lower = robjects.r('list(a=.001, rT2=.001)')
|
523
|
|
- #my_upper = robjects.r('list(a=1.5, rT2=.300, nb =100.)')
|
524
|
|
- my_upper = robjects.r('list(a=1.5, rT2=.300)')
|
525
|
|
-
|
526
|
|
- #my_list = robjects.r('list(a=.2, rT2=0.03, nb=.1)')
|
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
|
|
- #fmla = robjects.Formula('Xri ~ c(a*Re((wL) / (wL^2+(s+1/rT2)^2 )), a*Im((wL)/(wL^2 + (s+1/rT2)^2 )))') # envelope
|
531
|
|
- ##fmla = robjects.Formula('Xri ~ c(a*Re((wL) / (wL^2+(s+1/rT2)^2 )), a*Im((wL)/(wL^2 + (s+1/rT2)^2 )))') # envelope
|
532
|
|
- #fmla = robjects.Formula('XX ~ a*(wL) / (wL^2 + (s+1/rT2)^2 )') # complex
|
533
|
|
- #fmla = robjects.Formula('Xa ~ abs(a*(wL) / (wL^2 + (s+1/rT2)^2 )) + nb') # complex
|
534
|
|
- fmla = robjects.Formula('Xa ~ abs(a*(wL) / (wL^2 + (s+1/rT2)^2 ))') # complex
|
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
|
|
- #fit = robjects.r.tryCatch(robjects.r.nls(fmla,start=my_list, control=my_cont)) #, lower=my_lower, algorithm='port')) #, \
|
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
|
|
- #print report
|
548
|
|
- #print r.warnings()
|
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
|
|
-# Regress for T2 using rpy2 interface
|
558
|
|
-def regressSpecComplex(w, wL, X): #,sigma2=1,intercept=True):
|
559
|
|
-
|
560
|
|
- # compute s
|
561
|
|
- s = -1j*w
|
562
|
|
-
|
563
|
|
- # TODO, if regression fails, it might be because there is no exponential
|
564
|
|
- # term, maybe do a second regression then on a linear model.
|
565
|
|
- a = 1 # Linear
|
566
|
|
- rT2 = 0.1 # T2 regressed
|
567
|
|
- r = robjects.r
|
568
|
|
- phi2 = 0 # phase
|
569
|
|
- wL2 = wL
|
570
|
|
-
|
571
|
|
- # Variable shared between R and Python
|
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
|
|
- #Kw = robjects.globalenv['Kwri']
|
590
|
|
-
|
591
|
|
- #print (numpy.shape(X))
|
592
|
|
-
|
593
|
|
- #my_lower = robjects.r('list(a=.001, rT2=.001, nb=.0001)')
|
594
|
|
- #my_lower = robjects.r('list(a=.001, rT2=.001)') # Working
|
595
|
|
- my_lower = robjects.r('list(a=.001, rT2=.001, phi2=-3.14, wL2=wL-5)')
|
596
|
|
- #my_upper = robjects.r('list(a=1.5, rT2=.300, nb =100.)')
|
597
|
|
- my_upper = robjects.r('list(a=3.5, rT2=.300, phi2=3.14, wL2=wL+5)')
|
598
|
|
-
|
599
|
|
- #my_list = robjects.r('list(a=.2, rT2=0.03, nb=.1)')
|
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
|
|
- #fmla = robjects.Formula('Xri ~ c(a*Re((wL) / (wL^2+(s+1/rT2)^2 )), a*Im((wL)/(wL^2 + (s+1/rT2)^2 )))') # envelope
|
604
|
|
- #fmla = robjects.Formula('Xi ~ Im(a*(sin(phi2)*s + ((1/rT2)*sin(phi2)) + wL*cos(phi2)) / (wL^2+(s+1/rT2)^2 ))') # envelope
|
605
|
|
- #fmla = robjects.Formula('Xri ~ c(Re(a*(sin(phi2)*s + ((1/rT2)*sin(phi2)) + wL*cos(phi2)) / (wL^2+(s+1/rT2)^2 )), Im(a*(sin(phi2)*s + ((1/rT2)*sin(phi2)) + wL*cos(phi2)) / (wL^2+(s+1/rT2)^2 )))') # envelope
|
606
|
|
-
|
607
|
|
- #fmlar = robjects.Formula('Xr ~ (Kwr(a, phi2, s, rT2, wL)) ') # envelope
|
608
|
|
- #fmlai = robjects.Formula('Xi ~ (Kwi(a, phi2, s, rT2, wL)) ') # envelope
|
609
|
|
- fmla = robjects.Formula('Xri ~ c(Kwr(a, phi2, s, rT2, wL2), Kwi(a, phi2, s, rT2, wL2) ) ') # envelope
|
610
|
|
- #fmla = robjects.Formula('Xri ~ (Kwri(a, phi2, s, rT2, wL)) ') # envelope
|
611
|
|
-
|
612
|
|
- #fmla = robjects.Formula('Xa ~ (abs(a*(sin(phi2)*s + ((1/rT2)*sin(phi2)) + wL*cos(phi2)) / (wL^2+(s+1/rT2)^2 )))') # envelope
|
613
|
|
- #fmla = robjects.Formula('XX ~ a*(wL) / (wL^2 + (s+1/rT2)^2 )') # complex
|
614
|
|
- #fmla = robjects.Formula('Xa ~ abs(a*(wL) / (wL^2 + (s+1/rT2)^2 )) + nb') # complex
|
615
|
|
-
|
616
|
|
- #fmla = robjects.Formula('Xri ~ c(a*Re((wL) / (wL^2+(s+1/rT2)^2 )), a*Im((wL)/(wL^2 + (s+1/rT2)^2 )))') # envelope
|
617
|
|
-
|
618
|
|
- # self.Gw[iw, iT2] = ((np.sin(phi2) * (alpha + 1j*self.w[iw]) + self.wL*np.cos(phi2)) / \
|
619
|
|
- # (self.wL**2 + (alpha+1.j*self.w[iw])**2 ))
|
620
|
|
- # self.Gw[iw, iT2] = ds * self.sc*((np.sin(phi2)*( alpha + 1j*self.w[iw]) + self.wL*np.cos(phi2)) / \
|
621
|
|
- # (self.wL**2 + (alpha+1.j*self.w[iw])**2 ))
|
622
|
|
-
|
623
|
|
- # Works Amplitude Only!
|
624
|
|
- #fmla = robjects.Formula('Xa ~ abs(a*(wL) / (wL^2 + (s+1/rT2)^2 ))') # complex
|
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)) #, lower=my_lower, algorithm='port')) #, \
|
635
|
|
- #fitr = robjects.r.tryCatch(robjects.r.nls(fmlar, start=my_list, control=my_cont, lower=my_lower, upper=my_upper, algorithm='port')) #, \
|
636
|
|
-
|
637
|
|
- #env = fmlai.getenvironment()
|
638
|
|
- #fiti = robjects.r.tryCatch(robjects.r.nls(fmlai, start=my_list, control=my_cont, lower=my_lower, upper=my_upper, algorithm='port')) #, \
|
639
|
|
-
|
640
|
|
- #reportr = r.summary(fitr)
|
641
|
|
- #reporti = r.summary(fiti)
|
642
|
|
- report = r.summary(fit)
|
643
|
|
- #print( report )
|
644
|
|
- #exit()
|
645
|
|
- #print( reportr )
|
646
|
|
- #print( reporti )
|
647
|
|
- #exit()
|
648
|
|
- #print r.warnings()
|
649
|
|
-
|
650
|
|
- #a = (r['$'](reportr,'par')[0] + r['$'](reporti,'par')[0]) / 2.
|
651
|
|
- #rT2 = (r['$'](reportr,'par')[1] + r['$'](reporti,'par')[1]) / 2.
|
652
|
|
- #nb = (r['$'](reportr,'par')[2] + r['$'](reporti,'par')[2]) / 2.
|
653
|
|
- a = r['$'](report,'par')[0]
|
654
|
|
- rT2 = r['$'](report,'par')[1]
|
655
|
|
- nb = r['$'](report,'par')[2] #phi2
|
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
|
###################################################################
|