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