Note
Click here to download the full example code
Spatial Parameter Estimation#
This example illustrates how Random Forest by the SpatialParamsRandomForest
can be used to estimate spatial gait parameters, including step_length and stride_length.
The gait event detection algorithm DiaoAdaptedEventDetection is applied to obtain gait events.
Getting some example data#
For this we take some example data that contains regular walking movements.
import pandas as pd
from eargait import EarGait
from eargait.event_detection import DiaoAdaptedEventDetection
from eargait.preprocessing import align_gravity_and_convert_ear_to_ebf, convert_ear_to_ebf, load
from eargait.spatial_params import SpatialParamsRandomForest
from eargait.utils.example_data import get_mat_example_data_path
# data directory
data_path = get_mat_example_data_path()
Loading the data#
A data session refers to a recording by signia hearing aids. A session can consist of a single *.mat file or two *.mat file, for left and right ear, respectively.
The session is loaded using the local path data_path` of the directory, in which the matlab file(s) are stored.
For more options regardind loading the data see Load Data by Signia Hearing Aids. example_gait_event_detection
target_sample_rate = 50
session = load(data_path, target_sample_rate_hz=target_sample_rate, skip_calibration=False)
session.info
/home/docs/checkouts/readthedocs.org/user_builds/eargait/envs/latest/lib/python3.9/site-packages/nilspodlib/calibration_utils.py:190: CalibrationWarning: For the sensor 001 no calibration could be located that was in 60 days, 0:00:00 of the 2021-06-01 10:53:58+00:00.The closest calibration is 222 days, 23:54:02 away.
return find_closest_calibration_info_to_date(
/home/docs/checkouts/readthedocs.org/user_builds/eargait/envs/latest/lib/python3.9/site-packages/nilspodlib/calibration_utils.py:190: CalibrationWarning: For the sensor 002 no calibration could be located that was in 60 days, 0:00:00 of the 2021-06-01 10:53:56+00:00.The closest calibration is 222 days, 23:54:04 away.
return find_closest_calibration_info_to_date(
<signialib.header._ProxyHeader object at 0x7ff6463bb730>
Gravity alignment and data transformation into body frame#
Align session to gravity and transform coordinate system into body frame
ear_data = align_gravity_and_convert_ear_to_ebf(session)
# Alternatively, you can skip the gravity alignment by using the following function: convert_ear_to_ebf
# ear_data = convert_ear_to_ebf(session)
/home/docs/checkouts/readthedocs.org/user_builds/eargait/checkouts/latest/eargait/preprocessing/rotations.py:170: UserWarning: No gravity alignment method provided. Using MeanTrimGravityAlignment.
warnings.warn("No gravity alignment method provided. Using MeanTrimGravityAlignment.")
Extract walking interval#
Note: Here prior knowledge about walking sequence within the given data session are applied. load csv file containing walking bouts
rescale_factor = 200 / target_sample_rate
walking_bout_list = pd.read_csv(data_path.parent.joinpath("walking_bout_indices.csv"))
interval = (int(walking_bout_list["start"][0] / rescale_factor), int(walking_bout_list["stop"][0] / rescale_factor))
# alternative if walking bout indices are already known, e.g.
# interval = (14317, 17637) --> indices need to be replaced based on data
ear_data_short = {}
for side in ear_data.keys():
ear_data_short[side] = ear_data[side][interval[0] : interval[1]]
Initializing event detection algorithm#
Recommended parameters: apply filter = True <br /> sampling_rate_hz needs to correspond to target_sample_rate_hz <br /> window_length should be equal to sampling_rate_hz
event_detection_algorithm = DiaoAdaptedEventDetection(
sample_rate_hz=target_sample_rate, window_length=target_sample_rate
)
Initializing spatial parameter estimation method#
Two Alternatives 1. spatial_method = SpatialParamsRandomForestDemographics(target_sample_rate, age, gender, height, weight) 2. spatial_method = SpatialParamsCNN(target_sample_rate)
spatial_method = SpatialParamsRandomForest(target_sample_rate)
Initializing Gait Analysis Pipeline#
Recommended parameters: sampling_rate_hz needs to correspond to target_sample_rate_hz <br />
ear_gait = EarGait(
sample_rate_hz=target_sample_rate,
event_detection_method=event_detection_algorithm,
spatial_params_method=spatial_method,
bool_use_event_list_consistent=True,
)
Detect gait events of gait sequence#
ear_gait.detect(ear_data_short)
gait_events = ear_gait.event_list
Get spatial parameter for walking bout#
spatial_params = ear_gait.spatial_params
spatial_params
{'right_sensor': step_length stride_length gait_velocity side
s_id
0 NaN NaN NaN contralateral
1 0.694539 1.389077 1.389077 ipsilateral
2 0.763567 1.527135 1.527135 contralateral
3 0.836603 1.673206 1.673206 ipsilateral
4 0.833778 1.667556 1.737037 contralateral
5 0.835001 1.670002 1.739585 ipsilateral
6 0.860160 1.720320 1.720320 contralateral
7 0.830181 1.660363 1.729544 ipsilateral
8 0.820921 1.641841 1.710251 contralateral
9 0.794940 1.589879 1.589879 ipsilateral
10 0.630958 1.261916 1.261916 contralateral
11 0.605793 1.211586 1.262069 ipsilateral
12 0.612922 1.225844 1.532306 contralateral
13 0.682229 1.364458 1.421311 ipsilateral
14 0.783562 1.567124 1.567124 contralateral
15 0.837847 1.675695 1.675695 ipsilateral
16 0.824688 1.649375 1.718099 contralateral
17 0.859322 1.718644 1.718644 ipsilateral
18 0.834872 1.669745 1.739317 contralateral
19 0.856414 1.712828 1.712828 ipsilateral
20 0.825928 1.651856 1.651856 contralateral
21 0.781277 1.562554 1.627661 ipsilateral
22 0.698586 NaN 1.397171 contralateral, 'left_sensor': step_length stride_length gait_velocity side
s_id
0 NaN NaN NaN ipsilateral
1 0.688018 1.376036 1.376036 contralateral
2 0.778775 1.557550 1.557550 ipsilateral
3 0.831000 1.662000 1.662000 contralateral
4 0.836897 1.673794 1.743536 ipsilateral
5 0.833923 1.667845 1.737339 contralateral
6 0.843920 1.687840 1.758166 ipsilateral
7 0.852531 1.705063 1.705063 contralateral
8 0.821491 1.642982 1.711439 ipsilateral
9 0.794771 1.589542 1.655773 contralateral
10 NaN NaN NaN ipsilateral
11 NaN NaN NaN ipsilateral
12 NaN NaN NaN contralateral
13 0.802355 1.604711 1.604711 ipsilateral
14 0.829709 1.659418 1.728561 contralateral
15 0.844934 1.689869 1.689869 ipsilateral
16 0.840688 1.681375 1.751433 contralateral
17 0.850179 1.700358 1.700358 ipsilateral
18 0.839817 1.679635 1.679635 contralateral
19 0.827547 1.655094 1.724056 ipsilateral
20 0.798630 1.597260 1.597260 contralateral
21 0.696382 NaN 1.392764 ipsilateral}
Get average spatial parameter over walking bout#
spatial_params_average = ear_gait.average_spatial_params
spatial_params_average
{'right_sensor': step_length stride_length gait_velocity
mean 0.777459 1.562429 1.595547
std 0.083571 0.167422 0.156308, 'left_sensor': step_length stride_length gait_velocity
mean 0.811754 1.637081 1.654197
std 0.048092 0.079411 0.112798}
Total running time of the script: ( 0 minutes 5.744 seconds)
Estimated memory usage: 12 MB