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