[6fd89d4] | 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 |
---|
[6060a86] | 12 | import logging |
---|
| 13 | import numpy as np |
---|
[6fd89d4] | 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 | |
---|
[6060a86] | 30 | logger = logging.getLogger(__name__) |
---|
| 31 | |
---|
[60bf0db] | 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 | ) |
---|
[6fd89d4] | 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 | |
---|
[60bf0db] | 47 | def __init__(self, pr_state, nfunc, parent=None): |
---|
[6fd89d4] | 48 | super(DmaxWindow, self).__init__() |
---|
| 49 | self.setupUi(self) |
---|
| 50 | |
---|
| 51 | self.setWindowTitle("Dâââ Explorer") |
---|
| 52 | |
---|
| 53 | self.pr_state = pr_state |
---|
[60bf0db] | 54 | self.nfunc = nfunc |
---|
[6fd89d4] | 55 | self.communicator = GuiUtils.Communicate() |
---|
| 56 | |
---|
| 57 | self.plot = PlotterWidget(self, self) |
---|
[5fe52594] | 58 | self.hasPlot = None |
---|
[6fd89d4] | 59 | self.verticalLayout.insertWidget(0, self.plot) |
---|
| 60 | |
---|
| 61 | # Let's choose the Standard Item Model. |
---|
| 62 | self.model = QtGui.QStandardItemModel(self) |
---|
[6060a86] | 63 | self.mapper = None |
---|
[6fd89d4] | 64 | |
---|
| 65 | # # Connect buttons to slots. |
---|
| 66 | # # Needs to be done early so default values propagate properly. |
---|
[60bf0db] | 67 | self.setupSlots() |
---|
[6fd89d4] | 68 | |
---|
[60bf0db] | 69 | # Set up the model. |
---|
| 70 | self.setupModel() |
---|
[6fd89d4] | 71 | |
---|
| 72 | # # Set up the mapper |
---|
[60bf0db] | 73 | self.setupMapper() |
---|
| 74 | |
---|
| 75 | def setupSlots(self): |
---|
| 76 | self.closeButton.clicked.connect(self.close) |
---|
| 77 | |
---|
[477c473] | 78 | self.dependentVariable.currentIndexChanged.connect(self.dVChanged) |
---|
| 79 | |
---|
[60bf0db] | 80 | self.model.itemChanged.connect(self.modelChanged) |
---|
| 81 | |
---|
| 82 | def setupModel(self): |
---|
| 83 | self.model.setItem(W.NPTS, QtGui.QStandardItem(str(self.nfunc))) |
---|
| 84 | self.model.setItem(W.DMIN, |
---|
| 85 | QtGui.QStandardItem( |
---|
| 86 | str(0.9*self.pr_state.d_max))) |
---|
| 87 | self.model.setItem(W.DMAX, |
---|
| 88 | QtGui.QStandardItem( |
---|
| 89 | str(1.1*self.pr_state.d_max))) |
---|
[481c4f4] | 90 | self.model.setItem(W.VARIABLE, |
---|
| 91 | QtGui.QStandardItem( "ϲ/dof")) |
---|
[60bf0db] | 92 | |
---|
| 93 | def setupMapper(self): |
---|
| 94 | self.mapper = QtGui.QDataWidgetMapper(self) |
---|
| 95 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
| 96 | self.mapper.setModel(self.model) |
---|
| 97 | |
---|
| 98 | self.mapper.addMapping(self.Npts, W.NPTS) |
---|
| 99 | self.mapper.addMapping(self.minDist, W.DMIN) |
---|
| 100 | self.mapper.addMapping(self.maxDist, W.DMAX) |
---|
| 101 | self.mapper.addMapping(self.dependentVariable, W.VARIABLE) |
---|
| 102 | |
---|
| 103 | self.mapper.toFirst() |
---|
| 104 | |
---|
[477c473] | 105 | def dVChanged(self): |
---|
| 106 | self.modelChanged() |
---|
| 107 | |
---|
| 108 | def modelChanged(self): |
---|
[6060a86] | 109 | if not self.mapper: |
---|
| 110 | return |
---|
| 111 | |
---|
| 112 | iq0 = [] |
---|
| 113 | rg = [] |
---|
| 114 | pos = [] |
---|
| 115 | pos_err = [] |
---|
| 116 | osc = [] |
---|
[4aa62c8] | 117 | bck = [] |
---|
| 118 | chi2 = [] |
---|
| 119 | |
---|
[6060a86] | 120 | xs = np.linspace(float(self.model.item(W.DMIN).text()), |
---|
| 121 | float(self.model.item(W.DMAX).text()), |
---|
| 122 | float(self.model.item(W.NPTS).text())) |
---|
| 123 | |
---|
[3e20577] | 124 | original = self.pr_state.d_max |
---|
[6060a86] | 125 | for x in xs: |
---|
| 126 | self.pr_state.d_max = x |
---|
| 127 | try: |
---|
[d8c53fa3] | 128 | out, cov = self.pr_state.invert(self.pr_state.nfunc) |
---|
[6060a86] | 129 | |
---|
| 130 | iq0.append(self.pr_state.iq0(out)) |
---|
| 131 | rg.append(self.pr_state.rg(out)) |
---|
| 132 | pos.append(self.pr_state.get_positive(out)) |
---|
| 133 | pos_err.append(self.pr_state.get_pos_err(out, cov)) |
---|
| 134 | osc.append(self.pr_state.oscillations(out)) |
---|
[4aa62c8] | 135 | bck.append(self.pr_state.background) |
---|
| 136 | chi2.append(self.pr_state.chi2) |
---|
[6060a86] | 137 | except: |
---|
| 138 | # This inversion failed, skip this D_max value |
---|
| 139 | msg = "ExploreDialog: inversion failed " |
---|
| 140 | msg += "for D_max=%s\n%s" % (str(x), sys.exc_value) |
---|
| 141 | print(msg) |
---|
| 142 | # logger.error(msg) |
---|
[73a24fa] | 143 | |
---|
| 144 | #Return the invertor to its original state |
---|
[3e20577] | 145 | self.pr_state.d_max = original |
---|
[73a24fa] | 146 | self.pr_state.invert(self.nfunc) |
---|
[3e20577] | 147 | |
---|
[481c4f4] | 148 | plotter = unicode(self.model.item(W.VARIABLE).text()) |
---|
[e742b63c] | 149 | if plotter == u"ϲ/dof": |
---|
[481c4f4] | 150 | ys = chi2 |
---|
| 151 | elif plotter == u"I(Q=0)": |
---|
| 152 | ys = iq0 |
---|
| 153 | elif plotter == u"Rg": |
---|
| 154 | ys = rg |
---|
| 155 | elif plotter == u"Oscillation parameter": |
---|
| 156 | ys = osc |
---|
| 157 | elif plotter == u"Background": |
---|
| 158 | ys = bck |
---|
| 159 | elif plotter == u"Positive Fraction": |
---|
| 160 | ys = pos |
---|
[e742b63c] | 161 | else: |
---|
| 162 | ys = pos_err |
---|
[481c4f4] | 163 | |
---|
[5fe52594] | 164 | data = Data1D(xs, ys) |
---|
| 165 | if self.hasPlot: |
---|
| 166 | self.plot.removePlot(None) |
---|
| 167 | self.hasPlot = True |
---|
| 168 | self.plot.plot(data=data, marker="-") |
---|
[6fd89d4] | 169 | |
---|
| 170 | |
---|
| 171 | if __name__ == "__main__": |
---|
| 172 | APP = QtGui.QApplication([]) |
---|
| 173 | import qt4reactor |
---|
| 174 | qt4reactor.install() |
---|
| 175 | # DO NOT move the following import to the top! |
---|
| 176 | # (unless you know what you're doing) |
---|
| 177 | from twisted.internet import reactor |
---|
[60bf0db] | 178 | from sas.sascalc.pr.invertor import Invertor |
---|
[3e20577] | 179 | from sas.sascalc.dataloader.loader import Loader |
---|
| 180 | |
---|
[d8c53fa3] | 181 | data = Loader().load("test/pr_inversion/test/sphere_80.txt")[0] |
---|
[60bf0db] | 182 | pr_state = Invertor() |
---|
[d8c53fa3] | 183 | pr_state.d_max = 130.0 |
---|
[73a24fa] | 184 | pr_state.alpha = 0.0007 |
---|
[3e20577] | 185 | pr_state.x = data.x |
---|
| 186 | pr_state.y = data.y |
---|
[f582224] | 187 | pr_state.err = 0.15*np.sqrt(data.y[0])*np.sqrt(data.y) |
---|
[d8c53fa3] | 188 | pr_state.nfunc = 15 |
---|
| 189 | |
---|
[73a24fa] | 190 | DLG = DmaxWindow(pr_state, 15, reactor) |
---|
[6fd89d4] | 191 | DLG.show() |
---|
| 192 | reactor.run() |
---|