| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import logging |
| 26 |
|
| 27 |
import os |
| 28 |
import platform |
| 29 |
import subprocess |
| 30 |
import tempfile |
| 31 |
import shutil |
| 32 |
|
| 33 |
import pyqtgraph as pg |
| 34 |
|
| 35 |
from ..browser import BrowserItem |
| 36 |
from ..manager import Manager, Experiment |
| 37 |
from ..Qt import QtCore, QtWidgets, QtGui |
| 38 |
from ..widgets import ( |
| 39 |
PlotWidget, |
| 40 |
BrowserWidget, |
| 41 |
InputsWidget, |
| 42 |
LogWidget, |
| 43 |
ResultsDialog, |
| 44 |
SequencerWidget, |
| 45 |
FileInputWidget, |
| 46 |
EstimatorWidget, |
| 47 |
) |
| 48 |
from ...experiment import Results, Procedure, unique_filename |
| 49 |
|
| 50 |
log = logging.getLogger(__name__) |
| 51 |
log.addHandler(logging.NullHandler()) |
| 52 |
|
| 53 |
|
| 54 |
class ManagedWindowBase(QtWidgets.QMainWindow): |
| 55 |
""" |
| 56 |
Base class for GUI experiment management . |
| 57 |
|
| 58 |
The ManagedWindowBase provides an interface for inputting experiment |
| 59 |
parameters, running several experiments |
| 60 |
(:class:`~pymeasure.experiment.procedure.Procedure`), plotting |
| 61 |
result curves, and listing the experiments conducted during a session. |
| 62 |
|
| 63 |
The ManagedWindowBase uses a Manager to control Workers in a Queue, |
| 64 |
and provides a simple interface. |
| 65 |
The :meth:`~pymeasure.display.windows.managed_window.ManagedWindowBase.queue` method must be |
| 66 |
overridden by the child class. |
| 67 |
|
| 68 |
The ManagedWindowBase allow user to define a set of widget that display information about the |
| 69 |
experiment. The information displayed may include: plots, tabular view, logging information,... |
| 70 |
|
| 71 |
This class is not intended to be used directly, but it should be subclassed to provide some |
| 72 |
appropriate widget list. Example of classes usable as element of widget list are: |
| 73 |
|
| 74 |
- :class:`~pymeasure.display.widgets.log_widget.LogWidget` |
| 75 |
- :class:`~pymeasure.display.widgets.plot_widget.PlotWidget` |
| 76 |
- :class:`~pymeasure.display.widgets.image_widget.ImageWidget` |
| 77 |
|
| 78 |
Of course, users can define its own widget making sure that inherits from |
| 79 |
:class:`~pymeasure.display.widgets.tab_widget.TabWidget`. |
| 80 |
|
| 81 |
Examples of ready to use classes inherited from ManagedWindowBase are: |
| 82 |
|
| 83 |
- :class:`~pymeasure.display.windows.managed_window.ManagedWindow` |
| 84 |
- :class:`~pymeasure.display.windows.managed_image_window.ManagedImageWindow` |
| 85 |
|
| 86 |
.. seealso:: |
| 87 |
|
| 88 |
Tutorial :ref:`tutorial-managedwindow` |
| 89 |
A tutorial and example on the basic configuration and usage of ManagedWindow. |
| 90 |
|
| 91 |
Parameters for :code:`__init__` constructor. |
| 92 |
|
| 93 |
:param procedure_class: procedure class describing the experiment (see |
| 94 |
:class:`~pymeasure.experiment.procedure.Procedure`) |
| 95 |
:param widget_list: list of widget to be displayed in the GUI |
| 96 |
:param inputs: list of :class:`~pymeasure.experiment.parameters.Parameter` instance variable |
| 97 |
names, which the display will generate graphical fields for |
| 98 |
:param displays: list of :class:`~pymeasure.experiment.parameters.Parameter` instance variable |
| 99 |
names displayed in the browser window |
| 100 |
:param log_channel: :code:`logging.Logger` instance to use for logging output |
| 101 |
:param log_level: logging level |
| 102 |
:param parent: Parent widget or :code:`None` |
| 103 |
:param sequencer: a boolean stating whether or not the sequencer has to be included into the |
| 104 |
window |
| 105 |
:param sequencer_inputs: either :code:`None` or a list of the parameter names to be scanned |
| 106 |
over. If no list of parameters is given, the parameters displayed in the manager queue |
| 107 |
are used. |
| 108 |
:param sequence_file: simple text file to quickly load a pre-defined sequence with the |
| 109 |
:code:`Load sequence` button |
| 110 |
:param inputs_in_scrollarea: boolean that display or hide a scrollbar to the input area |
| 111 |
:param enable_file_input: a boolean controlling whether a |
| 112 |
:class:`~pymeasure.display.widgets.fileinput_widget.FileInputWidget` to specify where the |
| 113 |
experiment's result will be saved is displayed (True, default) or not (False). This widget |
| 114 |
contains a field to enter the (base of the) filename (with or without extension; if absent, |
| 115 |
the extension will be appended). This field also allows for placeholders to use |
| 116 |
parameter-values and metadata-value in the filename. The widget also has a field to select |
| 117 |
the directory where the file is to be stored, and a toggle to control whether the data |
| 118 |
should be saved to the selected file, or not (i.e., to a temporary file instead). |
| 119 |
:param hide_groups: a boolean controlling whether parameter groups are hidden (True, default) |
| 120 |
or disabled/grayed-out (False) when the group conditions are not met. |
| 121 |
|
| 122 |
""" |
| 123 |
|
| 124 |
def __init__(self, |
| 125 |
procedure_class, |
| 126 |
widget_list=(), |
| 127 |
inputs=(), |
| 128 |
displays=(), |
| 129 |
log_channel='', |
| 130 |
log_level=logging.INFO, |
| 131 |
parent=None, |
| 132 |
sequencer=False, |
| 133 |
sequencer_inputs=None, |
| 134 |
sequence_file=None, |
| 135 |
inputs_in_scrollarea=False, |
| 136 |
enable_file_input=True, |
| 137 |
hide_groups=True, |
| 138 |
): |
| 139 |
|
| 140 |
super().__init__(parent) |
| 141 |
app = QtCore.QCoreApplication.instance() |
| 142 |
app.aboutToQuit.connect(self.quit) |
| 143 |
self.procedure_class = procedure_class |
| 144 |
self.inputs = inputs |
| 145 |
self.hide_groups = hide_groups |
| 146 |
self.displays = displays |
| 147 |
self.use_sequencer = sequencer |
| 148 |
self.sequencer_inputs = sequencer_inputs |
| 149 |
self.sequence_file = sequence_file |
| 150 |
self.inputs_in_scrollarea = inputs_in_scrollarea |
| 151 |
self.enable_file_input = enable_file_input |
| 152 |
self.log = logging.getLogger(log_channel) |
| 153 |
self.log_level = log_level |
| 154 |
log.setLevel(log_level) |
| 155 |
self.log.setLevel(log_level) |
| 156 |
self.widget_list = widget_list |
| 157 |
|
| 158 |
# Check if the get_estimates function is reimplemented |
| 159 |
self.use_estimator = not self.procedure_class.get_estimates == Procedure.get_estimates |
| 160 |
|
| 161 |
# Validate DATA_COLUMNS fit pymeasure column header format |
| 162 |
Procedure.parse_columns(self.procedure_class.DATA_COLUMNS) |
| 163 |
|
| 164 |
self._setup_ui() |
| 165 |
self._layout() |
| 166 |
|
| 167 |
def _setup_ui(self): |
| 168 |
|
| 169 |
self.queue_button = QtWidgets.QPushButton('Queue', self) |
| 170 |
self.queue_button.clicked.connect(self._queue) |
| 171 |
|
| 172 |
self.abort_button = QtWidgets.QPushButton('Abort', self) |
| 173 |
self.abort_button.setEnabled(False) |
| 174 |
self.abort_button.clicked.connect(self.abort) |
| 175 |
|
| 176 |
self.browser_widget = BrowserWidget( |
| 177 |
self.procedure_class, |
| 178 |
self.displays, |
| 179 |
[], # This value will be patched by subclasses, if needed |
| 180 |
parent=self |
| 181 |
) |
| 182 |
self.browser_widget.show_button.clicked.connect(self.show_experiments) |
| 183 |
self.browser_widget.hide_button.clicked.connect(self.hide_experiments) |
| 184 |
self.browser_widget.clear_button.clicked.connect(self.clear_experiments) |
| 185 |
self.browser_widget.open_button.clicked.connect(self.open_experiment) |
| 186 |
self.browser = self.browser_widget.browser |
| 187 |
|
| 188 |
self.browser.setContextMenuPolicy(QtCore.Qt.ContextMenuPolicy.CustomContextMenu) |
| 189 |
self.browser.customContextMenuRequested.connect(self.browser_item_menu) |
| 190 |
self.browser.itemChanged.connect(self.browser_item_changed) |
| 191 |
|
| 192 |
self.inputs = InputsWidget( |
| 193 |
self.procedure_class, |
| 194 |
self.inputs, |
| 195 |
parent=self, |
| 196 |
hide_groups=self.hide_groups, |
| 197 |
inputs_in_scrollarea=self.inputs_in_scrollarea, |
| 198 |
) |
| 199 |
if self.enable_file_input: |
| 200 |
self.file_input = FileInputWidget(parent=self) |
| 201 |
|
| 202 |
self.manager = Manager(self.widget_list, |
| 203 |
self.browser, |
| 204 |
log_level=self.log_level, |
| 205 |
parent=self) |
| 206 |
self.manager.abort_returned.connect(self.abort_returned) |
| 207 |
self.manager.queued.connect(self.queued) |
| 208 |
self.manager.running.connect(self.running) |
| 209 |
self.manager.finished.connect(self.finished) |
| 210 |
self.manager.log.connect(self.log.handle) |
| 211 |
|
| 212 |
if self.use_sequencer: |
| 213 |
self.sequencer = SequencerWidget( |
| 214 |
self.sequencer_inputs, |
| 215 |
self.sequence_file, |
| 216 |
parent=self |
| 217 |
) |
| 218 |
|
| 219 |
if self.use_estimator: |
| 220 |
self.estimator = EstimatorWidget( |
| 221 |
parent=self |
| 222 |
) |
| 223 |
|
| 224 |
def _layout(self): |
| 225 |
self.main = QtWidgets.QWidget(self) |
| 226 |
|
| 227 |
inputs_dock = QtWidgets.QWidget(self) |
| 228 |
inputs_vbox = QtWidgets.QVBoxLayout(self.main) |
| 229 |
|
| 230 |
queue_abort_hbox = QtWidgets.QHBoxLayout() |
| 231 |
queue_abort_hbox.setSpacing(10) |
| 232 |
queue_abort_hbox.setContentsMargins(-1, 6, -1, 6) |
| 233 |
queue_abort_hbox.addWidget(self.queue_button) |
| 234 |
queue_abort_hbox.addWidget(self.abort_button) |
| 235 |
queue_abort_hbox.addStretch() |
| 236 |
|
| 237 |
inputs_vbox.addWidget(self.inputs) |
| 238 |
inputs_vbox.addSpacing(15) |
| 239 |
if self.enable_file_input: |
| 240 |
inputs_vbox.addWidget(self.file_input) |
| 241 |
inputs_vbox.addSpacing(15) |
| 242 |
|
| 243 |
inputs_vbox.addLayout(queue_abort_hbox) |
| 244 |
|
| 245 |
inputs_vbox.addStretch(0) |
| 246 |
inputs_dock.setLayout(inputs_vbox) |
| 247 |
|
| 248 |
dock = QtWidgets.QDockWidget('Input Parameters') |
| 249 |
dock.setWidget(inputs_dock) |
| 250 |
dock.setFeatures(QtWidgets.QDockWidget.DockWidgetFeature.NoDockWidgetFeatures) |
| 251 |
self.addDockWidget(QtCore.Qt.DockWidgetArea.LeftDockWidgetArea, dock) |
| 252 |
|
| 253 |
if self.use_sequencer: |
| 254 |
sequencer_dock = QtWidgets.QDockWidget('Sequencer') |
| 255 |
sequencer_dock.setWidget(self.sequencer) |
| 256 |
sequencer_dock.setFeatures(QtWidgets.QDockWidget.DockWidgetFeature.NoDockWidgetFeatures) |
| 257 |
self.addDockWidget(QtCore.Qt.DockWidgetArea.LeftDockWidgetArea, sequencer_dock) |
| 258 |
|
| 259 |
if self.use_estimator: |
| 260 |
estimator_dock = QtWidgets.QDockWidget('Estimator') |
| 261 |
estimator_dock.setWidget(self.estimator) |
| 262 |
estimator_dock.setFeatures(QtWidgets.QDockWidget.DockWidgetFeature.NoDockWidgetFeatures) |
| 263 |
self.addDockWidget(QtCore.Qt.DockWidgetArea.LeftDockWidgetArea, estimator_dock) |
| 264 |
|
| 265 |
self.tabs = QtWidgets.QTabWidget(self.main) |
| 266 |
for wdg in self.widget_list: |
| 267 |
self.tabs.addTab(wdg, wdg.name) |
| 268 |
|
| 269 |
splitter = QtWidgets.QSplitter(QtCore.Qt.Orientation.Vertical) |
| 270 |
splitter.addWidget(self.tabs) |
| 271 |
splitter.addWidget(self.browser_widget) |
| 272 |
|
| 273 |
vbox = QtWidgets.QVBoxLayout(self.main) |
| 274 |
vbox.setSpacing(0) |
| 275 |
vbox.addWidget(splitter) |
| 276 |
|
| 277 |
self.main.setLayout(vbox) |
| 278 |
self.setCentralWidget(self.main) |
| 279 |
self.main.show() |
| 280 |
self.resize(1000, 800) |
| 281 |
|
| 282 |
def quit(self, evt=None): |
| 283 |
if self.manager.is_running(): |
| 284 |
self.abort() |
| 285 |
|
| 286 |
self.close() |
| 287 |
|
| 288 |
def browser_item_changed(self, item, column): |
| 289 |
if column == 0: |
| 290 |
state = item.checkState(0) |
| 291 |
experiment = self.manager.experiments.with_browser_item(item) |
| 292 |
if state == QtCore.Qt.CheckState.Unchecked: |
| 293 |
for curve in experiment.curve_list: |
| 294 |
if curve: |
| 295 |
curve.wdg.remove(curve) |
| 296 |
else: |
| 297 |
for curve in experiment.curve_list: |
| 298 |
if curve: |
| 299 |
curve.wdg.load(curve) |
| 300 |
|
| 301 |
def browser_item_menu(self, position): |
| 302 |
item = self.browser.itemAt(position) |
| 303 |
|
| 304 |
if item is not None: |
| 305 |
experiment = self.manager.experiments.with_browser_item(item) |
| 306 |
|
| 307 |
menu = QtWidgets.QMenu(self) |
| 308 |
|
| 309 |
# Open |
| 310 |
action_open = QtGui.QAction(menu) |
| 311 |
action_open.setText("Open Data Externally") |
| 312 |
action_open.triggered.connect( |
| 313 |
lambda: self.open_file_externally(experiment.results.data_filename)) |
| 314 |
menu.addAction(action_open) |
| 315 |
|
| 316 |
# Save a copy of the datafile |
| 317 |
action_save = QtGui.QAction(menu) |
| 318 |
action_save.setText("Save Data File Copy") |
| 319 |
action_save.triggered.connect( |
| 320 |
lambda: self.save_experiment_copy(experiment.results.data_filename)) |
| 321 |
menu.addAction(action_save) |
| 322 |
|
| 323 |
# Change Color |
| 324 |
action_change_color = QtGui.QAction(menu) |
| 325 |
action_change_color.setText("Change Color") |
| 326 |
action_change_color.triggered.connect( |
| 327 |
lambda: self.change_color(experiment)) |
| 328 |
menu.addAction(action_change_color) |
| 329 |
|
| 330 |
# Remove |
| 331 |
action_remove = QtGui.QAction(menu) |
| 332 |
action_remove.setText("Remove Graph") |
| 333 |
if self.manager.is_running(): |
| 334 |
if self.manager.running_experiment() == experiment: # Experiment running |
| 335 |
action_remove.setEnabled(False) |
| 336 |
action_remove.triggered.connect(lambda: self.remove_experiment(experiment)) |
| 337 |
menu.addAction(action_remove) |
| 338 |
|
| 339 |
# Delete |
| 340 |
action_delete = QtGui.QAction(menu) |
| 341 |
action_delete.setText("Delete Data File") |
| 342 |
if self.manager.is_running(): |
| 343 |
if self.manager.running_experiment() == experiment: # Experiment running |
| 344 |
action_delete.setEnabled(False) |
| 345 |
action_delete.triggered.connect(lambda: self.delete_experiment_data(experiment)) |
| 346 |
menu.addAction(action_delete) |
| 347 |
|
| 348 |
# Use parameters |
| 349 |
action_use = QtGui.QAction(menu) |
| 350 |
action_use.setText("Use These Parameters") |
| 351 |
action_use.triggered.connect( |
| 352 |
lambda: self.set_parameters(experiment.procedure.parameter_objects())) |
| 353 |
menu.addAction(action_use) |
| 354 |
menu.exec(self.browser.viewport().mapToGlobal(position)) |
| 355 |
|
| 356 |
def remove_experiment(self, experiment): |
| 357 |
reply = QtWidgets.QMessageBox.question(self, 'Remove Graph', |
| 358 |
"Are you sure you want to remove the graph?", |
| 359 |
QtWidgets.QMessageBox.StandardButton.Yes | |
| 360 |
QtWidgets.QMessageBox.StandardButton.No, |
| 361 |
QtWidgets.QMessageBox.StandardButton.No) |
| 362 |
if reply == QtWidgets.QMessageBox.StandardButton.Yes: |
| 363 |
self.manager.remove(experiment) |
| 364 |
|
| 365 |
def delete_experiment_data(self, experiment): |
| 366 |
reply = QtWidgets.QMessageBox.question(self, 'Delete Data', |
| 367 |
"Are you sure you want to delete this data file?", |
| 368 |
QtWidgets.QMessageBox.StandardButton.Yes | |
| 369 |
QtWidgets.QMessageBox.StandardButton.No, |
| 370 |
QtWidgets.QMessageBox.StandardButton.No) |
| 371 |
if reply == QtWidgets.QMessageBox.StandardButton.Yes: |
| 372 |
self.manager.remove(experiment) |
| 373 |
os.unlink(experiment.data_filename) |
| 374 |
|
| 375 |
def show_experiments(self): |
| 376 |
root = self.browser.invisibleRootItem() |
| 377 |
for i in range(root.childCount()): |
| 378 |
item = root.child(i) |
| 379 |
item.setCheckState(0, QtCore.Qt.CheckState.Checked) |
| 380 |
|
| 381 |
def hide_experiments(self): |
| 382 |
root = self.browser.invisibleRootItem() |
| 383 |
for i in range(root.childCount()): |
| 384 |
item = root.child(i) |
| 385 |
item.setCheckState(0, QtCore.Qt.CheckState.Unchecked) |
| 386 |
|
| 387 |
def clear_experiments(self): |
| 388 |
self.manager.clear() |
| 389 |
|
| 390 |
def open_experiment(self): |
| 391 |
dialog = ResultsDialog(self.procedure_class, |
| 392 |
widget_list=self.widget_list) |
| 393 |
if dialog.exec(): |
| 394 |
filenames = dialog.selectedFiles() |
| 395 |
for filename in map(str, filenames): |
| 396 |
if filename in self.manager.experiments: |
| 397 |
QtWidgets.QMessageBox.warning( |
| 398 |
self, "Load Error", |
| 399 |
"The file %s cannot be opened twice." % os.path.basename(filename) |
| 400 |
) |
| 401 |
elif filename == '': |
| 402 |
return |
| 403 |
else: |
| 404 |
results = Results.load(filename) |
| 405 |
experiment = self.new_experiment(results) |
| 406 |
for curve in experiment.curve_list: |
| 407 |
if curve: |
| 408 |
curve.update_data() |
| 409 |
experiment.browser_item.progressbar.setValue(100) |
| 410 |
self.manager.load(experiment) |
| 411 |
log.info('Opened data file %s' % filename) |
| 412 |
|
| 413 |
def save_experiment_copy(self, source_filename): |
| 414 |
"""Save a copy of the datafile to a selected folder and file. |
| 415 |
Primarily useful for experiments that are stored in a temporary file. |
| 416 |
""" |
| 417 |
dialog = QtWidgets.QFileDialog(self) |
| 418 |
dialog.setFileMode(QtWidgets.QFileDialog.AnyFile) |
| 419 |
dialog.setAcceptMode(QtWidgets.QFileDialog.AcceptSave) |
| 420 |
dialog.setDefaultSuffix('.csv') |
| 421 |
|
| 422 |
if dialog.exec(): |
| 423 |
filename = dialog.selectedFiles()[0] |
| 424 |
shutil.copy2(source_filename, filename) |
| 425 |
|
| 426 |
log.info(f"Copied data from '{source_filename}' to '{filename}'.") |
| 427 |
|
| 428 |
def change_color(self, experiment): |
| 429 |
color = QtWidgets.QColorDialog.getColor( |
| 430 |
parent=self) |
| 431 |
if color.isValid(): |
| 432 |
pixelmap = QtGui.QPixmap(24, 24) |
| 433 |
pixelmap.fill(color) |
| 434 |
experiment.browser_item.setIcon(0, QtGui.QIcon(pixelmap)) |
| 435 |
for curve in experiment.curve_list: |
| 436 |
if curve: |
| 437 |
curve.wdg.set_color(curve, color=color) |
| 438 |
|
| 439 |
def open_file_externally(self, filename): |
| 440 |
""" Method to open the datafile using an external editor or viewer. Uses the default |
| 441 |
application to open a datafile of this filetype, but can be overridden by the child |
| 442 |
class in order to open the file in another application of choice. |
| 443 |
""" |
| 444 |
system = platform.system() |
| 445 |
if (system == 'Windows'): |
| 446 |
# The empty argument after the start is needed to be able to cope |
| 447 |
# correctly with filenames with spaces |
| 448 |
_ = subprocess.Popen(['start', '', filename], shell=True) |
| 449 |
elif (system == 'Linux'): |
| 450 |
_ = subprocess.Popen(['xdg-open', filename]) |
| 451 |
elif (system == 'Darwin'): |
| 452 |
_ = subprocess.Popen(['open', filename]) |
| 453 |
else: |
| 454 |
raise Exception("{cls} method open_file_externally does not support {system} OS".format( |
| 455 |
cls=type(self).__name__, system=system)) |
| 456 |
|
| 457 |
def make_procedure(self): |
| 458 |
if not isinstance(self.inputs, InputsWidget): |
| 459 |
raise Exception("ManagedWindow can not make a Procedure" |
| 460 |
" without a InputsWidget type") |
| 461 |
return self.inputs.get_procedure() |
| 462 |
|
| 463 |
def new_curve(self, wdg, results, color=None, **kwargs): |
| 464 |
if color is None: |
| 465 |
color = pg.intColor(self.browser.topLevelItemCount() % 8) |
| 466 |
return wdg.new_curve(results, color=color, **kwargs) |
| 467 |
|
| 468 |
def new_experiment(self, results, curve=None): |
| 469 |
if curve is None: |
| 470 |
curve_list = [] |
| 471 |
for wdg in self.widget_list: |
| 472 |
new_curve = self.new_curve(wdg, results) |
| 473 |
if isinstance(new_curve, (tuple, list)): |
| 474 |
curve_list.extend(new_curve) |
| 475 |
else: |
| 476 |
curve_list.append(new_curve) |
| 477 |
else: |
| 478 |
curve_list = curve[:] |
| 479 |
|
| 480 |
curve_color = pg.intColor(0) |
| 481 |
for curve in curve_list: |
| 482 |
if hasattr(curve, 'color'): |
| 483 |
curve_color = curve.color |
| 484 |
break |
| 485 |
|
| 486 |
browser_item = BrowserItem(results, curve_color) |
| 487 |
return Experiment(results, curve_list, browser_item) |
| 488 |
|
| 489 |
def set_parameters(self, parameters): |
| 490 |
""" This method should be overwritten by the child class. The |
| 491 |
parameters argument is a dictionary of Parameter objects. |
| 492 |
The Parameters should overwrite the GUI values so that a user |
| 493 |
can click "Queue" to capture the same parameters. |
| 494 |
""" |
| 495 |
if not isinstance(self.inputs, InputsWidget): |
| 496 |
raise Exception("ManagedWindow can not set parameters" |
| 497 |
" without a InputsWidget") |
| 498 |
self.inputs.set_parameters(parameters) |
| 499 |
|
| 500 |
def _queue(self, checked): |
| 501 |
""" This method is a wrapper for the `self.queue` method to be connected |
| 502 |
to the `queue` button. It catches the positional argument that is passed |
| 503 |
when it is called by the button and calls the `self.queue` method without |
| 504 |
any arguments. |
| 505 |
""" |
| 506 |
self.queue() |
| 507 |
|
| 508 |
def queue(self, procedure=None): |
| 509 |
""" Queue a measurement based on the parameters in the input-widget. |
| 510 |
|
| 511 |
Semi-abstract method, which must be overridden by the child class if the filename- and |
| 512 |
directory-inputs are disabled. When filename- and directory inputs are enabled, overwriting |
| 513 |
is not required, but can be done for custom naming, input processing, or other features. |
| 514 |
|
| 515 |
Implementations must call ``self.manager.queue(experiment)`` and pass |
| 516 |
an ``experiment`` |
| 517 |
(:class:`~pymeasure.experiment.experiment.Experiment`) object which |
| 518 |
contains the |
| 519 |
:class:`~pymeasure.experiment.results.Results` and |
| 520 |
:class:`~pymeasure.experiment.procedure.Procedure` to be run. |
| 521 |
|
| 522 |
The optional `procedure` argument is not required for a basic implementation, |
| 523 |
but is required when the |
| 524 |
:class:`~pymeasure.display.widgets.sequencer_widget.SequencerWidget` is used. |
| 525 |
|
| 526 |
For example: |
| 527 |
|
| 528 |
.. code-block:: python |
| 529 |
|
| 530 |
def queue(self): |
| 531 |
filename = unique_filename('results', prefix="data") # from pymeasure.experiment |
| 532 |
|
| 533 |
procedure = self.make_procedure() # Procedure class was passed at construction |
| 534 |
results = Results(procedure, filename) |
| 535 |
experiment = self.new_experiment(results) |
| 536 |
|
| 537 |
self.manager.queue(experiment) |
| 538 |
|
| 539 |
""" |
| 540 |
|
| 541 |
# Check if the filename and the directory inputs are available |
| 542 |
if not self.enable_file_input: |
| 543 |
raise NotImplementedError("Queue method must be overwritten if the filename- and " |
| 544 |
"directory-inputs are disabled.") |
| 545 |
|
| 546 |
if procedure is None: |
| 547 |
procedure = self.make_procedure() |
| 548 |
|
| 549 |
if self.store_measurement: |
| 550 |
try: |
| 551 |
filename = unique_filename( |
| 552 |
self.directory, |
| 553 |
prefix=self.file_input.filename_base, |
| 554 |
datetimeformat="", |
| 555 |
procedure=procedure, |
| 556 |
ext=self.file_input.filename_extension, |
| 557 |
) |
| 558 |
except KeyError as E: |
| 559 |
if not E.args[0].startswith("The following placeholder-keys are not valid:"): |
| 560 |
raise E from None |
| 561 |
log.error(f"Invalid filename provided: {E.args[0]}") |
| 562 |
return |
| 563 |
else: |
| 564 |
filename = tempfile.mktemp(prefix='TempFile_', suffix='.csv') |
| 565 |
|
| 566 |
results = Results(procedure, filename) |
| 567 |
|
| 568 |
experiment = self.new_experiment(results) |
| 569 |
self.manager.queue(experiment) |
| 570 |
|
| 571 |
def abort(self): |
| 572 |
self.abort_button.setEnabled(False) |
| 573 |
self.abort_button.setText("Resume") |
| 574 |
self.abort_button.clicked.disconnect() |
| 575 |
self.abort_button.clicked.connect(self.resume) |
| 576 |
try: |
| 577 |
self.manager.abort() |
| 578 |
except: # noqa |
| 579 |
log.error('Failed to abort experiment', exc_info=True) |
| 580 |
self.abort_button.setText("Abort") |
| 581 |
self.abort_button.clicked.disconnect() |
| 582 |
self.abort_button.clicked.connect(self.abort) |
| 583 |
|
| 584 |
def resume(self): |
| 585 |
self.abort_button.setText("Abort") |
| 586 |
self.abort_button.clicked.disconnect() |
| 587 |
self.abort_button.clicked.connect(self.abort) |
| 588 |
if self.manager.experiments.has_next(): |
| 589 |
self.manager.resume() |
| 590 |
else: |
| 591 |
self.abort_button.setEnabled(False) |
| 592 |
|
| 593 |
def queued(self, experiment): |
| 594 |
self.abort_button.setEnabled(True) |
| 595 |
self.browser_widget.show_button.setEnabled(True) |
| 596 |
self.browser_widget.hide_button.setEnabled(True) |
| 597 |
self.browser_widget.clear_button.setEnabled(True) |
| 598 |
|
| 599 |
def running(self, experiment): |
| 600 |
self.browser_widget.clear_button.setEnabled(False) |
| 601 |
|
| 602 |
def abort_returned(self, experiment): |
| 603 |
if self.manager.experiments.has_next(): |
| 604 |
self.abort_button.setText("Resume") |
| 605 |
self.abort_button.setEnabled(True) |
| 606 |
else: |
| 607 |
self.browser_widget.clear_button.setEnabled(True) |
| 608 |
|
| 609 |
def finished(self, experiment): |
| 610 |
if not self.manager.experiments.has_next(): |
| 611 |
self.abort_button.setEnabled(False) |
| 612 |
self.browser_widget.clear_button.setEnabled(True) |
| 613 |
|
| 614 |
@property |
| 615 |
def directory(self): |
| 616 |
if not self.enable_file_input: |
| 617 |
raise AttributeError("File-input widget not enabled (i.e., enable_file_input == False)") |
| 618 |
return self.file_input.directory |
| 619 |
|
| 620 |
@directory.setter |
| 621 |
def directory(self, value): |
| 622 |
if not self.enable_file_input: |
| 623 |
raise AttributeError("File-input widget not enabled (i.e., enable_file_input == False)") |
| 624 |
self.file_input.directory = value |
| 625 |
|
| 626 |
@property |
| 627 |
def filename(self): |
| 628 |
if not self.enable_file_input: |
| 629 |
raise AttributeError("File-input widget not enabled (i.e., enable_file_input == False)") |
| 630 |
return self.file_input.filename |
| 631 |
|
| 632 |
@filename.setter |
| 633 |
def filename(self, value): |
| 634 |
if not self.enable_file_input: |
| 635 |
raise AttributeError("File-input widget not enabled (i.e., enable_file_input == False)") |
| 636 |
self.file_input.filename = value |
| 637 |
|
| 638 |
@property |
| 639 |
def store_measurement(self): |
| 640 |
if not self.enable_file_input: |
| 641 |
raise AttributeError("File-input widget not enabled (i.e., enable_file_input == False)") |
| 642 |
return self.file_input.store_measurement |
| 643 |
|
| 644 |
@store_measurement.setter |
| 645 |
def store_measurement(self, value): |
| 646 |
if not self.enable_file_input: |
| 647 |
raise AttributeError("File-input widget not enabled (i.e., enable_file_input == False)") |
| 648 |
self.file_input.store_measurement = value |
| 649 |
|
| 650 |
|
| 651 |
class ManagedWindow(ManagedWindowBase): |
| 652 |
""" |
| 653 |
Display experiment output with an |
| 654 |
:class:`~pymeasure.display.widgets.plot_widget.PlotWidget` class. |
| 655 |
|
| 656 |
.. seealso:: |
| 657 |
|
| 658 |
Tutorial :ref:`tutorial-managedwindow` |
| 659 |
A tutorial and example on the basic configuration and usage of ManagedWindow. |
| 660 |
|
| 661 |
:param procedure_class: procedure class describing the experiment (see |
| 662 |
:class:`~pymeasure.experiment.procedure.Procedure`) |
| 663 |
:param x_axis: the initial data-column for the x-axis of the plot |
| 664 |
:param y_axis: the initial data-column for the y-axis of the plot |
| 665 |
:param linewidth: linewidth for the displayed curves, default is 1 |
| 666 |
:param log_fmt: formatting string for the log-widget |
| 667 |
:param log_datefmt: formatting string for the date in the log-widget |
| 668 |
:param \\**kwargs: optional keyword arguments that will be passed to |
| 669 |
:class:`~pymeasure.display.windows.managed_window.ManagedWindowBase` |
| 670 |
|
| 671 |
""" |
| 672 |
|
| 673 |
def __init__(self, procedure_class, x_axis=None, y_axis=None, linewidth=1, |
| 674 |
log_fmt=None, log_datefmt=None, **kwargs): |
| 675 |
self.x_axis = x_axis |
| 676 |
self.y_axis = y_axis |
| 677 |
self.log_widget = LogWidget("Experiment Log", fmt=log_fmt, datefmt=log_datefmt) |
| 678 |
self.plot_widget = PlotWidget("Results Graph", procedure_class.DATA_COLUMNS, self.x_axis, |
| 679 |
self.y_axis, linewidth=linewidth) |
| 680 |
self.plot_widget.setMinimumSize(100, 200) |
| 681 |
|
| 682 |
if "widget_list" not in kwargs: |
| 683 |
kwargs["widget_list"] = () |
| 684 |
kwargs["widget_list"] = kwargs["widget_list"] + (self.plot_widget, self.log_widget) |
| 685 |
|
| 686 |
super().__init__(procedure_class, **kwargs) |
| 687 |
|
| 688 |
# Setup measured_quantities once we know x_axis and y_axis |
| 689 |
self.browser_widget.browser.measured_quantities.update([self.x_axis, self.y_axis]) |
| 690 |
|
| 691 |
logging.getLogger().addHandler(self.log_widget.handler) # needs to be in Qt context? |
| 692 |
log.setLevel(self.log_level) |
| 693 |
log.info("ManagedWindow connected to logging") |