Sessions#

A simple example showing how to work with Sensor Sessions.

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

Create and load a session#

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")
This session has 3 datasets
This session has 3 datasets

Apply operations to all datasets#

Like Datasets contain convenience methods to act on all Datastreams, Sessions provide methods that work on all datasets

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

Work with synchronized sessions#

The library differentiates between synchronised and not synchronised session. If your session is synchronised your should use a SyncedSession

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})")
The master of the session is 7fad
The slaves of the session are ['323c', '922a']

Total running time of the script: (0 minutes 0.291 seconds)

Estimated memory usage: 109 MB

Gallery generated by Sphinx-Gallery