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