1 | # -*- coding: utf-8 -*- |
---|
2 | """ |
---|
3 | Dialog panel to explore the P(r) inversion results for a range |
---|
4 | of D_max value. User picks a number of points and a range of |
---|
5 | distances, then can toggle between inversion outputs and see |
---|
6 | their distribution as a function of D_max. |
---|
7 | """ |
---|
8 | |
---|
9 | # global |
---|
10 | import sys |
---|
11 | import os |
---|
12 | import logging |
---|
13 | import numpy as np |
---|
14 | from PyQt5 import QtCore |
---|
15 | from PyQt5 import QtGui |
---|
16 | from PyQt5 import QtWidgets |
---|
17 | |
---|
18 | # sas-global |
---|
19 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
20 | from sas.qtgui.Plotting.Plotter import PlotterWidget |
---|
21 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
22 | |
---|
23 | # local |
---|
24 | from .UI.DMaxExplorer import Ui_DmaxExplorer |
---|
25 | |
---|
26 | logger = logging.getLogger(__name__) |
---|
27 | |
---|
28 | from sas.qtgui.Utilities.GuiUtils import enum |
---|
29 | |
---|
30 | W = enum( 'NPTS', #0 |
---|
31 | 'DMIN', #1 |
---|
32 | 'DMAX', #2 |
---|
33 | 'VARIABLE', #3 |
---|
34 | ) |
---|
35 | |
---|
36 | class DmaxWindow(QtWidgets.QDialog, Ui_DmaxExplorer): |
---|
37 | # The controller which is responsible for managing signal slots connections |
---|
38 | # for the gui and providing an interface to the data model. |
---|
39 | name = "Dmax Explorer" # For displaying in the combo box |
---|
40 | |
---|
41 | def __init__(self, pr_state, nfunc, parent=None): |
---|
42 | super(DmaxWindow, self).__init__() |
---|
43 | self.setupUi(self) |
---|
44 | |
---|
45 | self.setWindowTitle("Dââ Explorer") |
---|
46 | |
---|
47 | self.pr_state = pr_state |
---|
48 | self.nfunc = nfunc |
---|
49 | self.communicator = GuiUtils.Communicate() |
---|
50 | |
---|
51 | self.plot = PlotterWidget(self, self) |
---|
52 | self.hasPlot = None |
---|
53 | self.verticalLayout.insertWidget(0, self.plot) |
---|
54 | |
---|
55 | # Let's choose the Standard Item Model. |
---|
56 | self.model = QtGui.QStandardItemModel(self) |
---|
57 | self.mapper = None |
---|
58 | |
---|
59 | # Add validators on line edits |
---|
60 | self.setupValidators() |
---|
61 | |
---|
62 | # # Connect buttons to slots. |
---|
63 | # # Needs to be done early so default values propagate properly. |
---|
64 | self.setupSlots() |
---|
65 | |
---|
66 | # Set up the model. |
---|
67 | self.setupModel() |
---|
68 | |
---|
69 | # # Set up the mapper |
---|
70 | self.setupMapper() |
---|
71 | |
---|
72 | def setupValidators(self): |
---|
73 | """Add validators on relevant line edits""" |
---|
74 | self.Npts.setValidator(QtGui.QIntValidator()) |
---|
75 | self.minDist.setValidator(GuiUtils.DoubleValidator()) |
---|
76 | self.maxDist.setValidator(GuiUtils.DoubleValidator()) |
---|
77 | |
---|
78 | def setupSlots(self): |
---|
79 | self.closeButton.clicked.connect(self.close) |
---|
80 | self.model.itemChanged.connect(self.modelChanged) |
---|
81 | self.dependentVariable.currentIndexChanged.connect(lambda:self.modelChanged(None)) |
---|
82 | |
---|
83 | def setupModel(self): |
---|
84 | self.model.setItem(W.NPTS, QtGui.QStandardItem(str(self.nfunc))) |
---|
85 | self.model.setItem(W.DMIN, QtGui.QStandardItem("{:.1f}".format(0.9*self.pr_state.d_max))) |
---|
86 | self.model.setItem(W.DMAX, QtGui.QStandardItem("{:.1f}".format(1.1*self.pr_state.d_max))) |
---|
87 | self.model.setItem(W.VARIABLE, QtGui.QStandardItem( "ϲ/dof")) |
---|
88 | |
---|
89 | def setupMapper(self): |
---|
90 | self.mapper = QtWidgets.QDataWidgetMapper(self) |
---|
91 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
92 | self.mapper.setModel(self.model) |
---|
93 | |
---|
94 | self.mapper.addMapping(self.Npts, W.NPTS) |
---|
95 | self.mapper.addMapping(self.minDist, W.DMIN) |
---|
96 | self.mapper.addMapping(self.maxDist, W.DMAX) |
---|
97 | self.mapper.addMapping(self.dependentVariable, W.VARIABLE) |
---|
98 | |
---|
99 | self.mapper.toFirst() |
---|
100 | |
---|
101 | def modelChanged(self, item): |
---|
102 | if not self.mapper: |
---|
103 | return |
---|
104 | |
---|
105 | iq0 = [] |
---|
106 | rg = [] |
---|
107 | pos = [] |
---|
108 | pos_err = [] |
---|
109 | osc = [] |
---|
110 | bck = [] |
---|
111 | chi2 = [] |
---|
112 | |
---|
113 | xs = np.linspace(float(self.model.item(W.DMIN).text()), |
---|
114 | float(self.model.item(W.DMAX).text()), |
---|
115 | float(self.model.item(W.NPTS).text())) |
---|
116 | |
---|
117 | original = self.pr_state.d_max |
---|
118 | for x in xs: |
---|
119 | self.pr_state.d_max = x |
---|
120 | try: |
---|
121 | out, cov = self.pr_state.invert(self.pr_state.nfunc) |
---|
122 | |
---|
123 | iq0.append(self.pr_state.iq0(out)) |
---|
124 | rg.append(self.pr_state.rg(out)) |
---|
125 | pos.append(self.pr_state.get_positive(out)) |
---|
126 | pos_err.append(self.pr_state.get_pos_err(out, cov)) |
---|
127 | osc.append(self.pr_state.oscillations(out)) |
---|
128 | bck.append(self.pr_state.background) |
---|
129 | chi2.append(self.pr_state.chi2) |
---|
130 | except Exception as ex: |
---|
131 | # This inversion failed, skip this D_max value |
---|
132 | msg = "ExploreDialog: inversion failed " |
---|
133 | msg += "for D_max=%s\n%s" % (str(x), ex) |
---|
134 | print(msg) |
---|
135 | logger.error(msg) |
---|
136 | |
---|
137 | #Return the invertor to its original state |
---|
138 | self.pr_state.d_max = original |
---|
139 | try: |
---|
140 | self.pr_state.invert(self.nfunc) |
---|
141 | except RuntimeError as ex: |
---|
142 | msg = "ExploreDialog: inversion failed " |
---|
143 | msg += "for D_max=%s\n%s" % (str(x), ex) |
---|
144 | print(msg) |
---|
145 | logger.error(msg) |
---|
146 | |
---|
147 | plotter = self.model.item(W.VARIABLE).text() |
---|
148 | y_label = y_unit = "" |
---|
149 | x_label = "D_{max}" |
---|
150 | x_unit = "A" |
---|
151 | if plotter == "ϲ/dof": |
---|
152 | ys = chi2 |
---|
153 | y_label = "\chi^2/dof" |
---|
154 | y_unit = "a.u." |
---|
155 | elif plotter == "I(Q=0)": |
---|
156 | ys = iq0 |
---|
157 | y_label = "I(q=0)" |
---|
158 | y_unit = "\AA^{-1}" |
---|
159 | elif plotter == "Rg": |
---|
160 | ys = rg |
---|
161 | y_label = "R_g" |
---|
162 | y_unit = "\AA" |
---|
163 | elif plotter == "Oscillation parameter": |
---|
164 | ys = osc |
---|
165 | y_label = "Osc" |
---|
166 | y_unit = "a.u." |
---|
167 | elif plotter == "Background": |
---|
168 | ys = bck |
---|
169 | y_label = "Bckg" |
---|
170 | y_unit = "\AA^{-1}" |
---|
171 | elif plotter == "Positive Fraction": |
---|
172 | ys = pos |
---|
173 | y_label = "P^+" |
---|
174 | y_unit = "a.u." |
---|
175 | else: |
---|
176 | ys = pos_err |
---|
177 | y_label = "P^{+}_{1\sigma}" |
---|
178 | y_unit = "a.u." |
---|
179 | |
---|
180 | data = Data1D(xs, ys) |
---|
181 | if self.hasPlot: |
---|
182 | self.plot.removePlot(None) |
---|
183 | self.hasPlot = True |
---|
184 | data.title = plotter |
---|
185 | data._xaxis= x_label |
---|
186 | data._xunit = x_unit |
---|
187 | data._yaxis = y_label |
---|
188 | data._yunit = y_unit |
---|
189 | self.plot.plot(data=data, marker="-") |
---|