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 | |
---|
161 | self.xmin = data2d.xmin |
---|
162 | self.xmax = data2d.xmax |
---|
163 | self.ymin = data2d.ymin |
---|
164 | self.ymax = data2d.ymax |
---|
165 | if hasattr(data2d, "zmin"): |
---|
166 | self.zmin = data2d.zmin |
---|
167 | if hasattr(data2d, "zmax"): |
---|
168 | self.zmax = data2d.zmax |
---|
169 | |
---|
170 | self.xaxis(data2d._xaxis, data2d._xunit) |
---|
171 | self.yaxis(data2d._yaxis, data2d._yunit) |
---|
172 | |
---|
173 | def __str__(self): |
---|
174 | """ |
---|
175 | print data |
---|
176 | """ |
---|
177 | _str = "%s\n" % LoadData2D.__str__(self) |
---|
178 | |
---|
179 | return _str |
---|
180 | |
---|
181 | def _perform_operation(self, other, operation): |
---|
182 | """ |
---|
183 | Perform 2D operations between data sets |
---|
184 | |
---|
185 | @param other: other data set |
---|
186 | @param operation: function defining the operation |
---|
187 | """ |
---|
188 | # First, check the data compatibility |
---|
189 | dy, dy_other = self._validity_check(other) |
---|
190 | |
---|
191 | result = Data2D(image=None, qx_data=None, qy_data=None, |
---|
192 | err_image=None, xmin=None, xmax=None, |
---|
193 | ymin=None, ymax=None, zmin=None, zmax=None) |
---|
194 | |
---|
195 | result.clone_without_data(clone=self) |
---|
196 | result.copy_from_datainfo(data2d=self) |
---|
197 | |
---|
198 | for i in range(numpy.size(self.data,0)): |
---|
199 | for j in range(numpy.size(self.data,1)): |
---|
200 | result.data[i][j] = self.data[i][j] |
---|
201 | if self.err_data is not None and \ |
---|
202 | numpy.size(self.data)==numpy.size(self.err_data): |
---|
203 | result.err_data[i][j] = self.err_data[i][j] |
---|
204 | |
---|
205 | a = Uncertainty(self.data[i][j], dy[i][j]**2) |
---|
206 | if isinstance(other, Data2D): |
---|
207 | b = Uncertainty(other.data[i][j], dy_other[i][j]**2) |
---|
208 | else: |
---|
209 | b = other |
---|
210 | |
---|
211 | output = operation(a, b) |
---|
212 | result.data[i][j] = output.x |
---|
213 | result.err_data[i][j] = math.sqrt(math.fabs(output.variance)) |
---|
214 | return result |
---|
215 | |
---|