Note
Click here to download the full example code
Single Dataset#
A simple example on how to work with a single Dataset.
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')
Analog is disabled: True
The acc recordings are 3D and have the length 9597
The new datastream has a length of 4798
Acc has now a length of 4798
Gyro has now a length of 4798
The old and the new are identical: True
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
from pathlib import Path
import matplotlib.pyplot as plt
from nilspodlib import Dataset
FILEPATH = Path("../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)
# You can access the individual sensor data directly from the dataset object using the names provided
# in dataset.info.enabled_sensors
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))
# 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
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)
# After calibration and initial operations on all datastreams, the easiest way to interface with further processing
# pipelines is a conversion into a pandas DataFrame
df = dataset.data_as_df()
print(df.head())
df.plot()
plt.show()
Total running time of the script: ( 0 minutes 0.763 seconds)
Estimated memory usage: 48 MB