1 | import numpy as np |
---|
2 | |
---|
3 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
4 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
5 | |
---|
6 | from sas.sascalc.dataloader.data_info import Detector |
---|
7 | from sas.sascalc.dataloader.data_info import Source |
---|
8 | |
---|
9 | |
---|
10 | class FittingLogic(object): |
---|
11 | """ |
---|
12 | All the data-related logic. This class deals exclusively with Data1D/2D |
---|
13 | No QStandardModelIndex here. |
---|
14 | """ |
---|
15 | def __init__(self, data=None): |
---|
16 | self._data = data |
---|
17 | self.data_is_loaded = False |
---|
18 | if data is not None: |
---|
19 | self.data_is_loaded = True |
---|
20 | |
---|
21 | @property |
---|
22 | def data(self): |
---|
23 | return self._data |
---|
24 | |
---|
25 | @data.setter |
---|
26 | def data(self, value): |
---|
27 | """ data setter """ |
---|
28 | self._data = value |
---|
29 | self.data_is_loaded = True |
---|
30 | |
---|
31 | def isLoadedData(self): |
---|
32 | """ accessor """ |
---|
33 | return self.data_is_loaded |
---|
34 | |
---|
35 | def createDefault1dData(self, interval, tab_id=0): |
---|
36 | """ |
---|
37 | Create default data for fitting perspective |
---|
38 | Only when the page is on theory mode. |
---|
39 | """ |
---|
40 | self._data = Data1D(x=interval) |
---|
41 | self._data.xaxis('\\rm{Q}', "A^{-1}") |
---|
42 | self._data.yaxis('\\rm{Intensity}', "cm^{-1}") |
---|
43 | self._data.is_data = False |
---|
44 | self._data.id = str(tab_id) + " data" |
---|
45 | self._data.group_id = str(tab_id) + " Model1D" |
---|
46 | |
---|
47 | def createDefault2dData(self, qmax, qstep, tab_id=0): |
---|
48 | """ |
---|
49 | Create 2D data by default |
---|
50 | Only when the page is on theory mode. |
---|
51 | """ |
---|
52 | self._data = Data2D() |
---|
53 | self._data.xaxis('\\rm{Q_{x}}', 'A^{-1}') |
---|
54 | self._data.yaxis('\\rm{Q_{y}}', 'A^{-1}') |
---|
55 | self._data.is_data = False |
---|
56 | self._data.id = str(tab_id) + " data" |
---|
57 | self._data.group_id = str(tab_id) + " Model2D" |
---|
58 | |
---|
59 | # Default detector |
---|
60 | self._data.detector.append(Detector()) |
---|
61 | index = len(self._data.detector) - 1 |
---|
62 | self._data.detector[index].distance = 8000 # mm |
---|
63 | self._data.source.wavelength = 6 # A |
---|
64 | self._data.detector[index].pixel_size.x = 5 # mm |
---|
65 | self._data.detector[index].pixel_size.y = 5 # mm |
---|
66 | self._data.detector[index].beam_center.x = qmax |
---|
67 | self._data.detector[index].beam_center.y = qmax |
---|
68 | # theory default: assume the beam |
---|
69 | #center is located at the center of sqr detector |
---|
70 | xmax = qmax |
---|
71 | xmin = -qmax |
---|
72 | ymax = qmax |
---|
73 | ymin = -qmax |
---|
74 | |
---|
75 | x = np.linspace(start=xmin, stop=xmax, num=qstep, endpoint=True) |
---|
76 | y = np.linspace(start=ymin, stop=ymax, num=qstep, endpoint=True) |
---|
77 | # Use data info instead |
---|
78 | new_x = np.tile(x, (len(y), 1)) |
---|
79 | new_y = np.tile(y, (len(x), 1)) |
---|
80 | new_y = new_y.swapaxes(0, 1) |
---|
81 | |
---|
82 | # all data required in 1d array |
---|
83 | qx_data = new_x.flatten() |
---|
84 | qy_data = new_y.flatten() |
---|
85 | q_data = np.sqrt(qx_data * qx_data + qy_data * qy_data) |
---|
86 | |
---|
87 | # set all True (standing for unmasked) as default |
---|
88 | mask = np.ones(len(qx_data), dtype=bool) |
---|
89 | # calculate the range of qx and qy: this way, |
---|
90 | # it is a little more independent |
---|
91 | # store x and y bin centers in q space |
---|
92 | x_bins = x |
---|
93 | y_bins = y |
---|
94 | |
---|
95 | self._data.source = Source() |
---|
96 | self._data.data = np.ones(len(mask)) |
---|
97 | self._data.err_data = np.ones(len(mask)) |
---|
98 | self._data.qx_data = qx_data |
---|
99 | self._data.qy_data = qy_data |
---|
100 | self._data.q_data = q_data |
---|
101 | self._data.mask = mask |
---|
102 | self._data.x_bins = x_bins |
---|
103 | self._data.y_bins = y_bins |
---|
104 | # max and min taking account of the bin sizes |
---|
105 | self._data.xmin = xmin |
---|
106 | self._data.xmax = xmax |
---|
107 | self._data.ymin = ymin |
---|
108 | self._data.ymax = ymax |
---|
109 | |
---|
110 | def new1DPlot(self, return_data, tab_id): |
---|
111 | """ |
---|
112 | Create a new 1D data instance based on fitting results |
---|
113 | """ |
---|
114 | # Unpack return data from Calc1D |
---|
115 | x, y, page_id, state, weight,\ |
---|
116 | fid, toggle_mode_on, \ |
---|
117 | elapsed, index, model,\ |
---|
118 | data, update_chisqr, source = return_data |
---|
119 | |
---|
120 | # Create the new plot |
---|
121 | new_plot = Data1D(x=x, y=y) |
---|
122 | new_plot.is_data = False |
---|
123 | new_plot.dy = np.zeros(len(y)) |
---|
124 | _yaxis, _yunit = data.get_yaxis() |
---|
125 | _xaxis, _xunit = data.get_xaxis() |
---|
126 | |
---|
127 | new_plot.group_id = data.group_id |
---|
128 | new_plot.id = str(tab_id) + " " + data.name |
---|
129 | new_plot.name = model.name + " [" + data.name + "]" |
---|
130 | new_plot.title = new_plot.name |
---|
131 | new_plot.xaxis(_xaxis, _xunit) |
---|
132 | new_plot.yaxis(_yaxis, _yunit) |
---|
133 | |
---|
134 | return new_plot |
---|
135 | |
---|
136 | def new2DPlot(self, return_data): |
---|
137 | """ |
---|
138 | Create a new 2D data instance based on fitting results |
---|
139 | """ |
---|
140 | image, data, page_id, model, state, toggle_mode_on,\ |
---|
141 | elapsed, index, fid, qmin, qmax, weight, \ |
---|
142 | update_chisqr, source = return_data |
---|
143 | |
---|
144 | np.nan_to_num(image) |
---|
145 | new_plot = Data2D(image=image, err_image=data.err_data) |
---|
146 | new_plot.name = model.name + '2d' |
---|
147 | new_plot.title = "Analytical model 2D " |
---|
148 | new_plot.id = str(page_id) + " " + data.name |
---|
149 | new_plot.group_id = str(page_id) + " Model2D" |
---|
150 | new_plot.detector = data.detector |
---|
151 | new_plot.source = data.source |
---|
152 | new_plot.is_data = False |
---|
153 | new_plot.qx_data = data.qx_data |
---|
154 | new_plot.qy_data = data.qy_data |
---|
155 | new_plot.q_data = data.q_data |
---|
156 | new_plot.mask = data.mask |
---|
157 | ## plot boundaries |
---|
158 | new_plot.ymin = data.ymin |
---|
159 | new_plot.ymax = data.ymax |
---|
160 | new_plot.xmin = data.xmin |
---|
161 | new_plot.xmax = data.xmax |
---|
162 | |
---|
163 | title = data.title |
---|
164 | |
---|
165 | new_plot.is_data = False |
---|
166 | if data.is_data: |
---|
167 | data_name = str(data.name) |
---|
168 | else: |
---|
169 | data_name = str(model.__class__.__name__) + '2d' |
---|
170 | |
---|
171 | if len(title) > 1: |
---|
172 | new_plot.title = "Model2D for %s " % model.name + data_name |
---|
173 | new_plot.name = model.name + " [" + \ |
---|
174 | data_name + "]" |
---|
175 | |
---|
176 | return new_plot |
---|
177 | |
---|
178 | def computeDataRange(self): |
---|
179 | """ |
---|
180 | Compute the minimum and the maximum range of the data |
---|
181 | return the npts contains in data |
---|
182 | """ |
---|
183 | qmin, qmax, npts = None, None, None |
---|
184 | if isinstance(self.data, Data1D): |
---|
185 | try: |
---|
186 | qmin = min(self.data.x) |
---|
187 | qmax = max(self.data.x) |
---|
188 | npts = len(self.data.x) |
---|
189 | except (ValueError, TypeError): |
---|
190 | msg = "Unable to find min/max/length of \n data named %s" % \ |
---|
191 | self.data.filename |
---|
192 | raise ValueError, msg |
---|
193 | |
---|
194 | else: |
---|
195 | qmin = 0 |
---|
196 | try: |
---|
197 | x = max(np.fabs(self.data.xmin), np.fabs(self.data.xmax)) |
---|
198 | y = max(np.fabs(self.data.ymin), np.fabs(self.data.ymax)) |
---|
199 | except (ValueError, TypeError): |
---|
200 | msg = "Unable to find min/max of \n data named %s" % \ |
---|
201 | self.data.filename |
---|
202 | raise ValueError, msg |
---|
203 | qmax = np.sqrt(x * x + y * y) |
---|
204 | npts = len(self.data.data) |
---|
205 | return qmin, qmax, npts |
---|