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 PyQt4 import QtCore |
---|
15 | from PyQt4 import QtGui |
---|
16 | from PyQt4 import QtWebKit |
---|
17 | |
---|
18 | from twisted.internet import threads |
---|
19 | |
---|
20 | # sas-global |
---|
21 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
22 | from sas.qtgui.Plotting.Plotter import PlotterWidget |
---|
23 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
24 | |
---|
25 | # local |
---|
26 | from UI.dmax import Ui_DmaxExplorer |
---|
27 | # from InvariantDetails import DetailsDialog |
---|
28 | # from InvariantUtils import WIDGETS |
---|
29 | |
---|
30 | logger = logging.getLogger(__name__) |
---|
31 | |
---|
32 | def enum(*sequential, **named): |
---|
33 | enums = dict(zip(sequential, range(len(sequential))), **named) |
---|
34 | return type('Enum', (), enums) |
---|
35 | |
---|
36 | W = enum( 'NPTS', #0 |
---|
37 | 'DMIN', #1 |
---|
38 | 'DMAX', #2 |
---|
39 | 'VARIABLE', #3 |
---|
40 | ) |
---|
41 | |
---|
42 | class DmaxWindow(QtGui.QDialog, Ui_DmaxExplorer): |
---|
43 | # The controller which is responsible for managing signal slots connections |
---|
44 | # for the gui and providing an interface to the data model. |
---|
45 | name = "Dmax Explorer" # For displaying in the combo box |
---|
46 | |
---|
47 | def __init__(self, pr_state, nfunc, parent=None): |
---|
48 | super(DmaxWindow, self).__init__() |
---|
49 | self.setupUi(self) |
---|
50 | |
---|
51 | self.setWindowTitle("Dâââ Explorer") |
---|
52 | |
---|
53 | self.pr_state = pr_state |
---|
54 | self.nfunc = nfunc |
---|
55 | self.communicator = GuiUtils.Communicate() |
---|
56 | |
---|
57 | self.plot = PlotterWidget(self, self) |
---|
58 | self.verticalLayout.insertWidget(0, self.plot) |
---|
59 | |
---|
60 | # Let's choose the Standard Item Model. |
---|
61 | self.model = QtGui.QStandardItemModel(self) |
---|
62 | self.mapper = None |
---|
63 | |
---|
64 | # # Connect buttons to slots. |
---|
65 | # # Needs to be done early so default values propagate properly. |
---|
66 | self.setupSlots() |
---|
67 | |
---|
68 | # Set up the model. |
---|
69 | self.setupModel() |
---|
70 | |
---|
71 | # # Set up the mapper |
---|
72 | self.setupMapper() |
---|
73 | |
---|
74 | def setupSlots(self): |
---|
75 | self.closeButton.clicked.connect(self.close) |
---|
76 | |
---|
77 | self.model.itemChanged.connect(self.modelChanged) |
---|
78 | |
---|
79 | def setupModel(self): |
---|
80 | self.model.setItem(W.NPTS, QtGui.QStandardItem(str(self.nfunc))) |
---|
81 | self.model.setItem(W.DMIN, |
---|
82 | QtGui.QStandardItem( |
---|
83 | str(0.9*self.pr_state.d_max))) |
---|
84 | self.model.setItem(W.DMAX, |
---|
85 | QtGui.QStandardItem( |
---|
86 | str(1.1*self.pr_state.d_max))) |
---|
87 | |
---|
88 | def setupMapper(self): |
---|
89 | self.mapper = QtGui.QDataWidgetMapper(self) |
---|
90 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
91 | self.mapper.setModel(self.model) |
---|
92 | |
---|
93 | self.mapper.addMapping(self.Npts, W.NPTS) |
---|
94 | self.mapper.addMapping(self.minDist, W.DMIN) |
---|
95 | self.mapper.addMapping(self.maxDist, W.DMAX) |
---|
96 | self.mapper.addMapping(self.dependentVariable, W.VARIABLE) |
---|
97 | |
---|
98 | self.mapper.toFirst() |
---|
99 | |
---|
100 | def modelChanged(self, item): |
---|
101 | if not self.mapper: |
---|
102 | return |
---|
103 | |
---|
104 | iq0 = [] |
---|
105 | rg = [] |
---|
106 | pos = [] |
---|
107 | pos_err = [] |
---|
108 | osc = [] |
---|
109 | bck = [] |
---|
110 | chi2 = [] |
---|
111 | |
---|
112 | xs = np.linspace(float(self.model.item(W.DMIN).text()), |
---|
113 | float(self.model.item(W.DMAX).text()), |
---|
114 | float(self.model.item(W.NPTS).text())) |
---|
115 | |
---|
116 | original = self.pr_state.d_max |
---|
117 | for x in xs: |
---|
118 | self.pr_state.d_max = x |
---|
119 | try: |
---|
120 | out, cov = self.pr_state.invert(self.nfunc) |
---|
121 | |
---|
122 | iq0.append(self.pr_state.iq0(out)) |
---|
123 | rg.append(self.pr_state.rg(out)) |
---|
124 | pos.append(self.pr_state.get_positive(out)) |
---|
125 | pos_err.append(self.pr_state.get_pos_err(out, cov)) |
---|
126 | osc.append(self.pr_state.oscillations(out)) |
---|
127 | bck.append(self.pr_state.background) |
---|
128 | chi2.append(self.pr_state.chi2) |
---|
129 | except: |
---|
130 | # This inversion failed, skip this D_max value |
---|
131 | msg = "ExploreDialog: inversion failed " |
---|
132 | msg += "for D_max=%s\n%s" % (str(x), sys.exc_value) |
---|
133 | print(msg) |
---|
134 | # logger.error(msg) |
---|
135 | self.pr_state.d_max = original |
---|
136 | |
---|
137 | self.plot.plot(data=Data1D(xs, rg), marker="-") |
---|
138 | |
---|
139 | |
---|
140 | if __name__ == "__main__": |
---|
141 | APP = QtGui.QApplication([]) |
---|
142 | import qt4reactor |
---|
143 | qt4reactor.install() |
---|
144 | # DO NOT move the following import to the top! |
---|
145 | # (unless you know what you're doing) |
---|
146 | from twisted.internet import reactor |
---|
147 | from sas.sascalc.pr.invertor import Invertor |
---|
148 | from sas.sascalc.dataloader.loader import Loader |
---|
149 | |
---|
150 | data = Loader().load("../../../../../test/pr_inversion/test/sphere_80.txt")[0] |
---|
151 | pr_state = Invertor() |
---|
152 | pr_state.x = data.x |
---|
153 | pr_state.y = data.y |
---|
154 | pr_state.err = data.dy+1e-3 |
---|
155 | |
---|
156 | DLG = DmaxWindow(pr_state, 10, reactor) |
---|
157 | DLG.show() |
---|
158 | reactor.run() |
---|