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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since c416a17 was cd2cc745, checked in by Piotr Rozyczko <rozyczko@…>, 8 years ago

Workaround for the resource file requirement in each UI directory

  • Property mode set to 100644
File size: 8.1 KB
Line 
1# global
2import logging
3from PyQt4 import QtGui, QtCore
4
5from periodictable import formula as Formula
6from periodictable.xsf import xray_energy, xray_sld_from_atoms
7from periodictable.nsf import neutron_scattering
8
9from sas.qtgui.Utilities.GuiUtils import FormulaValidator
10from sas.qtgui.UI import main_resources_rc
11
12# Local UI
13from sas.qtgui.Calculators.UI.SldPanel import Ui_SldPanel
14
15def enum(*sequential, **named):
16    enums = dict(zip(sequential, range(len(sequential))), **named)
17    return type('Enum', (), enums)
18
19MODEL = enum(
20    'MOLECULAR_FORMULA',
21    'MASS_DENSITY',
22    'WAVELENGTH',
23    'NEUTRON_SLD_REAL',
24    'NEUTRON_SLD_IMAG',
25    'CU_KA_SLD_REAL',
26    'CU_KA_SLD_IMAG',
27    'MO_KA_SLD_REAL',
28    'MO_KA_SLD_IMAG',
29    'NEUTRON_INC_XS',
30    'NEUTRON_ABS_XS',
31    'NEUTRON_LENGTH',
32)
33
34class SldResult(object):
35    def __init__(self, molecular_formula, mass_density, wavelength,
36        neutron_sld_real, neutron_sld_imag,
37        cu_ka_sld_real, cu_ka_sld_imag,
38        mo_ka_sld_real, mo_ka_sld_imag,
39        neutron_inc_xs, neutron_abs_xs, neutron_length):
40
41        self.molecular_formula = molecular_formula
42        self.mass_density = mass_density
43        self.wavelength = wavelength
44        self.neutron_sld_real = neutron_sld_real
45        self.neutron_sld_imag = neutron_sld_imag
46        self.cu_ka_sld_real = cu_ka_sld_real
47        self.cu_ka_sld_imag = cu_ka_sld_imag
48        self.mo_ka_sld_real = mo_ka_sld_real
49        self.mo_ka_sld_imag = mo_ka_sld_imag
50        self.neutron_inc_xs = neutron_inc_xs
51        self.neutron_abs_xs = neutron_abs_xs
52        self.neutron_length = neutron_length
53
54def sldAlgorithm(molecular_formula, mass_density, wavelength):
55
56    sld_formula = Formula(molecular_formula, density=mass_density)
57
58    def calculate_sld(formula):
59        if len(formula.atoms) != 1:
60            raise NotImplementedError()
61        energy = xray_energy(formula.atoms.keys()[0].K_alpha)
62        return xray_sld_from_atoms(
63            sld_formula.atoms,
64            density=mass_density,
65            energy=energy)
66
67    cu_real, cu_imag = calculate_sld(Formula("Cu"))
68    mo_real, mo_imag = calculate_sld(Formula("Mo"))
69
70    (sld_real, sld_imag, _), (_, neutron_abs_xs, neutron_inc_xs), neutron_length = \
71        neutron_scattering(
72            compound=molecular_formula,
73            density=mass_density,
74            wavelength=wavelength)
75
76    SCALE = 1e-6
77
78    # neutron sld
79    neutron_sld_real = SCALE * sld_real
80    neutron_sld_imag = SCALE * abs(sld_imag)
81
82    # Cu sld
83    cu_ka_sld_real = SCALE * cu_real
84    cu_ka_sld_imag = SCALE * abs(cu_imag)
85
86    # Mo sld
87    mo_ka_sld_real = SCALE * mo_real
88    mo_ka_sld_imag = SCALE * abs(mo_imag)
89
90    return SldResult(
91        molecular_formula, mass_density, wavelength,
92        neutron_sld_real, neutron_sld_imag,
93        cu_ka_sld_real, cu_ka_sld_imag,
94        mo_ka_sld_real, mo_ka_sld_imag,
95        neutron_inc_xs, neutron_abs_xs, neutron_length)
96
97
98class SldPanel(QtGui.QDialog):
99
100    def __init__(self, parent=None):
101        super(SldPanel, self).__init__()
102
103        self.manager = parent
104
105        self.setupUi()
106        self.setupModel()
107        self.setupMapper()
108
109    def _getOutputs(self):
110        return {
111            MODEL.NEUTRON_SLD_REAL: self.ui.editNeutronSldReal,
112            MODEL.NEUTRON_SLD_IMAG: self.ui.editNeutronSldImag,
113            MODEL.CU_KA_SLD_REAL: self.ui.editCuKaSldReal,
114            MODEL.CU_KA_SLD_IMAG: self.ui.editCuKaSldImag,
115            MODEL.MO_KA_SLD_REAL: self.ui.editMoKaSldReal,
116            MODEL.MO_KA_SLD_IMAG: self.ui.editMoKaSldImag,
117            MODEL.NEUTRON_INC_XS: self.ui.editNeutronIncXs,
118            MODEL.NEUTRON_ABS_XS: self.ui.editNeutronAbsXs,
119            MODEL.NEUTRON_LENGTH: self.ui.editNeutronLength
120        }
121
122    def setupUi(self):
123        self.ui = Ui_SldPanel()
124        self.ui.setupUi(self)
125
126        # set validators
127        self.ui.editMolecularFormula.setValidator(FormulaValidator(self.ui.editMolecularFormula))
128
129        rx = QtCore.QRegExp("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?")
130        self.ui.editMassDensity.setValidator(QtGui.QRegExpValidator(rx, self.ui.editMassDensity))
131        self.ui.editWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editWavelength))
132
133        # signals
134        self.ui.buttonBox.button(QtGui.QDialogButtonBox.Reset).clicked.connect(self.modelReset)
135        self.ui.buttonBox.button(QtGui.QDialogButtonBox.Help).clicked.connect(self.displayHelp)
136
137    def setupModel(self):
138        self.model = QtGui.QStandardItemModel(self)
139        self.model.setItem(MODEL.MOLECULAR_FORMULA, QtGui.QStandardItem())
140        self.model.setItem(MODEL.MASS_DENSITY     , QtGui.QStandardItem())
141        self.model.setItem(MODEL.WAVELENGTH       , QtGui.QStandardItem())
142
143        for key in self._getOutputs().keys():
144            self.model.setItem(key, QtGui.QStandardItem())
145
146        QtCore.QObject.connect(
147            self.model,
148            QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"),
149            self.dataChanged)
150
151        self.modelReset()
152
153    def setupMapper(self):
154        self.mapper = QtGui.QDataWidgetMapper(self)
155        self.mapper.setModel(self.model)
156        self.mapper.setOrientation(QtCore.Qt.Vertical)
157
158        self.mapper.addMapping(self.ui.editMolecularFormula, MODEL.MOLECULAR_FORMULA)
159        self.mapper.addMapping(self.ui.editMassDensity     , MODEL.MASS_DENSITY)
160        self.mapper.addMapping(self.ui.editWavelength      , MODEL.WAVELENGTH)
161
162        for key, edit in self._getOutputs().iteritems():
163            self.mapper.addMapping(edit, key)
164
165        self.mapper.toFirst()
166
167    def dataChanged(self, top, bottom):
168        update = False
169        for index in xrange(top.row(), bottom.row() + 1):
170            if (index == MODEL.MOLECULAR_FORMULA) or (index == MODEL.MASS_DENSITY) or (index == MODEL.WAVELENGTH):
171                update = True
172
173        # calcualtion
174        if update:
175            formula = self.model.item(MODEL.MOLECULAR_FORMULA).text()
176            density = self.model.item(MODEL.MASS_DENSITY).text()
177            wavelength = self.model.item(MODEL.WAVELENGTH).text()
178            if len(formula) > 0 and len(density) > 0 and len(wavelength) > 0:
179                try:
180                    results = sldAlgorithm(str(formula), float(density), float(wavelength))
181
182                    def format(value):
183                        return ("%-5.3g" % value).strip()
184
185                    self.model.item(MODEL.NEUTRON_SLD_REAL).setText(format(results.neutron_sld_real))
186                    self.model.item(MODEL.NEUTRON_SLD_IMAG).setText(format(results.neutron_sld_imag))
187
188                    self.model.item(MODEL.CU_KA_SLD_REAL).setText(format(results.cu_ka_sld_real))
189                    self.model.item(MODEL.CU_KA_SLD_IMAG).setText(format(results.cu_ka_sld_imag))
190
191                    self.model.item(MODEL.MO_KA_SLD_REAL).setText(format(results.mo_ka_sld_real))
192                    self.model.item(MODEL.MO_KA_SLD_IMAG).setText(format(results.mo_ka_sld_imag))
193
194                    self.model.item(MODEL.NEUTRON_INC_XS).setText(format(results.neutron_inc_xs))
195                    self.model.item(MODEL.NEUTRON_ABS_XS).setText(format(results.neutron_abs_xs))
196                    self.model.item(MODEL.NEUTRON_LENGTH).setText(format(results.neutron_length))
197
198                    return
199           
200                except Exception as e:
201                    pass
202
203            for key in self._getOutputs().keys():
204                self.model.item(key).setText("")
205
206    def modelReset(self):
207        #self.model.beginResetModel()
208        try:
209            self.model.item(MODEL.MOLECULAR_FORMULA).setText("H2O")
210            self.model.item(MODEL.MASS_DENSITY     ).setText("1")
211            self.model.item(MODEL.WAVELENGTH       ).setText("6")
212        finally:
213            pass
214            #self.model.endResetModel()
215
216    def displayHelp(self):
217        try:
218            location = self.manager.HELP_DIRECTORY_LOCATION + \
219                "/user/sasgui/perspectives/calculator/sld_calculator_help.html"
220            self.manager._helpView.load(QtCore.QUrl(location))
221            self.manager._helpView.show()
222        except AttributeError:
223            # No manager defined - testing and standalone runs
224            pass
225
Note: See TracBrowser for help on using the repository browser.