source: sasview/src/sas/qtgui/Calculators/SldPanel.py

ESS_GUI
Last change on this file was 9f3db13, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 6 years ago

Removed Recalculate button. Added check for 0 < eps for wavelengths so
the denominators are happy. Simplified signalling. SASVIEW-1142

  • Property mode set to 100644
File size: 9.6 KB
Line 
1# global
2import numpy as np
3import logging
4from PyQt5 import QtCore
5from PyQt5 import QtGui
6from PyQt5 import QtWidgets
7
8from periodictable import formula as Formula
9from periodictable.xsf import xray_energy, xray_sld
10from periodictable.nsf import neutron_scattering
11
12import sas.qtgui.Utilities.GuiUtils as GuiUtils
13
14from sas.qtgui.UI import main_resources_rc
15
16# Local UI
17from sas.qtgui.Calculators.UI.SldPanel import Ui_SldPanel
18
19from sas.qtgui.Utilities.GuiUtils import enum
20
21MODEL = enum(
22    'MOLECULAR_FORMULA',
23    'MASS_DENSITY',
24    'NEUTRON_WAVELENGTH',
25    'NEUTRON_SLD_REAL',
26    'NEUTRON_SLD_IMAG',
27    'XRAY_WAVELENGTH',
28    'XRAY_SLD_REAL',
29    'XRAY_SLD_IMAG',
30    'NEUTRON_INC_XS',
31    'NEUTRON_ABS_XS',
32    'NEUTRON_LENGTH',
33)
34
35class NeutronSldResult(object):
36    def __init__(self, neutron_wavelength, neutron_sld_real,
37                 neutron_sld_imag, neutron_inc_xs, neutron_abs_xs,
38                 neutron_length):
39
40        self.neutron_wavelength = neutron_wavelength
41        self.neutron_sld_real = neutron_sld_real
42        self.neutron_sld_imag = neutron_sld_imag
43        self.neutron_inc_xs = neutron_inc_xs
44        self.neutron_abs_xs = neutron_abs_xs
45        self.neutron_length = neutron_length
46
47class XraySldResult(object):
48    def __init__(self, xray_wavelength, xray_sld_real, xray_sld_imag):
49
50        self.xray_wavelength = xray_wavelength
51        self.xray_sld_real = xray_sld_real
52        self.xray_sld_imag = xray_sld_imag
53
54def neutronSldAlgorithm(molecular_formula, mass_density, neutron_wavelength):
55
56    (neutron_sld_real, neutron_sld_imag, _), (_, neutron_abs_xs, neutron_inc_xs), neutron_length = \
57        neutron_scattering(
58            compound=molecular_formula,
59            density=mass_density,
60            wavelength=neutron_wavelength)
61
62    SCALE = 1e-6
63
64    # neutron sld
65    scaled_neutron_sld_real = SCALE * neutron_sld_real
66    scaled_neutron_sld_imag = SCALE * abs(neutron_sld_imag)
67
68    return NeutronSldResult(neutron_wavelength, scaled_neutron_sld_real,
69                            scaled_neutron_sld_imag, neutron_inc_xs,
70                            neutron_abs_xs, neutron_length)
71
72def xraySldAlgorithm(molecular_formula, mass_density, xray_wavelength):
73
74    xray_sld_real, xray_sld_imag = xray_sld(
75            compound=molecular_formula,
76            density=mass_density,
77            wavelength=xray_wavelength)
78
79    SCALE = 1e-6
80
81    # xray sld
82    scaled_xray_sld_real = SCALE * xray_sld_real
83    scaled_xray_sld_imag = SCALE * abs(xray_sld_imag)
84
85
86    return XraySldResult(xray_wavelength, scaled_xray_sld_real,
87                         scaled_xray_sld_imag)
88
89
90class SldPanel(QtWidgets.QDialog):
91
92    def __init__(self, parent=None):
93        super(SldPanel, self).__init__()
94
95        self.manager = parent
96
97        self.setupUi()
98        # disable the context help icon
99        self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint)
100
101        self.setupModel()
102        self.setupMapper()
103
104    def _getOutputs(self):
105        return {
106            MODEL.NEUTRON_SLD_REAL: self.ui.editNeutronSldReal,
107            MODEL.NEUTRON_SLD_IMAG: self.ui.editNeutronSldImag,
108            MODEL.XRAY_SLD_REAL: self.ui.editXraySldReal,
109            MODEL.XRAY_SLD_IMAG: self.ui.editXraySldImag,
110            MODEL.NEUTRON_INC_XS: self.ui.editNeutronIncXs,
111            MODEL.NEUTRON_ABS_XS: self.ui.editNeutronAbsXs,
112            MODEL.NEUTRON_LENGTH: self.ui.editNeutronLength
113        }
114
115    def setupUi(self):
116        self.ui = Ui_SldPanel()
117        self.ui.setupUi(self)
118
119        # set validators
120        # TODO: GuiUtils.FormulaValidator() crashes with Qt5 - fix
121        #self.ui.editMolecularFormula.setValidator(GuiUtils.FormulaValidator(self.ui.editMolecularFormula))
122
123        # No need for recalculate
124        self.ui.recalculateButton.setVisible(False)
125
126        rx = QtCore.QRegExp("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?")
127        self.ui.editMassDensity.setValidator(QtGui.QRegExpValidator(rx, self.ui.editMassDensity))
128        self.ui.editNeutronWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editNeutronWavelength))
129        self.ui.editXrayWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editXrayWavelength))
130
131        # signals
132        self.ui.helpButton.clicked.connect(self.displayHelp)
133        self.ui.closeButton.clicked.connect(self.closePanel)
134        self.ui.recalculateButton.clicked.connect(self.calculateSLD)
135
136    def calculateSLD(self):
137        self.recalculateSLD()
138
139    def setupModel(self):
140        self.model = QtGui.QStandardItemModel(self)
141        self.model.setItem(MODEL.MOLECULAR_FORMULA , QtGui.QStandardItem())
142        self.model.setItem(MODEL.MASS_DENSITY      , QtGui.QStandardItem())
143        self.model.setItem(MODEL.NEUTRON_WAVELENGTH, QtGui.QStandardItem())
144        self.model.setItem(MODEL.XRAY_WAVELENGTH   , QtGui.QStandardItem())
145
146        for key in list(self._getOutputs().keys()):
147            self.model.setItem(key, QtGui.QStandardItem())
148
149        #self.model.dataChanged.connect(self.dataChanged)
150
151        self.ui.editMassDensity.textChanged.connect(self.recalculateSLD)
152        self.ui.editMolecularFormula.textChanged.connect(self.recalculateSLD)
153        self.ui.editNeutronWavelength.textChanged.connect(self.recalculateSLD)
154        self.ui.editXrayWavelength.textChanged.connect(self.recalculateSLD)
155
156        self.modelReset()
157
158    def setupMapper(self):
159        self.mapper = QtWidgets.QDataWidgetMapper(self)
160        self.mapper.setModel(self.model)
161        self.mapper.setOrientation(QtCore.Qt.Vertical)
162        self.mapper.addMapping(self.ui.editMolecularFormula , MODEL.MOLECULAR_FORMULA)
163        self.mapper.addMapping(self.ui.editMassDensity      , MODEL.MASS_DENSITY)
164        self.mapper.addMapping(self.ui.editNeutronWavelength, MODEL.NEUTRON_WAVELENGTH)
165        self.mapper.addMapping(self.ui.editXrayWavelength   , MODEL.XRAY_WAVELENGTH)
166
167        for key, edit in self._getOutputs().items():
168            self.mapper.addMapping(edit, key)
169
170        self.mapper.toFirst()
171
172    def dataChanged(self, top, bottom):
173        update = False
174        for index in range(top.row(), bottom.row() + 1):
175            if (index == MODEL.MOLECULAR_FORMULA) or (index == MODEL.MASS_DENSITY) or (index == MODEL.NEUTRON_WAVELENGTH) or (index == MODEL.XRAY_WAVELENGTH):
176                update = True
177
178        # calculation
179        if update:
180            self.recalculateSLD()
181
182    def recalculateSLD(self):
183        formula = self.ui.editMolecularFormula.text()
184        density = self.ui.editMassDensity.text()
185        neutronWavelength = self.ui.editNeutronWavelength.text()
186        xrayWavelength = self.ui.editXrayWavelength.text()
187
188        if not formula or not density:
189            return
190
191        def format(value):
192            return ("%-5.3g" % value).strip()
193
194        if neutronWavelength and float(neutronWavelength) > np.finfo(float).eps:
195            results = neutronSldAlgorithm(str(formula), float(density), float(neutronWavelength))
196
197            self.model.item(MODEL.NEUTRON_SLD_REAL).setText(format(results.neutron_sld_real))
198            self.model.item(MODEL.NEUTRON_SLD_IMAG).setText(format(results.neutron_sld_imag))
199            self.model.item(MODEL.NEUTRON_INC_XS).setText(format(results.neutron_inc_xs))
200            self.model.item(MODEL.NEUTRON_ABS_XS).setText(format(results.neutron_abs_xs))
201            self.model.item(MODEL.NEUTRON_LENGTH).setText(format(results.neutron_length))
202            self.model.item(MODEL.NEUTRON_LENGTH).setEnabled(True)
203            self.ui.editNeutronSldReal.setEnabled(True)
204            self.ui.editNeutronSldImag.setEnabled(True)
205            self.ui.editNeutronIncXs.setEnabled(True)
206            self.ui.editNeutronLength.setEnabled(True)
207            self.ui.editNeutronAbsXs.setEnabled(True)
208        else:
209            self.model.item(MODEL.NEUTRON_SLD_REAL).setText("")
210            self.model.item(MODEL.NEUTRON_SLD_IMAG).setText("")
211            self.model.item(MODEL.NEUTRON_INC_XS).setText("")
212            self.model.item(MODEL.NEUTRON_ABS_XS).setText("")
213            self.model.item(MODEL.NEUTRON_LENGTH).setText("")
214            self.ui.editNeutronSldReal.setEnabled(False)
215            self.ui.editNeutronSldImag.setEnabled(False)
216            self.ui.editNeutronIncXs.setEnabled(False)
217            self.ui.editNeutronLength.setEnabled(False)
218            self.ui.editNeutronAbsXs.setEnabled(False)
219
220        if xrayWavelength and float(xrayWavelength) > np.finfo(float).eps:
221            results = xraySldAlgorithm(str(formula), float(density), float(xrayWavelength))
222
223            self.model.item(MODEL.XRAY_SLD_REAL).setText(format(results.xray_sld_real))
224            self.model.item(MODEL.XRAY_SLD_IMAG).setText(format(results.xray_sld_imag))
225            self.ui.editXraySldReal.setEnabled(True)
226            self.ui.editXraySldImag.setEnabled(True)
227        else:
228            self.model.item(MODEL.XRAY_SLD_REAL).setText("")
229            self.model.item(MODEL.XRAY_SLD_IMAG).setText("")
230            self.ui.editXraySldReal.setEnabled(False)
231            self.ui.editXraySldImag.setEnabled(False)
232
233    def modelReset(self):
234        #self.model.beginResetModel()
235        try:
236            self.model.item(MODEL.MOLECULAR_FORMULA ).setText("H2O")
237            self.model.item(MODEL.MASS_DENSITY      ).setText("1.0")
238            self.model.item(MODEL.NEUTRON_WAVELENGTH).setText("6.0")
239            self.model.item(MODEL.XRAY_WAVELENGTH   ).setText("1.0")
240            self.recalculateSLD()
241        finally:
242            pass
243        #self.model.endResetModel()
244
245    def displayHelp(self):
246        location = "/user/qtgui/Calculators/sld_calculator_help.html"
247        self.manager.showHelp(location)
248
249
250    def closePanel(self):
251        """
252        close the window containing this panel
253        """
254        self.close()
255
Note: See TracBrowser for help on using the repository browser.