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