pymodaq 5.0.17__py3-none-any.whl → 5.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pymodaq might be problematic. Click here for more details.

Files changed (92) hide show
  1. pymodaq/__init__.py +23 -11
  2. pymodaq/control_modules/__init__.py +1 -0
  3. pymodaq/control_modules/daq_move.py +458 -246
  4. pymodaq/control_modules/daq_move_ui/__init__.py +0 -0
  5. pymodaq/control_modules/daq_move_ui/factory.py +48 -0
  6. pymodaq/control_modules/{daq_move_ui.py → daq_move_ui/ui_base.py} +168 -210
  7. pymodaq/control_modules/daq_move_ui/uis/__init__.py +0 -0
  8. pymodaq/control_modules/daq_move_ui/uis/binary.py +139 -0
  9. pymodaq/control_modules/daq_move_ui/uis/original.py +120 -0
  10. pymodaq/control_modules/daq_move_ui/uis/relative.py +124 -0
  11. pymodaq/control_modules/daq_move_ui/uis/simple.py +126 -0
  12. pymodaq/control_modules/daq_viewer.py +113 -101
  13. pymodaq/control_modules/daq_viewer_ui.py +41 -31
  14. pymodaq/control_modules/mocks.py +2 -2
  15. pymodaq/control_modules/move_utility_classes.py +113 -41
  16. pymodaq/control_modules/thread_commands.py +137 -0
  17. pymodaq/control_modules/ui_utils.py +72 -0
  18. pymodaq/control_modules/utils.py +107 -63
  19. pymodaq/control_modules/viewer_utility_classes.py +13 -17
  20. pymodaq/dashboard.py +1294 -625
  21. pymodaq/examples/qt_less_standalone_module.py +48 -11
  22. pymodaq/extensions/__init__.py +8 -3
  23. pymodaq/extensions/adaptive/__init__.py +2 -0
  24. pymodaq/extensions/adaptive/adaptive_optimization.py +179 -0
  25. pymodaq/extensions/adaptive/loss_function/_1d_loss_functions.py +73 -0
  26. pymodaq/extensions/adaptive/loss_function/_2d_loss_functions.py +73 -0
  27. pymodaq/extensions/adaptive/loss_function/__init__.py +3 -0
  28. pymodaq/extensions/adaptive/loss_function/loss_factory.py +110 -0
  29. pymodaq/extensions/adaptive/utils.py +123 -0
  30. pymodaq/extensions/bayesian/__init__.py +1 -1
  31. pymodaq/extensions/bayesian/acquisition/__init__.py +2 -0
  32. pymodaq/extensions/bayesian/acquisition/acquisition_function_factory.py +80 -0
  33. pymodaq/extensions/bayesian/acquisition/base_acquisition_function.py +105 -0
  34. pymodaq/extensions/bayesian/bayesian_optimization.py +143 -0
  35. pymodaq/extensions/bayesian/utils.py +71 -297
  36. pymodaq/extensions/daq_logger/daq_logger.py +7 -12
  37. pymodaq/extensions/daq_logger/h5logging.py +1 -1
  38. pymodaq/extensions/daq_scan.py +30 -55
  39. pymodaq/extensions/data_mixer/__init__.py +0 -0
  40. pymodaq/extensions/data_mixer/daq_0Dviewer_DataMixer.py +97 -0
  41. pymodaq/extensions/data_mixer/data_mixer.py +262 -0
  42. pymodaq/extensions/data_mixer/model.py +108 -0
  43. pymodaq/extensions/data_mixer/models/__init__.py +0 -0
  44. pymodaq/extensions/data_mixer/models/equation_model.py +91 -0
  45. pymodaq/extensions/data_mixer/models/gaussian_fit_model.py +65 -0
  46. pymodaq/extensions/data_mixer/parser.py +53 -0
  47. pymodaq/extensions/data_mixer/utils.py +23 -0
  48. pymodaq/extensions/h5browser.py +3 -34
  49. pymodaq/extensions/optimizers_base/__init__.py +0 -0
  50. pymodaq/extensions/optimizers_base/optimizer.py +1016 -0
  51. pymodaq/extensions/optimizers_base/thread_commands.py +22 -0
  52. pymodaq/extensions/optimizers_base/utils.py +427 -0
  53. pymodaq/extensions/pid/actuator_controller.py +3 -2
  54. pymodaq/extensions/pid/daq_move_PID.py +107 -30
  55. pymodaq/extensions/pid/pid_controller.py +613 -287
  56. pymodaq/extensions/pid/utils.py +8 -5
  57. pymodaq/extensions/utils.py +17 -2
  58. pymodaq/resources/config_template.toml +57 -0
  59. pymodaq/resources/preset_default.xml +1 -1
  60. pymodaq/utils/config.py +13 -4
  61. pymodaq/utils/daq_utils.py +14 -0
  62. pymodaq/utils/data.py +1 -0
  63. pymodaq/utils/gui_utils/loader_utils.py +25 -15
  64. pymodaq/utils/h5modules/module_saving.py +134 -22
  65. pymodaq/utils/leco/daq_move_LECODirector.py +123 -84
  66. pymodaq/utils/leco/daq_xDviewer_LECODirector.py +84 -97
  67. pymodaq/utils/leco/director_utils.py +32 -16
  68. pymodaq/utils/leco/leco_director.py +104 -27
  69. pymodaq/utils/leco/pymodaq_listener.py +186 -97
  70. pymodaq/utils/leco/rpc_method_definitions.py +43 -0
  71. pymodaq/utils/leco/utils.py +25 -25
  72. pymodaq/utils/managers/batchscan_manager.py +12 -11
  73. pymodaq/utils/managers/modules_manager.py +74 -33
  74. pymodaq/utils/managers/overshoot_manager.py +11 -10
  75. pymodaq/utils/managers/preset_manager.py +100 -64
  76. pymodaq/utils/managers/preset_manager_utils.py +163 -107
  77. pymodaq/utils/managers/remote_manager.py +21 -16
  78. pymodaq/utils/scanner/scan_factory.py +18 -4
  79. pymodaq/utils/scanner/scan_selector.py +1 -3
  80. pymodaq/utils/scanner/scanner.py +35 -6
  81. pymodaq/utils/scanner/scanners/_1d_scanners.py +15 -46
  82. pymodaq/utils/scanner/scanners/_2d_scanners.py +21 -68
  83. pymodaq/utils/scanner/scanners/sequential.py +50 -31
  84. pymodaq/utils/scanner/scanners/tabular.py +45 -28
  85. {pymodaq-5.0.17.dist-info → pymodaq-5.1.0.dist-info}/METADATA +7 -6
  86. pymodaq-5.1.0.dist-info/RECORD +154 -0
  87. {pymodaq-5.0.17.dist-info → pymodaq-5.1.0.dist-info}/entry_points.txt +0 -2
  88. pymodaq/extensions/bayesian/bayesian_optimisation.py +0 -685
  89. pymodaq/utils/leco/desktop.ini +0 -2
  90. pymodaq-5.0.17.dist-info/RECORD +0 -121
  91. {pymodaq-5.0.17.dist-info → pymodaq-5.1.0.dist-info}/WHEEL +0 -0
  92. {pymodaq-5.0.17.dist-info → pymodaq-5.1.0.dist-info}/licenses/LICENSE +0 -0
@@ -1,685 +0,0 @@
1
- from typing import List, Union, Optional
2
- import tempfile
3
- from pathlib import Path
4
-
5
- from qtpy import QtWidgets, QtCore
6
- import time
7
- import numpy as np
8
-
9
- from pymodaq.utils.data import DataToExport, DataToActuators, DataCalculated, DataActuator
10
- from pymodaq.utils.managers.modules_manager import ModulesManager
11
- from pymodaq_utils import utils
12
- from pymodaq_utils import config as config_mod
13
- from pymodaq_utils.enums import BaseEnum
14
-
15
- from pymodaq_gui.config import ConfigSaverLoader
16
- from pymodaq_utils.logger import set_logger, get_module_name
17
-
18
- from pymodaq_gui.plotting.data_viewers.viewer0D import Viewer0D
19
- from pymodaq_gui.plotting.data_viewers.viewer import ViewerDispatcher, ViewersEnum
20
- from pymodaq_gui.utils import QLED
21
- from pymodaq_gui.utils.utils import mkQApp
22
- from pymodaq_gui import utils as gutils
23
- from pymodaq_gui.parameter import utils as putils
24
- from pymodaq_gui.h5modules.saving import H5Saver
25
-
26
- from pymodaq_data.h5modules.data_saving import DataEnlargeableSaver
27
-
28
-
29
- from pymodaq.extensions.bayesian.utils import (get_bayesian_models, BayesianModelGeneric,
30
- BayesianAlgorithm, UtilityKind,
31
- UtilityParameters, StopType, StoppingParameters)
32
- from pymodaq.post_treatment.load_and_plot import LoaderPlotter
33
- from pymodaq.extensions.bayesian.utils import BayesianConfig
34
- from pymodaq.extensions.utils import CustomExt
35
-
36
- EXTENSION_NAME = 'BayesianOptimisation'
37
- CLASS_NAME = 'BayesianOptimisation'
38
-
39
- logger = set_logger(get_module_name(__file__))
40
- config = config_mod.Config()
41
-
42
-
43
- class DataNames(BaseEnum):
44
- Fitness = 0
45
- Individual = 1
46
- ProbedData = 2
47
- Actuators = 3
48
- Kappa = 4
49
-
50
-
51
- class BayesianOptimisation(CustomExt):
52
- """ PyMoDAQ extension of the DashBoard to perform the optimization of a target signal
53
- taken form the detectors as a function of one or more parameters controlled by the actuators.
54
- """
55
-
56
- command_runner = QtCore.Signal(utils.ThreadCommand)
57
- models = get_bayesian_models()
58
- explored_viewer_name = 'algo/ProbedData'
59
- optimisation_done_signal = QtCore.Signal(DataToExport)
60
-
61
- params = [
62
- {'title': 'Main Settings:', 'name': 'main_settings', 'expanded': True, 'type': 'group',
63
- 'children': [
64
- {'title': 'Utility Function:', 'name': 'utility', 'expanded': False, 'type': 'group',
65
- 'children': [
66
- {'title': 'Kind', 'name': 'kind', 'type': 'list',
67
- 'limits': UtilityKind.to_dict_value()},
68
- {'title': 'Kappa:', 'name': 'kappa', 'type': 'slide', 'value': 2.576,
69
- 'min': 0.001, 'max': 100, 'subtype': 'log',
70
- 'tip': 'Parameter to indicate how closed are the next parameters sampled.'
71
- 'Higher value = favors spaces that are least explored.'
72
- 'Lower value = favors spaces where the regression function is the '
73
- 'highest.'},
74
- {'title': 'Kappa actual:', 'name': 'kappa_actual', 'type': 'float', 'value': 2.576,
75
- 'tip': 'Current value of the kappa parameter', 'readonly': True},
76
- {'title': 'xi:', 'name': 'xi', 'type': 'slide', 'value': 0,
77
- 'tip': 'Governs the exploration/exploitation tradeoff.'
78
- 'Lower prefers exploitation, higher prefers exploration.'},
79
- {'title': 'Kappa decay:', 'name': 'kappa_decay', 'type': 'float', 'value': 0.9,
80
- 'tip': 'kappa is multiplied by this factor every iteration.'},
81
- {'title': 'Kappa decay delay:', 'name': 'kappa_decay_delay', 'type': 'int',
82
- 'value': 20, 'tip': 'Number of iterations that must have passed before applying '
83
- 'the decay to kappa.'},
84
- ]},
85
- {'title': 'Stopping Criteria:', 'name': 'stopping', 'expanded': False, 'type': 'group',
86
- 'children': [
87
- {'title': 'Niteration', 'name': 'niter', 'type': 'int', 'value': 100, 'min': -1},
88
- {'title': 'Type:', 'name': 'stop_type', 'type': 'list',
89
- 'limits': StopType.names()},
90
- {'title': 'Tolerance', 'name': 'tolerance', 'type': 'slide', 'value': 1e-2,
91
- 'min': 1e-8, 'max': 1, 'subtype': 'log',},
92
- {'title': 'Npoints', 'name': 'npoints', 'type': 'int', 'value': 5, 'min': 1},
93
- ]},
94
- {'title': 'Ini. State', 'name': 'ini_random', 'type': 'int', 'value': 5},
95
- {'title': 'bounds', 'name': 'bounds', 'type': 'group', 'children': []},
96
- ]},
97
-
98
- {'title': 'Models', 'name': 'models', 'type': 'group', 'expanded': True, 'visible': True,
99
- 'children': [
100
- {'title': 'Models class:', 'name': 'model_class', 'type': 'list',
101
- 'limits': [d['name'] for d in models]},
102
- {'title': 'Ini Model', 'name': 'ini_model', 'type': 'action', },
103
- {'title': 'Ini Algo', 'name': 'ini_runner', 'type': 'action', 'enabled': False},
104
- {'title': 'Model params:', 'name': 'model_params', 'type': 'group', 'children': []},
105
- ]},
106
- {'title': 'Move settings:', 'name': 'move_settings', 'expanded': True, 'type': 'group',
107
- 'visible': False, 'children': [
108
- {'title': 'Units:', 'name': 'units', 'type': 'str', 'value': ''}]},
109
-
110
- ]
111
-
112
- def __init__(self, dockarea, dashboard):
113
- super().__init__(dockarea, dashboard)
114
-
115
- self.algorithm: Optional[BayesianAlgorithm] = None
116
- self.viewer_fitness: Optional[Viewer0D] = None
117
- self.viewer_observable: Optional[ViewerDispatcher] = None
118
- self.model_class: Optional[BayesianModelGeneric] = None
119
- self._save_main_settings = True
120
- self._modules_manager = ModulesManager(self.dashboard.detector_modules,
121
- self.dashboard.actuators_modules)
122
- self.modules_manager.actuators_changed[list].connect(self.update_actuators)
123
- self.modules_manager.settings.child('data_dimensions').setOpts(expanded=False)
124
- self.modules_manager.settings.child('actuators_positions').setOpts(expanded=False)
125
- self.setup_ui()
126
-
127
- self.bayesian_config = BayesianConfig()
128
- self.mainsettings_saver_loader = ConfigSaverLoader(
129
- self.settings.child('main_settings'), self.bayesian_config)
130
-
131
- self.h5temp: H5Saver = None
132
- self.temp_path: tempfile.TemporaryDirectory = None
133
-
134
- self.enlargeable_saver: DataEnlargeableSaver = None
135
- self.live_plotter = LoaderPlotter(self.dockarea)
136
-
137
- self.enl_index = 0
138
-
139
- self.settings.child('models', 'ini_model').sigActivated.connect(
140
- self.get_action('ini_model').trigger)
141
-
142
- self.settings.child('models', 'ini_runner').sigActivated.connect(
143
- self.get_action('ini_runner').trigger)
144
-
145
- @property
146
- def modules_manager(self) -> ModulesManager:
147
- return self._modules_manager
148
-
149
- def setup_docks(self):
150
- """
151
- to be subclassed to setup the docks layout
152
- for instance:
153
-
154
- self.docks['ADock'] = gutils.Dock('ADock name)
155
- self.dockarea.addDock(self.docks['ADock"])
156
- self.docks['AnotherDock'] = gutils.Dock('AnotherDock name)
157
- self.dockarea.addDock(self.docks['AnotherDock"], 'bottom', self.docks['ADock"])
158
-
159
- See Also
160
- ########
161
- pyqtgraph.dockarea.Dock
162
- """
163
- self.docks['settings'] = gutils.Dock('Settings')
164
- self.dockarea.addDock(self.docks['settings'])
165
- splitter = QtWidgets.QSplitter(QtCore.Qt.Vertical)
166
- self.docks['settings'].addWidget(splitter)
167
- splitter.addWidget(self.settings_tree)
168
- splitter.addWidget(self.modules_manager.settings_tree)
169
- self.modules_manager.show_only_control_modules(False)
170
- splitter.setSizes((int(self.dockarea.height() / 2),
171
- int(self.dockarea.height() / 2)))
172
-
173
- widget_observable = QtWidgets.QWidget()
174
- widget_observable.setLayout(QtWidgets.QHBoxLayout())
175
- observable_dockarea = gutils.DockArea()
176
- widget_observable.layout().addWidget(observable_dockarea)
177
- self.viewer_observable = ViewerDispatcher(observable_dockarea, direction='bottom')
178
- self.docks['observable'] = gutils.Dock('Observable')
179
- self.dockarea.addDock(self.docks['observable'], 'right', self.docks['settings'])
180
- self.docks['observable'].addWidget(widget_observable)
181
-
182
- if len(self.models) != 0:
183
- self.get_set_model_params(self.models[0]['name'])
184
-
185
- def get_set_model_params(self, model_name):
186
- self.settings.child('models', 'model_params').clearChildren()
187
- if len(self.models) > 0:
188
- model_class = utils.find_dict_in_list_from_key_val(self.models, 'name', model_name)['class']
189
- params = getattr(model_class, 'params')
190
- self.settings.child('models', 'model_params').addChildren(params)
191
-
192
- def setup_menu(self, menubar: QtWidgets.QMenuBar = None):
193
- '''
194
- to be subclassed
195
- create menu for actions contained into the self.actions_manager, for instance:
196
-
197
- For instance:
198
-
199
- file_menu = self.menubar.addMenu('File')
200
- self.actions_manager.affect_to('load', file_menu)
201
- self.actions_manager.affect_to('save', file_menu)
202
-
203
- file_menu.addSeparator()
204
- self.actions_manager.affect_to('quit', file_menu)
205
- '''
206
- pass
207
-
208
- def value_changed(self, param):
209
- ''' to be subclassed for actions to perform when one of the param's value in self.settings is changed
210
-
211
- For instance:
212
- if param.name() == 'do_something':
213
- if param.value():
214
- print('Do something')
215
- self.settings.child('main_settings', 'something_done').setValue(False)
216
-
217
- Parameters
218
- ----------
219
- param: (Parameter) the parameter whose value just changed
220
- '''
221
- if param.name() == 'model_class':
222
- self.get_set_model_params(param.value())
223
- elif param.name() in putils.iter_children(self.settings.child('models', 'model_params'), []):
224
- if self.model_class is not None:
225
- self.model_class.update_settings(param)
226
- elif param.name() in putils.iter_children(
227
- self.settings.child('main_settings', 'utility'), []):
228
- if param.name() != 'kappa_actual':
229
- self.update_utility_function()
230
- elif param.name() in putils.iter_children(
231
- self.settings.child('main_settings', 'bounds'), []):
232
- self.update_bounds()
233
- elif param.name() in putils.iter_children(
234
- self.settings.child('main_settings', 'stopping'), []):
235
- self.update_stopping_criteria()
236
- if self._save_main_settings and param.name() in putils.iter_children(
237
- self.settings.child('main_settings'), []):
238
- self.mainsettings_saver_loader.save_config()
239
-
240
- def update_utility_function(self):
241
- utility_settings = self.settings.child('main_settings', 'utility')
242
- uparams = UtilityParameters(utility_settings['kind'], utility_settings['kappa'],
243
- utility_settings['xi'], utility_settings['kappa_decay'],
244
- utility_settings['kappa_decay_delay'])
245
- self.command_runner.emit(utils.ThreadCommand('utility', uparams))
246
-
247
- def get_stopping_parameters(self) -> StoppingParameters:
248
- stopping_settings = self.settings.child('main_settings', 'stopping')
249
- stopping_params = StoppingParameters(stopping_settings['niter'],
250
- stopping_settings['stop_type'],
251
- stopping_settings['tolerance'],
252
- stopping_settings['npoints'])
253
- return stopping_params
254
-
255
- def update_stopping_criteria(self):
256
- self.command_runner.emit(utils.ThreadCommand('stopping', self.get_stopping_parameters()))
257
-
258
- def update_bounds(self):
259
- bounds = {}
260
- for child in self.settings.child('main_settings', 'bounds').children():
261
- bounds[child.name()] = (child['min'], child['max'])
262
-
263
- self.command_runner.emit(utils.ThreadCommand('bounds', bounds))
264
-
265
- def setup_actions(self):
266
- logger.debug('setting actions')
267
- self.add_action('quit', 'Quit', 'close2', "Quit program")
268
- self.add_action('ini_model', 'Init Model', 'ini')
269
- self.add_widget('model_led', QLED, toolbar=self.toolbar)
270
- self.add_action('ini_runner', 'Init the Optimisation Algorithm', 'ini', checkable=True,
271
- enabled=False)
272
- self.add_widget('runner_led', QLED, toolbar=self.toolbar)
273
- self.add_action('run', 'Run Optimisation', 'run2', checkable=True, enabled=False)
274
- self.add_action('gotobest', 'Got to best individual', 'move_contour', enabled=False,
275
- tip='Go to the best individual guessed by the algorithm')
276
- logger.debug('actions set')
277
-
278
- def connect_things(self):
279
- logger.debug('connecting things')
280
- self.connect_action('quit', self.quit, )
281
- self.connect_action('ini_model', self.ini_model)
282
- self.connect_action('ini_runner', self.ini_optimisation_runner)
283
- self.connect_action('run', self.run_optimisation)
284
- self.connect_action('gotobest', self.go_to_best)
285
-
286
- def go_to_best(self):
287
- best_individual = self.algorithm.best_individual
288
- actuators = self.modules_manager.selected_actuators_name
289
- dte_act = DataToActuators('best', data=[
290
- DataActuator(actuators[ind], data=float(best_individual[ind])) for ind in range(len(best_individual))
291
- ],
292
- mode='abs')
293
- self.modules_manager.connect_actuators(True)
294
- self.modules_manager.move_actuators(dte_act, polling=True)
295
- self.modules_manager.connect_actuators(False)
296
-
297
- self.modules_manager.grab_datas()
298
-
299
- def quit(self):
300
- self.dockarea.parent().close()
301
- self.clean_h5_temp()
302
-
303
- def set_model(self):
304
- model_name = self.settings.child('models', 'model_class').value()
305
- self.model_class = utils.find_dict_in_list_from_key_val(
306
- self.models, 'name', model_name)['class'](self)
307
- self.model_class.ini_model_base()
308
-
309
- def ini_temp_file(self):
310
- self.clean_h5_temp()
311
-
312
- self.h5temp = H5Saver()
313
- self.temp_path = tempfile.TemporaryDirectory(prefix='pymo')
314
- addhoc_file_path = Path(self.temp_path.name).joinpath('bayesian_temp_data.h5')
315
- self.h5temp.init_file(custom_naming=True, addhoc_file_path=addhoc_file_path)
316
- act_names = [child.name() for child in self.settings.child( 'main_settings',
317
- 'bounds').children()]
318
- act_units = [self.modules_manager.get_mod_from_name(act_name, 'act').units for act_name
319
- in act_names]
320
- self.enlargeable_saver = DataEnlargeableSaver(
321
- self.h5temp,
322
- enl_axis_names=act_names,
323
- enl_axis_units=act_units)
324
-
325
- def ini_live_plot(self):
326
- self.live_plotter.h5saver = self.h5temp
327
- act_names = [child.name() for child in self.settings.child('main_settings',
328
- 'bounds').children()]
329
- act_units = [self.modules_manager.get_mod_from_name(act_name, 'act').units for act_name
330
- in act_names]
331
- if len(act_names) == 1:
332
- viewer_enum = 'Viewer1D'
333
- elif len(act_names) == 2:
334
- viewer_enum = 'Viewer2D'
335
- else:
336
- viewer_enum = 'ViewerND'
337
- viewers = self.live_plotter.prepare_viewers([viewer_enum],
338
- viewers_name=[self.explored_viewer_name])
339
- for viewer in viewers:
340
- if viewer.has_action('crosshair'):
341
- viewer.get_action('crosshair').trigger()
342
- if hasattr(viewer.view, 'collapse_lineout_widgets'):
343
- viewer.view.collapse_lineout_widgets()
344
- if viewer.has_action('sort'):
345
- if not viewer.is_action_checked('sort'):
346
- viewer.get_action('sort').trigger()
347
- if viewer.has_action('scatter'):
348
- if not viewer.is_action_checked('scatter'):
349
- viewer.get_action('scatter').trigger()
350
-
351
- QtWidgets.QApplication.processEvents()
352
- win_width = self.dockarea.width()
353
- self.docks['settings'].container().setSizes((int(win_width / 5),
354
- int(2 * win_width / 5),
355
- int(2 * win_width / 5), 10, 10))
356
-
357
- def update_actuators(self, actuators: List[str]):
358
- if self.is_action_checked('ini_runner'):
359
- self.get_action('ini_runner').trigger()
360
- QtWidgets.QApplication.processEvents()
361
-
362
- self._save_main_settings = False
363
-
364
- for child in self.settings.child('main_settings', 'bounds').children():
365
- self.settings.child('main_settings', 'bounds').removeChild(child)
366
- params = []
367
- for actuator in actuators:
368
- params.append({'title': actuator, 'name': actuator, 'type': 'group', 'children': [
369
- {'title': 'min', 'name': 'min', 'type': 'float', 'value': -5},
370
- {'title': 'max', 'name': 'max', 'type': 'float', 'value': 5},
371
- ]})
372
- self.settings.child('main_settings', 'bounds').addChildren(params)
373
- self.mainsettings_saver_loader.base_path = [self.model_class.__class__.__name__] + \
374
- self.modules_manager.selected_actuators_name
375
- self.mainsettings_saver_loader.load_config()
376
- self._save_main_settings = True
377
-
378
- def format_bounds(self):
379
- bound_dict = {}
380
- for bound in self.settings.child('main_settings', 'bounds').children():
381
- bound_dict.update({bound.name(): (bound['min'], bound['max'])})
382
- return bound_dict
383
-
384
- def set_algorithm(self):
385
- self.algorithm = BayesianAlgorithm(
386
- ini_random=self.settings['main_settings', 'ini_random'],
387
- bounds=self.format_bounds(),)
388
-
389
- def ini_model(self):
390
- try:
391
- if self.model_class is None:
392
- self.set_model()
393
-
394
- self.modules_manager.selected_actuators_name = self.model_class.actuators_name
395
- self.modules_manager.selected_detectors_name = self.model_class.detectors_name
396
-
397
- self.enable_controls_opti(True)
398
- self.get_action('model_led').set_as_true()
399
- self.set_action_enabled('ini_model', False)
400
-
401
- self.viewer_observable.update_viewers(['Viewer0D', 'Viewer0D'],
402
- ['Fitness', 'Individual'])
403
- self.settings.child('models', 'ini_model').setValue(True)
404
- self.settings.child('models', 'ini_runner').setOpts(enabled=True)
405
- self.set_action_enabled('ini_runner', True)
406
-
407
- self.mainsettings_saver_loader.base_path = [self.model_class.__class__.__name__] + \
408
- self.modules_manager.selected_actuators_name
409
- self.mainsettings_saver_loader.load_config()
410
-
411
- try: # this is correct for Default Model and probably for all models...
412
- self.model_class.settings.child('optimizing_signal', 'data_probe').activate()
413
- except Exception:
414
- pass
415
-
416
- except Exception as e:
417
- logger.exception(str(e))
418
-
419
- def ini_optimisation_runner(self):
420
- if self.is_action_checked('ini_runner'):
421
- self.set_algorithm()
422
-
423
- self.settings.child('models', 'ini_runner').setValue(True)
424
- self.enl_index = 0
425
-
426
- self.ini_temp_file()
427
- self.ini_live_plot()
428
-
429
- self.runner_thread = QtCore.QThread()
430
- runner = OptimisationRunner(self.model_class, self.modules_manager, self.algorithm,
431
- self.get_stopping_parameters())
432
- self.runner_thread.runner = runner
433
- runner.algo_output_signal.connect(self.process_output)
434
- runner.algo_finished.connect(self.optimisation_done)
435
- self.command_runner.connect(runner.queue_command)
436
-
437
- runner.moveToThread(self.runner_thread)
438
-
439
- self.runner_thread.start()
440
- self.get_action('runner_led').set_as_true()
441
- self.set_action_enabled('run', True)
442
- self.model_class.runner_initialized()
443
- self.update_utility_function()
444
- else:
445
- if self.is_action_checked('run'):
446
- self.get_action('run').trigger()
447
- QtWidgets.QApplication.processEvents()
448
- self.runner_thread.terminate()
449
- self.get_action('runner_led').set_as_false()
450
-
451
- def clean_h5_temp(self):
452
- if self.temp_path is not None:
453
- try:
454
- self.h5temp.close()
455
- self.temp_path.cleanup()
456
- except Exception as e:
457
- logger.exception(str(e))
458
-
459
- def optimisation_done(self, dte: DataToExport):
460
- self.go_to_best()
461
- self.optimisation_done_signal.emit(dte)
462
-
463
- def process_output(self, dte: DataToExport):
464
-
465
- self.enl_index += 1
466
- dwa_kappa = dte.remove(dte.get_data_from_name(DataNames.Kappa.name))
467
- self.settings.child('main_settings', 'utility', 'kappa_actual').setValue(
468
- float(dwa_kappa[0][0])
469
- )
470
-
471
- dwa_data = dte.remove(dte.get_data_from_name(DataNames.ProbedData.name))
472
- dwa_actuators: DataActuator = dte.remove(dte.get_data_from_name(DataNames.Actuators.name))
473
- self.viewer_observable.show_data(dte)
474
-
475
- # dwa_observations = self.algorithm.get_dwa_obervations(
476
- # self.modules_manager.selected_actuators_name)
477
- self.model_class.update_plots()
478
-
479
- best_individual = dte.get_data_from_name(DataNames.Individual.name)
480
- best_indiv_as_list = [float(best_individual[ind][0]) for ind in range(len(best_individual))]
481
-
482
-
483
- self.enlargeable_saver.add_data('/RawData', dwa_data,
484
- axis_values=dwa_actuators.values())
485
- if len(best_indiv_as_list) == 1 or (
486
- len(best_indiv_as_list) == 2 and self.enl_index >= 3):
487
- self.update_data_plot(target_at=dwa_actuators.values(),
488
- crosshair_at=best_indiv_as_list)
489
-
490
- def update_data_plot(self, target_at=None, crosshair_at=None):
491
- self.live_plotter.load_plot_data(remove_navigation=False,
492
- crosshair_at=crosshair_at,
493
- target_at=target_at)
494
-
495
- def enable_controls_opti(self, enable: bool):
496
- pass
497
-
498
- def run_optimisation(self):
499
- if self.is_action_checked('run'):
500
- self.get_action('run').set_icon('pause')
501
- self.command_runner.emit(utils.ThreadCommand('start', {}))
502
- QtWidgets.QApplication.processEvents()
503
- QtWidgets.QApplication.processEvents()
504
- self.command_runner.emit(utils.ThreadCommand('run', {}))
505
- else:
506
- self.get_action('run').set_icon('run2')
507
- self.command_runner.emit(utils.ThreadCommand('stop', {}))
508
- self.set_action_enabled('gotobest', True)
509
-
510
- QtWidgets.QApplication.processEvents()
511
-
512
-
513
- class OptimisationRunner(QtCore.QObject):
514
- algo_output_signal = QtCore.Signal(DataToExport)
515
- algo_finished = QtCore.Signal(DataToExport)
516
-
517
- def __init__(self, model_class: BayesianModelGeneric, modules_manager: ModulesManager,
518
- algorithm: BayesianAlgorithm, stopping_params: StoppingParameters):
519
- super().__init__()
520
-
521
- self.det_done_datas: DataToExport = None
522
- self.input_from_dets: float = None
523
- self.outputs: List[np.ndarray] = []
524
- self.dte_actuators: DataToExport = None
525
- self.stopping_params: StoppingParameters = stopping_params
526
-
527
- self.model_class: BayesianModelGeneric = model_class
528
- self.modules_manager: ModulesManager = modules_manager
529
-
530
- self.running = True
531
-
532
- self.optimisation_algorithm: BayesianAlgorithm = algorithm
533
-
534
- self._ind_iter: int = 0
535
-
536
- @QtCore.Slot(utils.ThreadCommand)
537
- def queue_command(self, command: utils.ThreadCommand):
538
- """
539
- """
540
- if command.command == "run":
541
- self.run_opti(**command.attribute)
542
-
543
- elif command.command == "stop":
544
- self.running = False
545
-
546
- elif command.command == 'utility':
547
- utility_params: UtilityParameters = command.attribute
548
- self.optimisation_algorithm.set_utility_function(
549
- utility_params.kind,
550
- kappa=utility_params.kappa,
551
- xi=utility_params.xi,
552
- kappa_decay=utility_params.kappa_decay,
553
- kappa_decay_delay=utility_params.kappa_decay_delay)
554
-
555
- elif command.command == 'stopping':
556
- self.stopping_params: StoppingParameters = command.attribute
557
-
558
- elif command.command == 'bounds':
559
- self.optimisation_algorithm.set_bounds(command.attribute)
560
-
561
- def run_opti(self, sync_detectors=True, sync_acts=True):
562
- """Start the optimisation loop
563
-
564
- Parameters
565
- ----------
566
- sync_detectors: (bool) if True will make sure all selected detectors (if any) all got their data before calling
567
- the model
568
- sync_acts: (bool) if True will make sure all selected actuators (if any) all reached their target position
569
- before calling the model
570
- """
571
- self.running = True
572
- converged = False
573
- try:
574
- if sync_detectors:
575
- self.modules_manager.connect_detectors()
576
- if sync_acts:
577
- self.modules_manager.connect_actuators()
578
-
579
- self.current_time = time.perf_counter()
580
- logger.info('Optimisation loop starting')
581
- while self.running:
582
- self._ind_iter += 1
583
-
584
- next_target = self.optimisation_algorithm.ask()
585
-
586
- self.outputs = next_target
587
- self.output_to_actuators: DataToActuators =\
588
- self.model_class.convert_output(
589
- self.outputs,
590
- best_individual=self.optimisation_algorithm.best_individual
591
- )
592
-
593
- self.modules_manager.move_actuators(self.output_to_actuators,
594
- self.output_to_actuators.mode,
595
- polling=sync_acts)
596
-
597
- # Do the evaluation (measurements)
598
- self.det_done_datas = self.modules_manager.grab_data()
599
- self.input_from_dets = self.model_class.convert_input(self.det_done_datas)
600
-
601
- # Run the algo internal mechanic
602
- self.optimisation_algorithm.tell(float(self.input_from_dets))
603
-
604
- dte = DataToExport('algo',
605
- data=[self.individual_as_data(
606
- np.array([self.optimisation_algorithm.best_fitness]),
607
- DataNames.Fitness.name),
608
- self.individual_as_data(
609
- self.optimisation_algorithm.best_individual,
610
- DataNames.Individual.name),
611
- DataCalculated(DataNames.ProbedData.name,
612
- data=[np.array([self.input_from_dets])],
613
- ),
614
- self.output_to_actuators.merge_as_dwa(
615
- 'Data0D', DataNames.Actuators.name),
616
- DataCalculated(
617
- DataNames.Kappa.name,
618
- data=[
619
- np.array([self.optimisation_algorithm.kappa])])
620
- ])
621
- self.algo_output_signal.emit(dte)
622
-
623
- self.optimisation_algorithm.update_utility_function()
624
-
625
- if self.optimisation_algorithm.stopping(self._ind_iter, self.stopping_params):
626
- converged = True
627
- break
628
-
629
- self.current_time = time.perf_counter()
630
- QtWidgets.QApplication.processEvents()
631
-
632
- logger.info('Optimisation loop exiting')
633
- self.modules_manager.connect_actuators(False)
634
- self.modules_manager.connect_detectors(False)
635
-
636
- if converged:
637
- self.algo_finished.emit(dte)
638
-
639
- except Exception as e:
640
- logger.exception(str(e))
641
-
642
- @staticmethod
643
- def individual_as_data(individual: np.ndarray, name: str = 'Individual') -> DataCalculated:
644
- return DataCalculated(name, data=[np.atleast_1d(np.squeeze(coordinate)) for coordinate in
645
- np.atleast_1d(np.squeeze(individual))])
646
-
647
-
648
- def main(init_qt=True):
649
- import sys
650
- from pathlib import Path
651
- from pymodaq.utils.daq_utils import get_set_preset_path
652
-
653
- if init_qt: # used for the test suite
654
- app = mkQApp("PyMoDAQ Dashboard")
655
-
656
- from pymodaq.dashboard import DashBoard
657
-
658
- win = QtWidgets.QMainWindow()
659
- area = gutils.dock.DockArea()
660
- win.setCentralWidget(area)
661
- win.resize(1000, 500)
662
-
663
- dashboard = DashBoard(area)
664
- daq_scan = None
665
- file = Path(get_set_preset_path()).joinpath(f"{'beam_steering_mock'}.xml")
666
-
667
- if file.exists():
668
- dashboard.set_preset_mode(file)
669
- daq_scan = dashboard.load_bayesian()
670
- else:
671
- msgBox = QtWidgets.QMessageBox()
672
- msgBox.setText(f"The default file specified in the configuration file does not exists!\n"
673
- f"{file}\n"
674
- f"Impossible to load the DAQScan Module")
675
- msgBox.setStandardButtons(msgBox.Ok)
676
- ret = msgBox.exec()
677
-
678
- if init_qt:
679
- sys.exit(app.exec_())
680
- return dashboard, daq_scan, win
681
-
682
-
683
- if __name__ == '__main__':
684
- main()
685
-
@@ -1,2 +0,0 @@
1
- [LocalizedFileNames]
2
- Command Prompt.lnk=@%SystemRoot%\system32\shell32.dll,-22022