[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) |
---|
| 58 | self.verticalLayout.insertWidget(0, self.plot) |
---|
| 59 | |
---|
| 60 | # Let's choose the Standard Item Model. |
---|
| 61 | self.model = QtGui.QStandardItemModel(self) |
---|
[6060a86] | 62 | self.mapper = None |
---|
[6fd89d4] | 63 | |
---|
| 64 | # # Connect buttons to slots. |
---|
| 65 | # # Needs to be done early so default values propagate properly. |
---|
[60bf0db] | 66 | self.setupSlots() |
---|
[6fd89d4] | 67 | |
---|
[60bf0db] | 68 | # Set up the model. |
---|
| 69 | self.setupModel() |
---|
[6fd89d4] | 70 | |
---|
| 71 | # # Set up the mapper |
---|
[60bf0db] | 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): |
---|
[6060a86] | 101 | if not self.mapper: |
---|
| 102 | return |
---|
| 103 | |
---|
| 104 | iq0 = [] |
---|
| 105 | rg = [] |
---|
| 106 | pos = [] |
---|
| 107 | pos_err = [] |
---|
| 108 | osc = [] |
---|
| 109 | xs = np.linspace(float(self.model.item(W.DMIN).text()), |
---|
| 110 | float(self.model.item(W.DMAX).text()), |
---|
| 111 | float(self.model.item(W.NPTS).text())) |
---|
| 112 | |
---|
| 113 | for x in xs: |
---|
| 114 | self.pr_state.d_max = x |
---|
| 115 | try: |
---|
| 116 | out, cov = self.pr_state.invert(self.nfunc) |
---|
| 117 | |
---|
| 118 | iq0.append(self.pr_state.iq0(out)) |
---|
| 119 | rg.append(self.pr_state.rg(out)) |
---|
| 120 | pos.append(self.pr_state.get_positive(out)) |
---|
| 121 | pos_err.append(self.pr_state.get_pos_err(out, cov)) |
---|
| 122 | osc.append(self.pr_state.oscillations(out)) |
---|
| 123 | except: |
---|
| 124 | # This inversion failed, skip this D_max value |
---|
| 125 | msg = "ExploreDialog: inversion failed " |
---|
| 126 | msg += "for D_max=%s\n%s" % (str(x), sys.exc_value) |
---|
| 127 | print(msg) |
---|
| 128 | # logger.error(msg) |
---|
| 129 | self.plot.plot(data=Data1D(xs, rg), marker="-") |
---|
[6fd89d4] | 130 | |
---|
| 131 | |
---|
| 132 | if __name__ == "__main__": |
---|
| 133 | APP = QtGui.QApplication([]) |
---|
| 134 | import qt4reactor |
---|
| 135 | qt4reactor.install() |
---|
| 136 | # DO NOT move the following import to the top! |
---|
| 137 | # (unless you know what you're doing) |
---|
| 138 | from twisted.internet import reactor |
---|
[60bf0db] | 139 | from sas.sascalc.pr.invertor import Invertor |
---|
| 140 | pr_state = Invertor() |
---|
| 141 | DLG = DmaxWindow(pr_state, 10, reactor) |
---|
[6fd89d4] | 142 | DLG.show() |
---|
| 143 | reactor.run() |
---|