[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) |
---|
| 160 | |
---|
| 161 | self.xmin = data2d.xmin |
---|
| 162 | self.xmax = data2d.xmax |
---|
| 163 | self.ymin = data2d.ymin |
---|
| 164 | self.ymax = data2d.ymax |
---|
[901142f] | 165 | self.zmin = data2d.zmin |
---|
| 166 | self.zmax = data2d.zmax |
---|
[ff3f900b] | 167 | |
---|
[901142f] | 168 | self.xaxis(data2d._xaxis, data2d._xunit) |
---|
| 169 | self.yaxis(data2d._yaxis, data2d._yunit) |
---|
[ff3f900b] | 170 | |
---|
[3562fbc] | 171 | def __str__(self): |
---|
| 172 | """ |
---|
| 173 | print data |
---|
| 174 | """ |
---|
| 175 | _str = "%s\n" % LoadData2D.__str__(self) |
---|
| 176 | |
---|
| 177 | return _str |
---|
[901142f] | 178 | |
---|
| 179 | def _perform_operation(self, other, operation): |
---|
| 180 | """ |
---|
| 181 | Perform 2D operations between data sets |
---|
| 182 | |
---|
| 183 | @param other: other data set |
---|
| 184 | @param operation: function defining the operation |
---|
| 185 | """ |
---|
| 186 | # First, check the data compatibility |
---|
| 187 | dy, dy_other = self._validity_check(other) |
---|
| 188 | |
---|
| 189 | result = Data2D(image=None, qx_data=None, qy_data=None, |
---|
| 190 | err_image=None, xmin=None, xmax=None, |
---|
| 191 | ymin=None, ymax=None, zmin=None, zmax=None) |
---|
| 192 | |
---|
| 193 | result.clone_without_data(clone=self) |
---|
| 194 | result.copy_from_datainfo(data2d=self) |
---|
| 195 | |
---|
| 196 | for i in range(numpy.size(self.data,0)): |
---|
| 197 | for j in range(numpy.size(self.data,1)): |
---|
| 198 | result.data[i][j] = self.data[i][j] |
---|
| 199 | if self.err_data is not None and \ |
---|
| 200 | numpy.size(self.data)==numpy.size(self.err_data): |
---|
| 201 | result.err_data[i][j] = self.err_data[i][j] |
---|
| 202 | |
---|
| 203 | a = Uncertainty(self.data[i][j], dy[i][j]**2) |
---|
| 204 | if isinstance(other, Data2D): |
---|
| 205 | b = Uncertainty(other.data[i][j], dy_other[i][j]**2) |
---|
| 206 | else: |
---|
| 207 | b = other |
---|
| 208 | |
---|
| 209 | output = operation(a, b) |
---|
| 210 | result.data[i][j] = output.x |
---|
| 211 | result.err_data[i][j] = math.sqrt(math.fabs(output.variance)) |
---|
| 212 | return result |
---|
[ff3f900b] | 213 | |
---|