source: sasview/src/sas/qtgui/Perspectives/Corfunc/CorfuncPerspective.py @ 7b536da

Last change on this file since 7b536da was 7b536da, checked in by Adam Washington <adam.washington@…>, 7 years ago

Extrapolate out the curve for Corfunc

  • Property mode set to 100644
File size: 5.6 KB
Line 
1# global
2import sys
3from PyQt4 import QtCore
4from PyQt4 import QtGui
5from PyQt4 import QtWebKit
6
7from twisted.internet import threads
8from twisted.internet import reactor
9
10# sas-global
11from sas.qtgui.Plotting.PlotterData import Data1D
12import sas.qtgui.Utilities.GuiUtils as GuiUtils
13from sas.sascalc.corfunc.corfunc_calculator import CorfuncCalculator
14
15# local
16from UI.CorfuncPanel import Ui_CorfuncDialog
17# from InvariantDetails import DetailsDialog
18from CorfuncUtils import WIDGETS as W
19
20
21class CorfuncWindow(QtGui.QDialog, Ui_CorfuncDialog):
22    # The controller which is responsible for managing signal slots connections
23    # for the gui and providing an interface to the data model.
24    name = "Corfunc" # For displaying in the combo box
25    #def __init__(self, manager=None, parent=None):
26    def __init__(self, parent=None):
27        #super(InvariantWindow, self).__init__(parent)
28        super(CorfuncWindow, self).__init__()
29        self.setupUi(self)
30
31        self.setWindowTitle("Corfunc Perspective")
32
33        self.model = QtGui.QStandardItemModel(self)
34        self.communicate = GuiUtils.Communicate()
35        self._calculator = CorfuncCalculator()
36
37        # Connect buttons to slots.
38        # Needs to be done early so default values propagate properly.
39        self.setupSlots()
40
41        # Set up the model.
42        self.setupModel()
43
44        # Set up the mapper
45        self.setupMapper()
46
47    def setupSlots(self):
48        self.extractBtn.clicked.connect(self.action)
49        self.extrapolateBtn.clicked.connect(self.extrapolate)
50        self.transformBtn.clicked.connect(self.action)
51
52        self.calculateBgBtn.clicked.connect(self.calculateBackground)
53
54        self.hilbertBtn.clicked.connect(self.action)
55        self.fourierBtn.clicked.connect(self.action)
56
57        self.model.itemChanged.connect(self.modelChanged)
58
59    def setupModel(self):
60        self.model.setItem(W.W_QMIN,
61                           QtGui.QStandardItem("0"))
62        self.model.setItem(W.W_QMAX,
63                           QtGui.QStandardItem("0"))
64        self.model.setItem(W.W_QCUTOFF,
65                           QtGui.QStandardItem("0"))
66        self.model.setItem(W.W_BACKGROUND,
67                           QtGui.QStandardItem("0"))
68        self.model.setItem(W.W_TRANSFORM,
69                           QtGui.QStandardItem("Fourier"))
70
71    def modelChanged(self, item):
72        if item.row() == W.W_QMIN:
73            value = float(self.model.item(W.W_QMIN).text())
74            self.qMin.setValue(value)
75            self._calculator.lowerq = value
76        elif item.row() == W.W_QMAX:
77            value = float(self.model.item(W.W_QMAX).text())
78            self.qMax1.setValue(value)
79            self._calculator.upperq = (value, self._calculator.upperq[1])
80        elif item.row() == W.W_QCUTOFF:
81            value = float(self.model.item(W.W_QCUTOFF).text())
82            self.qMax2.setValue(value)
83            self._calculator.upperq = (self._calculator.upperq[0], value)
84        elif item.row() == W.W_BACKGROUND:
85            value = float(self.model.item(W.W_BACKGROUND).text())
86            self.bg.setValue(value)
87            self._calculator.background = value
88        else:
89            print("{} Changed".format(item))
90
91
92    def extrapolate(self):
93        params, extrapolation = self._calculator.compute_extrapolation()
94        self.guinierA.setValue(params['A'])
95        self.guinierB.setValue(params['B'])
96        self.porodK.setValue(params['K'])
97        self.porodSigma.setValue(params['sigma'])
98        print(params)
99
100
101
102    def setupMapper(self):
103        self.mapper = QtGui.QDataWidgetMapper(self)
104        self.mapper.setOrientation(QtCore.Qt.Vertical)
105        self.mapper.setModel(self.model)
106
107        self.mapper.addMapping(self.qMin, W.W_QMIN)
108        self.mapper.addMapping(self.qMax1, W.W_QMAX)
109        self.mapper.addMapping(self.qMax2, W.W_QCUTOFF)
110        self.mapper.addMapping(self.bg, W.W_BACKGROUND)
111
112        self.mapper.toFirst()
113
114    def calculateBackground(self):
115        bg = self._calculator.compute_background()
116        print(bg)
117        self.model.setItem(W.W_BACKGROUND, QtGui.QStandardItem(str(bg)))
118
119    def action(self):
120        print("Called an action!")
121        print(self.model)
122        print(self.mapper)
123
124    def allowBatch(self):
125        """
126        We cannot perform corfunc analysis in batch at this time.
127        """
128        return False
129
130    def setData(self, data_item, is_batch=False):
131        """
132        Obtain a QStandardItem object and dissect it to get Data1D/2D
133        Pass it over to the calculator
134        """
135        if not isinstance(data_item, list):
136            msg = "Incorrect type passed to the Corfunc Perpsective"
137            raise AttributeError(msg)
138
139        if not isinstance(data_item[0], QtGui.QStandardItem):
140            msg = "Incorrect type passed to the Corfunc Perspective"
141            raise AttributeError(msg)
142
143        self._model_item = data_item[0]
144        data = GuiUtils.dataFromItem(self._model_item)
145        self._calculator.lowerq = 1e-3
146        self._calculator.upperq = (2e-1, 3e-1)
147        self._calculator.set_data(data)
148
149        # self.model.item(WIDGETS.W_FILENAME).setData(QtCoreQVariant(self._model_item.text()))
150
151    def setClosable(self, value=True):
152        """
153        Allow outsiders close this widget
154        """
155        assert isinstance(value, bool)
156
157        self._allow_close = value
158
159
160if __name__ == "__main__":
161    app = QtGui.QApplication([])
162    import qt4reactor
163    # qt4reactor.install()
164    # DO NOT move the following import to the top!
165    # (unless you know what you're doing)
166    from twisted.internet import reactor
167    dlg = CorfuncWindow(reactor)
168    print(dlg)
169    dlg.show()
170    # print(reactor)
171    # reactor.run()
172    sys.exit(app.exec_())
Note: See TracBrowser for help on using the repository browser.