Skip to content

Commit

Permalink
Refactored Gait configuration files and improved constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
vedran-kasalica committed Sep 5, 2024
1 parent ac39234 commit d44b3af
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 167 deletions.
60 changes: 36 additions & 24 deletions src/paradigma/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,59 @@
@dataclass(frozen=True)
class DataColumns():
"""
Enum for the data channels in tsdf.
Class containing the data channels in `tsdf`.
"""
ACCELEROMETER_X = "accelerometer_x"
ACCELEROMETER_Y = "accelerometer_y"
ACCELEROMETER_Z = "accelerometer_z"
GYROSCOPE_X = "gyroscope_x"
GYROSCOPE_Y = "gyroscope_y"
GYROSCOPE_Z = "gyroscope_z"
PPG = "green"
TIME = "time"
ACCELEROMETER_X : str = "accelerometer_x"
ACCELEROMETER_Y : str = "accelerometer_y"
ACCELEROMETER_Z : str = "accelerometer_z"
GYROSCOPE_X : str = "gyroscope_x"
GYROSCOPE_Y : str = "gyroscope_y"
GYROSCOPE_Z : str = "gyroscope_z"
PPG : str = "green"
TIME : str = "time"

# The following are used in gait analysis
GRAV_ACCELEROMETER_X = "grav_accelerometer_x"
GRAV_ACCELEROMETER_Y = "grav_accelerometer_y"
GRAV_ACCELEROMETER_Z = "grav_accelerometer_z"
PRED_GAIT = "pred_gait"
ANGLE = "angle"
ANGLE_SMOOTH = "angle_smooth"
VELOCITY = "velocity"
SEGMENT_NR = "segment_nr"
GRAV_ACCELEROMETER_X : str = "grav_accelerometer_x"
GRAV_ACCELEROMETER_Y : str = "grav_accelerometer_y"
GRAV_ACCELEROMETER_Z : str = "grav_accelerometer_z"
PRED_GAIT : str = "pred_gait"
PRED_ARM_SWING : str = "pred_arm_swing"
ANGLE : str = "angle"
ANGLE_SMOOTH : str = "angle_smooth"
VELOCITY : str = "velocity"
SEGMENT_NR : str = "segment_nr"

@dataclass(frozen=True)
class DataUnits():
"""
Enum for the data channel unit types in tsdf.
Class containing the data channel unit types in `tsdf`.
"""
ACCELERATION = "m/s^2"
ACCELERATION: str = "m/s^2"
""" The acceleration is in m/s^2. """
ROTATION = "deg/s"

ROTATION: str = "deg/s"
""" The rotation is in degrees per second. """

GRAVITY: str = "g"
""" The acceleration due to gravity is in g. """

POWER_SPECTRAL_DENSITY: str = "g^2/Hz"
""" The power spectral density is in g^2/Hz. """

FREQUENCY: str = "Hz"
""" The frequency is in Hz. """


@dataclass(frozen=True)
class TimeUnit():
"""
Enum for the `time` channel unit types in tsdf.
Class containing the `time` channel unit types in `tsdf`.
"""
RELATIVE_MS = "relative_ms"
RELATIVE_MS : str = "relative_ms"
""" The time is relative to the start time in milliseconds. """
ABSOLUTE_MS = "absolute_ms"
ABSOLUTE_MS : str = "absolute_ms"
""" The time is absolute in milliseconds. """
DIFFERENCE_MS = "difference_ms"
DIFFERENCE_MS : str = "difference_ms"
""" The time is the difference between consecutive samples in milliseconds. """

UNIX_TICKS_MS: int = 1000
4 changes: 2 additions & 2 deletions src/paradigma/feature_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from scipy.signal import find_peaks

from paradigma.constants import DataColumns
from paradigma.gait_analysis_config import GaitFeatureExtractionConfig, ArmSwingFeatureExtractionConfig
from paradigma.gait_analysis_config import IMUConfig


def generate_statistics(
Expand Down Expand Up @@ -606,7 +606,7 @@ def extract_peak_angular_velocity(
return


def extract_temporal_domain_features(config:GaitFeatureExtractionConfig | ArmSwingFeatureExtractionConfig, df_windowed:pd.DataFrame, l_gravity_stats=['mean', 'std']) -> pd.DataFrame:
def extract_temporal_domain_features(config: IMUConfig, df_windowed:pd.DataFrame, l_gravity_stats=['mean', 'std']) -> pd.DataFrame:
"""
Compute temporal domain features for the accelerometer signal. The features are added to the dataframe. Therefore the original dataframe is modified, and the modified dataframe is returned.
Expand Down
24 changes: 12 additions & 12 deletions src/paradigma/gait_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ def detect_gait(input_path: Union[str, Path], output_path: Union[str, Path], pat
threshold = float(f.read())

# Prepare the data
clf.feature_names_in_ = [f'{x}_power_below_gait' for x in config.l_accel_cols] + \
[f'{x}_power_gait' for x in config.l_accel_cols] + \
[f'{x}_power_tremor' for x in config.l_accel_cols] + \
[f'{x}_power_above_tremor' for x in config.l_accel_cols] + \
['std_norm_acc'] + [f'cc_{i}_accelerometer' for i in range(1, 13)] + [f'grav_{x}_{y}' for x in config.l_accel_cols for y in ['mean', 'std']] + \
[f'{x}_dominant_frequency' for x in config.l_accel_cols]
clf.feature_names_in_ = [f'{x}_power_below_gait' for x in config.l_accelerometer_cols] + \
[f'{x}_power_gait' for x in config.l_accelerometer_cols] + \
[f'{x}_power_tremor' for x in config.l_accelerometer_cols] + \
[f'{x}_power_above_tremor' for x in config.l_accelerometer_cols] + \
['std_norm_acc'] + [f'cc_{i}_accelerometer' for i in range(1, 13)] + [f'grav_{x}_{y}' for x in config.l_accelerometer_cols for y in ['mean', 'std']] + \
[f'{x}_dominant_frequency' for x in config.l_accelerometer_cols]
X = df.loc[:, clf.feature_names_in_]

# Make prediction
Expand Down Expand Up @@ -292,15 +292,15 @@ def detect_arm_swing(input_path: Union[str, Path], output_path: Union[str, Path]
clf = pd.read_pickle(os.path.join(path_to_classifier_input, config.classifier_file_name))

# Prepare the data
clf.feature_names_in_ = ['std_norm_acc'] + [f'{x}_power_below_gait' for x in config.l_accel_cols] + \
[f'{x}_power_gait' for x in config.l_accel_cols] + \
[f'{x}_power_tremor' for x in config.l_accel_cols] + \
[f'{x}_power_above_tremor' for x in config.l_accel_cols] + \
clf.feature_names_in_ = ['std_norm_acc'] + [f'{x}_power_below_gait' for x in config.l_accelerometer_cols] + \
[f'{x}_power_gait' for x in config.l_accelerometer_cols] + \
[f'{x}_power_tremor' for x in config.l_accelerometer_cols] + \
[f'{x}_power_above_tremor' for x in config.l_accelerometer_cols] + \
[f'cc_{i}_accelerometer' for i in range(1, 13)] + [f'cc_{i}_gyroscope' for i in range(1, 13)] + \
[f'grav_{x}_mean' for x in config.l_accel_cols] + [f'grav_{x}_std' for x in config.l_accel_cols] + \
[f'grav_{x}_mean' for x in config.l_accelerometer_cols] + [f'grav_{x}_std' for x in config.l_accelerometer_cols] + \
['range_of_motion', 'forward_peak_ang_vel_mean', 'backward_peak_ang_vel_mean', 'forward_peak_ang_vel_std',
'backward_peak_ang_vel_std', 'angle_perc_power', 'angle_dominant_frequency'] + \
[f'{x}_dominant_frequency' for x in config.l_accel_cols]
[f'{x}_dominant_frequency' for x in config.l_accelerometer_cols]

X = df.loc[:, clf.feature_names_in_]

Expand Down
Loading

0 comments on commit d44b3af

Please sign in to comment.