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