-
Notifications
You must be signed in to change notification settings - Fork 2
/
xye.py
executable file
·672 lines (571 loc) · 22.7 KB
/
xye.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
from pylab import *
#import scipy.interpolate as spintp
import scipy.interpolate as intp
import scipy.optimize
import pdb
import copy, itertools
#### need to impliment
# pass both counts and monitor into initialization
# parameter should return either counts or counts/mon
# add some assert commands to improve robustness of program
"""
s is single xye class instance
S is list of xye objects
"""
class xye(object):
"""General data processing class
Parameters:
-----------
xye.x : ndarray
x data
xye.y : ndarray
y data in raw counts
xye.err : ndarray
y error in raw counts
xye.mon : ndarray or single value in numpy array
montor values either for individal points or for
the overall spectrum. Can be either a real counter
or per second. Default = 1
xye.label : string
label associate with xye.
This is used in the plot legend
xye.plotmon : True | False (default)
If true data is plotted as counts over monitor
"""
def __init__(self, x, y, err, mon=array([1]), label='', plotmon=False):
self.x = self.__checkForArray(x)
self.y = self.__checkForArray(y)
self.err = self.__checkForArray(err)
mon = self.__checkForArray(mon)
if len(mon) == 1:
mon = tile(mon, shape(self.x))
self.mon = mon
self.label = label
self.plotmon = plotmon
self.__orderdata()
def __orderdata(self):
"""Put data in order of increasing x"""
xorder = argsort(self.x)
self.x = self.x[xorder]
self.y = self.y[xorder]
self.err = self.err[xorder]
self.mon = self.mon[xorder]
def __checkForArray(self, a):
"""Check that input is a numpy array and return array.
if a list in input convert to numpy array and then return it"""
if type(a) == list:
a = array(a)
elif type(a) == int or type(a) == float:
a = array([a+ 0.0])
if self.__isnumeric(a):
return a
else:
print ("Is x, y, or err is not numeric")
def __isnumeric(self, obj):
"""Check that input array is numeric."""
try:
obj+obj, obj-obj, obj*obj
except ZeroDivisionError:
return True
except Exception:
return False
else:
return True
def __add__(self, obj):
""" Return new class with obj added to input
obj can be either class or constant
this method returns new object with modified label
This can be used for quick plotting without modifying the class
using the operator +
"""
Stemp = xye(x=self.x, y=self.y, err=self.err, mon=self.mon,
plotmon=self.plotmon)
Stemp.add(obj)
if type(obj) == xye:
newlabel = self.label + " + " + obj.label
else:
newlabel = self.label + " + " + str(obj)
Stemp.label = newlabel
return Stemp
def __sub__(self, obj):
"""Return new class with obj subtracted from input
obj can be either class or constant
this method returns new object with modified label
This can be used for quick plotting without modifying the class
using the operator -
"""
Stemp = xye(x=self.x, y=self.y, err=self.err, mon=self.mon)
Stemp.sub(obj)
if type(obj) == xye:
newlabel = self.label + " - " + obj.label
else:
newlabel = self.label + " - " + str(obj)
Stemp.label = newlabel
return Stemp
def __mul__(self, obj):
"""Return new class with obj multiplied by input
obj can be either class or constant
this method returns new object with modified label
This can be used for quick plotting without modifying the class
using the operator +
"""
Stemp = xye(x=self.x, y=self.y, err=self.err, mon=self.mon,
plotmon=self.plotmon)
Stemp.mul(obj)
if type(obj) == xye:
newlabel = self.label + " * " + obj.label
else:
newlabel = self.label + " * " + str(obj)
Stemp.label = newlabel
return Stemp
def __div__(self, obj):
""" Return new class with obj divided by input
obj can be either class or constant
this method returns a new object with modified label
This can be used for quick plotting without modifying the class
using the operator /
"""
Stemp = xye(x=self.x, y=self.y, err=self.err, mon=self.mon,
plotmon=self.plotmon)
Stemp.div(obj)
if type(obj) == xye:
newlabel = self.label + " / " + obj.label
else:
newlabel = self.label + " / " + str(obj)
Stemp.label = newlabel
return Stemp
def __truediv__(self, obj):
""" Return new class with obj divided by input
obj can be either class or constant
this method returns a new object with modified label
This can be used for quick plotting without modifying the class
using the operator /
"""
Stemp = xye(x=self.x, y=self.y, err=self.err, mon=self.mon,
plotmon=self.plotmon)
Stemp.div(obj)
if type(obj) == xye:
newlabel = self.label + " / " + obj.label
else:
newlabel = self.label + " / " + str(obj)
Stemp.label = newlabel
return Stemp
def __mod__(self, shift):
""" Return new class with x shifted by input
shiftx must be a float or int
this method returns a new object with modified label
This can be used for quick plotting without modifying the class
using the operator %
"""
Stemp = xye(x=self.x, y=self.y, err=self.err, mon=self.mon,
plotmon=self.plotmon)
Stemp.shift_x(shift)
newlabel = self.label + " % " + str(shift)
Stemp.label = newlabel
return Stemp
def __and__(self, obj):
""" Combine self and obj and return a new object with modified label
This can be used for quick plotting without modifying the class using
the operator +=
"""
Stemp = xye(x=self.x, y=self.y, err=self.err, mon=self.mon,
plotmon=self.plotmon)
Stemp.combine(obj)
newlabel = self.label + " += " + obj.label
Stemp.label = newlabel
return Stemp
def gety(self, xvals, fill_value=NaN):
"""Use interpolation to return axis_to_get = y, err, mon at xvals"""
func = intp.interp1d(self.x, self.y, kind='linear',
bounds_error=False, fill_value=fill_value)
return func(xvals)
def geterr(self, xvals, fill_value=NaN):
"""Use interpolation to return axis_to_get = y, err, mon at xvals"""
func = intp.interp1d(self.x, self.err, kind='linear',
bounds_error=False, fill_value=fill_value)
return func(xvals)
def getmon(self, xvals, fill_value=NaN):
"""Use interpolation to return axis_to_get = y, err, mon at xvals"""
func = intp.interp1d(self.x, self.mon, kind='linear',
bounds_error=False, fill_value=fill_value)
return func(xvals)
def throwawayNaN(self):
"""Remove all Nan values from self"""
inds = ~np.logical_or( np.logical_or(isnan(self.x), isnan(self.y)),
np.logical_or(isnan(self.err), isnan(self.mon)))
self.x = self.x[inds]
self.y = self.y[inds]
self.err = self.err[inds]
self.mon = self.mon[inds]
def combine(self, obj):
"""Combine data in obj into self"""
self.x = hstack((self.x, obj.x))
self.y = hstack((self.y, obj.y))
self.err = hstack((self.err, obj.err))
self.mon = hstack((self.mon, obj.mon))
self.__orderdata()
def add(self, obj):
""" Add obj onto xye class
obj can be either class or constant
Throws away data where spectrum
to add does not overlap
"""
if type(obj) == xye:
self.y = self.y + obj.gety(self.x)
self.err = sqrt( self.err**2 + (obj.geterr(self.x))**2 )
self.mon = self.mon + obj.getmon(self.x)
elif self.__isnumeric(obj):
self.y = self.y + obj
else:
raise Exception("argument to add neither xye nor number!")
self.throwawayNaN()
def sub(self, obj):
""" Subtract obj from xye class
obj can be either class or constant
Does not alter the monitor values. Throws away data where spectrum
to subtract does not overlap
"""
if type(obj) == xye:
self.y = self.y - obj.gety(self.x)
self.err = sqrt( self.err**2 + obj.gety(self.x)**2 )
self.mon = self.mon + obj.getmon(self.x)
elif self.__isnumeric(obj):
self.y = self.y - obj
else:
raise Exception("argument to sub neither xye nor number!")
def mul(self, obj):
""" Multipy xye class by obj
obj can be either class or constant
Does not alter the monitor values. Throws away data where spectrum
to multiply does not overlap
"""
if type(obj) == xye:
self.y = self.y * obj.gety(self.x)
rel_err = sqrt( (self.err/self.y)**2 +
(obj.geterr(self.x)/obj.gety(self.x))**2 )
self.err = self.y * rel_err
elif self.__isnumeric(obj):
self.y = self.y * obj
self.err = self.err * obj
else:
raise Exception("argument to mul neither xye nor number!")
self.throwawayNaN()
def div(self, obj):
""" Divide xye class by obj
obj can be either class or constant
Does not alter the monitor values. Throws away data where spectrum
to divide does not overlap
"""
if type(obj) == xye:
self.y = self.y / obj.gety(self.x, fill_value=NaN)
rel_err = sqrt( (self.err/self.y)**2 +
(obj.geterr(self.x)/obj.gety(self.x))**2 )
self.err = self.y * rel_err
if any( obj.gety(self.x) == 0):
raise Exception("Attempt to divde by zero in self.div")
elif self.__isnumeric(obj):
if obj == 0:
raise Exception("Attempt to divde by zero in self.div")
self.y = self.y / obj
self.err = self.err / obj
else:
raise Exception("argument to div neither xye nor number!")
self.throwawayNaN()
def get_intBG(self, xmin=-inf, xmax=+inf):
""" Integrate up linear background based on trapezium rule."""
if xmax <= xmin:
raise Exception("wimax <= xmin")
y_xmin = self.get_yval(xmin)
y_xmax = self.get_yval(xmax)
if y_xmin<0 or y_xmax<0:
raise Exception("Negative y values")
I_BG = (xmax - xmin) * (y_xmin + y_xmax) / 2
return I_BG
def get_intval(self, xmin=-inf, xmax=+inf):
""" Return numerical integral of function between xmin and xmax.
The integral is calculated using Trapezoidal rule method
"""
self.__orderdata()
Stemp = copy.copy(self)
Stemp.cut(xmin,xmax)
xcut = Stemp.x
ycut = Stemp.y
endind = len(xcut)
dx = xcut[1:endind] - xcut[0:(endind-1)]
dy = (ycut[0:(endind-1)] + ycut[1:endind])/2
I = sum(dx*dy)
return I
def get_peakval(self, xmin=-inf, xmax=+inf):
""" Get x, y, err values at the maximum Y value"""
self.__orderdata()
Stemp = copy.copy(self)
Stemp.cut(xmin,xmax)
xcut = Stemp.x
ycut = Stemp.y
ecut = Stemp.e
maxind = argmax(ycut)
peakX = xcut[maxind]
peakY = ycut[maxind]
peakE = ecut[maxind]
return peakX, peakY, peakE
def get_overlap(self, S2, xmin=-inf, xmax=+inf):
if xmax <= xmin:
raise Exception("wimax <= xmin")
self.__orderdata()
S2.__orderdata()
xin1 = self.x
yin1 = self.y
xin2 = S2.x
yin2 = S2.y
overlapmin = max(xmin, min(xin1), min(xin2) )
overlapmax = min(xmax, max(xin1), max(xin2) )
if overlapmax <= overlapmin:
raise Exception("No overlap to perform ")
# determine number step to oversample
dx1 = mean(xin1[1:len(xin1)] - xin1[0:(len(xin1) - 1)])
dx2 = mean(xin2[1:len(xin2)] - xin2[0:(len(xin2) - 1)])
NoStep = 5* (overlapmax - overlapmin) / min(dx1, dx2)
if NoStep <= 10:
raise Exception("Insufficient number of steps for convolution")
x = scipy.linspace(overlapmin, overlapmax, NoStep)
dx = x[1]-x[0]
fdatin1 = intp.interp1d(xin1, yin1, kind='linear',
bounds_error = True)
fdatin2 = intp.interp1d(xin2, yin2, kind='linear',
bounds_error = True)
y1 = fdatin1(x)
y2 = fdatin2(x)
if argmax(y1) == 0 or argmax(y2)== 0 or argmax(y1) == (len(y1)-1) or argmax(y2) == (len(y2)-1):
raise Exception("Overlap region doesn't seem to contain a peak")
# compute the cross-correlation between y1 and y2
ycorr = scipy.correlate(y1, y2, mode='full')
xcorr = scipy.linspace(0, len(ycorr)-1, num=len(ycorr))
# define a gaussian fitting function where
# p[0] = amplitude
# p[1] = mean
# p[2] = sigma
fitfunc = lambda p, x: p[0]*scipy.exp(-(x-p[1])**2/(2.0*p[2]**2))
errfunc = lambda p, x, y: fitfunc(p,x)-y
# guess some fit parameters
p0 = scipy.c_[max(ycorr), scipy.where(ycorr==max(ycorr))[0], 5]
# fit a gaussian to the correlation function
p1, success = scipy.optimize.leastsq(errfunc, p0.copy()[0],
args=(xcorr,ycorr))
# convert index to lag steps
# the first point has index=0 but the largest (negative) lag
# there is a simple mapping between index and lag
nLags = p1[1]-(len(y1)-1)
return -nLags*dx
#x, indexshift, dx, x, xcorr, ycorr, yselfcorr
def get_binedges(self, binwidth):
"""Get x array covering spectrum separated by binwidth"""
steps = floor((max(self.x) - min(self.x)) / binwdith)
return linspace(min(self.x), max(self.x), steps)
def rebin(self, binedges):
"""Rebin the spectrum in bins set by edges"""
xout = array([])
yout = array([])
errout = array([])
monout = array([])
for i in range(1, len(binedges)):
inds = (self.x > binedges[i-1]) & (self.x <= binedges[i])
ycurr = self.y[inds]
errcurr = self.err[inds]
moncurr = self.mon[inds]
if len(ycurr>0):
xout = hstack((xout, (binedges[i] + binedges[i-1])/2 ))
yout = hstack((yout, np.sum(ycurr)))
errout = hstack((errout, sqrt(np.sum(errcurr**2))))
monout = hstack((monout, np.sum(moncurr)))
self.x = xout
self.y = yout
self.err = errout
self.mon = monout
def get_peakfit(self, xmin=-inf, xmax=+inf):
if xmax <= xmin:
raise Exception("wimax <= xmin")
#pdb.set_trace()
Stemp = copy.copy(self)
Stemp.cut(xmin=-inf, xmax=+inf)
# gaussian function
fitfunc = lambda p, x: p[0]* exp(-0.5 * (x - p[1])**2 / p[2]**2) + p[3]
# error function
errfunc = lambda p, x, y: fitfunc(p, x) - y
# initial guess
ind = argmax(Stemp.y)
pic = Stemp.y[ind] - min(Stemp.y)
cen = Stemp.x[ind]
offset = min(Stemp.y)
wid = abs(xmax - xmin)/4
p0 = [pic, cen, wid, offset] # Initial guess for the parameters
p1, success = scipy.optimize.leastsq(errfunc, p0[:],
args=(Stemp.x, Stemp.y))
if ((success == 1) or (success == 2) or (success == 3) or (success == 4)):
print 'Fit was sucessful'
else:
raise Exception("Fit did not work")
return p1[1], p1[0]
def shift_x(self, shift):
""" Add the input value to all of self.x"""
self.x = self.x + shift
def shift_y(self, shift):
""" Add the input value to all of self.y"""
self.y = self.y + shift
def normint(self, xmin=-inf, xmax=+inf):
""" Normalize the spectrum by the integral between xmin and xmax"""
if xmax <= xmin:
raise Exception("wimax <= xmin")
I = self.get_intval(xmin=xmin, xmax=xmax)
self.y = self.y / I
self.err = self.err / I
def normint_subBG(self, xmin=-inf, xmax=+inf):
""" Normalize the spectrum by the integral between xmin and xmax.
A linear background is removed """
if xmax <= xmin:
raise Exception("wimax <= xmin")
I = self.get_intval(xmin=xmin, xmax=xmax)
I_BG = self.get_intBG(xmin=xmin, xmax=xmin)
if I_BG>= I:
raise Exception("Background bigger than peak!")
self.y = self.y / (I - I_BG)
self.err = self.err / (I - I_BG)
def cut(self, xmin=-inf, xmax=+inf):
"""Throw away values set by xmin and xmax.
If xmax < xmin central region is thrown away
If xmax > xmin edges are thrown away"""
if xmax < xmin:
indices = (self.x > xmin) | (self.x < xmax)
elif xmax > xmin:
indices = (self.x > xmin) & (self.x < xmax)
else:
indices = isreal(self.x)
self.x = self.x[indices]
self.y = self.y[indices]
self.err = self.err[indices]
self.mon = self.mon[indices]
def errorbar(self, *args, **kwargs):
""" Method for errorbar plot of spectrum"""
if self.plotmon:
errorbar(x=self.x, y=self.y/self.mon, yerr=self.err/self.mon, label=self.label,
*args, **kwargs)
else:
errorbar(x=self.x, y=self.y, yerr=self.err, label=self.label,
*args, **kwargs)
if self.label!=None:
legend()
def plot(self, *args, **kwargs):
""" Method to plot spectrum """
if self.plotmon:
plot(self.x, self.y/self.mon, label=self.label, *args, **kwargs)
else:
plot(self.x, self.y, label=self.label, *args, **kwargs)
if self.label!=None:
legend()
##Some functions
def unpack(*obj):
"""convert spectra into list with no nesting"""
if type(obj) == xye:
S = [obj]
else:
def flatten(x):
result = []
for el in x:
if hasattr(el, "__iter__") and not isinstance(el, basestring):
result.extend(flatten(el))
else:
result.append(el)
return result
S = flatten(obj)
return S
def isxye(obj):
""" Print warning if input arguments aren't xye class instances. """
S = unpack(obj)
for s in S:
if type(s) != xye:
print "Some arguments are not xye class instances"
def addxye(*obj):
""" sum spectra
obj can be any list containing xye objects
The resulting spectra will share the same axis as the first spectrum
subsequent spectra are interpolated onto the first x-axis
"""
S = unpack(obj)
isxye(S)
if len(S) < 2:
print "Only one spectrum was sent to addxye. The same spectrum will be returned"
#Sout = xye(x=S[0].x, y=S[0].y, err=S[0].err, label=S[0].label)
Sout = copy.copy(S[0])
for s in S[1:]:
Sout.add(s)
Sout.label += ' + ' + s.label
return Sout
def plotxye(obj, *args, **kwargs):
""" plot spectra
Arguments are:
*obj spectra passed either as individual spectra or lists of spectra
in any configuration
additional arguments or keyword arguments are passed onto plot fucntion
hold = True | False (Default True)
If hold = False window is initially cleared. Subsequent plots are always
kept regardless
"""
if kwargs.has_key('hold'):
if kwargs['hold'] == False:
clf()
del(kwargs['hold'])
S = unpack(obj)
isxye(S)
# plot everything
color=iter(cm.rainbow(np.linspace(0,1,len(S))))
for s in S:
c = next(color)
if len(S) >7:
s.plot(*args, c=c, **kwargs)
else:
s.plot(*args, **kwargs)
def errorbarxye(obj, *args, **kwargs):
""" errorbar spectra
Arguments are:
*obj spectra passed either as individual spectrum or lists of spectra
in any configuration
additional arguments or keyword arguments are passed onto plot fucntion
hold = True | False (Default True)
If hold = False window is initially cleared. Subsequent plots are always
kept regardless
"""
#pdb.set_trace()
if kwargs.has_key('hold'):
if kwargs['hold'] == False:
clf()
del(kwargs['hold'])
S = unpack(obj)
isxye(S)
# plot everything
color=iter(cm.rainbow(np.linspace(0,1,len(S))))
for s in S:
if type(s) == xye:
c=next(color)
if len(S) >7:
s.errorbar(*args, c=c, **kwargs)
else:
s.errorbar(*args, **kwargs)
def lineup(obj):
""" Line up spectra
input is list of xye class instances in any order
output is list of xye class instances referenced to 1st class instance
"""
S = unpack(obj)
for i in range(1,len(S)):
shiftx = S[i-1].get_overlap(S[i], 0.8, 3.5)
print "i = " + str(i) + " shiftx = " + str(shiftx)
S[i].shift_x(-shiftx)
return S
def normintxye(obj, xmin=-inf, xmax=+inf):
""" Line up spectra
input is list of xye class instances in any order
output is list of xye class instances referenced to 1st class instance
"""
S = unpack(obj)
for i in range(len(S)):
S[i].normint(xmin=xmin, xmax=xmax)
return S