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