-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpfit.py
1432 lines (1294 loc) · 50.4 KB
/
dpfit.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import scipy.optimize
import numpy as np
from numpy import linalg
import time
from matplotlib import pyplot as plt
from functools import reduce
import multiprocessing
import sys
"""
IDEA: fit Y = F(X,A) where A is a dictionnary describing the
parameters of the function.
note that the items in the dictionnary should all be scalar!
author: amerand@eso.org
Tue 29 Jan 2013 17:03:21 CLST: working on adding correlations -> NOT WORKING!!!
Thu 28 Feb 2013 12:34:31 CLST: correcting leading to x2 for chi2 display
Mon 8 Apr 2013 10:51:03 BRT: alternate algorithms
Wed Aug 19 14:26:24 UTC 2015: updated randomParam
Wed Jan 24 10:20:06 CET 2018: Python 3 version
http://www.rhinocerus.net/forum/lang-idl-pvwave/355826-generalized-least-squares.html
"""
verboseTime=time.time()
def polyN(x, params):
"""
Polynomial function. e.g. params={'A0':1.0, 'A2':2.0} returns
x->1+2*x**2. The coefficients do not have to start at 0 and do not
need to be continuous. Actually, the way the function is written,
it will accept any '_x' where '_' is any character and x is a float.
"""
res = 0
for k in params.keys():
res += params[k]*np.array(x)**float(k[1:])
return res
def example():
"""
very simple example
"""
N = 50
# -- generate data set
X = np.linspace(-2,3,N)
Y = -0.7 - 0.4*X + 0.1*X**2 + .1*X**3
# -- error vector
E = np.ones(N)*0.3
np.random.seed(1234) # -- enure repeatibility
Y += E*np.random.randn(N) # -- random errors
# -- error covariance matrix
C = np.zeros((N, N))
for i in range(N):
for j in range(N):
if i==j:
C[i,j] = E[i]**2
else:
C[i,j] = 0.0*E[i]*E[j]
p0 = {'A0':0.0, 'A1':0., 'A2':0, 'A3':0.0}
m = ''
for k in sorted(p0.keys()):
i = int(k[1:])
if len(m)>0:
m+= ' + '
if i==0:
m+= k
else:
m+= k+'*X^%d'%i
print('model: Y = '+m)
# -- do the fit with simple error
fit = leastsqFit(dpfunc.polyN, X, p0, Y, err=E, verbose=2,
doNotFit=[], ftol=1e-5, maxfev=500)
# -- display data and best fit model
plt.figure(1)
plt.clf()
plt.errorbar(X, Y, yerr=E, label='data', fmt='o')
plt.plot(X, fit['model'], '.-g', linewidth=2, label='fit')
# -- show uncertainties in the model, a bit oustside range
x = np.linspace(X.min()-0.1*X.ptp(), X.max()+0.1*X.ptp(), 100)
fit = randomParam(fit, N=100, x=x)
plt.fill_between(x, fit['r_ym1s'], fit['r_yp1s'], color='g',
label='fit uncertainty', alpha=0.3)
plt.legend(loc='upper center')
# -- do the fit with covariance errors (uses curvefit)
fitc = leastsqFit(dpfunc.polyN, X, p0, Y, err=C, verbose=0,
doNotFit=[], ftol=1e-5, maxfev=500)
print('nfev=', fitc['info']['nfev'])
test = 0.
for k in fitc['best'].keys():
test += (fit['best'][k]-fitc['best'][k])**2
test += (fit['uncer'][k]-fitc['uncer'][k])**2
print('difference leastsq / curve_fit:', test)
return
def exampleBootstrap(centered=True):
x = np.linspace(-0.02,1.73,36)
if centered:
x0 = x.mean() # reduces correlations
else:
x0 = 0 # lead to correlations and biases
a = {'A0':9085., 'A1':-7736., 'A2':4781.0, 'A3':-1343.}
y = dpfunc.polyN(x,a)
a_ = leastsqFit(dpfunc.polyN, x-x0, a, y, verbose=0)['best']
fits = []
e = np.ones(len(x))*50
y += np.random.randn(len(x))*e
fits = bootstrap(dpfunc.polyN, x-x0, a_, y, err=e, verbose=1,
fitOnly=['A0','A1','A2','A3'])
#-- all bootstraped fit, with average value and error bar:
plotCovMatrix(fits[1:], fig=0)
#-- first fit (with all data points), with error elipses:
plotCovMatrix(fits[0], fig=None)
#-- true value
N = len(fits[0]['fitOnly'])
for i,ki in enumerate(fits[0]['fitOnly']):
for j,kj in enumerate(fits[0]['fitOnly']):
plt.subplot(N,N,i+N*j+1)
xl = plt.xlim()
if i!=j:
plt.plot(a_[ki], a_[kj], 'oc', markersize=10, alpha=0.5,
label='true')
else:
plt.vlines(a_[ki], 0, 0.99*plt.ylim()[1], color='c',
linewidth=3, alpha=0.5, label='true')
plt.legend(loc='center right', prop={'size':7},
numpoints=1)
plt.figure(1)
plt.clf()
label='bootstraping'
for f in fits[1:]:
plt.plot(x, dpfunc.polyN(x-x0,f['best']),'-', alpha=0.1, color='0.3',
label=label)
label=''
plt.errorbar(x,y,marker='o',color='k',yerr=e, linestyle='none',
label='data')
plt.plot(x,fits[0]['model'], '-b', linewidth=3, label='all points fit')
plt.plot(x,dpfunc.polyN(x-x0,a_), '-c', linewidth=3, label='true')
plt.legend()
return
def exampleCorrelation():
"""
very simple example with correlated error bars
"""
# -- generate fake data:
N, noise, offset = 100, 0.2, 0.1
X = np.linspace(0,10,N)
Y = 0.0 + 1.0*np.sin(2*np.pi*X/1.2)
Y += noise*np.random.randn(N)
Y[:N//2] += offset
Y[N//2:] -= offset
# -- errors:
E = np.ones(N)*np.sqrt(noise**2+offset**2)
# -- covariance matric
C = np.zeros((N, N))
# -- diagonal = error
C[range(N), range(N)] = noise**2
rho = offset/np.sqrt(noise**2+offset**2)
# -- non-diag: correlations
for i in range(N):
for j in range(N):
if i!=j:
if i<N//2 and j<N//2:
C[i,j] = rho*noise**2
elif i>=N//2 and j>=N//2:
C[i,j] = rho*noise**2
else:
C[i,j] = -0*rho*noise**2
#print(np.round(C, 3))
print('#'*12, 'without correlations', '#'*12)
E = np.ones(len(X), )*noise
fit=leastsqFit(dpfunc.fourier, X,
{'A0':0.1, 'A1':1.,'PHI1':0., 'WAV':1.2},
Y, err=E, verbose=1, normalizedUncer=0)
plt.figure(0)
plt.clf()
plt.errorbar(X,Y,yerr=E,linestyle='none', fmt='.')
plt.plot(X,fit['model'], color='r')
plt.title('without correlations')
print('#'*12, 'with correlations', '#'*12)
#print np.round(E, 2)
fit=leastsqFit(dpfunc.fourier, X,
{'A0':0.1, 'A1':1.1,'PHI1':0., 'WAV':1.2},
Y, err=linalg.inv(C), verbose=1, normalizedUncer=0)
plt.figure(1)
plt.clf()
plt.errorbar(X,Y,yerr=np.sqrt(np.diag(C)),linestyle='none', fmt='.')
plt.plot(X,fit['model'], color='r')
plt.title('with correlations')
return
def meta(x, params):
"""
allows to call any combination of function defines inside dpfunc:
params={'funcA;1:p1':, 'funcA;1:p2':,
'funcA;2:p1':, 'funcA;2:p2':,
'funcB:p1':, etc}
funcA and funcB should be defined in dpfunc.py. Allows to call many
instances of the same function (here funcA) and combine different functions.
Outputs of the difference functions will be sumed usinf operator '+'. """
# -- list of functions:
funcs = set([k.strip().split(':')[0].strip() for k in params.keys()])
#print funcs
res = 0
for f in funcs: # for each function
# -- keep only relevant keywords
kz = filter(lambda k: k.strip().split(':')[0].strip()==f, params.keys())
tmp = {}
for k in kz:
# -- build temporary dict pf parameters
tmp[k.split(':')[1].strip()]=params[k]
ff = f.split(';')[0].strip() # actual function name
if not ff in dpfunc.__dict__.keys():
raise NameError(ff+' not defined in dpfunc')
# -- add to result the function result
res += dpfunc.__dict__[ff](x, tmp)
return res
def iterable(obj):
"""
https://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable
"""
try:
iter(obj)
except Exception:
return False
else:
return True
Ncalls=0
Tcalls=0
trackP={}
def leastsqFit(func, x, params, y, err=None, fitOnly=None,
verbose=False, doNotFit=[], epsfcn=1e-7, showBest=True,
ftol=1e-5, fullOutput=True, normalizedUncer=True,
follow=None, maxfev=5000, bounds={}, factor=100):
"""
- params is a Dict containing the first guess.
- fits 'y +- err = func(x,params)'. errors are optionnal. in case err is a
ndarray of 2 dimensions, it is treated as the covariance of the
errors.
np.array([[err1**2, 0, .., 0],
[0, err2**2, 0, .., 0],
[0, .., 0, errN**2]]) is the equivalent of 1D errors
- follow=[...] list of parameters to "follow" in the fit, i.e. to print in
verbose mode
- fitOnly is a LIST of keywords to fit. By default, it fits all
parameters in 'params'. Alternatively, one can give a list of
parameters not to be fitted, as 'doNotFit='
- doNotFit has a similar purpose: for example if params={'a0':,
'a1': 'b1':, 'b2':}, doNotFit=['a'] will result in fitting only
'b1' and 'b2'. WARNING: if you name parameter 'A' and another one 'AA',
you cannot use doNotFit to exclude only 'A' since 'AA' will be excluded as
well...
- normalizedUncer=True: the uncertainties are independent of the Chi2, in
other words the uncertainties are scaled to the Chi2. If set to False, it
will trust the values of the error bars: it means that if you grossely
underestimate the data's error bars, the uncertainties of the parameters
will also be underestimated (and vice versa).
- bounds = dictionnary with lower/upper bounds. if bounds are not specified,
(-inf/inf will be used)
- verbose:
True (or 1): show progress
2: show progress and best fit with errors
3: show progress, best fit with errors and correlations
returns dictionary with:
'best': bestparam,
'uncer': uncertainties,
'chi2': chi2_reduced,
'model': func(x, bestparam)
'cov': covariance matrix (normalized if normalizedUncer)
'fitOnly': names of the columns of 'cov'
"""
global Ncalls, pfitKeys, pfix, _func, data_errors, trackP
# -- fit all parameters by default
if fitOnly is None:
if len(doNotFit)>0:
fitOnly = filter(lambda x: x not in doNotFit, params.keys())
else:
fitOnly = params.keys()
fitOnly = list(fitOnly)
fitOnly.sort() # makes some display nicer
# -- check that all parameters are numbers
NaNs = []
for k in fitOnly:
#if not (type(params[k])==float or type(params[k])==int):
if not (np.isscalar(params[k]) and type(params[k])!=str):
NaNs.append(k)
fitOnly = sorted(list(filter(lambda x: not x in NaNs, fitOnly)))
# -- build fitted parameters vector:
pfit = [params[k] for k in fitOnly]
# -- built fixed parameters dict:
pfix = {}
for k in params.keys():
if k not in fitOnly:
pfix[k]=params[k]
if verbose:
print('[dpfit] %d FITTED parameters:'%len(fitOnly), end=' ')
if len(fitOnly)<100 or (type(verbose)==int and verbose>1):
print(fitOnly)
print('[dpfit] epsfcn=', epsfcn, 'ftol=', ftol)
else:
print(' ')
# -- actual fit
Ncalls=0
trackP={}
t0=time.time()
mesg=''
if np.iterable(err) and len(np.array(err).shape)==2:
if verbose:
print('[dpfit] using scipy.optimize.curve_fit')
# -- assumes err matrix is co-covariance
_func = func
pfitKeys = fitOnly
plsq, cov = scipy.optimize.curve_fit(_fitFunc2, x, y, pfit,
sigma=err, epsfcn=epsfcn, ftol=ftol)
info, mesg, ier = {'nfev':Ncalls, 'exec time':time.time()-t0}, 'curve_fit', None
else:
if bounds is None or bounds == {}:
# ==== LEGACY! ===========================
if verbose:
print('[dpfit] using scipy.optimize.leastsq')
plsq, cov, info, mesg, ier = \
scipy.optimize.leastsq(_fitFunc, pfit,
args=(fitOnly,x,y,err,func,pfix,verbose,follow,),
full_output=True, epsfcn=epsfcn, ftol=ftol,
maxfev=maxfev, factor=factor)
info['exec time'] = time.time() - t0
mesg = mesg.replace('\n', '')
else:
method = 'L-BFGS-B'
#method = 'SLSQP'
#method = 'TNC'
#method = 'trust-constr'
if verbose:
print('[dpfit] using scipy.optimize.minimize (%s)'%method)
Bounds = []
for k in fitOnly:
if k in bounds.keys():
Bounds.append(bounds[k])
else:
Bounds.append((-np.inf, np.inf))
result = scipy.optimize.minimize(_fitFuncMin, pfit,
tol=ftol, options={'maxiter':maxfev},
bounds = Bounds, method=method,
args=(fitOnly,x,y,err,func,pfix,verbose,follow,)
)
plsq = result.x
display(result)
try:
# https://github.com/scipy/scipy/blob/2526df72e5d4ca8bad6e2f4b3cbdfbc33e805865/scipy/optimize/minpack.py#L739
# Do Moore-Penrose inverse discarding zero singular values.
_, s, VT = np.linalg.svd(result.jac, full_matrices=False)
threshold = np.finfo(float).eps * max(result.jac.shape) * s[0]
if verbose:
print('[dpfit] zeros in cov?', any(s<=threshold))
s = s[s > threshold]
VT = VT[:s.size]
cov = np.dot(VT.T / s**2, VT)
except:
cov = np.zeros((len(fitOnly), len(fitOnly)))
# ------------------------------------------------------
info = {'nfev':Ncalls, 'exec time':time.time()-t0},
mesg, ier = result.message, None
if verbose:
print('[dpfit]', mesg)
#print('[dpfit] ier:', ier)
print('[dpfit]', info['nfev'], 'function calls', end=' ')
t = 1000*info['exec time']/info['nfev']
n=-int(np.log10(t))+3
print('(',round(t, n), 'ms on average)')
notsig = []
if cov is None:
if verbose:
print('[dpfit] \033[31mWARNING: singular covariance matrix,', end=' ')
print('uncertainties cannot be computed\033[0m')
mesg += '; singular covariance matrix'
#print(' ', info['fjac'].shape)
# -- try to figure out what is going on!
delta = np.array(pfit)-np.array(plsq)
for i,k in enumerate(fitOnly):
if 'fjac' in info:
#test = max(np.abs(info['fjac'][i,:]))==0
_i = list(info['ipvt']).index(i+1)
test = max(np.abs(info['fjac'][_i,:]))==0
else:
test = np.abs(delta[i])<=epsfcn
if test:
if verbose:
print('[dpfit] \033[31m parameter "'+k+'" does not change CHI2:', end=' ')
print('IT CANNOT BE FITTED\033[0m')
mesg += '; parameter "'+k+'" does not change CHI2'
notsig.append(k)
cov = np.zeros((len(fitOnly), len(fitOnly)))
# -- best fit -> agregate to pfix
for i,k in enumerate(fitOnly):
pfix[k] = plsq[i]
# -- reduced chi2
model = func(x,pfix)
# -- residuals
if np.iterable(err) and len(np.array(err).shape)==2:
# -- assumes err matrix is co-covariance
r = y - model
chi2 = np.dot(np.dot(np.transpose(r), np.linalg.inv(err)), r)
ndof = len(y)-len(pfit)+1
reducedChi2 = chi2/ndof
else:
tmp = _fitFunc(plsq, fitOnly, x, y, err, func, pfix)
try:
chi2 = (np.array(tmp)**2).sum()
except:
chi2=0.0
for x in tmp:
chi2+=np.sum(x**2)
ndof = np.sum([1 if np.isscalar(i) else len(i) for i in tmp])-len(pfit)+1
reducedChi2 = chi2/ndof
if not np.isscalar(reducedChi2):
reducedChi2 = np.mean(reducedChi2)
if normalizedUncer:
try:
cov *= reducedChi2
except:
pass
# -- uncertainties:
uncer = {}
for k in pfix.keys():
if not k in fitOnly:
uncer[k]=0 # not fitted, uncertatinties to 0
else:
i = fitOnly.index(k)
if cov is None:
uncer[k]= -1
else:
uncer[k]= np.sqrt(np.abs(np.diag(cov)[i]))
# -- simple criteria to see if step is too large
notconverg = []
for k in filter(lambda x: x!='reduced chi2', trackP.keys()):
n = len(trackP[k])
std2 = np.std(trackP[k][(3*n)//4:])
ptp2 = np.ptp(trackP[k][(3*n)//4:])
if std2>2*uncer[k] and not k in notsig:
notconverg.append(k)
if len(notconverg) and verbose:
print('[dpfit] \033[33mParameters', notconverg,
'may not be converging properly\033[0m')
print('[dpfit] \033[33mcheck with "showFit" '+
'(too sensitive to relative variations?)\033[0m')
if type(verbose)==int and verbose>1 and showBest:
#print('-'*30)
print('# -- CHI2=', chi2)
print('# -- red CHI2=', reducedChi2)
print('# -- NDOF=', int(chi2/reducedChi2))
#print('-'*30)
dispBest({'best':pfix, 'uncer':uncer, 'fitOnly':fitOnly})
# -- result:
if fullOutput:
cor = np.sqrt(np.diag(cov))
cor = cor[:,None]*cor[None,:]
cor[cor==0] = 1e-6
cor = cov/cor
for k in trackP.keys():
trackP[k] = np.array(trackP[k])
pfix= {'func':func,
'best':pfix, 'uncer':uncer,
'chi2':reducedChi2, 'model':model,
'cov':cov, 'fitOnly':fitOnly,
'epsfcn':epsfcn, 'ftol':ftol,
'info':info, 'cor':cor, 'x':x, 'y':y, 'ndof':ndof,
'doNotFit':doNotFit,
'covd':{ki:{kj:cov[i,j] for j,kj in enumerate(fitOnly)}
for i,ki in enumerate(fitOnly)},
'cord':{ki:{kj:cor[i,j] for j,kj in enumerate(fitOnly)}
for i,ki in enumerate(fitOnly)},
'normalized uncertainties':normalizedUncer,
'maxfev':maxfev, 'firstGuess':params,
'track':trackP, 'mesg':mesg,
'not significant':notsig,
'not converging':notconverg,
'factor':factor,
}
if type(verbose)==int and verbose>2 and np.size(cor)>1 and showBest:
dispCor(pfix)
return pfix
_prog_N = 1
_prog_Nmax = 0
_prog_t0 = time.time()
def progress(results=None):
global _prog_N, _prog_Nmax, _prog_t0
_nb = 60 # length of the progress bar
tleft = (time.time()-_prog_t0)/max(_prog_N, 1)*(_prog_Nmax-_prog_N)
if tleft>100:
tleft = '%3.0fmin'%(tleft/60)
else:
tleft = '%3.0fs '%(tleft)
fmt = '%'+'%d'%int(np.ceil(np.log10(_prog_Nmax)))+'d'
fmt = '%s/%s'%(fmt, fmt)+' %s left'
res = time.asctime()+': '+\
'['+bytes((219,)).decode('cp437')*int(_nb*_prog_N/max(_prog_Nmax, 1))+\
'.'*(_nb-int(_nb*_prog_N/max(_prog_Nmax, 1))) + ']'+\
fmt%(_prog_N, _prog_Nmax, tleft)+'\r'
#print(res)
sys.stdout.write(res)
_prog_N+=1
def randomParam(fit, N=None, x='auto', multi=False):
"""
get a set of randomized parameters (list of dictionnaries) around the best
fited value, using a gaussian probability, taking into account the correlations
from the covariance matrix.
fit is the result of leastsqFit (dictionnary)
returns a fit dictionnary with: 'ymin', 'ymax' and 'r_param' (a list of the
randomized parameters)
"""
global _prog_N, _prog_Nmax, _prog_t0
if N is None:
N = len(fit['x'])
m = np.array([fit['best'][k] for k in fit['fitOnly']])
res = [] # list of dictionnaries
for k in range(N):
p = dict(zip(fit['fitOnly'],np.random.multivariate_normal(m, fit['cov'])))
p.update({k:fit['best'][k] for k in fit['best'].keys() if not k in
fit['fitOnly']})
res.append(p)
ymin, ymax = None, None
tmp = []
if x == 'auto':
x = fit['x']
if not x is None:
if not multi:
for r in res:
tmp.append(fit['func'](x, r))
tmp = np.array(tmp)
else:
if type(multi)==int:
pool = multiprocessing.Pool(multi)
else:
pool = multiprocessing.Pool()
_prog_N = 1
_prog_Nmax = N
_prog_t0 = time.time()
M = []
for r in res:
M.append(pool.apply_async(fit['func'], (x, r, ), callback=progress))
pool.close()
pool.join()
tmp = np.array([m.get(timeout=1) for m in M])
fit['all_y'] = tmp
fit['r_y'] = fit['func'](x, fit['best'])
fit['r_ym1s'] = np.percentile(tmp, 16, axis=0)
fit['r_yp1s'] = np.percentile(tmp, 84, axis=0)
fit['r_param'] = res
fit['r_x'] = x
return fit
randomParam = randomParam # legacy
def bootstrap(func, x, params, y, err=None, fitOnly=None,
verbose=False, doNotFit=[], epsfcn=1e-7,
ftol=1e-5, fullOutput=True, normalizedUncer=True,
follow=None, Nboot=None):
"""
bootstraping, called like leastsqFit. returns a list of fits: the first one
is the 'normal' one, the Nboot following one are with ramdomization of data. If
Nboot is not given, it is set to 10*len(x).
"""
if Nboot==None:
Nboot = 10*len(x)
if 'fitOnly' in params and fitOnly is None:
fitOnly = params['fitOnly']
if 'best' in params:
params = params['best']
# first fit is the "normal" one
fits = [leastsqFit(func, x, params, y,
err=err, fitOnly=fitOnly, verbose=False,
doNotFit=doNotFit, epsfcn=epsfcn,
ftol=ftol, fullOutput=True,
normalizedUncer=True)]
for k in range(Nboot):
s = np.int_(len(x)*np.random.rand(len(x)))
fits.append(leastsqFit(func, x[s], params, y[s],
err=err, fitOnly=fitOnly, verbose=False,
doNotFit=doNotFit, epsfcn=epsfcn,
ftol=ftol, fullOutput=True,
normalizedUncer=True))
return fits
def showBootstrap(boot, fig=1, fontsize=8):
global _AX, _AY
plt.close(fig); plt.figure(fig)
n = len(boot[0]['fitOnly'])
_AX, _AY = {}, {}
for i1, k1 in enumerate(sorted(boot[0]['fitOnly'])):
_AX[k1] = plt.subplot(n,n,i1*n+i1+1)
_AX[k1].yaxis.set_visible(False)
X = [b['best'][k1] for b in boot]
bins = max(int(np.sqrt(len(X))), 10)
# -- bootstraped fits
h = plt.hist(X[1:], color='k', histtype='step', density=True, bins=bins)
h = plt.hist(X[1:], color='0.9', density=True, bins=bins)
# -- fit to all data
plt.errorbar(X[0], 0.45*np.max(h[0]), xerr=boot[0]['uncer'][k1], color='orange',
marker='s', capsize=5, label='all data')
# -- 1 sigma asymetric uncertainties
X0, Xm, XM = np.percentile(X[1:], 50), np.percentile(X[1:], 16), np.percentile(X[1:], 100-16)
Xm, XM = X0-Xm, XM-X0
plt.errorbar(X0, 0.55*np.max(h[0]), xerr=([Xm], [XM]), color='blue',
marker='d', capsize=5, label='bootstrapped')
d = int(2-np.round(.5*(np.log10(Xm)+np.log10(XM)), 0))
fmt = '%s=\n$%.'+str(d)+'f^{+%.'+str(d)+'f}_{-%.'+str(d)+'f}$'
plt.title(fmt%(k1, X0, Xm, XM), fontsize=1.2*fontsize)
if i1!=len(sorted(boot[0]['fitOnly']))-1:
_AX[k1].xaxis.set_visible(False)
else:
_AX[k1].tick_params(axis='x', labelsize=fontsize)
#_AX[k1].callbacks.connect('xlim_changed', _callbackAxesBoot)
plt.legend(loc='upper right', fontsize=0.6*fontsize)
for i1, k1 in enumerate(sorted(boot[0]['fitOnly'])):
for i2, k2 in enumerate(sorted(boot[0]['fitOnly'])):
if i1<i2:
if i1==0:
_AY[k2] = plt.subplot(n,n,i2*n+i1+1, sharex=_AX[k1])
_AY[k2].callbacks.connect('ylim_changed', _callbackAxesBoot)
plt.ylabel(k2, fontsize=1.2*fontsize)
ax = _AY[k2]
ax.tick_params(axis='y', labelsize=fontsize)
else:
ax = plt.subplot(n,n,i2*n+i1+1, #sharex=_AX[k1],
sharey=_AY[k2])
ax.yaxis.set_visible(False)
X, Y = [b['best'][k1] for b in boot], [b['best'][k2] for b in boot]
# -- bootstrap
plt.plot(X[1:], Y[1:], '.k', alpha=0.1)
plt.plot(np.median(X[1:]), np.median(Y[1:]), 'db', alpha=0.5)
# -- error ellipse
cov = np.cov([X[1:], Y[1:]])
t = np.linspace(0,2*np.pi,100)
sMa, sma, a = _ellParam(cov[0,0], cov[1,1], cov[0,1])
Xe,Ye = sMa*np.cos(t), sma*np.sin(t)
Xe,Ye = Xe*np.cos(a)+Ye*np.sin(a),-Xe*np.sin(a)+Ye*np.cos(a)
plt.plot(np.median(X[1:])+Xe, np.median(Y[1:])+Ye, '-b', alpha=0.5)
# -- fit to all data:
plt.plot(X[0], Y[0], marker='s', color='orange', alpha=0.5)
# -- err ellipse
ell = errorEllipse(boot[0], k1, k2)
plt.plot(ell[0], ell[1], '-', color='orange', alpha=0.5)
if i2!=len(sorted(boot[0]['fitOnly']))-1:
ax.xaxis.set_visible(False)
else:
ax.tick_params(axis='x', labelsize=fontsize)
plt.subplots_adjust(wspace=0, hspace=0)
def _callbackAxesBoot(ax):
global _AX, _AY
i = None
for k in _AY.keys():
if ax==_AY[k]:
i = k
if not i is None:
_AX[i].set_xlim(ax.get_ylim())
else:
pass
i = None
for k in _AX.keys():
if ax==_AX[k]:
i = k
if not i is None:
_AY[i].set_ylim(ax.get_xlim())
else:
pass
return
def randomize(func, x, params, y, err=None, fitOnly=None,
verbose=False, doNotFit=[], epsfcn=1e-7,
ftol=1e-5, fullOutput=True, normalizedUncer=True,
follow=None, Nboot=None):
"""
bootstraping, called like leastsqFit. returns a list of fits: the first one
is the 'normal' one, the Nboot following one are with ramdomization of data. If
Nboot is not given, it is set to 10*len(x).
"""
if Nboot==None:
Nboot = 10*len(x)
# first fit is the "normal" one
fits = [leastsqFit(func, x, params, y,
err=err, fitOnly=fitOnly, verbose=False,
doNotFit=doNotFit, epsfcn=epsfcn,
ftol=ftol, fullOutput=True,
normalizedUncer=True)]
for k in range(Nboot):
s = err*np.random.randn(len(y))
fits.append(leastsqFit(func, x, params, y+s,
err=err, fitOnly=fitOnly, verbose=False,
doNotFit=doNotFit, epsfcn=epsfcn,
ftol=ftol, fullOutput=True,
normalizedUncer=True))
return fits
def _fitFunc(pfit, pfitKeys, x, y, err=None, func=None, pfix=None, verbose=False, follow=None):
"""
interface scipy.optimize.leastsq:
- x,y,err are the data to fit: f(x) = y +- err
- pfit is a list of the paramters
- pfitsKeys are the keys to build the dict
pfit and pfix (optional) and combines the two
in 'A', in order to call F(X,A)
in case err is a ndarray of 2 dimensions, it is treated as the
covariance of the errors.
np.array([[err1**2, 0, .., 0],
[ 0, err2**2, 0, .., 0],
[0, .., 0, errN**2]]) is the equivalent of 1D errors
"""
global verboseTime, Ncalls, trackP
Ncalls+=1
params = {}
# -- build dic from parameters to fit and their values:
for i,k in enumerate(pfitKeys):
params[k]=pfit[i]
# -- complete with the non fitted parameters:
for k in pfix:
params[k]=pfix[k]
if err is None:
err = np.ones(np.array(y).shape)
# -- compute residuals
if (type(y)==np.ndarray and type(err)==np.ndarray) or \
(np.isscalar(y) and type(err)==np.ndarray) or \
(type(y)==np.ndarray and np.isscalar(err)) or \
(np.isscalar(y) and np.isscalar(err)):
model = func(x, params)
res = ((np.array(y)-model)/err).flatten()
else:
# much slower: this time assumes y (and the result from func) is
# a list of things, each convertible in np.array
res = []
tmp = func(x,params)
if np.isscalar(err):
err = 0*y + err
#print 'DEBUG:', tmp.shape, y.shape, err.shape
for k in range(len(y)):
df = (np.array(tmp[k])-np.array(y[k]))/np.array(err[k])
try:
res.extend(list(df))
except:
res.append(df)
try:
chi2=(res**2).sum/(len(res)-len(pfit)+1.0)
except:
# list of elements
chi2 = 0
N = 0
#res2 = []
for r in res:
if np.isscalar(r):
chi2 += r**2
N+=1
r#es2.append(r)
else:
chi2 += np.sum(np.array(r)**2)
N+=len(r)
#res2.extend(list(r))
chi2 /= N-len(pfit)+1.0
if verbose and time.time()>(verboseTime+10):
verboseTime = time.time()
print('[dpfit]', time.asctime(), '%03d/%03d'%(Ncalls, int(Ncalls/len(pfit))), end=' ')
print('CHI2: %6.4e'%chi2,end='|')
if follow is None:
print('')
else:
_follow = list(filter(lambda x: x in params.keys() and
type(params[x]) in [float, np.double], follow))
print('|'.join([k+'='+'%5.2e'%params[k] for k in _follow]))
for i,k in enumerate(pfitKeys):
if not k in trackP:
trackP[k] = [pfit[i]]
else:
trackP[k].append(pfit[i])
if not 'reduced chi2' in trackP:
trackP['reduced chi2'] = [chi2]
else:
trackP['reduced chi2'].append(chi2)
return res
def _fitFuncMin(pfit, pfitKeys, x, y, err=None, func=None, pfix=None, verbose=False, follow=None):
"""
interface scipy.optimize.minimize:
- x,y,err are the data to fit: f(x) = y +- err
- pfit is a list of the paramters
- pfitsKeys are the keys to build the dict
pfit and pfix (optional) and combines the two
in 'A', in order to call F(X,A)
in case err is a ndarray of 2 dimensions, it is treated as the
covariance of the errors.
np.array([[err1**2, 0, .., 0],
[ 0, err2**2, 0, .., 0],
[0, .., 0, errN**2]]) is the equivalent of 1D errors
"""
global verboseTime, Ncalls, trackP
Ncalls+=1
params = {}
# -- build dic from parameters to fit and their values:
for i,k in enumerate(pfitKeys):
params[k]=pfit[i]
# -- complete with the non fitted parameters:
for k in pfix:
params[k]=pfix[k]
if err is None:
err = np.ones(np.array(y).shape)
# -- compute residuals
if type(y)==np.ndarray and type(err)==np.ndarray:
model = func(x,params)
res= ((np.array(y)-model)/err).flatten()
else:
# much slower: this time assumes y (and the result from func) is
# a list of things, each convertible in np.array
res = []
tmp = func(x,params)
if np.isscalar(err):
err = 0*y + err
#print 'DEBUG:', tmp.shape, y.shape, err.shape
for k in range(len(y)):
df = (np.array(tmp[k])-np.array(y[k]))/np.array(err[k])
try:
res.extend(list(df))
except:
res.append(df)
try:
chi2=(res**2).sum/(len(res)-len(pfit)+1.0)
except:
# list of elements
chi2 = 0
N = 0
res2 = []
for r in res:
if np.isscalar(r):
chi2 += r**2
N+=1
res2.append(r)
else:
chi2 += np.sum(np.array(r)**2)
N+=len(r)
res2.extend(list(r))
res = res2
chi2 /= float(N-len(pfit)+1)
if verbose and time.time()>(verboseTime+10):
verboseTime = time.time()
print('[dpfit]', time.asctime(), '%5d'%Ncalls,end='')
print('CHI2: %6.4e'%chi2,end='|')
if follow is None:
print('')
else:
_follow = list(filter(lambda x: x in params.keys(), follow))
print('|'.join([k+'='+'%5.2e'%params[k] for k in _follow]))
return chi2
def _fitFunc2(x, *pfit, verbose=True, follow=[], errs=None):
"""
for curve_fit
"""
global pfitKeys, pfix, _func, Ncalls, verboseTime
Ncalls +=1
params = {}
# -- build dic from parameters to fit and their values:
for i,k in enumerate(pfitKeys):
params[k]=pfit[i]
# -- complete with the non fitted parameters:
for k in pfix:
params[k]=pfix[k]
res = _func(x, params)
if verbose and time.time()>(verboseTime+10):
verboseTime = time.time()
print('[dpfit]', time.asctime(), '%5d'%Ncalls,end='')
try:
chi2=np.sum(res**2)/(len(res)-len(pfit)+1.0)
print('CHI2: %6.4e'%chi2,end='')
except:
# list of elements
chi2 = 0
N = 0
res2 = []
for r in res:
if np.isscalar(r):
chi2 += r**2
N+=1
res2.append(r)
else:
chi2 += np.sum(np.array(r)**2)
N+=len(r)
res2.extend(list(r))
res = res2
print('CHI2: %6.4e'%(chi2/float(N-len(pfit)+1)), end=' ')
if follow is None:
print('')
else:
_follow = list(filter(lambda x: x in params.keys(), follow))
print(' '.join([k+'='+'%5.2e'%params[k] for k in _follow]))
return res
def errorEllipse(fit, p1, p2, n=100):
"""
fit is a result from leastsqFit (dict)
p1, p2 are parameters name (str)
n number of point in ellipse (int, default 100)
returns ellipse of errors (x1, x2), computed from the covariance. The n values are centered
around fit['best']['p1'] and fit['best']['p2']
"""
t = np.linspace(0,2*np.pi,n)
if 'covd' in fit:
sMa, sma, a = _ellParam(fit['covd'][p1][p1], fit['covd'][p2][p2], fit['covd'][p1][p2])
else:
i1 = fit['fitOnly'].index(p1)
i2 = fit['fitOnly'].index(p2)
sMa, sma, a = _ellParam(fit['cov'][i1,i1], fit['cov'][i2,i2], fit['cov'][i1,i2])
X,Y = sMa*np.cos(t), sma*np.sin(t)
X,Y = X*np.cos(a)+Y*np.sin(a),-X*np.sin(a)+Y*np.cos(a)
return fit['best'][p1]+X, fit['best'][p2]+Y
def _ellParam(sA2, sB2, sAB):