-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp_widget.py
1129 lines (914 loc) · 42.3 KB
/
sp_widget.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import division
import os
import math
import re
import numpy as np
from astropy.modeling import Parameter, Fittable1DModel
import signal_slot
import models_registry
import sp_adjust
import sp_model_io
from PyQt4.QtCore import *
from PyQt4.QtGui import *
AVAILABLE_COMPONENTS = "Available components"
FONT_SIZE_INCREASE = 2
# To memorize last visited directory.
_model_directory = os.environ["HOME"]
# Builds a compound model by adding together all components in
# the list. This is used when the input offers no clue on how
# to combine the components.
def _buildSummedCompoundModel(components):
if len(components) < 1:
return None
result = components[0]
if len(components) > 1:
for component in components[1:]:
result += component
return result
# Finds the level at which a tree is being selected.
# Also finds the index for the zero-th level parent
# that is associated with the selected item.
def findLevelAndIndex(indices):
if len(indices) <= 0:
return -1, -1
level = 0
index0 = -1
if len(indices) > 0:
index0 = indices[0]
while index0.parent().isValid():
index0 = index0.parent()
level += 1
return level, index0
# Subclasses QTreeView in order to detect selections on tree elements
# and enable/disable other GUI elements accordingly.
class _MyQTreeView(QTreeView):
def __init__(self, model):
super(_MyQTreeView, self).__init__()
# Model is required so we can have access to all
# rows, not just the currently selected one.
self.model = model
# Default behavior is the same as the superclass'.
self.b_up = None
self.b_down = None
self.b_delete = None
# By providing button instances, the specialized
# behavior is triggered on.
def setButtons(self, b_up, b_down, b_delete, b_save, model):
self.b_up = b_up
self.b_down = b_down
self.b_delete = b_delete
self.b_save = b_save
self.b_up.setEnabled(False)
self.b_down.setEnabled(False)
self.b_delete.setEnabled(False)
self.b_save.setEnabled(model and len(model.items) > 0)
# Overrides QTreeView to provide
# sensitivity to selection events.
def selectionChanged(self, selected, deselected):
# IndexError may happen in normal GUI usage and it's normal.
try:
self._handleTreeSelectionEvent(selected, deselected)
except IndexError:
pass
# Overrides QTreeView to capture and handle a data changed event.
# These data changes occur in the model associated with the tree,
# when a Data object gets changed, such as when the user types in
# a new value for a Parameter instance.
def dataChanged(self, top, bottom):
self.emit(SIGNAL("dataChanged"), 0)
super(_MyQTreeView, self).dataChanged(top, bottom)
# Here is the logic to gray out buttons based on context.
def _handleTreeSelectionEvent(self, selected, deselected):
# only acts if actual button instances exist.
if self.b_up and self.b_down and self.b_delete:
# nothing is selected, so no button action is allowed.
if selected.count() == 0 or selected.count() > 1:
self.b_up.setEnabled(False)
self.b_down.setEnabled(False)
self.b_delete.setEnabled(False)
# one row is selected, but behavior depends
# on how many rows there are in the tree.
else:
# if a row is selected, it can always be deleted.
self.b_delete.setEnabled(True)
if self.model.rowCount() == 1:
# only one row in tree, thus only deletion is allowed.
self.b_up.setEnabled(False)
self.b_down.setEnabled(False)
else:
# two or more rows in tree; see which one is selected.
# Watch out for the level though, must be root level.
level, index0 = findLevelAndIndex(selected.indexes())
row = index0.row()
if row > 0 and row < (self.model.rowCount() - 1):
# selected row is in the middle; can be moved up or down.
self.b_up.setEnabled(True)
self.b_down.setEnabled(True)
elif row == 0:
# selected row is top row; can only be moved down.
self.b_up.setEnabled(False)
self.b_down.setEnabled(True)
else:
# selected row is bottom row; can only be moved up.
self.b_up.setEnabled(True)
self.b_down.setEnabled(False)
# Base window that holds a tree widget that supports contextual menus.
# It needs an instance of QStandardItemModel in order to build the tree.
class _BaseWindow(QWidget):
def __init__(self, model):
QWidget.__init__(self)
self.model = model
font = QFont(self.font())
font.setPointSize(font.pointSize() + FONT_SIZE_INCREASE)
self.setFont(font)
QToolTip.setFont(font)
self.treeView = _MyQTreeView(self.model)
self.treeView.setContextMenuPolicy(Qt.CustomContextMenu)
self.treeView.customContextMenuRequested.connect(self.openMenu)
self.treeView.setModel(self.model)
grid_layout = QGridLayout()
grid_layout.addWidget(self.treeView, 0, 0)
# the following are not used by this class but provide
# places where sub classes can put in their own widgets.
self.expression_layout = QHBoxLayout()
grid_layout.addLayout(self.expression_layout, 1, 0)
self.button_layout = QHBoxLayout()
self.button_layout.addStretch()
grid_layout.addLayout(self.button_layout, 2, 0)
self.setLayout(grid_layout)
def openMenu(self, position):
raise NotImplementedError
# Returns the selected model.
def getSelectedModel(self):
indexes = self.treeView.selectedIndexes()
if len(indexes) > 0:
level, index = self.findTreeLevel()
if len(indexes) > 0:
data = self.model.item(index.row()).item
return data
return None
# Returns the level at which the tree is being selected.
# Also returns the index for the zero-th level parent that
# is associated with the selected item.
def findTreeLevel(self):
indices = self.treeView.selectedIndexes()
return findLevelAndIndex(indices)
# Connects a slot to the "triggered" signal in a QWidget.
# This is used to associate callbacks to contextual menus.
def createAction(self, widget, text, slot=None, shortcut=None,
icon=None, tip=None, checkable=False):
action = QAction(text, widget)
action.setCheckable(checkable)
if icon is not None:
action.setIcon(QIcon("/%s.png" % icon))
if shortcut is not None:
action.setShortcut(shortcut)
if tip is not None:
action.setToolTip(tip)
action.setStatusTip(tip)
if slot is not None:
action.triggered.connect(slot)
return action
# Window with the active spectral components -------------------------------------------
class _SpectralModelsGUI(object):
def __init__(self, components):
self.model = ActiveComponentsModel(components, name="Active components")
self.window = _SpectralModelsWindow(self.model)
self.mapper = QDataWidgetMapper()
self.mapper.setModel(self.model)
self.mapper.addMapping(self.window.treeView, 0)
# TODO use QDataWidgetMapper
# this violation of MVC design principles is necessary
# so our model manager class can work with the modified
# code in modelmvc.py. This probably could be done via
# the QDataWidgetMapper stuff instead.
self.model.setWindow(self.window)
@property
def model(self):
return self._model
@model.setter
def model(self, model):
self._model = model
def updateModel(self, component):
self._model.addOneElement(component)
# new components are added to existing compound model
if hasattr(self._model, 'compound_model'):
self._model.compound_model = self._model.compound_model + component
else:
self._model.compound_model = component
self.window.updateExpressionField(self._model.compound_model )
self.window.emit(SIGNAL("treeChanged"), 0)
def getSelectedModel(self):
return self.window.getSelectedModel()
class _SpectralModelsWindow(_BaseWindow):
def __init__(self, model):
super(_SpectralModelsWindow, self).__init__(model)
# Contextual menus do not always work under ipython
# non-block mode. These buttons are an alternative
# way of implementing the same actions.
up_button = QPushButton('Up', self)
up_button.setFocusPolicy(Qt.NoFocus)
up_button.setToolTip('Moves selected component up')
self.connect(up_button, SIGNAL('clicked()'), self.moveComponentUp)
self.button_layout.addWidget(up_button)
down_button = QPushButton('Down', self)
down_button.setFocusPolicy(Qt.NoFocus)
down_button.setToolTip('Moves selected component down')
self.connect(down_button, SIGNAL('clicked()'), self.moveComponentDown)
self.button_layout.addWidget(down_button)
delete_button = QPushButton('Delete', self)
delete_button.setFocusPolicy(Qt.NoFocus)
delete_button.setToolTip('Remove selected component from model manager instance')
self.connect(delete_button, SIGNAL('clicked()'), self.deleteComponent)
self.button_layout.addWidget(delete_button)
# read and save buttons are not accessible from contextual menus.
self.read_button = QPushButton('Read', self)
self.read_button.setFocusPolicy(Qt.NoFocus)
self.read_button.setToolTip('Rad model from file.')
self.button_layout.addWidget(self.read_button)
self.save_button = QPushButton('Save', self)
self.save_button.setFocusPolicy(Qt.NoFocus)
self.save_button.setToolTip('Save model to file.')
self.button_layout.addWidget(self.save_button)
# expression text field
self.expression_field = QLineEdit('Expression goes here blah b;ah b;ah', self)
self.expression_field.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
self.expression_field.setToolTip('Model expression.')
self.expression_layout.addWidget(self.expression_field)
self.expression_field.setFocusPolicy(Qt.NoFocus) # remove to enable editing
if hasattr(model, 'compound_model'):
compound_model = model.compound_model
self.updateExpressionField(compound_model)
# setup to gray out buttons based on context.
self.treeView.setButtons(up_button, down_button, delete_button, self.save_button, model)
# connect signals.
self.connect(self.save_button, SIGNAL('clicked()'), self.saveModel)
self.connect(self.read_button, SIGNAL('clicked()'), self.readModel)
self.connect(self, SIGNAL("treeChanged"), self._setSaveButtonLooks)
# this will change the Save button appearance depending on how many
# components are stored in the current active list. For now we don't
# allow single components to be saved to file, since the concept
# itself is mostly useful for saving complex compound models. It
# remains to be seen if this assumption will hold under user scrutiny.
def _setSaveButtonLooks(self):
self.save_button.setEnabled(len(self.model.items) > 0)
def updateExpressionField(self, compound_model):
expression = ""
if compound_model and hasattr(compound_model, '_format_expression'):
expression = compound_model._format_expression()
self.expression_field.setText(expression)
# contextual menu
def openMenu(self, position):
self.position = position
level, index = self.findTreeLevel()
if index.isValid():
menu = QMenu()
menu.addAction(self.createAction(menu, "Delete component", self.deleteComponent))
row = index.row()
if row > 0:
menu.addAction(self.createAction(menu, "Move component up", self.moveComponentUp))
if row < self.model.rowCount() - 1:
menu.addAction(self.createAction(menu, "Move component down", self.moveComponentDown))
# We would use code like this in case we need contextual
# menus at other levels in the tree besides the first level.
#
# level = self.findTreeLevel()
# if level == 0:
# menu.addAction(self.createAction(menu, "Delete component", self.deleteComponent))
# row = self.treeView.indexAt(self.position).row()
# if row > 0:
# menu.addAction(self.createAction(menu, "Move component up", self.moveComponentUp))
# if row < self.model.rowCount()-1:
# menu.addAction(self.createAction(menu, "Move component down", self.moveComponentDown))
# elif level == 1:
# placeholder for edit parameter functionality.
# elif level == 2:
# menu.addAction(self.createAction(menu, "Edit parameter value", self.editParameterValue))
menu.exec_(self.treeView.viewport().mapToGlobal(position))
# Callbacks for contextual menus and buttons. The 'treeChanged'
# signal is emitted so caller code can be notified when buttons
# and menus are activated and the trees get re-arranged.
def deleteComponent(self):
level, index = self.findTreeLevel()
if level >= 0:
self.model.takeRow(index.row())
self.treeView.clearSelection()
self.emit(SIGNAL("treeChanged"), index.row())
def moveComponentUp(self):
level, index = self.findTreeLevel()
if level >= 0 and index.row() > 0:
is_expanded = self.treeView.isExpanded(index)
items = self.model.takeRow(index.row())
self.model.insertRow(index.row() - 1, items)
index_above = self.treeView.indexAbove(index)
self.treeView.setExpanded(index_above, is_expanded)
self.treeView.clearSelection()
self.emit(SIGNAL("treeChanged"), index.row())
def moveComponentDown(self):
level, index = self.findTreeLevel()
if level >= 0 and index.row() < self.model.rowCount() - 1:
is_expanded = self.treeView.isExpanded(index)
items = self.model.takeRow(index.row())
self.model.insertRow(index.row() + 1, items)
index_below = self.treeView.indexBelow(index)
self.treeView.setExpanded(index_below, is_expanded)
self.treeView.clearSelection()
self.emit(SIGNAL("treeChanged"), index.row())
def saveModel(self):
global _model_directory # retains memory of last visited directory
sp_model_io.saveModelToFile(self, self.model.compound_model, _model_directory)
def readModel(self):
global _model_directory # retains memory of last visited directory
fname = QFileDialog.getOpenFileName(self, 'Open file', _model_directory)
compound_model, _model_directory = sp_model_io.buildModelFromFile(fname)
expression = ""
if compound_model:
if hasattr(compound_model, '_submodels'):
for model in compound_model:
self.model.addOneElement(model)
else:
self.model.addOneElement(compound_model)
if hasattr(compound_model, '_format_expression'):
expression = compound_model._format_expression()
self.emit(SIGNAL("treeChanged"), 0)
self.expression_field.setText(expression)
# Parameter values can be edited directly from their QStandardItem
# representation. The code below (still incomplete) is an attempt
# to use contextual menus for the same purpose.
#
# def editParameterValue(self):
# parameter_index = self.treeView.indexAt(self.position).parent()
# parameter_row = parameter_index.row()
#
# function_row = parameter_index.parent().row()
# item = self.model.item(function_row)
# function = item.getDataItem()
#
# for param_name in function.param_names:
# if function._param_orders[param_name] == parameter_row:
# break
# parameter = function.__getattribute__(param_name)
#
# print "AstropyModelingTest - line 163: ", parameter.value
# Window with the spectral component library ----------------------------------
class _SpectralLibraryGUI(object):
# Attempt to get classes directly from the models module.
# Doesn't work, need to get classes from Model registry instead.
# def __init__(self, models_gui):
# data = []
# for key in models.__all__:
# function_metaclass = models.__dict__[key]
# if issubclass(function_metaclass, Fittable1DModel):
# data.append(function_metaclass)
#
# self.window = LibraryWindow(data, models_gui)
def __init__(self, models_gui, x, y, drop_down=True):
data = []
keys = sorted(models_registry.registry.keys())
for key in keys:
function = models_registry.registry[key]
# redundant test for now, but needed in case we
# switch to introspection from the models registry.
# if issubclass(function.__class__, Fittable1DModel) or \
# issubclass(function.__class__, PolynomialModel):
# TODO Polynomials do not carry internal instances of
# Parameter. This makes the code in this module unusable,
# since it relies on the existence of parameter instances
# in the spectral model functions. To make it usable, we
# need to add special handling code that can get and set
# polynomial coefficients. Thus suggests that polynomials
# were not designed to be mixed in with instances of
# Fittable1DModel. This could make sense from a software
# design standpoint, but it is hardly what the use cases
# seem to imply.
if issubclass(function.__class__, Fittable1DModel):
data.append(function)
self.model = SpectralComponentsModel(name=AVAILABLE_COMPONENTS)
self.model.addItems(data)
# Look-and-feel can be based either on a split pane or a drop down menu.
if drop_down:
self.window = _LibraryComboBox(self.model, models_gui, x, y)
else:
self.window = _LibraryWindow(self.model, models_gui, x, y)
def getSelectedModel(self):
return self.window.getSelectedModel()
def setArrays(self, x, y):
self.window.setArrays(x, y)
class _LibraryWindow(_BaseWindow):
def __init__(self, model, models_gui, x, y):
super(_LibraryWindow, self).__init__(model)
self.models_gui = models_gui
# numpy arrays used to instantiate functions.
self.x = x
self.y = y
# Contextual menus do not always work under ipython
# non-block mode. The Add button is an alternative
# way of implementing the same action.
add_button = QPushButton('Add', self)
add_button.setFocusPolicy(Qt.NoFocus)
add_button.setToolTip('Adds selected component to active model')
self.connect(add_button, SIGNAL('clicked()'), self.addComponent)
self.button_layout.addWidget(add_button)
# callback for the Add button
def addComponent(self):
function = self.getSelectedModel()
sp_adjust.adjust(function, self.x, self.y)
self._addComponentToActive(function)
self.treeView.clearSelection()
# contextual menu.
def openMenu(self, position):
index = self.treeView.indexAt(position)
if index.isValid():
menu = QMenu()
menu.addAction(self.tr("Add component"))
menu.exec_(self.treeView.viewport().mapToGlobal(position))
# no need to add an action to this menu since it has only one
# element. Just do the action straight away.
item = self.model.item(index.row())
if item:
function = item.getDataItem()
self._addComponentToActive(function)
self.treeView.clearSelection()
# This is an attempt to instantiate from the class registry.
#
# param_names = inspect.getargspec(meta.__init__)[0]
# param_values = np.ones(len(param_names)-1)
#
# inst = models_registry[name](param_values)
#
# cls = type.__new__(type(meta), name, (Fittable1DModel,), {'param_names': param_names[1:]})
# cls = type(name, (Fittable1DModel,), {'param_names': param_names[1:]})
#
# args = {}
# i = 0
# for pn in param_names[1:]:
# args[pn] = param_values[i]
# i += 1
#
# inst = cls.__init__(**args)
def setArrays(self, x, y):
self.x = x
self.y = y
# Adds the selected spectral model component to the active model.
def _addComponentToActive(self, component):
name = models_registry.get_component_name(component)
self.finalizeAddingComponent(name)
def finalizeAddingComponent(self, name):
# this should perhaps be done by instantiating from a
# class. We instead resort to brute force and copy the
# instance instead. It works.....
if name in models_registry.registry:
component = models_registry.registry[name].copy()
if component:
sp_adjust.adjust(component, self.x, self.y)
self.models_gui.updateModel(component)
class _LibraryComboBox(QComboBox, _LibraryWindow):
def __init__(self, model, models_gui, x, y):
QComboBox.__init__(self)
_LibraryWindow.__init__(self, model, models_gui, x, y)
self.addItem(AVAILABLE_COMPONENTS)
nrows = self.model.rowCount()
for i in range(nrows):
index = self.model.index(i, 0)
data = self.model.data(index)
self.addItem(data.toString())
self.activated.connect(self._addSelectedComponent)
# Adds the selected spectral components.
def _addSelectedComponent(self):
name = str(self.currentText())
self.finalizeAddingComponent(name)
# The MVC Model classes -----------------------------------------
# Item classes
# Base item is a QStandardItem with the ability to directly
# hold a reference to the spectral object being represented
# in the tree.
class SpectralComponentItem(QStandardItem):
def __init__(self, name):
QStandardItem.__init__(self)
if name is None:
name = "None"
self.setData(name, role=Qt.DisplayRole)
self.setEditable(False)
def setDataItem(self, item):
self.item = item
def getDataItem(self):
return self.item
# Value item specializes the base item to make it editable.
# or checkable. The slot connected to the tree model's
# itemChanged signal must be able to differentiate among the
# several possible items, using the 'type' attribute and the
# 'isCheckable' property.
class SpectralComponentValueItem(SpectralComponentItem):
def __init__(self, parameter, type, checkable=False, editable=True):
self.parameter = parameter
self.type = type
# boolean attributes display attribute
# value via a checkbox, not text.
id_str = type
if not checkable:
id_str = type + ": " + str(getattr(self.parameter, type))
SpectralComponentItem.__init__(self, id_str)
self.setEditable(editable)
self.setCheckable(checkable)
# checkbox setup.
if checkable and getattr(self.parameter, type):
self.setCheckState(Qt.Checked)
# Tied item specializes the base item to handle the specifics
# of a callable tie. The slot connected to the tree model's
# itemChanged signal must be able to differentiate among the
# several possible items, using the 'type' attribute and the
# 'isCheckable' property. This is not necessary for now, since
# this item type is being defined as non-editable. For now, the
# only way for the user to modify a tie is to directly edit an
# importable file with the model definition.
class SpectralComponentTiedItem(SpectralComponentItem):
def __init__(self, parameter):
self.parameter = parameter
self.type = "tied"
tie = getattr(self.parameter, self.type)
id_str = self.type + ": " + sp_model_io.get_tie_text(tie)
SpectralComponentItem.__init__(self, id_str)
self.setEditable(False) # for now!!
self.setCheckable(False)
# Model classes
# This class provides the base model for both the active
# and the library windows. The base model handles the
# tree's first level, where the component names are held.
class SpectralComponentsModel(QStandardItemModel):
def __init__(self, name):
QStandardItemModel.__init__(self)
self.setHorizontalHeaderLabels([name])
def addItems(self, elements):
if hasattr(elements, '__getitem__'):
for element in elements:
self.addOneElement(element)
else:
self.addOneElement(elements)
def addOneElement(self, element):
name = models_registry.get_component_name(element)
self.addToModel(name, element)
def addToModel(self, name, element):
item = SpectralComponentItem(name)
item.setDataItem(element)
parent = self.invisibleRootItem()
parent.appendRow(item)
# RE pattern to decode scientific and floating point notation.
_pattern = re.compile(r"[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?")
def _float_check(value):
""" Checks for a valid float in either scientific or floating point notation"""
substring = _pattern.findall(str(value))
if substring:
number = float(substring[0])
if len(substring) > 1:
number *= math.pow(10., int(substring[1]))
return number
else:
return False
# This class adds to the base model class the ability to handle
# two additional tree levels. These levels hold respectively
# the parameter names of each component, and each parameter's
# editable attributes.
class ActiveComponentsModel(SpectralComponentsModel):
def __init__(self, components, name):
SpectralComponentsModel.__init__(self, name)
if components:
self.compound_model = components
self.addItems(self.compound_model)
self.itemChanged.connect(self._onItemChanged)
# TODO use QDataWidgetMapper
# this violation of MVC design principles is necessary
# so our model manager class can work with the modified
# code in modelmvc.py. This probably could be done via
# the QDataWidgetMapper stuff instead.
def setWindow(self, window):
self._window = window
def addToModel(self, name, element):
# add component to tree root
if element.name:
item = SpectralComponentItem(name + " (" + str(element.name) + ")")
else:
item = SpectralComponentItem(name)
item.setDataItem(element)
parent = self.invisibleRootItem()
parent.appendRow(item)
nameItem = SpectralComponentValueItem(element, "name")
nameItem.setDataItem(element.name)
# nameItem.setEditable(True)
item.appendRow(nameItem)
# now add parameters to component in tree.
for e in element.param_names:
par = element.__getattribute__(e)
if isinstance(par, Parameter):
# add parameter. Parameter name is followed
# by its value when displaying in tree.
parItem = SpectralComponentItem(par.name + ": " + str(par.value))
parItem.setDataItem(par)
item.appendRow(parItem)
# add parameter value and other attributes to parameter element.
valueItem = SpectralComponentValueItem(par, "value")
valueItem.setDataItem(par.value)
parItem.appendRow(valueItem)
minItem = SpectralComponentValueItem(par, "min")
minItem.setDataItem(par.min)
parItem.appendRow(minItem)
maxItem = SpectralComponentValueItem(par, "max")
maxItem.setDataItem(par.max)
parItem.appendRow(maxItem)
fixedItem = SpectralComponentValueItem(par, "fixed", checkable=True)
fixedItem.setDataItem(par.fixed)
parItem.appendRow(fixedItem)
tiedItem = SpectralComponentTiedItem(par)
tiedItem.setDataItem(par.tied)
parItem.appendRow(tiedItem)
@property
def items(self):
result = []
for i in range(self.rowCount()):
result.append(self.item(i).item)
return result
def _floatItemChanged(self, item):
type = item.type
number = _float_check(item.text())
if number:
if hasattr(item, 'parameter'):
setattr(item.parameter, type, number)
item.setData(type + ": " + str(number), role=Qt.DisplayRole)
# parameter name is followed by its value when displaying in tree.
if type == 'value':
item.parent().setData(item.parameter.name + ": " + str(number), role=Qt.DisplayRole)
else:
item.setData(type + ": " + str(getattr(item.parameter, type)), role=Qt.DisplayRole)
def _nameChanged(self, item):
old_name = item.item
new_name = str(item.text())
# remove actual new name from the "name:newname" in the tree display
index = new_name.find(":")
if index > -1:
new_name = new_name[index+2:]
if len(new_name) > 0:
setattr(item, "item", new_name)
if index <= -1:
item.setData("name: " + new_name, role=Qt.DisplayRole)
# function name is followed by component name when displaying on tree
item_parent = item.parent()
setattr(item_parent.item, "_name", new_name)
id_string = str(item_parent.text())
index = id_string.find("(")
function_name = id_string[:index-1]
item_parent.setData(function_name + " (" + new_name + ")", role=Qt.DisplayRole)
# name was successfully changed; now check to see if any tied parameters depend om it.
self._modify_tied_components(item, old_name, new_name)
else:
item.setData("name: " + old_name, role=Qt.DisplayRole)
def _booleanItemChecked(self, item):
setattr(item.parameter, item.type, (item.checkState() == Qt.Checked))
def _onItemChanged(self, item):
if item.isCheckable():
self._booleanItemChecked(item)
elif item.type == "name":
self._nameChanged(item)
elif item.type in ("value", "min", "max"):
self._floatItemChanged(item)
# scans all parameters in all components in the model, looking for
# tied parameters that point to the old name. Replace the old name
# with the new name in the tie. This assumes that we use the standard
# lambda form for ties.
def _modify_tied_components(self, reference_item, old_name, new_name):
for row, component in enumerate(self.items):
if component.tied:
row2 = 1
for key, tie in component.tied.items():
row2 += 1
if tie:
tie_text = sp_model_io.get_tie_text(tie)
if old_name in tie_text:
# modify actual component
new_tie_text = tie_text.replace(old_name, new_name)
new_tie = eval(new_tie_text)
component.tied[key] = new_tie
# modify element in tree
item = self.item(row)
# sometimes the tree returns a None item.
if item:
tie_element = item.child(row2).child(4)
text = tie_element.text()
new_text = text.replace(old_name, new_name)
tie_element.setData(new_text, role=Qt.DisplayRole)
class SpectralModelManager(QObject):
""" Basic class to be called by external code.
It is responsible for building the GUI trees and putting them together
into a split pane layout. An alternate, single pane plus drop-down
menu, is also available. The class also provides accessors to the active
model individual spectral components and to the library functions,
as well as to the spectrum that results from a compound model call.
It inherits from QObject for the sole purpose of being able to
respond to Qt signals.
Parameters
----------
model: list or string or variable, optional
List with instances of spectral components from
astropy.modeling.functional_models. If not provided,
the instance will be initialized with an empty list.
Or it can be a string with a fully specified file name
which contains a compound model specification. Or, it
can be a Python reference to a compound model instance.
drop_down: boolean, optional
Defines GUI looks. Default is True, meaning that the available
spectral components from the astropy.modeling.models library
are accessed via a drop down menu. If set to False, the
components are accessed from a separate tree on a split pane
window.
"""
def __init__(self, model=None, drop_down=True):
super(SpectralModelManager, self).__init__()
# _init_compound_model is used just to hold a reference
# to any compound model one wishes to use to start up
# the tool. The actual compound model used in operations
# is set by the buildMainPanel method. It lives in
# self.model_gui.model.compound_model.
if model == None:
self._init_compound_model = None
elif type(model) == type(list):
self._init_compound_model = _buildSummedCompoundModel(model)
elif type(model) == type(""):
global _model_directory
self._init_compound_model, _model_directory = sp_model_io.buildModelFromFile(model)
else:
self._init_compound_model = model
self._drop_down = drop_down
self.x = None
self.y = None
self.changed = SignalModelChanged()
self.selected = SignalComponentSelected()
def setArrays(self, x, y):
''' Defines the region in spectral coordinate vs. flux
'space' to which the components in the model should refer
to.
For now, this region is being defined by the data arrays
associated with the observational data at hand. The region
could conceivably be defined by any other means, as long
as the functional components can then use the region data
to initialize their parameters with sensible values.
This region is used by code in module sp_adjust. If no
X and/or Y arrays are provided via this method, spectral
components added to the compound model will be initialized
to a default set of parameter values.
Parameters
----------
x: numpy array
Array with spectral coordinates
y: numpy array
Array with flux values
'''
self.x = x
self.y = y
if hasattr(self, '_library_gui'):
self._library_gui.setArrays(self.x, self.y)
def buildMainPanel(self, model=None):
""" Builds the main panel with the active and the library
trees of spectral components.
Parameters
----------
model: list or str, optional
List with instances of spectral components from
astropy.modeling.functional_models. Or, a file name
in the 'specfit' format from where a compound model
can be imported. If not provided, the list of components
will exist but will be empty.
Returns
-------
instance of either QMainWindow or QSplitter
"""
# override whatever model was passed to the constructor.
# This specific form of the conditional avoids a mishap
# when self._init_compound_model is an empty list.
if model == None:
self._init_compound_model = None
elif type(model) == type(list):
self._init_compound_model = _buildSummedCompoundModel(model)
elif type(model) == type(""):
global _model_directory
self._init_compound_model, _model_directory = sp_model_io.buildModelFromFile(model)
else:
self._init_compound_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'):
# note that _init_compound_model is passed as an initializer, but
# any other reference to the actual compound model that lives in
# the GUI must be done via reference self.models_gui.model.compound_model.
self.models_gui = _SpectralModelsGUI(self._init_compound_model)
self._library_gui = _SpectralLibraryGUI(self.models_gui, self.x, self.y, drop_down=self._drop_down)
if self._drop_down:
# window contains the active tree in the central
# widget, and the library tree in the menu widget.
main_widget = QMainWindow();
main_widget.setMenuWidget(self._library_gui.window)
main_widget.setCentralWidget(self.models_gui.window)
else:
# split window contains the active tree in the first
# pane and library tree in the second pane.
main_widget = QSplitter();
main_widget.addWidget(self.models_gui.window)
main_widget.addWidget(self._library_gui.window)
main_widget.setStretchFactor(0, 1)
main_widget.setStretchFactor(1, 0)
# Data change and click events must be propagated to the outside world.
self.connect(self.models_gui.window, SIGNAL("treeChanged"), self._broadcastChangedSignal)
self.connect(self.models_gui.window.treeView, SIGNAL("dataChanged"), self._broadcastChangedSignal)
self.models_gui.window.treeView.clicked.connect(self._broadcastSelectedSignal)