[fa81e94] | 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 |
---|
[6a3e1fe] | 24 | from .UI.DMaxExplorer import Ui_DmaxExplorer |
---|
[fa81e94] | 25 | |
---|
| 26 | logger = logging.getLogger(__name__) |
---|
| 27 | |
---|
[d4881f6a] | 28 | from sas.qtgui.Utilities.GuiUtils import enum |
---|
[fa81e94] | 29 | |
---|
[8f83719f] | 30 | W = enum( 'NPTS', #0 |
---|
| 31 | 'DMIN', #1 |
---|
| 32 | 'DMAX', #2 |
---|
| 33 | 'VARIABLE', #3 |
---|
[fa81e94] | 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 | |
---|
[0261bc1] | 45 | self.setWindowTitle("Dââ Explorer") |
---|
[fa81e94] | 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 | |
---|
[8f83719f] | 59 | # Add validators on line edits |
---|
| 60 | self.setupValidators() |
---|
| 61 | |
---|
[fa81e94] | 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 | |
---|
[8f83719f] | 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 | |
---|
[fa81e94] | 78 | def setupSlots(self): |
---|
| 79 | self.closeButton.clicked.connect(self.close) |
---|
| 80 | self.model.itemChanged.connect(self.modelChanged) |
---|
[8f83719f] | 81 | self.dependentVariable.currentIndexChanged.connect(lambda:self.modelChanged(None)) |
---|
[fa81e94] | 82 | |
---|
| 83 | def setupModel(self): |
---|
| 84 | self.model.setItem(W.NPTS, QtGui.QStandardItem(str(self.nfunc))) |
---|
[8f83719f] | 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))) |
---|
[6a3e1fe] | 87 | self.model.setItem(W.VARIABLE, QtGui.QStandardItem( "ϲ/dof")) |
---|
[fa81e94] | 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) |
---|
[8f83719f] | 130 | except Exception as ex: |
---|
[fa81e94] | 131 | # This inversion failed, skip this D_max value |
---|
| 132 | msg = "ExploreDialog: inversion failed " |
---|
[8f83719f] | 133 | msg += "for D_max=%s\n%s" % (str(x), ex) |
---|
[fa81e94] | 134 | print(msg) |
---|
[6a3e1fe] | 135 | logger.error(msg) |
---|
[fa81e94] | 136 | |
---|
| 137 | #Return the invertor to its original state |
---|
| 138 | self.pr_state.d_max = original |
---|
[6a3e1fe] | 139 | try: |
---|
| 140 | self.pr_state.invert(self.nfunc) |
---|
| 141 | except RuntimeError as ex: |
---|
| 142 | msg = "ExploreDialog: inversion failed " |
---|
[8f83719f] | 143 | msg += "for D_max=%s\n%s" % (str(x), ex) |
---|
[6a3e1fe] | 144 | print(msg) |
---|
| 145 | logger.error(msg) |
---|
[fa81e94] | 146 | |
---|
[8f83719f] | 147 | plotter = self.model.item(W.VARIABLE).text() |
---|
| 148 | y_label = y_unit = "" |
---|
| 149 | x_label = "D_{max}" |
---|
| 150 | x_unit = "A" |
---|
[fa81e94] | 151 | if plotter == "ϲ/dof": |
---|
| 152 | ys = chi2 |
---|
[8f83719f] | 153 | y_label = "\chi^2/dof" |
---|
| 154 | y_unit = "a.u." |
---|
[fa81e94] | 155 | elif plotter == "I(Q=0)": |
---|
| 156 | ys = iq0 |
---|
[8f83719f] | 157 | y_label = "I(q=0)" |
---|
| 158 | y_unit = "\AA^{-1}" |
---|
[fa81e94] | 159 | elif plotter == "Rg": |
---|
| 160 | ys = rg |
---|
[8f83719f] | 161 | y_label = "R_g" |
---|
| 162 | y_unit = "\AA" |
---|
[fa81e94] | 163 | elif plotter == "Oscillation parameter": |
---|
| 164 | ys = osc |
---|
[8f83719f] | 165 | y_label = "Osc" |
---|
| 166 | y_unit = "a.u." |
---|
[fa81e94] | 167 | elif plotter == "Background": |
---|
| 168 | ys = bck |
---|
[8f83719f] | 169 | y_label = "Bckg" |
---|
| 170 | y_unit = "\AA^{-1}" |
---|
[fa81e94] | 171 | elif plotter == "Positive Fraction": |
---|
| 172 | ys = pos |
---|
[8f83719f] | 173 | y_label = "P^+" |
---|
| 174 | y_unit = "a.u." |
---|
[fa81e94] | 175 | else: |
---|
| 176 | ys = pos_err |
---|
[8f83719f] | 177 | y_label = "P^{+}_{1\sigma}" |
---|
| 178 | y_unit = "a.u." |
---|
[fa81e94] | 179 | |
---|
| 180 | data = Data1D(xs, ys) |
---|
| 181 | if self.hasPlot: |
---|
| 182 | self.plot.removePlot(None) |
---|
| 183 | self.hasPlot = True |
---|
[8f83719f] | 184 | data.title = plotter |
---|
| 185 | data._xaxis= x_label |
---|
| 186 | data._xunit = x_unit |
---|
| 187 | data._yaxis = y_label |
---|
| 188 | data._yunit = y_unit |
---|
[fa81e94] | 189 | self.plot.plot(data=data, marker="-") |
---|