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