.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/initial_contacts/_01_icd_ionescu.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__01_icd_ionescu.py: .. _icd_ionescu: ICD Ionescu =========== This example shows how to use the initial_contacts Ionescu algorithm and how its results compare to the original matlab implementation. .. GENERATED FROM PYTHON SOURCE LINES 13-14 Import useful modules and packages .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: default import matplotlib.pyplot as plt import pandas as pd from mobgap.data import LabExampleDataset from mobgap.initial_contacts import IcdIonescu .. GENERATED FROM PYTHON SOURCE LINES 20-26 Loading some example 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 "InitialContact_Event" output as ground truth. .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: default example_data = LabExampleDataset( reference_system="INDIP", reference_para_level="wb" ) .. GENERATED FROM PYTHON SOURCE LINES 32-38 Performance on a single lab trial --------------------------------- Below we apply the algorithm to a lab trail, where we only expect a single gait sequence. 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 38-51 .. code-block:: default from mobgap.utils.conversions import to_body_frame 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 52-56 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 56-68 .. code-block:: default from mobgap.pipeline import GsIterator iterator = GsIterator() for (gs, data), result in iterator.iterate(imu_data, reference_wbs): result.ic_list = ( IcdIonescu().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 697
1 760
2 814
3 872
4 924
1 0 2929
1 2989
2 3056
3 3109
4 3176
5 3264
2 0 3913
1 3975
2 4085
3 4141
4 4201
5 4273
6 4355
7 4488
8 4553
9 4638
10 4758
11 4815
12 4863
13 4968
14 5038
3 0 7739
1 7846
2 7983
3 8096
4 8166
5 8229
6 8281
7 8336
8 8399
9 8461
10 8541
11 8606
4 0 9531
1 9593
2 9659
3 9723
4 9791
5 9853
5 0 12044
1 12099
2 12157
3 12211
4 12271
5 12414


.. GENERATED FROM PYTHON SOURCE LINES 69-72 Matlab Outputs -------------- To check if the algorithm was implemented correctly, we compare the results to the matlab implementation. .. GENERATED FROM PYTHON SOURCE LINES 72-102 .. 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_ionescu/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 700
1 763
2 818
3 875
4 930
2 0 2932
1 2992
2 3060
3 3115
4 3179
5 3267
3 0 3916
1 3979
2 4086
3 4144
4 4204
5 4276
6 4359
7 4491
8 4556
9 4641
10 4759
11 4816
12 4866
13 4969
14 5041
4 0 7742
1 7849
2 7987
3 8099
4 8169
5 8232
6 8284
7 8339
8 8402
9 8464
10 8544
11 8609
5 0 9534
1 9597
2 9661
3 9729
4 9794
5 9857
6 0 12047
1 12102
2 12160
3 12215
4 12275
5 12420


.. GENERATED FROM PYTHON SOURCE LINES 103-116 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 the same ICs as the matlab version, but wil a small shift to the left (around 2-5 samples/20-50 ms). This is likely due to some differences in the downsampling process. 2. Compared to the ground truth reference, both versions detect the IC too early most of the time. 3. 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 116-137 .. 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="icd_ionescu_py", ) plt.plot( detected_ics_matlab["ic"], imu_data["acc_is"].iloc[detected_ics_matlab["ic"]], "+", label="icd_ionescu_matlab", ) plt.xlim(reference_wbs.iloc[2]["start"] - 50, reference_wbs.iloc[2]["end"] + 50) plt.legend() plt.show() .. image-sg:: /auto_examples/initial_contacts/images/sphx_glr__01_icd_ionescu_001.png :alt: 01 icd ionescu :srcset: /auto_examples/initial_contacts/images/sphx_glr__01_icd_ionescu_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 138-142 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.324 seconds) **Estimated memory usage:** 9 MB .. _sphx_glr_download_auto_examples_initial_contacts__01_icd_ionescu.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: _01_icd_ionescu.py <_01_icd_ionescu.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: _01_icd_ionescu.ipynb <_01_icd_ionescu.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_