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