.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/initial_contacts/_03_hklee_algo.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_initial_contacts__03_hklee_algo.py: HKLee algo ========== This example shows how to use the improved HKLee algorithm and some examples on how the results compare to the original matlab implementation. .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: default import pandas as pd from matplotlib import pyplot as plt from mobgap.data import LabExampleDataset from mobgap.initial_contacts._hklee_algo_improved import IcdHKLeeImproved .. GENERATED FROM PYTHON SOURCE LINES 16-25 Loading data ------------ .. note :: More infos about data loading can be found in the :ref:`data loading example `. We load example data from the lab dataset together with the INDIP reference system. We will use the INDIP output for initial contacts ("ic") as ground truth. Like most algorithms, the GsdIluz requires the data to be in body frame coordinates. As we know the sensor was well aligned, we can just use ``to_body_frame`` to transform the data. .. GENERATED FROM PYTHON SOURCE LINES 25-27 .. code-block:: default from mobgap.utils.conversions import to_body_frame .. GENERATED FROM PYTHON SOURCE LINES 28-33 Loading some data ----------------- We load example data from the lab dataset together with the INDIP reference system. Like most algorithms, the algorithm requires the data to be in body frame coordinates. As we know the sensor was well aligned, we can just use ``to_body_frame`` to transform the data. .. GENERATED FROM PYTHON SOURCE LINES 33-48 .. code-block:: default example_data = LabExampleDataset( reference_system="INDIP", reference_para_level="wb" ) single_test = example_data.get_subset( cohort="HA", participant_id="001", test="Test11", trial="Trial1" ) imu_data = to_body_frame(single_test.data_ss) reference_wbs = single_test.reference_parameters_.wb_list sampling_rate_hz = single_test.sampling_rate_hz ref_ics = single_test.reference_parameters_.ic_list reference_wbs .. raw:: html
start end n_strides duration_s length_m avg_walking_speed_mps avg_cadence_spm avg_stride_length_m termination_reason
wb_id
0 632 988 5 3.55 3.428989 0.975373 104.069084 1.124391 Pause
1 2864 3325 4 4.60 1.452572 0.411857 81.296475 0.581029 Pause
2 3853 5085 16 12.31 7.044042 0.617801 89.246331 0.838960 Pause
3 7641 8621 12 9.79 4.396574 0.510108 94.370318 0.645176 Pause
4 9451 9932 6 4.80 3.545277 0.755728 88.778698 1.021695 Pause
5 11989 12517 6 5.27 3.514735 0.880632 95.832693 1.021576 Pause


.. GENERATED FROM PYTHON SOURCE LINES 49-53 Applying the algorithm ---------------------- Below we apply the shin algorithm to a lab trial. We will use the `GsIterator` to iterate over the gait sequences and apply the algorithm to each wb. .. GENERATED FROM PYTHON SOURCE LINES 53-67 .. code-block:: default from mobgap.pipeline import GsIterator iterator = GsIterator() for (gs, data), result in iterator.iterate(imu_data, reference_wbs): result.ic_list = ( IcdHKLeeImproved() .detect(data, sampling_rate_hz=sampling_rate_hz) .ic_list_ ) detected_ics = iterator.results_.ic_list detected_ics .. raw:: html
ic
wb_id step_id
0 0 649
1 712
2 772
3 829
4 885
5 941
1 0 2879
1 2943
2 3006
3 3075
4 3127
5 3204
6 3276
2 0 3866
1 3927
2 3994
3 4093
4 4156
5 4225
6 4292
7 4365
8 4438
9 4504
10 4573
11 4651
12 4706
13 4771
14 4827
15 4879
16 4984
3 0 7654
1 7748
2 7797
3 7858
4 7913
5 8003
6 8059
7 8111
8 8181
9 8241
10 8293
11 8352
12 8414
13 8480
14 8557
4 0 9481
1 9546
2 9609
3 9674
4 9741
5 9809
6 9877
5 0 12002
1 12057
2 12114
3 12170
4 12226
5 12289
6 12379
7 12474


.. GENERATED FROM PYTHON SOURCE LINES 68-71 Matlab Outputs -------------- To check if the algorithm was implemented correctly, we compare the results to the matlab implementation. .. GENERATED FROM PYTHON SOURCE LINES 71-101 .. code-block:: default import json from mobgap import PACKAGE_ROOT def load_matlab_output(datapoint): p = datapoint.group_label with ( PACKAGE_ROOT.parent / f"example_data/original_results/icd_hklee_improved/lab/{p.cohort}/{p.participant_id}/SD_Output.json" ).open() as f: original_results = json.load(f)["SD_Output"][p.time_measure][p.test][ p.trial ]["SU"]["LowerBack"]["SD"] if not isinstance(original_results, list): original_results = [original_results] ics = {} for i, gs in enumerate(original_results, start=1): ics[i] = pd.DataFrame({"ic": gs["IC"]}).rename_axis(index="step_id") return ( pd.concat(ics, names=["wb_id", ics[1].index.name]) * datapoint.sampling_rate_hz ).astype("int64") detected_ics_matlab = load_matlab_output(single_test) detected_ics_matlab .. raw:: html
ic
wb_id step_id
1 0 652
1 717
2 777
3 834
4 892
... ... ...
6 4 12231
5 12294
6 12384
7 12479
8 12514

64 rows × 1 columns



.. GENERATED FROM PYTHON SOURCE LINES 102-119 Plotting the results -------------------- With that we can compare the python, matlab and ground truth results. We zoom in into one of the gait sequences to better see the output. We can make a couple of main observations: 1. The python version finds (for the most part) the same ICs as the matlab version, but detects them slightly earlies (around 2-8 samples/20-80 ms). This is likely due to some differences in the downsampling process. 2. Matlab and Python sometimes differ in the detection in the first and the last IC, if they are close to the border of the GS. This is because the morphological filter in the Python and the Matlab treat the border differently. 3. Compared to the ground truth reference, both versions detect the IC too late most of the time. 4. Both algorithms can not detect the first IC of the gait sequence. However, this is expected, as per definition, this first IC marks the start of the WB in the reference system. Hence, there are no samples before that point the algorithm can use to detect the IC. .. GENERATED FROM PYTHON SOURCE LINES 119-141 .. code-block:: default imu_data.reset_index(drop=True).plot(y="acc_is") plt.plot( ref_ics["ic"], imu_data["acc_is"].iloc[ref_ics["ic"]], "o", label="ref" ) plt.plot( detected_ics["ic"], imu_data["acc_is"].iloc[detected_ics["ic"]], "x", label="hklee_algo_py", ) plt.plot( detected_ics_matlab["ic"], imu_data["acc_is"].iloc[detected_ics_matlab["ic"]], "+", label="hklee_algo_matlab", ) plt.xlim(reference_wbs.iloc[3]["start"] - 50, reference_wbs.iloc[3]["end"] + 50) plt.legend() plt.show() .. image-sg:: /auto_examples/initial_contacts/images/sphx_glr__03_hklee_algo_001.png :alt: 03 hklee algo :srcset: /auto_examples/initial_contacts/images/sphx_glr__03_hklee_algo_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 142-146 Evaluation of the algorithm against a reference -------------------------------------------------- To quantify how the Python output compares to the reference labels, we are providing a range of evaluation functions. See the :ref:`example on ICD evaluation ` for more details. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.300 seconds) **Estimated memory usage:** 9 MB .. _sphx_glr_download_auto_examples_initial_contacts__03_hklee_algo.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: _03_hklee_algo.py <_03_hklee_algo.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: _03_hklee_algo.ipynb <_03_hklee_algo.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_