| 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 copy |
| 28 |
import argparse |
| 29 |
|
| 30 |
try: |
| 31 |
import progressbar |
| 32 |
# Check that progressbar is progressbar2 |
| 33 |
progressbar.streams |
| 34 |
except (AttributeError, ImportError): |
| 35 |
progressbar = None |
| 36 |
from .Qt import QtCore |
| 37 |
import signal |
| 38 |
from ..log import console_log |
| 39 |
|
| 40 |
from .browser import BaseBrowserItem |
| 41 |
from .manager import BaseManager, Experiment |
| 42 |
|
| 43 |
from ..experiment import Results, Procedure, unique_filename |
| 44 |
|
| 45 |
log = logging.getLogger(__name__) |
| 46 |
log.addHandler(logging.NullHandler()) |
| 47 |
|
| 48 |
|
| 49 |
class ConsoleBrowserItem(BaseBrowserItem): |
| 50 |
|
| 51 |
def __init__(self, progress_bar): |
| 52 |
self.bar = progress_bar |
| 53 |
|
| 54 |
def setStatus(self, status): |
| 55 |
if self.bar: |
| 56 |
self.bar.update(status=self.status_label[status]) |
| 57 |
|
| 58 |
def setProgress(self, progress): |
| 59 |
if self.bar: |
| 60 |
self.bar.update(progress) |
| 61 |
|
| 62 |
|
| 63 |
class ConsoleArgumentParser(argparse.ArgumentParser): |
| 64 |
special_options = { |
| 65 |
"no-progressbar": {"default": False, |
| 66 |
"desc": "Disable progressbar", |
| 67 |
"help_fields": ["default"], |
| 68 |
"action": 'store_true'}, |
| 69 |
"log-level": {"default": 'INFO', |
| 70 |
"choices": list(logging._nameToLevel.keys()), |
| 71 |
"desc": "Set log level (logging module values)", |
| 72 |
"help_fields": ["default"]}, |
| 73 |
"sequence-file": {"default": None, |
| 74 |
"desc": "Sequencer file as used/defined by the sequencer widget to " |
| 75 |
"execute a sequence of measurements", |
| 76 |
"help_fields": ["default"]}, |
| 77 |
"result-directory": {"default": ".", |
| 78 |
"desc": "Directory where experiment's result are saved", |
| 79 |
"help_fields": ["default"]}, |
| 80 |
"result-file": {"default": None, |
| 81 |
"desc": "File name where results are stored; this string is handled " |
| 82 |
"by the `unique_filename` function and hence allows for " |
| 83 |
"filling in parameter values and is suffixed by the date " |
| 84 |
"(`YYYY-MM-DD`) and an index number", |
| 85 |
"help_fields": ["default"]}, |
| 86 |
"use-result-file": {"default": None, |
| 87 |
"desc": "Result file to retrieve params from", |
| 88 |
"help_fields": ["default"]}, |
| 89 |
} |
| 90 |
|
| 91 |
def __init__(self, procedure_class, **kwargs): |
| 92 |
super().__init__(**kwargs) |
| 93 |
self.procedure_class = procedure_class |
| 94 |
self.setup_parser() |
| 95 |
|
| 96 |
def setup_parser(self): |
| 97 |
""" Setup command line arguments parsing from parameters information """ |
| 98 |
|
| 99 |
self.procedure = self.procedure_class() |
| 100 |
parameter_objects = self.procedure.parameter_objects() |
| 101 |
|
| 102 |
special_options = copy.deepcopy(self.special_options) |
| 103 |
special_opts_group = self.add_argument_group("Common options") |
| 104 |
for option, kwargs in special_options.items(): |
| 105 |
help_fields = [('units are', 'units')] + kwargs['help_fields'] |
| 106 |
desc = kwargs['desc'] |
| 107 |
kwargs['help'] = self._cli_help_fields(desc, kwargs, help_fields) |
| 108 |
del kwargs['help_fields'] |
| 109 |
del kwargs['desc'] |
| 110 |
special_opts_group.add_argument("--" + option, **kwargs) |
| 111 |
|
| 112 |
experiment_opts_group = self.add_argument_group("Experiment options") |
| 113 |
for name in parameter_objects: |
| 114 |
if name in special_options: |
| 115 |
raise Exception(f"Experiment option {name} " + |
| 116 |
"is already defined as common options") |
| 117 |
kwargs = {} |
| 118 |
parameter = parameter_objects[name] |
| 119 |
default, help_fields, _type = parameter.cli_args |
| 120 |
kwargs['help'] = self._cli_help_fields(parameter.name, parameter, help_fields) |
| 121 |
kwargs['default'] = default |
| 122 |
if _type is not None: |
| 123 |
kwargs['type'] = _type |
| 124 |
experiment_opts_group.add_argument("--" + name, **kwargs) |
| 125 |
|
| 126 |
@staticmethod |
| 127 |
def _cli_help_fields(name, inst, help_fields): |
| 128 |
def hasattr_dict(inst, key): |
| 129 |
return key in inst |
| 130 |
|
| 131 |
def getattr_dict(inst, key): |
| 132 |
return inst[key] |
| 133 |
|
| 134 |
if isinstance(inst, dict): |
| 135 |
hasattribute = hasattr_dict |
| 136 |
getattribute = getattr_dict |
| 137 |
else: |
| 138 |
hasattribute = hasattr |
| 139 |
getattribute = getattr |
| 140 |
|
| 141 |
message = name |
| 142 |
for field in help_fields: |
| 143 |
if isinstance(field, str): |
| 144 |
field = ["{} is".format(field), field] |
| 145 |
|
| 146 |
if hasattribute(inst, field[1]) and getattribute(inst, field[1]) is not None: |
| 147 |
prefix = field[0] |
| 148 |
value = getattribute(inst, field[1]) |
| 149 |
message += ", {} {}".format(prefix, value) |
| 150 |
|
| 151 |
message = message.replace("%", "%%") |
| 152 |
return message |
| 153 |
|
| 154 |
|
| 155 |
class ManagedConsole(QtCore.QCoreApplication): |
| 156 |
""" |
| 157 |
Base class for console experiment management. |
| 158 |
|
| 159 |
Parameters for :code:`__init__` constructor. |
| 160 |
|
| 161 |
:param procedure_class: procedure class describing the experiment |
| 162 |
(see :class:`~pymeasure.experiment.procedure.Procedure`) |
| 163 |
:param log_channel: :code:`logging.Logger` instance to use for logging output |
| 164 |
:param log_level: logging level |
| 165 |
""" |
| 166 |
|
| 167 |
def __init__(self, |
| 168 |
procedure_class, |
| 169 |
log_channel='', |
| 170 |
log_level=logging.INFO, |
| 171 |
): |
| 172 |
|
| 173 |
super().__init__([]) |
| 174 |
self.procedure_class = procedure_class |
| 175 |
self.log_channel = log_channel |
| 176 |
self.log = logging.getLogger(log_channel) |
| 177 |
self.log_level = log_level |
| 178 |
log.setLevel(log_level) |
| 179 |
self.log.setLevel(log_level) |
| 180 |
|
| 181 |
# Check if the get_estimates function is reimplemented |
| 182 |
self.use_estimator = not self.procedure_class.get_estimates == Procedure.get_estimates |
| 183 |
if self.use_estimator: |
| 184 |
log.warning("Estimator not yet implemented") |
| 185 |
|
| 186 |
# Handle Ctrl+C nicely |
| 187 |
signal.signal(signal.SIGINT, lambda sig, _: self.abort()) |
| 188 |
|
| 189 |
# Parse command line arguments |
| 190 |
parser = ConsoleArgumentParser(procedure_class) |
| 191 |
args = vars(parser.parse_args()) |
| 192 |
|
| 193 |
self.directory = args['result_directory'] |
| 194 |
self.filename = args['result_file'] |
| 195 |
try: |
| 196 |
log_level = int(args['log_level']) |
| 197 |
except ValueError: |
| 198 |
# Ignore and assume it is a valid level string |
| 199 |
log_level = args['log_level'] |
| 200 |
self.log_level = log_level |
| 201 |
log.setLevel(self.log_level) |
| 202 |
self.log.setLevel(self.log_level) |
| 203 |
|
| 204 |
if args['sequence_file'] is not None: |
| 205 |
raise NotImplementedError("Sequencer not yet implemented") |
| 206 |
|
| 207 |
# Set procedure parameters |
| 208 |
self.parameter_values = {} |
| 209 |
|
| 210 |
if args['use_result_file'] is not None: |
| 211 |
# Special case set parameters from log file |
| 212 |
results = Results.load(args['use_result_file']) |
| 213 |
for name in results.parameters: |
| 214 |
self.parameter_values[name] = results.parameters[name].value |
| 215 |
else: |
| 216 |
for name in args: |
| 217 |
opt_name = name.replace("_", "-") |
| 218 |
if not (opt_name in parser.special_options): |
| 219 |
self.parameter_values[name] = args[name] |
| 220 |
|
| 221 |
if progressbar and not args['no_progressbar']: |
| 222 |
progressbar.streams.wrap_stderr() |
| 223 |
self.bar = progressbar.ProgressBar(max_value=100, |
| 224 |
prefix='{variables.status}: ', |
| 225 |
variables={'status': "Unknown"}) |
| 226 |
else: |
| 227 |
self.bar = None |
| 228 |
scribe = console_log(self.log, level=self.log_level) |
| 229 |
scribe.start() |
| 230 |
|
| 231 |
# Setup Manager |
| 232 |
self.manager = BaseManager( |
| 233 |
log_level=self.log_level, |
| 234 |
parent=self) |
| 235 |
self.manager.abort_returned.connect(self._terminate) |
| 236 |
self.manager.failed.connect(self._terminate) |
| 237 |
self.manager.finished.connect(self._terminate) |
| 238 |
self.manager.log.connect(self.log.handle) |
| 239 |
|
| 240 |
def get_filename(self, directory, procedure=None): |
| 241 |
""" Return filename for saving results file |
| 242 |
|
| 243 |
:param directory: directory of the returned filename. |
| 244 |
|
| 245 |
""" |
| 246 |
if self.filename is not None: |
| 247 |
return unique_filename(directory, prefix=self.filename, procedure=procedure) |
| 248 |
else: |
| 249 |
return unique_filename(directory) |
| 250 |
|
| 251 |
def queue(self): |
| 252 |
procedure = self.procedure_class() |
| 253 |
procedure.set_parameters(self.parameter_values) |
| 254 |
filename = self.get_filename(self.directory, procedure) |
| 255 |
results = Results(procedure, filename) |
| 256 |
experiment = self.new_experiment(results) |
| 257 |
|
| 258 |
self.manager.queue(experiment) |
| 259 |
|
| 260 |
def _terminate(self): |
| 261 |
if not self.manager.experiments.has_next(): |
| 262 |
self.quit() |
| 263 |
|
| 264 |
def abort(self): |
| 265 |
""" Aborts the currently running Experiment, but raises an exception if |
| 266 |
there is no running experiment |
| 267 |
""" |
| 268 |
self.manager.abort() |
| 269 |
|
| 270 |
def new_experiment(self, results): |
| 271 |
browser_item = ConsoleBrowserItem(self.bar) |
| 272 |
return Experiment(results, browser_item=browser_item) |
| 273 |
|
| 274 |
def exec(self): |
| 275 |
self.queue() |
| 276 |
super().exec() |