-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp_model_manager_threaded.py
350 lines (271 loc) · 11.5 KB
/
sp_model_manager_threaded.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from __future__ import division
import sys
import numpy as np
import astropy
from astropy.modeling import Parameter, Fittable1DModel, SummedCompositeModel
import astropy.modeling.functional_models as models
import sp_widget
from PyQt4.QtCore import *
from PyQt4.QtGui import *
# Main classes ------------------------------------------------
# Base class to be called by external apps.
class SpectralModelManager():
def __init__(self, model=None):
self._model = model
if not self._model:
self._model = []
def buildSplitPanel(self, model=None):
# override whatever model was passed to the constructor
if model:
self._model = model
# When called the first time, build the two trees.
# Subsequent calls must re-use the existing trees
# so as to preserve user selections and such.
if not hasattr(self, 'models_gui'):
self.models_gui = sp_widget._SpectralModelsGUI(self._model)
self._library_gui = sp_widget._SpectralLibraryGUI(self.models_gui, drop_down=False)
splitter = QSplitter();
splitter.addWidget(self.models_gui.window)
splitter.addWidget(self._library_gui.window)
splitter.setStretchFactor(0, 1)
return splitter
@property
def treeWidget(self):
return self.models_gui.window.treeView
@property
def components(self):
return self.models_gui.model.items
def spectrum(self, wave):
if len(self.components) > 0:
sum_of_models = SummedCompositeModel(self.components)
return sum_of_models(wave)
else:
return np.zeros(len(wave))
def addModel(self, model):
self.models_gui.updateModel(model)
# Returns model selected in the library window.
def getSelectedModel(self):
return self._library_gui.getSelectedModel()
# Returns model selected in the active components window.
def selectedModel(self):
return self.models_gui.getSelectedModel()
def modifyModels(self, new_components):
# This method must be called with a list of components that
# matches the existing components in the model manager active
# list. The method's purpose is to replace the parameter
# values of each component with the values of the paired
# component in the input list. Thus the lists have to match
# perfectly.
for i, c in enumerate(self.components):
nc = new_components[i]
c.parameters = nc.parameters
# modify the tree model so the fit results
# show immediately on the display.
for j, value in enumerate(c.parameters):
item = self.models_gui.model.item(i).child(j).child(0)
item.setData("value: " + str(value), role=Qt.DisplayRole)
# Derived app class that builds the QApplication and runs it as a modal dialog.
class SpectralModelManagerApp(SpectralModelManager):
def __init__(self, model=None):
app = QApplication(sys.argv)
splitter = self.buildSplitPanel(model)
splitter.show()
splitter.resize(550, 400)
splitter.setSizes([350, 250])
app.exec_()
# Functions and class that run the model manager in a separate
# thread so it can be called from an interactive session without
# blocking the interaction. Multiple models are supported; they
# are told apart by the 'name' parameter passed to the
# ModelManager constructor.
#
# Under MacOS with PyQt running in native Cocoa graphics mode,
# the code below will fail if run from a terminal window. It
# works when run from IPython though, or alternatively, when
# PyQt is built to run under a X11 server.
import pyqt_thread_helper
from pyqt_nonblock import QtNonblock
# GUI is a Singleton.
__dialog = None
# Selects threaded mode.
__threaded = False
__app = None
# Makes the GUI visible.
def display():
global __dialog
__dialog.emit(SIGNAL("show"))
# Adds a model manager to the GUI widget.
def add(manager, name=None):
global __dialog
__dialog.emit(SIGNAL("addManager"), manager.manager, name)
# Removes a model manager from the GUI widget.
def remove(name):
global __dialog
__dialog.emit(SIGNAL("removeManager"), name)
# Utility to generate text strings for the tabs in the
# tabbed pane, when the user fails to provide then.
__name_index = 0
def _getName(name):
if not name or type(name) != type(" "):
global __name_index
__name_index += 1
name = str(__name_index)
return name
class _ModelManagerWidget(QTabWidget):
def __init__(self, manager, name, parent=None, threaded=False):
QTabWidget.__init__(self, parent)
self._addManager(manager, name)
self.setGeometry(100, 100, 650, 400)
# I find this a more readable font.
font = QFont(self.font())
font.setPointSize(font.pointSize() + sp_widget.FONT_SIZE_INCREASE)
self.setFont(font)
# Because the widget and all its underlying PyQt stuff are running
# in a separate thread, the only way to pass anything to the widget is via
# Qt signal-slot events. Here we connect each supported signal to its
# associated callback slot.
self.connect(self, SIGNAL("addManager"), self._addManager)
self.connect(self, SIGNAL("removeManager"), self._removeManager)
self.connect(self, SIGNAL("show"), self._show)
self.connect(self, SIGNAL('triggered()'), self.closeEvent)
self.show()
def _addManager(self, manager, name):
widget = self._buildWidget(manager)
name = _getName(name)
if self.count() > 0:
for i in range(0,self.count()):
text = self.tabText(i)
if text == name:
self.removeTab(i)
self.insertTab(i, widget, name)
self.setCurrentWidget(widget)
return
self.addTab(widget, name)
self.setCurrentWidget(widget)
def _removeManager(self, name):
name = _getName(name)
if self.count() > 0:
for i in range(0,self.count()):
text = self.tabText(i)
if text == name:
self.removeTab(i)
# to avoid a potential memory leak and completely get
# rid of the manager, at this point one should go deep
# into the View (ultimately, to the QApplication instance)
# to clear any references to the manager. We shouldn't be
# dealing with View code (this is the whole reason behind
# MVC), so lets leave it as is and see how it plays out
# (see comment below).
return
def _buildWidget(self, manager):
main_panel = manager.buildMainPanel()
grid_layout = QGridLayout()
grid_layout.addWidget(main_panel, 0, 0)
exit_button = QPushButton('Close', self)
exit_button.setFocusPolicy(Qt.NoFocus)
self.connect(exit_button, SIGNAL('clicked()'), self._hide)
button_layout = QHBoxLayout()
button_layout.addStretch()
button_layout.addWidget(exit_button)
grid_layout.addLayout(button_layout, 1, 0)
result = QWidget()
result.setLayout(grid_layout)
return result
def _hide(self):
self.setVisible(False)
def _show(self):
self.setVisible(True)
# Overrides the default behavior so as to ignore window closing
# requests (such as from the platform-dependent red X button) and
# respond instead by just making the window invisible. This in
# fact renders the widget impossible to close except by terminating
# the Python interactive session. This is required by the nature of
# the underlying QApplication, when it is run in a secondary thread.
# Under those conditions, only one QApplication can exist, and only
# one can be created in any given Python session. A (desirable)
# side effect is that the GUI contents, including user selections,
# get preserved in between closed (invisible) and open (visible)
# states. A (undesirable) side effect is that this might potentially
# lead to memory leaks, since the GUI will prevent the garbage
# collector to rid of unused model manager instances from the user's
# interactive scope. In practice this might not be a problem unless
# the user is dealing with thousands of manager instances, in which
# case he/she shouldn't be using a GUI approach to begin with.
def closeEvent(self, event):
event.ignore()
self._hide()
# Adds non-blocking behavior under ipython. This is done by
# just subclassing QtNonblock.
class _IPythonModelManagerWidget(_ModelManagerWidget, QtNonblock):
def __init__(self, manager, name):
super(_IPythonModelManagerWidget, self).__init__(manager, name)
def _runGUIInThread(manager, name):
app = pyqt_thread_helper.getApplication()
global __dialog
if not __dialog:
__dialog = _ModelManagerWidget(manager.manager, name)
app.exec_()
def _runGUIDirectly(manager, name):
global __dialog
global __app
if not __dialog:
__app = QApplication([])
__dialog = _ModelManagerWidget(manager.manager, name)
return __dialog
def _runGUIInIPython(manager, name):
global __dialog
if not __dialog:
__dialog = _IPythonModelManagerWidget.start_gui(manager.manager, name)
def _is_running_from_ipython():
try:
__IPYTHON__
return True
except NameError:
return False
# The first time this function is called, it starts the thread to
# run the GUI, and passes to it the first instance of a model manager.
# Subsequent calls will just send signals to the GUI to add a new
# model manager and make the widget visible. Under ipython there is
# no need to start a separate thread though, since we use the qt_noblock
# mechanism to handle non-blocking behavior under the hood.
def _displayGUI(manager, name):
global __dialog
if not __dialog:
global __threaded
if __threaded:
if not _is_running_from_ipython():
pyqt_thread_helper.queueCommand(_runGUIInThread, arguments=[manager, name])
else:
_runGUIInIPython(manager, name)
else:
return _runGUIDirectly(manager, name)
else:
__dialog.emit(SIGNAL("addManager"), manager.manager, name)
__dialog.emit(SIGNAL("show"))
# Main class. The user creates instances of this to manage a
# spectral model in each instance. For now, each instance starts
# with an empty model; the user then uses the GUI to add to and
# modify the model.
class ModelManager(object):
def __init__(self, name=None, model=None):
self.manager = SpectralModelManager(model)
a = _displayGUI(self, name)
# Use delegation to decouple the ModelManager API from
# the GUI model manager API.
def add(self, model):
self.manager.addModel(model)
@property
def selected(self):
return self.manager.selectedModel()
@property
def components(self):
return self.manager.components
def spectrum(self, wave):
return self.manager.spectrum(wave)
if __name__ == "__main__":
# valid formats for ModelManager constructor call.
mm1 = ModelManager()
# mm1 = ModelManager(model=[models.Gaussian1D(1.,1.,1.)])
# mm1 = ModelManager('test1')
# mm1 = ModelManager(model=[models.Gaussian1D(1.,1.,1.),models.Lorentz1D(1.,1.,1.)])
# mm1 = ModelManager("test2", [models.Gaussian1D(1.,1.,1.),models.Lorentz1D(1.,1.,1.)])