.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/full_pipelines/mad_gait_pipeline.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_full_pipelines_mad_gait_pipeline.py: MaD DiGait Pipeline =================== This pipeline showcases the current gait analysis pipeline used by the MaD-Lab with all required steps: Preprocessing -> Stride Segmentation -> Event Detection -> Trajectory Reconstruction -> Parameter Estimation This should serve as a compact example that can be copied and pasted into new projects. For more details on the individual steps have a look at the extended examples and the documentation of the main classes: - :ref:`Preprocessing ` - :ref:`Stride Segmentation (BarthDTW) ` - :ref:`Event Detection (RamppEventDetection) ` - :ref:`Trajectory Reconstruction (double Integration) ` - :ref:`Temporal Parameters ` and :ref:`Spatial Parameters ` .. GENERATED FROM PYTHON SOURCE LINES 20-22 Load example data ----------------- .. GENERATED FROM PYTHON SOURCE LINES 22-33 .. code-block:: default import numpy as np from gaitmap.example_data import get_healthy_example_imu_data_not_rotated np.random.seed(0) example_dataset = get_healthy_example_imu_data_not_rotated() sampling_rate_hz = 204.8 from gaitmap.preprocessing import sensor_alignment .. GENERATED FROM PYTHON SOURCE LINES 34-38 Preprocessing ------------- Fix the alignment between the sensor coordinate system and the gaitmap coordinate system. This will be different for each sensor position and recording. .. GENERATED FROM PYTHON SOURCE LINES 38-58 .. code-block:: default from gaitmap.utils.rotations import flip_dataset, rotation_from_angle # rotate left_sensor first by -90 deg around the x-axis, followed by a -90 deg rotation around the z-axis left_rot = rotation_from_angle(np.array([1, 0, 0]), np.deg2rad(-90)) * rotation_from_angle( np.array([0, 0, 1]), np.deg2rad(-90) ) # rotate right_sensor first by +90 deg around the x-axis, followed by a +90 deg rotation around the z-axis right_rot = rotation_from_angle(np.array([1, 0, 0]), np.deg2rad(90)) * rotation_from_angle( np.array([0, 0, 1]), np.deg2rad(90) ) rotations = {"left_sensor": left_rot, "right_sensor": right_rot} dataset_sf = flip_dataset(example_dataset, rotations) # Align to Gravity dataset_sf_aligned_to_gravity = sensor_alignment.align_dataset_to_gravity(dataset_sf, sampling_rate_hz) from gaitmap.stride_segmentation import BarthDtw .. GENERATED FROM PYTHON SOURCE LINES 59-64 Stride Segmentation ------------------- In this step the continuous datastream is segmented into individual strides. For longer datasets it might be required to first identify segments of walking to reduce the chance of false-positives. .. GENERATED FROM PYTHON SOURCE LINES 64-71 .. code-block:: default from gaitmap.utils.coordinate_conversion import convert_to_fbf dtw = BarthDtw() # Convert data to foot-frame bf_data = convert_to_fbf(dataset_sf_aligned_to_gravity, left_like="left_", right_like="right_") dtw = dtw.segment(data=bf_data, sampling_rate_hz=sampling_rate_hz) .. GENERATED FROM PYTHON SOURCE LINES 72-75 Event detection ---------------- For each identified stride, we now identify important stride events. .. GENERATED FROM PYTHON SOURCE LINES 75-80 .. code-block:: default from gaitmap.event_detection import RamppEventDetection ed = RamppEventDetection() ed = ed.detect(data=bf_data, stride_list=dtw.stride_list_, sampling_rate_hz=sampling_rate_hz) .. GENERATED FROM PYTHON SOURCE LINES 81-85 Trajectory Reconstruction ------------------------- Using the identified events the trajectory of each stride is reconstructed using double integration starting from the `min_vel` event of each stride. .. GENERATED FROM PYTHON SOURCE LINES 85-92 .. code-block:: default from gaitmap.trajectory_reconstruction import StrideLevelTrajectory trajectory = StrideLevelTrajectory() trajectory = trajectory.estimate( data=dataset_sf, stride_event_list=ed.min_vel_event_list_, sampling_rate_hz=sampling_rate_hz ) .. GENERATED FROM PYTHON SOURCE LINES 93-96 Temporal Parameter Calculation ------------------------------ Now we have all information to calculate relevant temporal parameters (like stride time) .. GENERATED FROM PYTHON SOURCE LINES 96-101 .. code-block:: default from gaitmap.parameters import TemporalParameterCalculation temporal_paras = TemporalParameterCalculation() temporal_paras = temporal_paras.calculate(stride_event_list=ed.min_vel_event_list_, sampling_rate_hz=sampling_rate_hz) .. GENERATED FROM PYTHON SOURCE LINES 102-105 Spatial Parameter Calculation ----------------------------- Like the temporal parameters, we can also calculate the spatial parameter. .. GENERATED FROM PYTHON SOURCE LINES 105-115 .. code-block:: default from gaitmap.parameters import SpatialParameterCalculation spatial_paras = SpatialParameterCalculation() spatial_paras = spatial_paras.calculate( stride_event_list=ed.min_vel_event_list_, positions=trajectory.position_, orientations=trajectory.orientation_, sampling_rate_hz=sampling_rate_hz, ) .. GENERATED FROM PYTHON SOURCE LINES 116-121 Inspecting the Results ---------------------- The class of each step allows you to inspect all results in detail. Here we will just print and plot the most important once. Note, that the plots below are for sure not the best way to represent results! .. GENERATED FROM PYTHON SOURCE LINES 121-127 .. code-block:: default import matplotlib.pyplot as plt print( f"The following number of strides were identified and parameterized for each sensor: {({k: len(v) for k, v in ed.min_vel_event_list_.items()})}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none The following number of strides were identified and parameterized for each sensor: {'left_sensor': 26, 'right_sensor': 26} .. GENERATED FROM PYTHON SOURCE LINES 128-132 .. code-block:: default for k, v in temporal_paras.parameters_pretty_.items(): v.plot() plt.title(f"All temporal parameters of sensor {k}") .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_001.png :alt: All temporal parameters of sensor left_sensor :srcset: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_002.png :alt: All temporal parameters of sensor right_sensor :srcset: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_002.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 133-137 .. code-block:: default for k, v in spatial_paras.parameters_pretty_.items(): v[["stride length [m]", "gait velocity [m/s]", "arc length [m]"]].plot() plt.title(f"All spatial parameters of sensor {k}") .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_003.png :alt: All spatial parameters of sensor left_sensor :srcset: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_004.png :alt: All spatial parameters of sensor right_sensor :srcset: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_004.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 138-141 .. code-block:: default for k, v in spatial_paras.parameters_pretty_.items(): v.filter(like="angle").plot() plt.title(f"All angle parameters of sensor {k}") .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_005.png :alt: All angle parameters of sensor left_sensor :srcset: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_005.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_006.png :alt: All angle parameters of sensor right_sensor :srcset: /auto_examples/full_pipelines/images/sphx_glr_mad_gait_pipeline_006.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 6.610 seconds) **Estimated memory usage:** 101 MB .. _sphx_glr_download_auto_examples_full_pipelines_mad_gait_pipeline.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: mad_gait_pipeline.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: mad_gait_pipeline.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_