.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/load_sensor_session.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_load_sensor_session.py: Sessions ========= A simple example showing how to work with Sensor Sessions. .. GENERATED FROM PYTHON SOURCE LINES 7-27 .. code-block:: Python from pathlib import Path import numpy as np from nilspodlib import Dataset, Session, SyncedSession def _repo_root() -> Path: search_roots = [Path.cwd()] if "__file__" in globals(): search_roots.insert(0, Path(__file__).resolve().parent) for root in search_roots: for parent in (root, *root.parents): if (parent / "pyproject.toml").exists(): return parent raise FileNotFoundError("Could not locate the repository root from the example path.") .. GENERATED FROM PYTHON SOURCE LINES 28-30 Create and load a session ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-42 .. code-block:: Python FILEPATH = _repo_root() / "tests/test_data/synced_sample_session" # A session consists of multiple datasets. By default this is also the way to create one datasets = [Dataset.from_bin_file(d) for d in FILEPATH.glob("*.bin")] session = Session(datasets) print(f"This session has {len(session.datasets)} datasets") # However, in many cases it is easier to use one of the Session constructors: session = Session.from_folder_path(FILEPATH, filter_pattern="*.bin") print(f"This session has {len(session.datasets)} datasets") .. rst-class:: sphx-glr-script-out .. code-block:: none This session has 3 datasets This session has 3 datasets .. GENERATED FROM PYTHON SOURCE LINES 43-47 Apply operations to all datasets -------------------------------- Like Datasets contain convenience methods to act on all Datastreams, Sessions provide methods that work on all datasets .. GENERATED FROM PYTHON SOURCE LINES 47-59 .. code-block:: Python downsampled_session = session.downsample(factor=2) for ds in downsampled_session.datasets: for name, d in ds.datastreams: print(f"{name} of {ds.info.sensor_id} has the length {len(d.data)}") # Further you can use the Proxy Attribute `info` to access the header infos of all sensors at the same time print("The included sensors are:", session.info.sensor_id) print("The samplingrates are:", session.info.sampling_rate_hz) print("The enabled sensor are:", session.info.enabled_sensors) .. rst-class:: sphx-glr-script-out .. code-block:: none gyro of 323c has the length 4799 acc of 323c has the length 4799 analog of 323c has the length 4799 gyro of 7fad has the length 4798 acc of 7fad has the length 4798 gyro of 922a has the length 4798 acc of 922a has the length 4798 The included sensors are: ('323c', '7fad', '922a') The samplingrates are: (204.8, 204.8, 204.8) The enabled sensor are: (('gyro', 'acc', 'analog'), ('gyro', 'acc'), ('gyro', 'acc')) .. GENERATED FROM PYTHON SOURCE LINES 60-64 Work with synchronized sessions ------------------------------- The library differentiates between synchronised and not synchronised session. If your session is synchronised your should use a SyncedSession .. GENERATED FROM PYTHON SOURCE LINES 64-87 .. code-block:: Python session = SyncedSession.from_folder_path(FILEPATH) # This will also validate that all datasets are compatible to be syncronised. # If you need to switch off this validation, you can disable it using: SyncedSession.VALIDATE_ON_INIT = False session = SyncedSession.from_folder_path(FILEPATH) # For synced sessions you can get the datasets of the master and the slaves separately print("The master of the session is", session.master.info.sensor_id) print("The slaves of the session are", [d.info.sensor_id for d in session.slaves]) # To make use of the sync information, all datasets need to be aligned. This can be done using the `cut_to_syncregion` # method. cut_session = session.cut_to_syncregion() # After this all session are aligned and the dataset counter are identical for d in cut_session.slaves: if np.array_equal(d.counter, cut_session.master.counter) is True: print(f"{d.info.sensor_id} has the same counter than master ({cut_session.master.info.sensor_id})") .. rst-class:: sphx-glr-script-out .. code-block:: none The master of the session is 7fad The slaves of the session are ['323c', '922a'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.291 seconds) **Estimated memory usage:** 109 MB .. _sphx_glr_download_auto_examples_load_sensor_session.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: load_sensor_session.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: load_sensor_session.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: load_sensor_session.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_