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