.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/load_sensor_data.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_data.py: Single Dataset ============== A simple example on how to work with a single Dataset. .. GENERATED FROM PYTHON SOURCE LINES 7-27 .. code-block:: Python from pathlib import Path import matplotlib.pyplot as plt from nilspodlib import Dataset 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 Load the dataset ---------------- .. GENERATED FROM PYTHON SOURCE LINES 30-43 .. code-block:: Python FILEPATH = _repo_root() / "tests/test_data/synced_sample_session/NilsPodX-7FAD_20190430_0933.bin" # Create a Dataset Object from the bin file dataset = Dataset.from_bin_file(FILEPATH) # You can access the metainformation about your dataset using the `info` attr. # For a full list of available attributes see nilspodlib.header._HeaderFields print("Sensor ID:", dataset.info.sensor_id) print("Start Date (UTC):", dataset.info.utc_datetime_start) print("Stop Date (UTC):", dataset.info.utc_datetime_stop) print("Enabled Sensors:", dataset.info.enabled_sensors) .. rst-class:: sphx-glr-script-out .. code-block:: none Sensor ID: 7fad Start Date (UTC): 2019-04-30 07:33:12+00:00 Stop Date (UTC): 2019-04-30 07:33:59+00:00 Enabled Sensors: ('gyro', 'acc') .. GENERATED FROM PYTHON SOURCE LINES 44-48 Work with individual datastreams -------------------------------- You can access the individual sensor data directly from the dataset object using the names provided in dataset.info.enabled_sensors .. GENERATED FROM PYTHON SOURCE LINES 48-66 .. code-block:: Python datastream_acc = dataset.acc # If a sensor is disabled, this will return `None` print("Analog is disabled:", dataset.analog is None) # Access the data of a datastream object as a numpy.array using the `data` attribute print("The acc recordings are {}D and have the length {}".format(*datastream_acc.data.T.shape)) # Convenience methods are available for common operations. E.g. Norm or downsample plt.figure() plt.title("Acc Norm") plt.plot(datastream_acc.norm()) plt.show() downsampled_datastream = datastream_acc.downsample(factor=2) print("The new datastream has a length of", len(downsampled_datastream.data)) .. image-sg:: /auto_examples/images/sphx_glr_load_sensor_data_001.png :alt: Acc Norm :srcset: /auto_examples/images/sphx_glr_load_sensor_data_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Analog is disabled: True The acc recordings are 3D and have the length 9597 The new datastream has a length of 4798 .. GENERATED FROM PYTHON SOURCE LINES 67-71 Apply operations to the full dataset ------------------------------------ However, for many operations it makes more sense to apply them to the Dataset instead of the Datastream. This will apply the operations to all Datastream and return a new Dataset object .. GENERATED FROM PYTHON SOURCE LINES 71-85 .. code-block:: Python downsampled_dataset = dataset.downsample(factor=2) print("Acc has now a length of", len(downsampled_dataset.acc.data)) print("Gyro has now a length of", len(downsampled_dataset.gyro.data)) # By default this returns a copy of the dataset and all datastreams. If this is a performance concern, the dataset can # be modified inplace: downsampled_dataset = dataset.downsample(factor=2, inplace=True) print("The old and the new are identical:", id(dataset) == id(downsampled_dataset)) # At this point you would usually apply a calibration to the IMU data (see other examples) .. rst-class:: sphx-glr-script-out .. code-block:: none Acc has now a length of 4798 Gyro has now a length of 4798 The old and the new are identical: True .. GENERATED FROM PYTHON SOURCE LINES 86-90 Export the data as dataframe ---------------------------- After calibration and initial operations on all datastreams, the easiest way to interface with further processing pipelines is a conversion into a pandas DataFrame .. GENERATED FROM PYTHON SOURCE LINES 90-97 .. code-block:: Python df = dataset.data_as_df() print(df.head()) df.plot() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_load_sensor_data_002.png :alt: load sensor data :srcset: /auto_examples/images/sphx_glr_load_sensor_data_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none gyr_x gyr_y gyr_z acc_x acc_y acc_z n_samples 0 -0.092530 -0.026791 -0.166483 0.537133 0.294263 9.969488 1 -0.338874 -0.158994 -0.307865 0.522367 0.312731 10.003687 2 0.001125 -0.025516 -0.170005 0.527848 0.340135 9.983830 3 -0.314390 -0.047710 0.073775 0.600002 0.342938 10.001858 4 0.053510 -0.243648 -0.107525 0.642641 0.344317 9.979044 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.351 seconds) **Estimated memory usage:** 162 MB .. _sphx_glr_download_auto_examples_load_sensor_data.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_data.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: load_sensor_data.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: load_sensor_data.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_