McCamley L/R detector#

The McCamley L/R detector is a simple algorithm to detect the laterality of initial contacts based on the sign of the angular velocity signal. We use a modified version of the original McCamley algorithm, which includes a smoothing filter to reduce the influence of noise on the detection.

This example shows how to use the algorithm and compares the output to the reference labels on some example data.

import pandas as pd

from mobgap.data import LabExampleDataset

Loading some example data#

Note

More infos about data loading can be found in the 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.

We only use the data from the “simulated daily living” activity test from a single particomand.

example_data = LabExampleDataset(reference_system="INDIP", reference_para_level="wb")
single_test = example_data.get_subset(cohort="MS", participant_id="001", test="Test11", trial="Trial1")

imu_data = single_test.data["LowerBack"]
reference_wbs = single_test.reference_parameters_.wb_list

sampling_rate_hz = single_test.sampling_rate_hz
ref_ics = single_test.reference_parameters_.ic_list
ref_ics_rel_to_gs = single_test.reference_parameters_relative_to_wb_.ic_list

reference_wbs
start end n_strides duration_s length_m avg_speed_mps avg_cadence_spm avg_stride_length_m termination_reason
wb_id
1 1019 1768 9 7.48 4.468932 0.847668 107.795850 0.942678 Pause
2 4534 5549 11 10.14 2.900453 0.365176 93.396106 0.483923 Pause
3 9665 10569 9 9.03 2.140232 0.294058 75.981133 0.506458 Pause
4 12337 14633 28 22.95 11.201110 0.634425 92.337768 0.803933 Pause
5 20151 20982 11 8.30 2.390709 0.371746 87.915774 0.507484 Pause
6 21378 22129 9 7.50 2.517558 0.492965 95.365740 0.599360 Pause


Applying the algorithm using reference ICs#

We use the McCamley algorithm to detect the laterality of the initial contacts. For this we need the IMU data and the indices of the initial contacts per GS. To focus this example on the L/R detection, we use the reference ICs from the INDIP system as input. In a real application, we would use the output of the IC-detectors as input.

We will use the GsIterator to iterate over the gait sequences and apply the algorithm to each wb. Note, that we use the ic_list result key, as the output of all L/R detectors is identical to the output of the IC-detectors, but with an additional lr_label column.

ic lr_label
wb_id step_id
1 0 1019 right
1 1065 left
2 1129 right
3 1172 left
4 1236 right
... ... ... ...
6 93 21896 right
94 21951 right
95 22042 left
96 22089 right
97 22128 right

91 rows × 2 columns



Compare the results to the reference#

We compare the detected initial contacts to the reference labels. One easy way to compare the results is to visualize them as colorful bars.

import matplotlib.pyplot as plt


def plot_lr(ref, detected):
    fig, ax = plt.subplots(figsize=(15, 5))
    # We plot one box either (red or blue depending on the laterality) for each detected IC ignoring the actual time
    for (_, row), (_, ref_row) in zip(detected.iterrows(), ref.iterrows()):
        ax.plot([row["ic"], row["ic"]], [0, 0.98], color="r" if row["lr_label"] == "left" else "b", linewidth=5)
        ax.plot(
            [ref_row["ic"], ref_row["ic"]], [1.02, 2], color="r" if ref_row["lr_label"] == "left" else "b", linewidth=5
        )

    ax.set_yticks([0.5, 1.5])
    ax.set_yticklabels(["Detected", "Reference"])
    return fig, ax


fig, _ = plot_lr(ref_ics, detected_ics)
fig.show()
01 mccamley

If we zoom in on a longer WB, we can see that for some ICs the L/R label does not match. But, in particular for regular gait in the center of the WB, the labels match quite well.

fig, ax = plot_lr(ref_ics, detected_ics)
ax.set_xlim(12000, 15000)
fig.show()
01 mccamley

We can also quantify the agreement between the detected and the reference labels using typical classification metrics.

from sklearn.metrics import classification_report

pd.DataFrame(
    classification_report(ref_ics.lr_label, detected_ics.lr_label, target_names=["left", "right"], output_dict=True)
).T
precision recall f1-score support
left 0.883721 0.760000 0.817204 50.000000
right 0.750000 0.878049 0.808989 41.000000
accuracy 0.813187 0.813187 0.813187 0.813187
macro avg 0.816860 0.819024 0.813097 91.000000
weighted avg 0.823473 0.813187 0.813503 91.000000


Total running time of the script: (0 minutes 2.338 seconds)

Estimated memory usage: 15 MB

Gallery generated by Sphinx-Gallery