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