1 | """ |
---|
2 | Adapters for fitting module |
---|
3 | """ |
---|
4 | import copy |
---|
5 | import numpy |
---|
6 | import math |
---|
7 | from data_util.uncertainty import Uncertainty |
---|
8 | from danse.common.plottools.plottables import Data1D as PlotData1D |
---|
9 | from danse.common.plottools.plottables import Data2D as PlotData2D |
---|
10 | from danse.common.plottools.plottables import Theory1D as PlotTheory1D |
---|
11 | |
---|
12 | from DataLoader.data_info import Data1D as LoadData1D |
---|
13 | from DataLoader.data_info import Data2D as LoadData2D |
---|
14 | |
---|
15 | |
---|
16 | class Data1D(PlotData1D, LoadData1D): |
---|
17 | """ |
---|
18 | """ |
---|
19 | def __init__(self, x=None, y=None, dx=None, dy=None): |
---|
20 | """ |
---|
21 | """ |
---|
22 | if x is None: |
---|
23 | x = [] |
---|
24 | if y is None: |
---|
25 | y = [] |
---|
26 | PlotData1D.__init__(self, x, y, dx, dy) |
---|
27 | LoadData1D.__init__(self, x, y, dx, dy) |
---|
28 | self.id = None |
---|
29 | self.list_group_id = [] |
---|
30 | self.group_id = None |
---|
31 | self.is_data = True |
---|
32 | self.path = None |
---|
33 | self.xtransform = None |
---|
34 | self.ytransform = None |
---|
35 | self.title = "" |
---|
36 | self.scale = None |
---|
37 | |
---|
38 | def copy_from_datainfo(self, data1d): |
---|
39 | """ |
---|
40 | copy values of Data1D of type DataLaoder.Data_info |
---|
41 | """ |
---|
42 | self.x = copy.deepcopy(data1d.x) |
---|
43 | self.y = copy.deepcopy(data1d.y) |
---|
44 | self.dy = copy.deepcopy(data1d.dy) |
---|
45 | |
---|
46 | if hasattr(data1d, "dx"): |
---|
47 | self.dx = copy.deepcopy(data1d.dx) |
---|
48 | if hasattr(data1d, "dxl"): |
---|
49 | self.dxl = copy.deepcopy(data1d.dxl) |
---|
50 | if hasattr(data1d, "dxw"): |
---|
51 | self.dxw = copy.deepcopy(data1d.dxw) |
---|
52 | |
---|
53 | self.xaxis(data1d._xaxis, data1d._xunit) |
---|
54 | self.yaxis(data1d._yaxis, data1d._yunit) |
---|
55 | self.title = data1d.title |
---|
56 | |
---|
57 | def __str__(self): |
---|
58 | """ |
---|
59 | print data |
---|
60 | """ |
---|
61 | _str = "%s\n" % LoadData1D.__str__(self) |
---|
62 | |
---|
63 | return _str |
---|
64 | |
---|
65 | def _perform_operation(self, other, operation): |
---|
66 | """ |
---|
67 | """ |
---|
68 | # First, check the data compatibility |
---|
69 | dy, dy_other = self._validity_check(other) |
---|
70 | result = Data1D(x=[], y=[], dx=None, dy=None) |
---|
71 | result.clone_without_data(clone=self) |
---|
72 | result.copy_from_datainfo(data1d=self) |
---|
73 | for i in range(len(self.x)): |
---|
74 | result.x[i] = self.x[i] |
---|
75 | if self.dx is not None and len(self.x) == len(self.dx): |
---|
76 | result.dx[i] = self.dx[i] |
---|
77 | |
---|
78 | a = Uncertainty(self.y[i], dy[i]**2) |
---|
79 | if isinstance(other, Data1D): |
---|
80 | b = Uncertainty(other.y[i], dy_other[i]**2) |
---|
81 | else: |
---|
82 | b = other |
---|
83 | |
---|
84 | output = operation(a, b) |
---|
85 | result.y[i] = output.x |
---|
86 | if result.dy is None: result.dy = numpy.zeros(len(self.x)) |
---|
87 | result.dy[i] = math.sqrt(math.fabs(output.variance)) |
---|
88 | return result |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | class Theory1D(PlotTheory1D, LoadData1D): |
---|
93 | """ |
---|
94 | """ |
---|
95 | def __init__(self, x=None, y=None, dy=None): |
---|
96 | """ |
---|
97 | """ |
---|
98 | if x is None: |
---|
99 | x = [] |
---|
100 | if y is None: |
---|
101 | y = [] |
---|
102 | PlotTheory1D.__init__(self, x, y, dy) |
---|
103 | LoadData1D.__init__(self, x, y, dy) |
---|
104 | self.id = None |
---|
105 | self.list_group_id = [] |
---|
106 | self.group_id = None |
---|
107 | self.is_data = True |
---|
108 | self.path = None |
---|
109 | self.xtransform = None |
---|
110 | self.ytransform = None |
---|
111 | self.title = "" |
---|
112 | self.scale = None |
---|
113 | |
---|
114 | def copy_from_datainfo(self, data1d): |
---|
115 | """ |
---|
116 | copy values of Data1D of type DataLaoder.Data_info |
---|
117 | """ |
---|
118 | self.x = copy.deepcopy(data1d.x) |
---|
119 | self.y = copy.deepcopy(data1d.y) |
---|
120 | self.dy = copy.deepcopy(data1d.dy) |
---|
121 | if hasattr(data1d, "dx"): |
---|
122 | self.dx = copy.deepcopy(data1d.dx) |
---|
123 | if hasattr(data1d, "dxl"): |
---|
124 | self.dxl = copy.deepcopy(data1d.dxl) |
---|
125 | if hasattr(data1d, "dxw"): |
---|
126 | self.dxw = copy.deepcopy(data1d.dxw) |
---|
127 | self.xaxis(data1d._xaxis, data1d._xunit) |
---|
128 | self.yaxis(data1d._yaxis, data1d._yunit) |
---|
129 | self.title = data1d.title |
---|
130 | |
---|
131 | def __str__(self): |
---|
132 | """ |
---|
133 | print data |
---|
134 | """ |
---|
135 | _str = "%s\n" % LoadData1D.__str__(self) |
---|
136 | |
---|
137 | return _str |
---|
138 | |
---|
139 | def _perform_operation(self, other, operation): |
---|
140 | """ |
---|
141 | """ |
---|
142 | # First, check the data compatibility |
---|
143 | dy, dy_other = self._validity_check(other) |
---|
144 | result = Theory1D(x=[], y=[], dy=None) |
---|
145 | result.clone_without_data(clone=self) |
---|
146 | result.copy_from_datainfo(data1d=self) |
---|
147 | for i in range(len(self.x)): |
---|
148 | result.x[i] = self.x[i] |
---|
149 | |
---|
150 | a = Uncertainty(self.y[i], dy[i]**2) |
---|
151 | if isinstance(other, Data1D): |
---|
152 | b = Uncertainty(other.y[i], dy_other[i]**2) |
---|
153 | else: |
---|
154 | b = other |
---|
155 | output = operation(a, b) |
---|
156 | result.y[i] = output.x |
---|
157 | if result.dy is None: |
---|
158 | result.dy = numpy.zeros(len(self.x)) |
---|
159 | result.dy[i] = math.sqrt(math.fabs(output.variance)) |
---|
160 | return result |
---|
161 | |
---|
162 | |
---|
163 | class Data2D(PlotData2D, LoadData2D): |
---|
164 | """ |
---|
165 | """ |
---|
166 | def __init__(self, image=None, err_image=None, |
---|
167 | xmin=None, xmax=None, ymin=None, ymax=None, |
---|
168 | zmin=None, zmax=None, qx_data=None, qy_data=None, |
---|
169 | q_data=None, mask=None, dqx_data=None, dqy_data=None): |
---|
170 | """ |
---|
171 | """ |
---|
172 | PlotData2D.__init__(self, image=image, err_image=err_image, |
---|
173 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, |
---|
174 | zmin=zmin, zmax=zmax, qx_data=qx_data, |
---|
175 | qy_data=qy_data) |
---|
176 | |
---|
177 | LoadData2D.__init__(self, data=image, err_data=err_image, |
---|
178 | qx_data=qx_data, qy_data=qy_data, |
---|
179 | dqx_data=dqx_data, dqy_data=dqy_data, |
---|
180 | q_data=q_data, mask=mask) |
---|
181 | self.id = None |
---|
182 | self.list_group_id = [] |
---|
183 | self.group_id = None |
---|
184 | self.is_data = True |
---|
185 | self.path = None |
---|
186 | self.xtransform = None |
---|
187 | self.ytransform = None |
---|
188 | self.title = "" |
---|
189 | self.scale = None |
---|
190 | |
---|
191 | def copy_from_datainfo(self, data2d): |
---|
192 | """ |
---|
193 | copy value of Data2D of type DataLoader.data_info |
---|
194 | """ |
---|
195 | self.data = copy.deepcopy(data2d.data) |
---|
196 | self.qx_data = copy.deepcopy(data2d.qx_data) |
---|
197 | self.qy_data = copy.deepcopy(data2d.qy_data) |
---|
198 | self.q_data = copy.deepcopy(data2d.q_data) |
---|
199 | self.mask = copy.deepcopy(data2d.mask) |
---|
200 | self.err_data = copy.deepcopy(data2d.err_data) |
---|
201 | self.x_bins = copy.deepcopy(data2d.x_bins) |
---|
202 | self.y_bins = copy.deepcopy(data2d.y_bins) |
---|
203 | if data2d.dqx_data is not None: |
---|
204 | self.dqx_data = copy.deepcopy(data2d.dqx_data) |
---|
205 | if data2d.dqy_data is not None: |
---|
206 | self.dqy_data = copy.deepcopy(data2d.dqy_data) |
---|
207 | self.xmin = data2d.xmin |
---|
208 | self.xmax = data2d.xmax |
---|
209 | self.ymin = data2d.ymin |
---|
210 | self.ymax = data2d.ymax |
---|
211 | if hasattr(data2d, "zmin"): |
---|
212 | self.zmin = data2d.zmin |
---|
213 | if hasattr(data2d, "zmax"): |
---|
214 | self.zmax = data2d.zmax |
---|
215 | self.xaxis(data2d._xaxis, data2d._xunit) |
---|
216 | self.yaxis(data2d._yaxis, data2d._yunit) |
---|
217 | self.title = data2d.title |
---|
218 | |
---|
219 | def __str__(self): |
---|
220 | """ |
---|
221 | print data |
---|
222 | """ |
---|
223 | _str = "%s\n" % LoadData2D.__str__(self) |
---|
224 | return _str |
---|
225 | |
---|
226 | def _perform_operation(self, other, operation): |
---|
227 | """ |
---|
228 | Perform 2D operations between data sets |
---|
229 | |
---|
230 | :param other: other data set |
---|
231 | :param operation: function defining the operation |
---|
232 | |
---|
233 | """ |
---|
234 | # First, check the data compatibility |
---|
235 | dy, dy_other = self._validity_check(other) |
---|
236 | |
---|
237 | result = Data2D(image=None, qx_data=None, qy_data=None, |
---|
238 | err_image=None, xmin=None, xmax=None, |
---|
239 | ymin=None, ymax=None, zmin=None, zmax=None) |
---|
240 | |
---|
241 | result.clone_without_data(clone=self) |
---|
242 | result.copy_from_datainfo(data2d=self) |
---|
243 | |
---|
244 | for i in range(numpy.size(self.data, 0)): |
---|
245 | for j in range(numpy.size(self.data, 1)): |
---|
246 | result.data[i][j] = self.data[i][j] |
---|
247 | if self.err_data is not None and \ |
---|
248 | numpy.size(self.data) == numpy.size(self.err_data): |
---|
249 | result.err_data[i][j] = self.err_data[i][j] |
---|
250 | |
---|
251 | a = Uncertainty(self.data[i][j], dy[i][j]**2) |
---|
252 | if isinstance(other, Data2D): |
---|
253 | b = Uncertainty(other.data[i][j], dy_other[i][j]**2) |
---|
254 | else: |
---|
255 | b = other |
---|
256 | output = operation(a, b) |
---|
257 | result.data[i][j] = output.x |
---|
258 | result.err_data[i][j] = math.sqrt(math.fabs(output.variance)) |
---|
259 | return result |
---|
260 | |
---|