1 | import numpy as np |
---|
2 | |
---|
3 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
4 | |
---|
5 | |
---|
6 | class InversionLogic(object): |
---|
7 | """ |
---|
8 | All the data-related logic. This class deals exclusively with Data1D/2D |
---|
9 | No QStandardModelIndex here. |
---|
10 | """ |
---|
11 | |
---|
12 | def __init__(self, data=None): |
---|
13 | self._data = data |
---|
14 | self.data_is_loaded = False |
---|
15 | if data is not None: |
---|
16 | self.data_is_loaded = True |
---|
17 | |
---|
18 | @property |
---|
19 | def data(self): |
---|
20 | return self._data |
---|
21 | |
---|
22 | @data.setter |
---|
23 | def data(self, value): |
---|
24 | """ data setter """ |
---|
25 | self._data = value |
---|
26 | self.data_is_loaded = True |
---|
27 | |
---|
28 | def isLoadedData(self): |
---|
29 | """ accessor """ |
---|
30 | return self.data_is_loaded |
---|
31 | |
---|
32 | def new1DPlot(self, return_data): |
---|
33 | """ |
---|
34 | Create a new 1D data instance based on fitting results |
---|
35 | """ |
---|
36 | # Unpack return data from Calc1D |
---|
37 | x, y, page_id, state, weight,\ |
---|
38 | fid, toggle_mode_on, \ |
---|
39 | elapsed, index, model,\ |
---|
40 | data, update_chisqr, source = return_data |
---|
41 | |
---|
42 | # Create the new plot |
---|
43 | new_plot = Data1D(x=x, y=y) |
---|
44 | new_plot.is_data = False |
---|
45 | new_plot.dy = np.zeros(len(y)) |
---|
46 | _yaxis, _yunit = data.get_yaxis() |
---|
47 | _xaxis, _xunit = data.get_xaxis() |
---|
48 | |
---|
49 | new_plot.group_id = data.group_id |
---|
50 | new_plot.id = data.name |
---|
51 | new_plot.name = model.name + " [" + data.name + "]" |
---|
52 | new_plot.title = new_plot.name |
---|
53 | new_plot.xaxis(_xaxis, _xunit) |
---|
54 | new_plot.yaxis(_yaxis, _yunit) |
---|
55 | |
---|
56 | return new_plot |
---|
57 | |
---|
58 | def computeDataRange(self): |
---|
59 | """ |
---|
60 | Wrapper for calculating the data range based on local dataset |
---|
61 | """ |
---|
62 | return self.computeRangeFromData(self.data) |
---|
63 | |
---|
64 | def computeRangeFromData(self, data): |
---|
65 | """ |
---|
66 | Compute the minimum and the maximum range of the data |
---|
67 | return the npts contains in data |
---|
68 | """ |
---|
69 | qmin, qmax, npts = None, None, None |
---|
70 | if isinstance(data, Data1D): |
---|
71 | try: |
---|
72 | qmin = min(data.x) |
---|
73 | qmax = max(data.x) |
---|
74 | npts = len(data.x) |
---|
75 | except (ValueError, TypeError): |
---|
76 | msg = "Unable to find min/max/length of \n data named %s" % \ |
---|
77 | self.data.filename |
---|
78 | raise ValueError, msg |
---|
79 | |
---|
80 | else: |
---|
81 | qmin = 0 |
---|
82 | try: |
---|
83 | x = max(np.fabs(data.xmin), np.fabs(data.xmax)) |
---|
84 | y = max(np.fabs(data.ymin), np.fabs(data.ymax)) |
---|
85 | except (ValueError, TypeError): |
---|
86 | msg = "Unable to find min/max of \n data named %s" % \ |
---|
87 | self.data.filename |
---|
88 | raise ValueError, msg |
---|
89 | qmax = np.sqrt(x * x + y * y) |
---|
90 | npts = len(data.data) |
---|
91 | return qmin, qmax, npts |
---|