.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/custom_calibration_info.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_custom_calibration_info.py: .. _custom_info_class: Custom Calibration Info Subclass ================================ Whenever you want to implement your own version of a calibration or simply want to add some meta information to your calibration info objects, you need to create a new subclass of :class:`~imucal.CalibrationInfo`. When creating a entirely new calibration, you should subclass from :class:`~imucal.CalibrationInfo` directly. If you just want to extend an existing object, subclass from the respective class. .. GENERATED FROM PYTHON SOURCE LINES 15-25 In the following, we will see how to extend the :class:`~imucal.FerrarisCalibrationInfo`. Here are the important things to keep in mind: 1. Each subclass needs to overwrite `CAL_TYPE`. Otherwise it is not recognised as a separate class when loading objects from file. 2. The new class needs to inherit from `dataclass`, if you add fields that should be serialized when using the export methods. 3. All new attributes need Type annotation and a default value (if in doubt use None) 4. Note, that all new attributes need to be json serializable by default, if you want ot use json export. .. GENERATED FROM PYTHON SOURCE LINES 25-38 .. code-block:: default from dataclasses import dataclass from typing import Optional from example_data import EXAMPLE_PATH from imucal import FerrarisCalibrationInfo, ferraris_regions_from_df @dataclass class ExtendedFerrarisCalibrationInfo(FerrarisCalibrationInfo): CAL_TYPE = "ExtendedFerraris" new_meta_info: Optional[str] = None .. GENERATED FROM PYTHON SOURCE LINES 39-43 With that we have a new subclass of the ferraris calibration info. As it is marked as dataclass, the `__init__` is created automatically and saving and loading from file is also taken care of. To use the new class, we need to provide it when initializing our `FerrarisCalibration`. .. GENERATED FROM PYTHON SOURCE LINES 43-47 .. code-block:: default from imucal import FerrarisCalibration cal = FerrarisCalibration(calibration_info_class=ExtendedFerrarisCalibrationInfo) .. GENERATED FROM PYTHON SOURCE LINES 48-50 Now, we can calculate the calibration as normal (here we just use some dummy data). To provide a value for our `new_meta_info` field, we can pass it directly to the `calculate` method. .. GENERATED FROM PYTHON SOURCE LINES 50-60 .. code-block:: default import pandas as pd cal_data = ferraris_regions_from_df(pd.read_csv(EXAMPLE_PATH / "annotated_session.csv", header=0, index_col=[0, 1])) cal_info = cal.compute( cal_data, sampling_rate_hz=204.8, from_acc_unit="a.u.", from_gyr_unit="a.u.", new_meta_info="my value" ) cal_info.new_meta_info .. rst-class:: sphx-glr-script-out .. code-block:: none 'my value' .. GENERATED FROM PYTHON SOURCE LINES 61-63 And of course we can simply export and reimport the new calibration info to json or hdf5 (we will use a tempfile here to not clutter the example folder). .. GENERATED FROM PYTHON SOURCE LINES 63-76 .. code-block:: default import tempfile from pathlib import Path from imucal.management import load_calibration_info with tempfile.TemporaryDirectory() as d: file_name = Path(d) / "my_cal_info.json" cal_info.to_json_file(file_name) # An load it again loaded_cal_info = load_calibration_info(file_name) loaded_cal_info.new_meta_info .. rst-class:: sphx-glr-script-out .. code-block:: none 'my value' .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.352 seconds) **Estimated memory usage:** 13 MB .. _sphx_glr_download_auto_examples_custom_calibration_info.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: custom_calibration_info.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: custom_calibration_info.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_