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 | class AcquisitionAnalysisManager(AcquisitionManager):
"""AcquisitionAnalysisManager.
# Init
```
aqm = AcquisitionAnalysisManager("tmp_data/", use_magic=False, save_files=False)
aqm.set_config_file("configuration.py")
```
# acquisition_cell:
```
aqm.acquisition_cell('simple_sine')
...
aqm.save_acquisition(x=x, y=y)
```
# analysis_cell:
```
aqm.analysis_cell()
...
plt.plot(aqm.d.x, aqm.d.y)
aqm.save_fig()
```
"""
_analysis_data: Optional[AnalysisData] = None
_analysis_cell_str = None
_is_old_data = False
_last_fig_name = None
_default_config_files: Tuple[str, ...] = ()
_acquisition_started = 0
_linting_external_vars = None
_connected_widgets: Optional[List["display_widget.WidgetProtocol"]] = None
def __init__(
self,
data_directory: Optional[Union[str, Any]] = None,
*,
config_files: Optional[List[str]] = None,
save_files: bool = False,
use_magic: bool = False,
save_on_edit: bool = True,
save_on_edit_analysis: Optional[bool] = None,
save_fig_inside_h5: bool = False,
shell: Any = True,
hooks: Optional[LifecycleHooks] = None,
):
"""
AcquisitionAnalysisManager.
Args:
data_directory:
Path to data_directory. Should be explicitly set here or as environ parameter.
config_files:
List of paths to config files. Defaults to empty.
save_files:
True to additionally save config files and the cells to files. Defaults to False.
So all information is saved inside the h5 file.
use_magic:
True to register the magic cells. Defaults to False.
save_on_edit:
True to save data for every change.
save_on_edit_analysis:
save_on_edit parameter for AnalysisManager i.e. data inside analysis_cell
shell:
could be provided or explicitly set to None. Defaults to get_ipython().
hooks:
Optional shared :class:`~labmate.acquisition.hooks.LifecycleHooks`.
If omitted, a new instance is created on the base manager.
"""
if shell is False or shell is True: # behavior by default shell
try:
from IPython.core.getipython import get_ipython
self.shell = get_ipython()
except ImportError:
self.shell = None
else: # if any shell is provided. even None
self.shell = shell
if use_magic:
from .acquisition_magic_class import ( # noqa: I001
load_ipython_extension,
)
load_ipython_extension(aqm=self, shell=self.shell)
if save_on_edit_analysis is None:
save_on_edit_analysis = save_on_edit
self._save_on_edit_analysis = save_on_edit_analysis
self._save_fig_inside_h5 = save_fig_inside_h5
self._logger = logger
super().__init__(
data_directory=str(data_directory),
config_files=config_files,
save_files=save_files,
save_on_edit=save_on_edit,
hooks=hooks,
)
@property
def logger(self):
return self._logger
@property
def current_acquisition(self):
"""Return current acquisition if it's not an old data analyses."""
if self._is_old_data:
return None
return super().current_acquisition
@property
def current_analysis(self):
"""Return the current analysis. Class where you cannot change existed keys."""
return self._analysis_data
@property
def data(self):
"""Same as current_analysis."""
if self._analysis_data is None:
raise ValueError("No data set")
return self._analysis_data
@property
def d(self): # pylint: disable=invalid-name
"""Shorter alias for data."""
return self.data
def save_fig_only(
self,
fig: Optional["FigureProtocol"] = None,
name: Optional[Union[str, int]] = None,
extensions: Optional[str] = None,
**kwds,
) -> "AcquisitionAnalysisManager":
"""Save the figure as a file."""
self.data.save_fig(fig=fig, name=name, extensions=extensions, **kwds)
return self
def save_analysis_cell(
self,
name: Optional[Union[str, int]] = None,
cell: Optional[Union[str, Literal["none"]]] = None,
) -> "AcquisitionAnalysisManager":
if name is None:
name = self.data.figure_last_name
if name is not None:
name = str(name)
cell = cell or self._analysis_cell_str
self.data.save_analysis_cell(code=cell, code_name=name)
return self
def save_fig(
self,
fig_or_name: Optional[Union["FigureProtocol", str, int]] = None,
/,
*,
fig: Optional["FigureProtocol"] = None,
name: Optional[Union[str, int]] = None,
cell: Optional[str] = None,
**kwds,
) -> "AcquisitionAnalysisManager":
if fig_or_name is not None:
if isinstance(fig_or_name, (str, int)):
name = name or fig_or_name
else:
fig = fig or fig_or_name
self.save_fig_only(fig=fig, name=name, **kwds)
self.save_analysis_cell(name=name, cell=cell)
if self.current_acquisition is not None:
self.hooks.dispatch_figure_saved(self.current_acquisition)
if self._connected_widgets:
display_widget.display_widgets(
self._connected_widgets,
aqm=self,
fig=fig,
)
return self
def __setitem__(self, __key: str, __value: Any) -> None:
if self._analysis_data is not None:
raise ValueError(
"This is the way to save acquisition data. But analysis data was loaded. "
"So you possibly run it outside of acquisition_cell"
)
acq_data = self.current_acquisition
if acq_data is None:
raise ValueError(
"Cannot save data to acquisition as current acquisition is None."
"Possibly because you have never run `acquisition_cell(..)` or it's an old data"
)
acq_data[__key] = __value # pylint: disable=E1137
def save_acquisition(
self,
update_: bool = True,
/,
file_suffix: Optional[str] = None,
**kwds,
) -> "AcquisitionAnalysisManager":
acquisition_finished = time.time()
if not self._once_saved:
additional_info: Dict[str, Any] = {
"acquisition_duration": acquisition_finished - self._acquisition_started,
"logs": self.logger.getvalue(),
"prints": self.logger.get_stdout(),
}
if self._default_config_files:
additional_info.update({"default_config_files": self._default_config_files})
kwds.update({"info": additional_info})
super().save_acquisition(update_, file_suffix=file_suffix, **kwds)
self._load_analysis_data()
return self
def _load_analysis_data(self, filepath: Optional[str] = None):
filepath = filepath or str(self.current_filepath)
self._analysis_data = self.load_file(filepath)
if self._save_on_edit_analysis is False:
self._analysis_data.save()
return self._analysis_data
def load_file(self, filename) -> "AnalysisData":
filename = self._get_full_filename(filename)
if not os.path.exists(filename if filename.endswith(".h5") else filename + ".h5"): # noqa: PTH110
raise ValueError(f"File {filename} cannot be found")
data = AnalysisData(
filepath=filename,
save_files=self._save_files,
save_on_edit=self._save_on_edit_analysis,
save_fig_inside_h5=self._save_fig_inside_h5,
open_on_init=False,
)
if not data.get("useful", True):
data.unlock_data("useful").update(**{"useful": True}).lock_data("useful")
if self._default_config_files:
data.set_default_config_files(self._default_config_files)
return data
def acquisition_cell(
self,
name: str,
cell: Optional[str] = None,
prerun: Optional[Union[_CallableWithNoArgs, List[_CallableWithNoArgs]]] = None,
save_on_edit: Optional[bool] = None,
step: int = 1,
) -> "AcquisitionAnalysisManager":
self._analysis_cell_str = None
self._analysis_data = None
self._is_old_data = False
self._acquisition_started = time.time()
cell = cell or get_current_cell(self.shell)
if step == 1:
self.logger.reset()
self.new_acquisition(name=name, cell=cell, save_on_edit=save_on_edit)
elif self._current_acquisition is None:
raise ValueError("Acquisition should start from step 1")
elif self._current_acquisition.current_step == step:
raise ValueError(
"This step was already run. Please run the next step or restart from step 1"
)
else:
if self._current_acquisition.experiment_name != name:
raise ValueError(
f"Current acquisition ('{self.current_experiment_name}') "
f"isn't the one expected ('{name}') for this acquisition. "
f"Possible solutions: run acquisition '{name}' with step 1; "
f"or change current acquisition name to '{self.current_experiment_name}'"
)
self._current_acquisition.current_step = step
self._current_acquisition.set_cell(cell, step=step)
self._current_acquisition.save_cell(cell, suffix=str(step))
configs_modified = self._get_configs_last_modified()
if configs_modified != self._configs_last_modified:
raise ValueError(
"Config files were modified since the previous acquisition step. "
"Please rerun the acquisition from the first step."
)
self.logger.stdout_flush()
self.logger.info(f"{step}:{self.current_filepath.basename}") # pylint: disable=W1203
if step == 1:
self.hooks.dispatch_acquisition_cell_ready()
utils.run_functions(prerun)
return self
def analysis_cell(
self,
filename: Optional[Union[str, "Path"]] = None,
*,
acquisition_name=None,
cell: Optional[str] = None,
filepath: Optional[Union[str, "Path"]] = None,
prerun: Optional[Union[_CallableWithNoArgs, List[_CallableWithNoArgs]]] = None,
) -> "AcquisitionAnalysisManager":
self._analysis_cell_str = cell or get_current_cell(self.shell)
if filename or filepath: # getting old data
self._is_old_data = True
if self.shell is not None:
from labmate.display.html_output import display_warning
display_warning("Old data analysis")
filename = str(filepath or self._get_full_filename(filename)) # type: ignore[arg-type]
filename = (filename.rsplit(".h5", 1)[0]) if filename.endswith(".h5") else filename
else:
self._is_old_data = False
if acquisition_name is not None:
import re
if (
len(acquisition_name) == 0
or (
acquisition_name[0] != r"^"
and acquisition_name != self.current_experiment_name
)
or (
acquisition_name[0] == r"^"
and re.match(acquisition_name, self.current_experiment_name) is None
)
):
raise ValueError(
f"Current acquisition ('{self.current_experiment_name}') "
f"isn't the one expected ('{acquisition_name}') for this analysis"
)
filename = str(self.current_filepath) # without h5
self.logger.info(os.path.basename(filename)) # noqa: PTH119
if (
(not self._is_old_data)
and (self.shell is not None)
and (
"acquisition_cell(" in self.shell.last_execution_result.info.raw_cell # type: ignore
and not self.shell.last_execution_result.success # type: ignore
)
):
raise ChildProcessError(
"Last executed cell was probably an `acquisition_cell` and failed to run. "
"Check if everything is ok and executive again"
)
full_h5 = filename + ".h5"
self.hooks.dispatch_analysis_data_loading(full_h5)
if os.path.exists(full_h5): # noqa: PTH110
self._load_analysis_data(filename)
else:
if self._is_old_data:
raise ValueError(f"Cannot load data from {filename}")
self._analysis_data = None
if cell is not None:
self.save_analysis_cell(cell=cell)
if (self._analysis_cell_str is not None) and (self._linting_external_vars is not None):
from ..acquisition import custom_lint
from ..utils import lint
lint_result = lint.find_variables_from_code(
self._analysis_cell_str,
self._linting_external_vars,
run_on_call=custom_lint.on_call_functions,
)
for var in lint_result.external_vars:
self.logger.warning("External variable used inside the analysis code: %s", var)
for error in lint_result.errors:
self.logger.warning(error)
self.hooks.dispatch_analysis_cell_ready()
utils.run_functions(prerun)
return self
def get_analysis_code(self, look_inside: bool = True) -> str:
code = self.data.get_analysis_code(update_code=look_inside)
if self.shell is not None:
self.shell.set_next_input(code) # type: ignore
return code
def _get_full_filename(self, filename: Union[str, "Path"]) -> str:
if filename is None:
raise ValueError("Filename cannot be None")
filepath = utils.get_path_from_filename(filename)
if isinstance(filepath, tuple):
return os.path.join(self.data_directory, *filepath) # noqa: PTH118
return filepath
def parse_config_file(self, config_file_name: str, /) -> "ConfigFile":
return self.data.parse_config_file(config_file_name)
def parse_config(self, config_files: Optional[Tuple[str, ...]] = None) -> "ConfigFile":
return self.data.parse_config(config_files=config_files)
@property
def cfg(self) -> "ConfigFile":
return self.data.cfg
def parse_config_str(self, values: List[str], /, max_length: Optional[int] = None) -> str:
return self.data.parse_config_str(values, max_length=max_length)
def linting(
self,
allowed_variables: Optional[Iterable[str]] = None,
init_file: Optional[str] = None,
):
from ..utils import lint
allowed = set() if allowed_variables is None else set(allowed_variables)
if init_file is not None:
allowed.update(lint.find_variables_from_file(init_file)[0])
self._linting_external_vars = allowed
def set_default_config_files(self, config_files: Union[str, Tuple[str, ...], List[str]], /):
self._default_config_files = (
(config_files,) if isinstance(config_files, str) else tuple(config_files)
)
if self._analysis_data:
self._analysis_data.set_default_config_files(self._default_config_files)
def set_analysis_cell_prerun_hook(
self,
hook: Union[
_CallableWithNoArgs,
List[_CallableWithNoArgs],
Tuple[_CallableWithNoArgs, ...],
],
):
self.hooks.set_analysis_cell_ready(hook)
def set_acquisition_cell_prerun_hook(
self,
hook: Union[
_CallableWithNoArgs,
List[_CallableWithNoArgs],
Tuple[_CallableWithNoArgs, ...],
],
):
self.hooks.set_acquisition_cell_ready(hook)
def find_param_in_config(self, param: str) -> Optional[Tuple[str, int]]:
for file in self._default_config_files:
for line_no, line in enumerate(self.d["configs", file].split("\n")):
if line.startswith(param):
return file, line_no + 1
return None
def display_param_link(
self,
params: Union[str, List[str], List[Tuple[str, str]]],
after_text: Optional[str] = None,
title: Optional[str] = None,
):
if after_text is not None:
if not isinstance(params, str):
raise ValueError(
"Cannot use after_text with multiple params. "
"Use params=[(param, after_text), ...] instead."
)
return self.display_param_link(params=[(params, after_text)], title=title)
if isinstance(params, str):
params = [params]
links = "" if not title else title + "<br/>"
for param in params:
if not isinstance(param, str):
param_text, after_text = param
else:
param_text, after_text = param, None
res = self.find_param_in_config(param_text)
if res is None:
self.logger.warning(
"Parameter '%s' cannot be found in default config files.", param
)
continue
file, line_no = res
file = self._config_files_names_to_path.get(file, file)
link = display.links.create_link(param_text, file, line_no, after_text)
links += link + "<br/>"
return display.display_html(links)
def display_cfg_link(self, parameters: Dict[str, Any], update_button: bool = False):
from labmate.display import html_output
links = []
for param, value in parameters.items():
param_eq = f"{param.strip()} = "
res = self.find_param_in_config(param_eq)
if res is None:
self.logger.warning(
"Parameter '%s' cannot be found in default config files.", param
)
continue
file, line_no = res
file = self._config_files_names_to_path.get(file, file)
def update_value(param, value):
self.update_config_params_on_disk({param: value})
buttons = (
[display.buttons.create_button(update_value, param, value, name="Update")]
if update_button
else None
)
link = html_output.create_link_row(
link_text=f"{param} = ",
link_url=f"{file}:{line_no}",
text=str(value),
buttons=buttons, # type: ignore
)
links.append(link)
return display.display_widgets_vertically(links, class_="labmate-params")
def update_config_params_on_disk(self, params: Dict[str, Any]):
for file in self.config_files:
file = self._config_files_names_to_path.get(file, file)
utils.file_read.update_file_variable(file, params)
return self
def connect_default_widget(
self,
objs: Union["display_widget.WidgetProtocol", List["display_widget.WidgetProtocol"]],
):
if not isinstance(objs, (list, tuple)):
objs = [objs]
if self._connected_widgets is None:
self._connected_widgets = []
self._connected_widgets.extend(objs)
|