forked from mike-a-yen/bpz-1.99.3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseful.py
1291 lines (1131 loc) · 35.9 KB
/
useful.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
## Automatically adapted for numpy Jun 08, 2006 by convertcode.py
#Useful functions and definitions
import os,sys
from types import *
#from Numeric import *
from numpy import *
#from MLab import *
from MLab_coe import *
from time import *
from spline import *
from string import * # AFTER numpy, WHICH HAS ITS OWN split, (and more?)
#import whrandom
import random
#To use the astrometrical functions defined below
#try: import ephem
#except: pass
#If biggles installed allow the plot options in the tests
plots=1
try:
from biggles import *
except: plots=0
pi=3.141592653
def ejecuta(command=None,verbose=1):
import os
if verbose: print command
os.system(command)
def ask(what="?"):
"""
Usage:
ans=ask(pregunta)
This function prints the string what,
(usually a question) and asks for input
from the user. It returns the value 0 if the
answer starts by 'n' and 1 otherwise, even
if the input is just hitting 'enter'
"""
if what[-1]<>'\n': what=what+'\n'
ans=raw_input(what)
try:
if ans[0]=='n': return 0
except:
pass
return 1
#Input/Output subroutines
#Read/write headers
def get_header(file):
""" Returns a string containing all the lines
at the top of a file which start by '#'"""
buffer=''
for line in open(file).readlines():
if line[0]=='#': buffer=buffer+line
else: break
return buffer
def put_header(file,text,comment=1):
"""Adds text (starting by '#' and ending by '\n')
to the top of a file."""
if len(text)==0: return
if text[0]<>'#' and comment: text='#'+text
if text[-1]<>'\n':text=text+'\n'
buffer=text+open(file).read()
open(file,'w').write(buffer)
#Files containing strings
def get_str(file,cols=0,nrows='all'):
"""
Reads strings from a file
Usage:
x,y,z=get_str('myfile.cat',(0,1,2))
x,y,z are returned as string lists
"""
if type(cols)==type(0):
cols=(cols,)
nvar=1
else: nvar=len(cols)
lista=[]
for i in range(nvar): lista.append([])
buffer=open(file).readlines()
if nrows=='all': nrows=len(buffer)
counter=0
for lines in buffer:
if counter>=nrows : break
if lines[0]=='#': continue
pieces=split(lines)
if len(pieces)==0: continue
for j in range(nvar):lista[j].append(pieces[cols[j]])
counter=counter+1
if nvar==1: return lista[0]
else: return tuple(lista)
def put_str(file,tupla):
""" Writes tuple of string lists to a file
Usage:
put_str(file,(x,y,z))
"""
if type(tupla)<>type((2,)):
raise 'Need a tuple of variables'
f=open(file,'w')
for i in range(1,len(tupla)):
if len(tupla[i])<>len(tupla[0]):
raise 'Variable lists have different lenght'
for i in range(len(tupla[0])):
cosas=[]
for j in range(len(tupla)):cosas.append(str(tupla[j][i]))
f.write(join(cosas)+'\n')
f.close()
#Files containing data
def get_data(file,cols=0,nrows='all'):
""" Returns data in the columns defined by the tuple
(or single integer) cols as a tuple of float arrays
(or a single float array)"""
if type(cols)==type(0):
cols=(cols,)
nvar=1
else: nvar=len(cols)
data=get_str(file,cols,nrows)
if nvar==1: return array(map(float,data))
else:
data=list(data)
for j in range(nvar): data[j]=array(map(float,data[j]))
return tuple(data)
def write(file,variables,header='',format='',append='no'):
""" Writes tuple of list/arrays to a file
Usage:
put_data(file,(x,y,z),header,format)
where header is any string
and format is a string of the type:
'%f %f %i '
The default format is all strings
"""
if type(variables)<>type((2,)):
raise 'Need a tuple of variables'
if format=='': format='%s '*len(variables)
if append=='yes': f=open(file,'a')
else: f=open(file,'w')
if header<>"":
if header[0]<>'#': header='#'+header
if header[-1]<>'\n': header=header+'\n'
f.write(header)
for i in range(len(variables[0])):
cosas=[]
for j in range(len(variables)):
cosas.append(variables[j][i])
line=format % tuple(cosas)
f.write(line+'\n')
f.close()
put_data=write
#Read/write 2D arrays
def get_2Darray(file,cols='all',nrows='all',verbose='no'):
"""Read the data on the defined columns of a file
to an 2 array
Usage:
x=get_2Darray(file)
x=get_2Darray(file,range(len(p))
x=get_2Darray(file,range(0,10,2),nrows=5000)
Returns x(nrows,ncols)
"""
if cols=='all':
#Get the number of columns in the file
for line in open(file).readlines():
pieces=split(line)
if len(pieces)==0: continue
if line[0]=='#':continue
nc=len(pieces)
cols=range(nc)
if verbose=='yes': print 'cols=',cols
break
else:
nc=len(cols)
lista=get_data(file,cols,nrows)
nl=len(lista[0])
x=zeros((nl,nc),float)
for i in range(nc):x[:,i]=lista[i]
return x
def put_2Darray(file,array,header='',format='',append='no'):
""" Writes a 2D array to a file, where the first
index changes along the lines and the second along
the columns
Usage: put_2Darray(file,a,header,format)
where header is any string
and format is a string of the type:
'%f %f %i '
"""
lista=[]
for i in range(array.shape[1]):lista.append(array[:,i])
lista=tuple(lista)
put_data(file,lista,header,format,append)
class watch:
def set(self):
self.time0=time()
print ''
print 'Current time ',ctime(self.time0)
print
def check(self):
if self.time0:
print
print "Elapsed time", strftime('%H:%M:%S',gmtime(time()-self.time0))
print
else:
print
print 'You have not set the initial time'
print
def params_file(file):
"""
Read a input file containing the name of several parameters
and their values with the following format:
KEY1 value1,value2,value3 # comment
KEY2 value
Returns the dictionary
dict['KEY1']=(value1,value2,value3)
dict['KEY2']=value
"""
dict={}
for line in open(file,'r').readlines():
if line[0]==' ' or line[0]=='#': continue
halves=split(line,'#')
#replace commas in case they're present
halves[0]=replace(halves[0],',',' ')
pieces=split(halves[0])
if len(pieces)==0: continue
key=pieces[0]
# if type(key)<>type(''):
# raise 'Keyword not string!'
if len(pieces)<2:
mensaje='No value(s) for parameter '+key
raise mensaje
dict[key]=tuple(pieces[1:])
if len(dict[key])==1: dict[key]=dict[key][0]
return dict
def params_commandline(lista):
""" Read an input list (e.g. command line)
containing the name of several parameters
and their values with the following format:
['-KEY1','value1,value2,value3','-KEY2','value',etc.]
Returns a dictionary containing
dict['KEY1']=(value1,value2,value3)
dict['KEY2']=value
etc.
"""
if len(lista)%2<>0:
print 'Error: The number of parameter names and values does not match'
sys.exit()
dict={}
for i in range(0,len(lista),2):
key=lista[i]
if type(key)<>type(''):
raise 'Keyword not string!'
#replace commas in case they're present
if key[0]=='-':key=key[1:]
lista[i+1]=replace(lista[i+1],',',' ')
values=tuple(split(lista[i+1]))
if len(values)<1:
mensaje='No value(s) for parameter '+key
raise mensaje
dict[key]=values
if len(dict[key])==1: dict[key]=dict[key][0]
return dict
def view_keys(dict):
"""Prints sorted dictionary keys"""
claves=dict.keys()
claves.sort()
for line in claves:
print upper(line),' = ',dict[line]
class params:
"""This class defines and manages a parameter dictionary"""
def __init__(self,d=None):
if d==None:self.d={}
else: self.d=d
# Define a few useful methods:
def fromfile(self,file):
"""Update the parameter dictionary with a file"""
self.d.update(params_file(file))
def fromcommandline(self,command_line):
"""Update the parameter dictionary with command line options (sys.argv[i:])"""
self.d.update(params_commandline(command_line))
def update(self,dict):
"""Update the parameter information with a dictionary"""
for key in dict.keys():
self.d[key]=dict[key]
def check(self):
"""Interactively check the values of the parameters"""
view_keys(self.d)
paso1=raw_input('Do you want to change any parameter?(y/n)\n')
while paso1[0] == 'y':
key=raw_input('Which one?\n')
if not self.d.has_key(key):
paso2=raw_input("This parameter is not in the dictionary.\
Do you want to include it?(y/n)\n")
if paso2[0]=='y':
value=raw_input('value(s) of '+key+'?= ')
self.d[key]=tuple(split(replace(value,',',' ')))
else:continue
else:
value=raw_input('New value(s) of '+key+'?= ')
self.d[key]=tuple(split(replace(value,',',' ')))
view_keys(self.d)
paso1=raw_input('Anything else?(y/n)\n')
def write(self,file):
claves=self.d.keys()
claves.sort()
buffer=''
for key in claves:
if type(self.d[key])==type((2,)):
values=map(str,self.d[key])
line=key+' '+string.join(values,',')
else:
line=key+' '+str(self.d[key])
buffer=buffer+line+'\n'
print line
open(file,'w').write(buffer)
#List of colors from biggles:
def biggles_colors():
try: import biggles
except: pass
return get_str('/home/txitxo/Python/biggles_colors.txt',0)
#Some miscellaneous numerical functions
def ascend(x):
"""True if vector x is monotonically ascendent, false otherwise
Recommended usage:
if not ascend(x): sort(x)
"""
return alltrue(greater_equal(x[1:],x[0:-1]))
#def match_resol(xg,yg,xf,method="linear"):
# """
# Interpolates and/or extrapolate yg, defined on xg, onto the xf coordinate set.
# Options are 'lineal' or 'spline' (uses spline.py from Johan Hibscham)
# Usage:
# ygn=match_resol(xg,yg,xf,'spline')
# """
# if method<>"spline":
# if type(xf)==type(1.): xf=array([xf])
# ng=len(xg)
# d=(yg[1:]-yg[0:-1])/(xg[1:]-xg[0:-1])
# #Get positions of the new x coordinates
# ind=clip(searchsorted(xg,xf)-1,0,ng-2)
# ygn=take(yg,ind)+take(d,ind)*(xf-take(xg,ind))
# if len(ygn)==1: ygn=ygn[0]
# return ygn
# else:
# low_slope=(yg[1]-yg[0])/(xg[1]-xg[0])
# high_slope=(yg[-1]-yg[-2])/(xg[-1]-xg[-2])
# sp=Spline(xg,yg,low_slope,high_slope)
# return sp(xf)
def match_resol(xg,yg,xf,method="linear"):
"""
Interpolates and/or extrapolate yg, defined on xg, onto the xf coordinate set.
Options are 'linear' or 'spline' (uses spline.py from Johan Hibscham)
Usage:
ygn=match_resol(xg,yg,xf,'spline')
"""
if method<>"spline":
if type(xf)==type(1.): xf=array([xf])
ng=len(xg)
# print argmin(xg[1:]-xg[0:-1]),min(xg[1:]-xg[0:-1]),xg[argmin(xg[1:]-xg[0:-1])]
d=(yg[1:]-yg[0:-1])/(xg[1:]-xg[0:-1])
#Get positions of the new x coordinates
ind=clip(searchsorted(xg,xf)-1,0,ng-2)
ygn=take(yg,ind)+take(d,ind)*(xf-take(xg,ind))
if len(ygn)==1: ygn=ygn[0]
return ygn
else:
low_slope=(yg[1]-yg[0])/(xg[1]-xg[0])
high_slope=(yg[-1]-yg[-2])/(xg[-1]-xg[-2])
sp=Spline(xg,yg,low_slope,high_slope)
return sp(xf)
def overlap(x,y):
"""Returns 1 if vectors x and y overlap, 0 otherwise"""
if (x[0]<=y[-1] and x[-1]>y[0]) or (y[0]<=x[-1] and y[-1]>x[0]):
return 1
else: return 0
def match_objects(coords1,coords2,tail1=(),tail2=(),accuracy=1.):
"""
where coords1 and coords2 are tuples containing 1-D arrays,
and tail1 and tail2 are tuples containing sequences of
arbitrary types
Usage:
results=match_objects((x1,y1),(x2,y2),(a1,b1,c1),(d2,e2),accuracy=.5)
It returns the sequence x1,y1,a1,b1,c1,d2,e2 for those objects
which have dist(x1,y1-x2,y2)< accuracy
"""
acc2=accuracy**2
nc=len(coords1)
np1=len(coords1[0])
np2=len(coords2[0])
a1=array(coords1)
a2=array(coords2)
nt1=len(tail1)
for i in range(nt1):
if len(tail1[i])<> np1: raise 'Not the same lenght as coordinates 1'
nt2=len(tail2)
for i in range(nt2):
if len(tail2[i])<> np2: raise 'Not the same lenght as coordinates 2'
match=zeros(np1, int)-1
for j in range(np1):
#dist=add.reduce((a1[:,j,NewAxis]-a2[:,:])**2)
a1j = a1[:,j]
dist=add.reduce((reshape(a1j, (len(a1j), 1)) - a2)**2)
i_min=argmin(dist)
if dist[i_min]<acc2:match[j]=i_min
good=greater_equal(match,0)
n1=compress(good,range(np1))
match=compress(good,match)
a1=compress(good,a1)
salida=list(a1)
for i in range(nt1):
if type(tail1[i][0])==type('si'):
t=[]
for j in n1: t.append(tail1[i][j])
else:
t=take(tail1[i],n1)
salida.append(t)
for i in range(nt2):
if type(tail2[i][0])==type('si'):
t=[]
for j in match: t.append(tail2[i][j])
else:
t=take(tail2[i],match)
salida.append(t)
return salida
def match_min(coords1,coords2,tail1=(),tail2=()):
"""
where coords1 and coords2 are tuples containing 1-D arrays,
and tail1 and tail2 are tuples containing sequences of
arbitrary types
Usage:
results=match_min((x1,y1),(x2,y2),(a1,b1,c1),(d2,e2))
It returns the sequence x1,y1,a1,b1,c1,d2,e2, dist_min
where dist_min is the minimal value of dist(x1,y1-x2,y2)
The match looks for the objects with minimal distance
"""
nc=len(coords1)
np1=len(coords1[0])
np2=len(coords2[0])
a1=array(coords1)
a2=array(coords2)
nt1=len(tail1)
for i in range(nt1):
if len(tail1[i])<> np1: raise 'Not the same lenght as coordinates 1'
nt2=len(tail2)
for i in range(nt2):
if len(tail2[i])<> np2: raise 'Not the same lenght as coordinates 2'
match=zeros(np1, int)-1
dist_min=zeros(np1)*1.
for j in range(np1):
#dist=sqrt(add.reduce((a1[:,j,NewAxis]-a2[:,:])**2))
a1j = a1[:,j]
dist=add.reduce((reshape(a1j, (len(a1j), 1)) - a2)**2)
i_min=argmin(dist)
dist_min[j]=dist[i_min]
match[j]=i_min
salida=list(a1)
for i in range(nt1):salida.append(tail1[i])
for i in range(nt2):
if type(tail2[i][0])==type('si'):
t=[]
for j in match: t.append(tail2[i][j])
else:
t=take(tail2[i],match)
salida.append(t)
salida.append(dist_min)
return tuple(salida)
def match_min2(coords1,coords2,tail1=(),tail2=()):
"""
where coords1 and coords2 are tuples containing 1-D arrays,
and tail1 and tail2 are tuples containing sequences of
arbitrary types
Usage:
results=match_min((x1,y1),(x2,y2),(a1,b1,c1),(d2,e2))
It returns the sequence x1,y1,x2,y2,a1,b1,c1,d2,e2, dist_min
where dist_min is the minimal value of dist(x1,y1-x2,y2)
The match looks for the objects with minimal distance
"""
nc=len(coords1)
np1=len(coords1[0])
np2=len(coords2[0])
a1=array(coords1)
a2=array(coords2)
nt1=len(tail1)
for i in range(nt1):
if len(tail1[i])<> np1: raise 'Not the same lenght as coordinates 1'
nt2=len(tail2)
for i in range(nt2):
if len(tail2[i])<> np2: raise 'Not the same lenght as coordinates 2'
match=zeros(np1, int)-1
dist_min=zeros(np1)*1.
x2=zeros(np1)*1.
y2=zeros(np1)*1.
for j in range(np1):
#dist=add.reduce((a1[:,j,NewAxis]-a2[:,:])**2)
a1j = a1[:,j]
dist=add.reduce((reshape(a1j, (len(a1j), 1)) - a2)**2)
i_min=argmin(dist)
dist_min[j]=dist[i_min]
x2[j],y2[j]=a2[0,i_min],a2[1,i_min]
match[j]=i_min
salida=list(a1)
salida.append(x2)
salida.append(y2)
for i in range(nt1):salida.append(tail1[i])
for i in range(nt2):
if type(tail2[i][0])==type('si'):
t=[]
for j in match: t.append(tail2[i][j])
else:
t=take(tail2[i],match)
salida.append(t)
salida.append(dist_min)
return tuple(salida)
def dist(x,y,xc=0.,yc=0.):
"""Distance between point (x,y) and a center (xc,yc)"""
return sqrt((x-xc)**2+(y-yc)**2)
def loc2d(a,extremum='max'):
""" Locates the maximum of an 2D array
Usage:
max_vec=max_loc2d(a)
"""
forma=a.shape
if len(forma)>2:raise "Array dimension > 2"
if extremum<>'min' and extremum<>'max':
raise 'Which extremum are you looking for?'
x=ravel(a)
if extremum=='min': i=argmin(x)
else: i=argmax(x)
i1=i/forma[1]
i2=i%forma[1]
return i1,i2
def hist(a,bins):
"""
Histogram of 'a' defined on the bin grid 'bins'
Usage: h=hist(p,xp)
"""
n=searchsorted(sort(a),bins)
n=concatenate([n,[len(a)]])
n=array(map(float,n))
# n=array(n)
return n[1:]-n[:-1]
#def hist2D(a,xbins,ybins):
# """
# Histogram of 'a' defined on the grid xbins X ybins
# Usage: h=hist2D(p,xp,yp)
# Points larger than xbins[-1],ybins[-1] are asigned to
# the 'last' bin
# """
# nx=len(xbins)
# ny=len(ybins)
# #We use searchsorted differenty from the 1-D case
# hx=searchsorted(xbins,a)
# hy=searchsorted(ybins,a)
# h=zeros((nx,ny))
# for i in range(len(hx)):
# for j in range(len(hy)):
# h[hx[i],hy[i]]=+1
# for k in range(len(a)):
# for i in range(len(xbins)):
# for j in range(len(ybins)):
# if a[k]>xbins[i] and a[k]<xbins[i+1] \
# and a[k]>ybins[i] and a[k]< ybins[i+1]:
# h[i,j]=h[i,j]+1
# break
# else:
def bin_stats(x,y,xbins,stat='average'):
"""Given the variable y=f(x), and
the bins limits xbins, return the
corresponding statistics, e.g. <y(xbins)>
Options are rms, median y average
"""
nbins=len(xbins)
if stat=='average' or stat=='mean': func=mean
elif stat=='median': func=median
elif stat=='rms' or stat=='std' : func=std
elif stat=='std_robust' or stat=='rms_robust': func=std_robust
elif stat=='mean_robust': func=mean_robust
elif stat=='median_robust': func=median_robust
elif stat=='sum': func=sum
results=[]
for i in range(nbins):
if i<nbins-1:
good=(greater_equal(x,xbins[i])
*less(x,xbins[i+1]))
else: good=(greater_equal(x,xbins[-1]))
if sum(good)>1.: results.append(func(compress(good,y)))
else:
results.append(0.)
print 'Bin starting at xbins[%i] has %i points' % (i,sum(good))
return array(results)
def bin_aver(x,y,xbins):
return bin_stats(x,y,xbins,stat='average')
def p2p(x):
return max(x) - min(x)
def autobin_stats(x,y,n_bins=8,stat='average',n_points=None):
"""
Given the variable y=f(x), form n_bins, distributing the
points equally among them. Return the average x position
of the points in each bin, and the corresponding statistic stat(y).
n_points supersedes the value of n_bins and makes the bins
have exactly n_points each
Usage:
xb,yb=autobin_stats(x,y,n_bins=8,'median')
xb,yb=autobin_stats(x,y,n_points=5)
"""
if not ascend(x):
ix=argsort(x)
x=take(x,ix)
y=take(y,ix)
n=len(x)
if n_points==None:
#This throws out some points
n_points=n/n_bins
else:
n_bins=n/n_points
#if there are more that 2 points in the last bin, add another bin
if n%n_points>2: n_bins=n_bins+1
if n_points<=1:
print 'Only 1 or less points per bin, output will be sorted input vector with rms==y'
return x,y
xb,yb=[],[]
#print 'stat', stat
if stat=='average' or stat=='mean': func=mean
elif stat=='median': func=median
elif stat=='rms' or stat=='std' : func=std
elif stat=='std_robust' or stat=='rms_robust': func=std_robust
elif stat=='mean_robust': func=mean_robust
elif stat=='median_robust': func=median_robust
elif stat=='p2p': func=p2p # --DC
elif stat=='min': func=min # --DC
elif stat=='max': func=max # --DC
for i in range(n_bins):
xb.append(mean(x[i*n_points:(i+1)*n_points]))
if func==std and n_points==2:
print 'n_points==2; too few points to determine rms'
print 'Returning abs(y1-y2)/2. in each bin as rms'
yb.append(abs(y[i*n_points]-y[i*n_points+1])/2.)
else:
yb.append(func(y[i*n_points:(i+1)*n_points]))
if i>2 and xb[-1]==xb[-2]:
yb[-2]=(yb[-2]+yb[-1])/2.
xb=xb[:-1]
yb=yb[:-1]
return array(xb),array(yb)
def purge_outliers(x,n_sigma=3.,n=5):
#Experimental yet. Only 1 dimension
for i in range(n):
med=median(x)
#rms=std_log(x)
rms=std(x)
x=compress(less_equal(abs(x-med),n_sigma*rms),x)
return x
class stat_robust:
#Generates robust statistics using a sigma clipping
#algorithm. It is controlled by the parameters n_sigma
#and n, the number of iterations
def __init__(self,x,n_sigma=3,n=5,reject_fraction=None):
self.x=x
self.n_sigma=n_sigma
self.n=n
self.reject_fraction=reject_fraction
def run(self):
good=ones(len(self.x))
nx=sum(good)
if self.reject_fraction==None:
for i in range(self.n):
if i>0: xs=compress(good,self.x)
else: xs=self.x
# aver=mean(xs)
aver=median(xs)
std1=std(xs)
good=good*less_equal(abs(self.x-aver),self.n_sigma*std1)
nnx=sum(good)
if nnx==nx: break
else: nx=nnx
else:
np=float(len(self.x))
nmin=int((0.5*self.reject_fraction)*np)
nmax=int((1.-0.5*self.reject_fraction)*np)
orden=argsort(self.x)
connect(arange(len(self.x)),sort(self.x))
good=greater(orden,nmin)*less(orden,nmax)
self.remaining=compress(good,self.x)
self.max=max(self.remaining)
self.min=min(self.remaining)
self.mean=mean(self.remaining)
self.rms=std(self.remaining)
self.rms0=rms(self.remaining) # --DC
self.median=median(self.remaining)
self.outliers=compress(logical_not(good),self.x)
self.n_remaining=len(self.remaining)
self.n_outliers=len(self.outliers)
self.fraction=1.-(float(self.n_remaining)/float(len(self.x)))
def std_robust(x,n_sigma=3.,n=5):
x=purge_outliers(x,n_sigma,n)
return std(x-mean(x))
def mean_robust(x,n_sigma=3.,n=5):
x=purge_outliers(x,n_sigma,n)
return mean(x)
def median_robust(x,n_sigma=3.,n=5):
x=purge_outliers(x,n_sigma,n)
return median(x)
def std_log(x,fa=sqrt(20.)):
dx=std(x)
#print "std(x)",dx,
#if abs(dx)<1e-100:dx=mean(abs(x))
a=fa*dx
#print sqrt(average(a*a*log(1.+x*x/(a*a)))),
#print std_robust(x,3,3),
#print len(x)
return sqrt(average(a*a*log(1.+x*x/(a*a))))
#def std_log(x,fa=20.):
# dx=median(abs(x))
# if abs(dx)<1e-100:dx=mean(abs(x))
# a=fa*dx
# return sqrt(average(a*a*log10(1.+x*x/(a*a))))
def med_thr(x,thr=0.2,max_it=10):
xm=median(x)
xm0=xm+thr
for i in range(max_it):
good=less_equal(x-xm,thr)*greater_equal(x-xm,-thr)
xm=median(compress(good,x))
if abs(xm-xm0)<thr/1000.: break
xm0=xm
# print xm
return xm
def std_thr(x,thr=0.2,max_it=10):
xm=med_thr(x,thr,max_it)
good=less_equal(x-xm,thr)*greater_equal(x-xm,-thr)
return std(compress(good,x))
def out_thr(x,thr=0.2,max_it=10):
xm=med_thr(x,thr,max_it)
good=less_equal(x-xm,thr)*greater_equal(x-xm,-thr)
return len(x)-sum(good)
#def bin_aver(x,y,xbins):
# """Given the variable y=f(x), and
# the bins limits xbins, return the
# average <y(xbins)>"""
# a=argsort(x)
# nbins=len(xbins)
# y=take(y,a)
# x=take(x,a)
# n=searchsorted(x,xbins)
# results=xbins*0.
# num=hist(x,xbins)
# for i in range(nbins):
# if i< nbins-1:
# results[i]=add.reduce(y[n[i]:n[i+1]])
# else:
# results[i]=add.reduce(y[n[i]:])
# if num[i]>0:
# results[i]=results[i]/num[i]
# return results
def multicompress(condition,variables):
lista=list(variables)
n=len(lista)
for i in range(n): lista[i]=compress(condition,lista[i])
return tuple(lista)
def multisort(first,followers):
#sorts the vector first and matches the ordering
# of followers to it
#Usage:
# new_followers=multi_sort(first,followers)
order=argsort(first)
if type(followers)<> type((1,)):
return take(followers,order)
else:
nvectors=len(followers)
lista=[]
for i in range(nvectors):
lista.append(take(followers[i],order))
return tuple(lista)
def erfc(x):
"""
Returns the complementary error function erfc(x)
erfc(x)=1-erf(x)=2/sqrt(pi)*\int_x^\inf e^-t^2 dt
"""
try: x.shape
except: x=array([x])
z=abs(x)
t=1./(1.+0.5*z)
erfcc=t*exp(-z*z-
1.26551223+t*(
1.00002368+t*(
0.37409196+t*(
0.09678418+t*(
-0.18628806+t*(
0.27886807+t*(
-1.13520398+t*(
1.48851587+t*(
-0.82215223+t*0.17087277)
))))))))
erfcc=where(less(x,0.),2.-erfcc,erfcc)
return erfcc
def erf(x):
"""
Returns the error function erf(x)
erf(x)=2/sqrt(pi)\int_0^x \int e^-t^2 dt
"""
return 1.-erfc(x)
def erf_brute(x):
step=0.00001
t=arange(0.,x+step,step)
f=2./sqrt(pi)*exp(-t*t)
return sum(f)*step
def erfc_brute(x):
return 1.-erf_brute(x)
def gauss_int_brute(x=arange(0.,3.,.01),average=0.,sigma=1.):
step=x[1]-x[0]
gn=1./sqrt(2.*pi)/sigma*exp(-(x-average)**2/2./sigma**2)
return add.accumulate(gn)*step
def gauss_int_erf(x=(0.,1.),average=0.,sigma=1.):
"""
Returns integral (x) of p=int_{-x1}^{+x} 1/sqrt(2 pi)/sigma exp(-(t-a)/2sigma^2) dt
"""
x=(x-average)/sqrt(2.)/sigma
return (erf(x)-erf(x[0]))*.5
gauss_int=gauss_int_erf
def inv_gauss_int(p):
#Brute force approach. Limited accuracy for >3sigma
#find something better
#DO NOT USE IN LOOPS (very slow)
"""
Calculates the x sigma value corresponding to p
p=int_{-x}^{+x} g(x) dx
"""
if p<0. or p>1.:
print 'Wrong value for p(',p,')!'
sys.exit()
step=.00001
xn=arange(0.,4.+step,step)
gn=1./sqrt(2.*pi)*exp(-xn**2/2.)
cgn=add.accumulate(gn)*step
p=p/2.
ind=searchsorted(cgn,p)
return xn[ind]
def points(x,y,limits=(None,None,None,None),title='Plot'):
#Quickly plot two vectors using biggles
p=FramedPlot()
if limits[0]<>None and limits[1]<>None:
p.xrange=limits[0],limits[1]
if limits[2]<>None and limits[3]<>None:
p.yrange=limits[2],limits[3]
p.add(Points(x,y))
p.add(Slope(0.))
p.title=title
p.show()
def pointswriteimg(x,y,limits=(None,None,None,None),title='Plot', xsize=600, ysize=600, name=''):
#Quickly plot two vectors using biggles
p=FramedPlot()
if limits[0]<>None and limits[1]<>None:
p.xrange=limits[0],limits[1]
if limits[2]<>None and limits[3]<>None:
p.yrange=limits[2],limits[3]
p.add(Points(x,y))
p.add(Slope(0.))
p.title=title
p.show()
if not name:
name = title + '.png'
p.write_img(xsize, ysize, name)
def connect(x,y,limits=(None,None,None,None)):
#Quickly plot two vectors using biggles
p=FramedPlot()
if limits[0]<>None and limits[1]<>None:
p.xrange=limits[0],limits[1]
if limits[2]<>None and limits[3]<>None:
p.yrange=limits[2],limits[3]
p.add(Curve(x,y))
p.show()
def mark_outliers(x,n_sigma=3.,n=5): # --DC
# from purge_outliers
#Experimental yet. Only 1 dimension
nx = len(x)
ii = range(nx)
for i in range(n):
med=median(x)
rms=std(x)
ii,x=compress(less_equal(abs(x-med),n_sigma*rms), (ii,x))
outliers = ones(nx)
put(outliers, ii.astype(int), 0)
return outliers
def mark_faroutliers(x,n_sigma=3.,n=5,n_farout=2): # --DC
# from purge_outliers
# REPEATS n TIMES
nx = len(x)
xg = x[:]
for i in range(n):
dx = abs(x - median(x))
outliers = greater(dx, n_sigma * std(xg))