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