Skip to content

Acquisition notebook manager.

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()
Source code in labmate/acquisition_notebook/acquisition_analysis_manager.py
 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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
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, ...] = tuple()
    _acquisition_started = 0
    _linting_external_vars = None
    _analysis_cell_prerun_hook: Optional[Tuple[_CallableWithNoArgs, ...]] = None
    _acquisition_cell_prerun_hook: Optional[Tuple[_CallableWithNoArgs, ...]] = 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,
    ):
        """
        AcquisitionAnalysisManager.

        Args:
            data_directory (Optional[str], optional):
                Path to data_directory. Should be explicitly set here or as environ parameter.
            config_files (Optional[List[str]], optional):
                List of paths to config files. Defaults to empty.
            save_files (bool, optional):
                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 (bool, optional):
                True to register the magic cells. Defaults to False.
            save_on_edit (bool. Defaults to True):
                True to save data for every change.
            save_on_edit_analysis (bool. Defaults to same as save_on_edit):
                save_on_edit parameter for AnalysisManager i.e. data inside analysis_cell
            shell (InteractiveShell | None, optional. Defaults to True):
                could be provided or explicitly set to None. Defaults to get_ipython().
        """
        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 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,
        )

    @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.

        Args:
            fig (Figure, optional): Figure that should be saved. Figure could be any class with
             function save_fig implemented. By default gets plt.gcf().
            name (str, optional): Name of the fig. It's a suffix that will be added to the filename. Defaults to None.
            extensions(str, optional): Extensions of the file. Defaults to `pdf`.
            tight_layout(bool, optional): True to call fig.tight_layout(). Defaults to True.

        Raises:
            ValueError: if analysis_data is not loaded

        Return:
            self
        """
        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._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

    def save_acquisition(self, **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(**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"
        ):
            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,
        )

        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 and self._current_acquisition is not None:
            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()
        else:
            self.logger.reset()
            self.new_acquisition(name=name, cell=cell, save_on_edit=save_on_edit)

        self.logger.info(  # pylint: disable=W1203
            f"{step}:{self.current_filepath.basename}"
        )

        if step == 1:
            utils.run_functions(self._acquisition_cell_prerun_hook)

        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.shell.get_local_scope(1)['result'].info.raw_cell  # type: ignore

        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
            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))

        if (
            (not self._is_old_data)
            and (self.shell is not None)
            and (
                "acquisition_cell(" in self.shell.last_execution_result.info.raw_cell
                and not self.shell.last_execution_result.success
            )
        ):
            raise ChildProcessError(
                "Last executed cell was probably an `acquisition_cell` and failed to run. "
                "Check if everything is ok and executive again"
            )

        if os.path.exists(filename + ".h5"):
            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

            # _, external_vars = lint.find_variables_from_code(
            #     self._analysis_cell_str, self._linting_external_vars
            # )
            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)

        utils.run_functions(self._analysis_cell_prerun_hook)
        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 open_analysis_fig(self) -> List[FigureProtocol]:
    #     return self.data.open_fig()

    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)
        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_variables = (
            set() if allowed_variables is None else set(allowed_variables)
        )
        if init_file is not None:
            allowed_variables.update(lint.find_variables_from_file(init_file)[0])
        self._linting_external_vars = allowed_variables

    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._analysis_cell_prerun_hook = (
            tuple(hook) if isinstance(hook, (list, tuple)) else (hook,)
        )

    def set_acquisition_cell_prerun_hook(
        self,
        hook: Union[
            _CallableWithNoArgs,
            List[_CallableWithNoArgs],
            Tuple[_CallableWithNoArgs, ...],
        ],
    ):
        self._acquisition_cell_prerun_hook = (
            tuple(hook) if isinstance(hook, (list, tuple)) else (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],
    ):
        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")
            ]

            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]):
        # params_per_files = {}
        # for param, value in params.items():
        #     res = self.find_param_in_data_config(param)
        #     if res is None:
        #         raise ValueError(
        #             f"Parameter '{param}' cannot be found in default config files."
        #         )
        #     file, _ = res
        #     params_per_files.setdefault(file, {})[param] = value

        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)

acquisition_tmp_data property writable

acquisition_tmp_data

Return information about the current acquisition.

Returns class attribute or read it from temp.json file if first is not set. Returns: AcquisitionTmpData(NamedTuple): experiment_name: current experiment name time_stamp: time stamp of the current acquisition configs: dict of configurations files to save directory: directory where the data is stored

current_acquisition property

current_acquisition

Return current acquisition if it's not an old data analyses.

current_analysis property

current_analysis

Return the current analysis. Class where you cannot change existed keys.

d property

d

Shorter alias for data.

data property

data

Same as current_analysis.

data_directory property writable

data_directory

Return the path to the directory where the data is stored.

Returns:

Name Type Description
Path Path

The path to the directory where the data is stored.

__init__

__init__(data_directory=None, *, config_files=None, save_files=False, use_magic=False, save_on_edit=True, save_on_edit_analysis=None, save_fig_inside_h5=False, shell=True)

AcquisitionAnalysisManager.

Parameters:

Name Type Description Default
data_directory Optional[str]

Path to data_directory. Should be explicitly set here or as environ parameter.

None
config_files Optional[List[str]]

List of paths to config files. Defaults to empty.

None
save_files bool

True to additionally save config files and the cells to files. Defaults to False. So all information is saved inside the h5 file.

False
use_magic bool

True to register the magic cells. Defaults to False.

False
save_on_edit bool. Defaults to True

True to save data for every change.

True
save_on_edit_analysis bool. Defaults to same as save_on_edit

save_on_edit parameter for AnalysisManager i.e. data inside analysis_cell

None
shell InteractiveShell | None, optional. Defaults to True

could be provided or explicitly set to None. Defaults to get_ipython().

True
Source code in labmate/acquisition_notebook/acquisition_analysis_manager.py
 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
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,
):
    """
    AcquisitionAnalysisManager.

    Args:
        data_directory (Optional[str], optional):
            Path to data_directory. Should be explicitly set here or as environ parameter.
        config_files (Optional[List[str]], optional):
            List of paths to config files. Defaults to empty.
        save_files (bool, optional):
            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 (bool, optional):
            True to register the magic cells. Defaults to False.
        save_on_edit (bool. Defaults to True):
            True to save data for every change.
        save_on_edit_analysis (bool. Defaults to same as save_on_edit):
            save_on_edit parameter for AnalysisManager i.e. data inside analysis_cell
        shell (InteractiveShell | None, optional. Defaults to True):
            could be provided or explicitly set to None. Defaults to get_ipython().
    """
    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 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,
    )

create_acquisition

create_acquisition(name=None, cell=None, save_on_edit=None)

Create a new acquisition with the given experiment name.

Source code in labmate/acquisition/acquisition_manager.py
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
def create_acquisition(
    self,
    name: Optional[str] = None,
    cell: Optional[str] = None,
    save_on_edit: Optional[bool] = None,
) -> NotebookAcquisitionData:
    """Create a new acquisition with the given experiment name."""
    configs = read_files(self.config_files)

    if self.config_files_eval:
        configs = append_values_from_modules_to_files(
            configs, self.config_files_eval
        )

    if name is None:
        name = self.current_experiment_name + "_item"

    dic = AcquisitionTmpData(
        experiment_name=name,
        time_stamp=get_timestamp(),
        configs=configs,
        directory=self.data_directory,
    )

    filepath = self.create_path_from_tmp_data(dic)
    configs = configs if configs else None
    save_on_edit = save_on_edit if save_on_edit is not None else self._save_on_edit

    return NotebookAcquisitionData(
        filepath=str(filepath),
        configs=configs,
        cell=cell or self.cell,
        overwrite=False,
        save_on_edit=save_on_edit,
        save_files=self._save_files,
    )

new_acquisition

new_acquisition(name, cell=None, save_on_edit=None)

Create a new acquisition with the given experiment name.

Source code in labmate/acquisition/acquisition_manager.py
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
def new_acquisition(
    self, name: str, cell: Optional[str] = None, save_on_edit: Optional[bool] = None
) -> NotebookAcquisitionData:
    """Create a new acquisition with the given experiment name."""
    self._current_acquisition = None
    self._once_saved = False
    self.cell = cell
    configs = read_files(self.config_files)
    self._configs_last_modified = self._get_configs_last_modified()

    if self.config_files_eval:
        configs = append_values_from_modules_to_files(
            configs, self.config_files_eval
        )

    dic = AcquisitionTmpData(
        experiment_name=name,
        time_stamp=get_timestamp(),
        configs=configs,
        directory=self.data_directory,
    )

    self.acquisition_tmp_data = dic

    self._current_acquisition = self.get_acquisition(
        replace=True, save_on_edit=save_on_edit
    )

    return self.current_acquisition

save_fig_only

save_fig_only(fig=None, name=None, extensions=None, **kwds)

Save the figure as a file.

Parameters:

Name Type Description Default
fig Figure

Figure that should be saved. Figure could be any class with function save_fig implemented. By default gets plt.gcf().

None
name str

Name of the fig. It's a suffix that will be added to the filename. Defaults to None.

None
extensions(str, optional

Extensions of the file. Defaults to pdf.

required
tight_layout(bool, optional

True to call fig.tight_layout(). Defaults to True.

required

Raises:

Type Description
ValueError

if analysis_data is not loaded

Return

self

Source code in labmate/acquisition_notebook/acquisition_analysis_manager.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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.

    Args:
        fig (Figure, optional): Figure that should be saved. Figure could be any class with
         function save_fig implemented. By default gets plt.gcf().
        name (str, optional): Name of the fig. It's a suffix that will be added to the filename. Defaults to None.
        extensions(str, optional): Extensions of the file. Defaults to `pdf`.
        tight_layout(bool, optional): True to call fig.tight_layout(). Defaults to True.

    Raises:
        ValueError: if analysis_data is not loaded

    Return:
        self
    """
    self.data.save_fig(fig=fig, name=name, extensions=extensions, **kwds)
    return self

set_config_file

set_config_file(filename)

Set self.config_file to filename. Verify if exists. Only set config file for future acquisition and will not change current acquisition

Source code in labmate/acquisition/acquisition_manager.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def set_config_file(
    self, filename: Union[str, List[str], Tuple[str, ...]]
) -> "AcquisitionManager":
    """Set self.config_file to filename. Verify if exists.
    Only set config file for future acquisition and will not change current acquisition
    """
    if not isinstance(filename, (list, set, tuple)):
        filename = [str(filename)]

    self.config_files = list(filename)
    self._config_files_names_to_path = {
        os.path.basename(file): file for file in self.config_files
    }

    for config_file in self.config_files:
        if not os.path.exists(config_file):
            raise ValueError(f"Configuration file at {config_file} does not exist")

    return self