1 | # global |
---|
2 | import sys |
---|
3 | import os |
---|
4 | from PyQt4 import QtCore |
---|
5 | from PyQt4 import QtGui |
---|
6 | from PyQt4 import QtWebKit |
---|
7 | |
---|
8 | from sas.qtgui.UI import images_rc |
---|
9 | from sas.qtgui.UI import main_resources_rc |
---|
10 | #from bumps.fitters import FITTERS, FIT_AVAILABLE_IDS, FIT_ACTIVE_IDS, FIT_DEFAULT_ID |
---|
11 | from bumps import fitters |
---|
12 | import bumps.options |
---|
13 | |
---|
14 | from sas.qtgui.Perspectives.Fitting.UI.FittingOptionsUI import Ui_FittingOptions |
---|
15 | |
---|
16 | # Set the default optimizer |
---|
17 | fitters.FIT_DEFAULT_ID = 'lm' |
---|
18 | |
---|
19 | class FittingOptions(QtGui.QDialog, Ui_FittingOptions): |
---|
20 | """ |
---|
21 | Hard-coded version of the fit options dialog available from BUMPS. |
---|
22 | This should be make more "dynamic". |
---|
23 | bumps.options.FIT_FIELDS gives mapping between parameter names, parameter strings and field type |
---|
24 | (double line edit, integer line edit, combo box etc.), e.g. |
---|
25 | FIT_FIELDS = dict( |
---|
26 | samples = ("Samples", parse_int), |
---|
27 | xtol = ("x tolerance", float)) |
---|
28 | |
---|
29 | bumps.fitters.<algorithm>.settings gives mapping between algorithm, parameter name and default value: |
---|
30 | e.g. |
---|
31 | settings = [('steps', 1000), ('starts', 1), ('radius', 0.15), ('xtol', 1e-6), ('ftol', 1e-8)] |
---|
32 | """ |
---|
33 | fit_option_changed = QtCore.pyqtSignal(str, dict) |
---|
34 | |
---|
35 | def __init__(self, parent=None, config=None): |
---|
36 | super(FittingOptions, self).__init__(parent) |
---|
37 | self.setupUi(self) |
---|
38 | |
---|
39 | self.config = config |
---|
40 | |
---|
41 | # no reason to have this widget resizable |
---|
42 | self.setFixedSize(self.minimumSizeHint()) |
---|
43 | |
---|
44 | self.setWindowTitle("Fitting Options") |
---|
45 | |
---|
46 | # Fill up the algorithm combo, based on what BUMPS says is available |
---|
47 | self.cbAlgorithm.addItems([n.name for n in fitters.FITTERS if n.id in fitters.FIT_ACTIVE_IDS]) |
---|
48 | |
---|
49 | # Handle the Apply button click |
---|
50 | self.buttonBox.button(QtGui.QDialogButtonBox.Ok).clicked.connect(self.onApply) |
---|
51 | |
---|
52 | # Handle the combo box changes |
---|
53 | self.cbAlgorithm.currentIndexChanged.connect(self.onAlgorithmChange) |
---|
54 | |
---|
55 | # Set the default index |
---|
56 | default_name = [n.name for n in fitters.FITTERS if n.id == fitters.FIT_DEFAULT_ID][0] |
---|
57 | default_index = self.cbAlgorithm.findText(default_name) |
---|
58 | self.cbAlgorithm.setCurrentIndex(default_index) |
---|
59 | self.current_fitter_id = fitters.FIT_DEFAULT_ID |
---|
60 | |
---|
61 | # Fill in options for all active fitters |
---|
62 | #[self.updateWidgetFromBumps(fitter_id) for fitter_id in fitters.FIT_ACTIVE_IDS] |
---|
63 | |
---|
64 | def onAlgorithmChange(self, index): |
---|
65 | """ |
---|
66 | Change the page in response to combo box index |
---|
67 | """ |
---|
68 | # Find the algorithm ID from name |
---|
69 | self.current_fitter_id = \ |
---|
70 | [n.id for n in fitters.FITTERS if n.name == str(self.cbAlgorithm.currentText())][0] |
---|
71 | |
---|
72 | # find the right stacked widget |
---|
73 | widget_name = "self.page_"+str(self.current_fitter_id) |
---|
74 | |
---|
75 | # Convert the name into widget instance |
---|
76 | widget_to_activate = eval(widget_name) |
---|
77 | |
---|
78 | # Select the requested widget |
---|
79 | self.stackedWidget.setCurrentIndex(self.stackedWidget.indexOf(widget_to_activate)) |
---|
80 | |
---|
81 | self.updateWidgetFromBumps(self.current_fitter_id) |
---|
82 | |
---|
83 | def onApply(self): |
---|
84 | """ |
---|
85 | Update the fitter object in perspective |
---|
86 | """ |
---|
87 | self.fit_option_changed.emit(self.cbAlgorithm.currentText(), {}) |
---|
88 | |
---|
89 | # or just do it locally |
---|
90 | for option in self.config.values[self.current_fitter_id].iterkeys(): |
---|
91 | widget = self.widgetFromOption(option) |
---|
92 | new_value = "" |
---|
93 | if isinstance(widget, QtGui.QComboBox): |
---|
94 | new_value = widget.currentText() |
---|
95 | else: |
---|
96 | new_value = float(widget.text()) |
---|
97 | self.config.values[self.current_fitter_id][option] = new_value |
---|
98 | |
---|
99 | def widgetFromOption(self, option_id): |
---|
100 | """ |
---|
101 | returns widget's element linked to the given option_id |
---|
102 | """ |
---|
103 | return eval('self.' + option_id + '_' + self.current_fitter_id) |
---|
104 | |
---|
105 | def getResults(self): |
---|
106 | """ |
---|
107 | Sends back the current choice of parameters |
---|
108 | """ |
---|
109 | algorithm = self.cbAlgorithm.currentText() |
---|
110 | return algorithm |
---|
111 | |
---|
112 | def updateWidgetFromBumps(self, fitter_id): |
---|
113 | """ |
---|
114 | Given the ID of the current optimizer, fetch the values |
---|
115 | and update the widget |
---|
116 | """ |
---|
117 | options = self.config.values[fitter_id] |
---|
118 | for option in options.iterkeys(): |
---|
119 | # Find the widget name of the option |
---|
120 | # e.g. 'samples' for 'dream' is 'self.samples_dream' |
---|
121 | widget_name = 'self.'+option+'_'+fitter_id |
---|
122 | if isinstance(bumps.options.FIT_FIELDS[option][1], bumps.options.ChoiceList): |
---|
123 | control = eval(widget_name) |
---|
124 | control.setCurrentIndex(control.findText(str(options[option]))) |
---|
125 | else: |
---|
126 | eval(widget_name).setText(str(options[option])) |
---|
127 | |
---|
128 | pass |
---|
129 | |
---|
130 | def updateBumpsOptions(self, optimizer_id): |
---|
131 | """ |
---|
132 | Given the ID of the optimizer, gather widget's values |
---|
133 | and update the bumps options |
---|
134 | """ |
---|
135 | pass |
---|
136 | |
---|