Note
Click here to download the full example code
Gait Sequence Detection for Session-Compatible Datasets#
This example illustrates how the gait sequence detection pipeline can be applied to Session-compatible datasets recorded by Signia Hearing Aids.
Getting example data#
Get example data, load it, align with gravity alignment and transformation into body frame. For mor information on data loading see Load Data by Signia Hearing Aids.
from eargait.preprocessing import align_gravity_and_convert_ear_to_ebf, load
from eargait.utils import TrimMeanGravityAlignment
from eargait.utils.example_data import get_mat_example_data_path, load_groundtruth
data_path = get_mat_example_data_path()
target_sample_rate = 50
session = load(data_path, target_sample_rate_hz=target_sample_rate, skip_calibration=True)
trim_method = TrimMeanGravityAlignment(sampling_rate_hz=target_sample_rate)
ear_data = align_gravity_and_convert_ear_to_ebf(session, trim_method)
/home/docs/checkouts/readthedocs.org/user_builds/eargait/checkouts/latest/eargait/preprocessing/load_data_helpers.py:54: UserWarning: Calibration was skipped. Calibration is strongly recommended.
warnings.warn("Calibration was skipped. Calibration is strongly recommended.")
Initialize Gait Sequence Detection#
We instantiate the GaitSequenceDetection class with the desired parameters.
strictness=0 and minimum_sequence_length=1 are seen as standard.
strictness is defined as >=0 while minimum_seq_length as >=1 definitions of these parameters in the Gsd class.
from eargait.gait_sequence_detection.gait_sequence_detector import GaitSequenceDetection
gsd = GaitSequenceDetection(sample_rate=target_sample_rate, strictness=0, minimum_seq_length=1)
Detect Gait Sequences#
Apply the detection algorithm to the processed data.
The activity parameter specifies the type of activity to detect, e.g., “walking”.
This uses a pretrained model for gait detection trained on 50hz data, so make sure to resample your data
according to this in “align_calibrate_sess”
gsd.detect(ear_data, activity="walking")
sequence_list = gsd.sequence_list_
print("Timeframes of specified activity", sequence_list)
/home/docs/checkouts/readthedocs.org/user_builds/eargait/envs/latest/lib/python3.9/site-packages/sklearn/base.py:288: UserWarning: Trying to unpickle estimator StandardScaler from version 1.1.3 when using version 1.2.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:
https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations
warnings.warn(
/home/docs/checkouts/readthedocs.org/user_builds/eargait/envs/latest/lib/python3.9/site-packages/sklearn/base.py:288: UserWarning: Trying to unpickle estimator StandardScaler from version 1.1.3 when using version 1.2.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:
https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations
warnings.warn(
Timeframes of specified activity {'right_sensor': start end
0 1200 1500
1 1800 2250
2 2700 3300
3 3750 4350
4 4650 5250
5 5550 6150
6 6450 7050
7 7350 7800, 'left_sensor': start end
0 1050 1500
1 1800 2250
2 2700 3300
3 3750 4350
4 4650 5250
5 5550 6150
6 6450 7050
7 7350 7800}
Plot Detected Sequences#
Visualize the detected sequences by overlaying them on the accelerometer data.
gsd.plot()

Advanced Usage and Customization#
The pipeline allows customization of parameters like strictness and minimum_seq_length to fine-tune the detection
process based on the specific requirements of your dataset.
gsd = GaitSequenceDetection(sample_rate=target_sample_rate, strictness=0, minimum_seq_length=1)
Handling Multiple Activities#
The GaitSequenceDetection class supports detecting multiple activities simultaneously
by passing a list of activities to the detect method.
gsd.detect(ear_data, activity=["walking", "jogging", "stairs up", "stairs down"])
/home/docs/checkouts/readthedocs.org/user_builds/eargait/envs/latest/lib/python3.9/site-packages/sklearn/base.py:288: UserWarning: Trying to unpickle estimator StandardScaler from version 1.1.3 when using version 1.2.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:
https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations
warnings.warn(
/home/docs/checkouts/readthedocs.org/user_builds/eargait/envs/latest/lib/python3.9/site-packages/sklearn/base.py:288: UserWarning: Trying to unpickle estimator StandardScaler from version 1.1.3 when using version 1.2.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:
https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations
warnings.warn(
GaitSequenceDetection(criteria_order='strictness_first', minimum_seq_length=1, sample_rate=50, strictness=0)
Further usefully analysis: Overlay Ground truth sequences and detected sequences#
To compare the detected sequences with eventually present Ground truth data we first need to make sure the Ground truth is present in the same sampling rate.
from eargait.utils.overlapping_regions import categorize_intervals, plot_categorized_intervals
csv_path_groundtruth = data_path.parent.joinpath("walking_bout_indices.csv")
csv_activity_table = load_groundtruth(csv_path_groundtruth, target_sample_rate=target_sample_rate)
csv_activity_table = csv_activity_table[csv_activity_table["speed"] == data_path.stem]
csv_activity_table = csv_activity_table.rename(columns={"stop": "end"})
# Plotting this overlays Ground truth and detected activity sequences.
gsd.plot(csv_activity_table)

Total running time of the script: ( 0 minutes 6.275 seconds)
Estimated memory usage: 95 MB