-
Notifications
You must be signed in to change notification settings - Fork 9
/
McaSpectrumBrick.py
137 lines (111 loc) · 4.16 KB
/
McaSpectrumBrick.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
#$Log: McaSpectrumBrick.py,v $
#Revision 1.3 2007/06/20 09:42:59 beteva
#changed Numeric to numpy & forceUpdate() to refreshWidgets()
#
#Revision 1.2 2007/06/06 09:13:32 beteva
#added more config parameters. Forced the display on energy.
#
#Revision 1.1 2007/06/04 14:58:27 beteva
#Initial revision
#
"""
[Name] McaSpectrumBrick
[Description]
The McaSpectrumBrick allows to display Mca Spectrum obtained in SPEC.
If configured, it will take into account the energy calibration factors and
the fit configuration file well as
[Properties]
[Signals]
[Slots]
-------------------------------------------------------------
| name | arguments | description
-------------------------------------------------------------
| setData | data | numeric array (x, y)
| calib | dictionary with the calibration factors (a,b,c)
| config | dictionary with the fit parameters
[HardwareObjects]
"""
__category__ = 'MCA'
import logging
from qt import *
from BlissFramework.BaseComponents import BlissWidget
from PyMca import McaAdvancedFit
import numpy
try:
from PyMca5.PyMca import ConfigDict
except ImportError:
from PyMca import ConfigDict
class McaSpectrumBrick(BlissWidget):
def __init__(self, *args):
BlissWidget.__init__(self, *args)
self.defineSlot('setData',())
self.mcafit = McaAdvancedFit.McaAdvancedFit(self)
self.mcafit.dismissButton.hide()
QVBoxLayout(self)
self.layout().addWidget(self.mcafit)
def setData(self, data, calib, config):
try:
configured = False
if config["file"] is not None:
self._configure(config)
configured = True
if data[0].size == 2:
x = numpy.array(data[:,0]) * 1.0
y = numpy.array(data[:,1])
else:
x = data[0] *1.0
y = data[1]
xmin = float(config["min"])
xmax = float(config["max"])
calib = numpy.ravel(calib).tolist()
kw = {}
kw.update(config)
kw['xmin'] = xmin
kw['xmax'] = xmax
kw['calibration'] = calib
self.mcafit.setdata(x, y, **kw)# xmin=xmin, xmax=xmax, calibration=calib)
self.mcafit._energyAxis = False
self.mcafit.toggleEnergyAxis()
result = self._fit()
self.mcafit.refreshWidgets()
#pyarch file name and directory
pf = config["legend"].split(".")
pd = pf[0].split("/")
outfile = pd[-1]
try:
outdir = config['htmldir']
except:
outdir,_ = config['legend'].split("//")
sourcename = config['legend']
if configured and result:
report = McaAdvancedFit.QtMcaAdvancedFitReport.\
QtMcaAdvancedFitReport(None, outfile=outfile,
outdir=outdir,fitresult=result,
sourcename=sourcename,
plotdict = {'logy' : False})
text = report.getText()
report.writeReport(text=text)
except:
logging.getLogger().exception('McaSpectrumBrick: problem fitting %s %s %s' % (str(data),str(calib),str(config)))
raise
def _fit(self):
if self.mcafit.isHidden():
self.mcafit.show()
return self.mcafit.fit()
def _configure(self,config):
d = ConfigDict.ConfigDict()
d.read(config["file"])
if not d.has_key('concentrations'):
d['concentrations']= {}
if not d.has_key('attenuators'):
d['attenuators']= {}
d['attenuators']['Matrix'] = [1, 'Water', 1.0, 0.01, 45.0, 45.0]
if config.has_key('flux'):
d['concentrations']['flux'] = float(config['flux'])
if config.has_key('time'):
d['concentrations']['time'] = float(config['time'])
self.mcafit.mcafit.configure(d)
def clear(self):
x = numpy.array([0])
y = numpy.array([0])
self.mcafit.setdata(x, y)