biosiglive.processing package

Submodules

biosiglive.processing.compute_mvc module

This file is part of biosiglive. it is an example to see how to use biosiglive to compute the maximal voluntary

contraction from EMG signals.

class biosiglive.processing.compute_mvc.ComputeMvc(interface_type: str | InterfaceType = InterfaceType.PytrignoClient, interface_ip: str = '127.0.0.1', interface_port: int = 801, output_file: str = None, muscle_names: list = None, emg_frequency: float = 2000, acquisition_rate: int = 100, mvc_windows: int = 2000, range_muscle: tuple | int = None, custom_interface: GenericInterface = None)[source]

Bases: object

run(show_data: bool = False) list[source]

Run the MVC program.

Parameters:

show_data (bool) – If True, the data will be displayed in live in a separate plot.

Returns:

The list of the MVC value for each muscle.

Return type:

list

set_processing_method(moving_average: bool = True, low_pass: bool = False, custom: bool = False, custom_function: callable = None, bandpass_frequency: tuple = (10, 425), lowpass_frequency: float = 5, lowpass_order: int = 4, butterworth_order: int = 4, ma_window: int = 200)[source]

Set the emg processing method. This method allow to customize the processing of the emg signal, so it need to be called before the start of the acquisition.

Parameters:
  • moving_average (bool) – If True, the emg data will be processed with a moving average.

  • low_pass (bool) – If True, the emg data will be processed with a low pass filter.

  • custom (bool) – If True, the emg data will be processed with a custom function.

  • custom_function (callable) – The custom function. Input : raw data, device frequency Output : processed data.

  • bandpass_frequency (tuple) – The frequency of the bandpass filter.

  • lowpass_frequency (float) – The frequency of the low pass filter.

  • lowpass_order (int) – The order of the low pass filter.

  • butterworth_order (int) – The order of the butterworth filter.

  • ma_window (int) – The size of the moving average window.

biosiglive.processing.data_processing module

This file contains the functions for data processing (offline and in real-time). Both class herites

from the GenericProcessing class.

class biosiglive.processing.data_processing.GenericProcessing[source]

Bases: object

butter_lowpass_filter(data: ndarray, lowcut: float, fs: float, order: int = 4) ndarray[source]

Apply a butter lowpass filter.

Parameters:
  • data (numpy.ndarray) – Data to filter.

  • lowcut (float) – Low cut frequency.

  • fs (float) – Sampling frequency.

  • order (int) – Order of the filter.

Returns:

Filtered data.

Return type:

numpy.ndarray

calibration_matrix(data: ndarray, matrix: ndarray) ndarray[source]

Apply a calibration matrix to the data.

Parameters:
  • data (numpy.ndarray) – Data to calibrate.

  • matrix (numpy.ndarray) – Calibration matrix.

Returns:

Calibrated data.

Return type:

numpy.ndarray

static center(emg_data: ndarray, center_value: float = None) ndarray[source]

Center the EMG data.

Parameters:
  • emg_data (numpy.ndarray) – EMG data.

  • center_value (int) – Value to center the data.

Returns:

Centered EMG data.

Return type:

numpy.ndarray

static normalize_emg(emg_data: ndarray, mvc_list: list) ndarray[source]

Normalize EMG data.

Parameters:
  • emg_data (numpy.ndarray) – EMG data.

  • mvc_list (list) – List of MVC values.

Returns:

Normalized EMG data.

Return type:

numpy.ndarray

process_generic_signal(data: ndarray, norm_values: list | tuple = None, band_pass_filter=True, low_pass_filter=False, moving_average=True, centering=True, absolute_value=True, normalization=False, moving_average_window=200, **kwargs) ndarray[source]

Process EMG data.

Parameters:
  • data (numpy.ndarray) – EMG data.

  • norm_values (list) – Values to normalize the signal.

  • band_pass_filter (bool) – Apply band pass filter.

  • low_pass_filter (bool) – Apply low pass filter.

  • moving_average (bool) – Apply moving average.

  • centering (bool) – Apply centering.

  • absolute_value (bool) – Apply absolute value.

  • normalization (bool) – Apply normalization.

  • moving_average_window (int) – Moving average window.

Returns:

Processed EMG data.

Return type:

numpy.ndarray

update_signal_processing_parameters(**kwargs)[source]

update the signal processing parameters.

Parameters:

kwargs (dict) – Dictionary of parameters.

class biosiglive.processing.data_processing.OfflineProcessing(data_rate: float = None, processing_window: int = None)[source]

Bases: GenericProcessing

static compute_mvc(nb_muscles: int, mvc_trials: ndarray, window_size: int, tmp_file: str = None, output_file: str = None, save_file: bool = False) list[source]

Compute MVC from several mvc_trials.

Parameters:
  • nb_muscles (int) – Number of muscles.

  • mvc_trials (numpy.ndarray) – EMG data for all trials.

  • window_size (int) – Size of the window to compute MVC. Usually it is 1 second so the data rate.

  • tmp_file (str) – Name of the temporary file.

  • output_file (str) – Name of the output file.

  • save_file (bool) – If true, save the results.

Returns:

MVC for each muscle.

Return type:

list

process_emg(data: ndarray, mvc_list: list = None, **kwargs) ndarray[source]

Process EMG data.

Parameters:
  • data (np.ndarray) – Raw EMG data.

  • mvc_list (list) – List of MVC for each muscle.

Returns:

Processed EMG data.

Return type:

np.ndarray

class biosiglive.processing.data_processing.RealTimeProcessing(data_rate: int | float, processing_window: int = None)[source]

Bases: GenericProcessing

custom_processing(funct: callable, data_tmp: ndarray, **kwargs) ndarray[source]

Allow to apply a custom processing function to the data.

Parameters:
  • funct (callable) – Function to apply to the data.

  • data_tmp (numpy.ndarray) – Data to process.

Returns:

Processed data.

Return type:

numpy.ndarray

get_mean_process_time()[source]
get_peaks(new_sample: ndarray, threshold: float, min_peaks_interval=None) tuple[source]

Allow to get the number of peaks for an analog signal (to get cadence from treadmill for instance).

Parameters:
  • new_sample (numpy.ndarray) – New sample to add to the signal.

  • threshold (float) – Threshold to detect peaks.

  • min_peaks_interval (float) – Minimum interval between two peaks.

Returns:

Number of peaks and the processed signal.

Return type:

tuple

process_emg(emg_data: ndarray, mvc_list: list | tuple = None, band_pass_filter=True, low_pass_filter=False, moving_average=True, centering=True, absolute_value=True, normalization=False, moving_average_window=200, window_weights: list = None, **kwargs) ndarray[source]

Process EMG data in real-time.

Parameters:
  • emg_data (numpy.ndarray) – Temporary EMG data (nb_emg, emg_sample).

  • mvc_list (list) – MVC values.

  • band_pass_filter (bool) – True if apply band pass filter.

  • low_pass_filter (bool) – True if apply low pass filter.

  • moving_average (bool) – True if apply moving average.

  • centering (bool) – True if apply centering.

  • absolute_value (bool) – True if apply absolute value.

  • normalization (bool) – True if apply normalization.

  • moving_average_window (int) – Moving average window.

Returns:

processed EMG data.

Return type:

np.ndarray

process_imu(im_data: ndarray, accel: bool = False, squared: bool = False, norm_min_bound: int = None, norm_max_bound: int = None, **kwargs) ndarray[source]

Process IMU data in real-time.

Parameters:
  • im_data (numpy.ndarray) – Temporary IMU data (nb_imu, im_sample).

  • accel (bool) – True if current data is acceleration data to adapt the processing.

  • squared (bool) – True if apply squared.

  • norm_min_bound (int) – Normalization minimum bound.

  • norm_max_bound (int) – Normalization maximum bound.

Returns:

processed IMU data.

Return type:

np.ndarray

biosiglive.processing.msk_functions module

This file contains biorbd specific functions for musculoskeletal analysis such as inverse or direct kinematics.

class biosiglive.processing.msk_functions.MskFunctions(model: str, data_buffer_size: int = 1, system_rate: int = 100)[source]

Bases: object

clean_all_buffers()[source]
compute_direct_kinematics(states: ndarray) ndarray[source]

Compute the direct kinematics using the joint angle and a biorbd model type.

Parameters:

states (np.ndarray) – The states to compute the direct kinematics.

Returns:

The markers.

Return type:

np.ndarray

compute_inverse_dynamics(joint_positions: ndarray = None, joint_velocities: ndarray = None, joint_accelerations: ndarray = None, state_idx_to_process: list = (), lowpass_frequency: list | int = None, windows_length: list | int = None, positions_from_inverse_kinematics: bool = False, velocities_from_inverse_kinematics: bool = False, accelerations_from_inverse_kinematics: bool = False, external_load: any = None) ndarray[source]

Compute the inverse dynamics using the model kinematics and a biorbd model type.

Parameters:
  • joint_positions (np.ndarray) – The joint position for each model joints

  • joint_velocities (np.ndarray) – The joint velocities for each model joints

  • joint_accelerations (np.ndarray) – The joint accelerations for each model joints

  • state_idx_to_process (list) – The list of the index of the data to apply a low pass filter on. If empty no filter will be applied.

  • lowpass_frequency (Union[list, int]) – the list of the frequency for the low pass filter, in the same order as the index, if an integer is given the same value will be used for each index

  • windows_length (Union[list, int]) –

    The list of the window length for the moving average filter,

    in the same order as the index, if an integer is given the same value will be used for each index

  • positions_from_inverse_kinematics (bool) –

    If true use the result of precomputed inverse kinematics.

    Note that the inverse kinematics must be computed before.

  • velocities_from_inverse_kinematics (bool) –

    If true use the result of precomputed inverse kinematics.

    Note that the inverse kinematics must be computed before.

Returns:

The generalized torque.

Return type:

np.ndarray

compute_inverse_kinematics(markers: ndarray, method: InverseKinematicsMethods | str = InverseKinematicsMethods.BiorbdLeastSquare, kalman_freq: int | float = 100, kalman: callable = None, custom_function: callable = None, initial_guess: ndarray | list = None, qdot_from_finite_difference: bool = False, noise_factor=1e-10, error_factor=1e-05, **kwargs) tuple[source]

Function to apply the inverse kinematics using the markers data and a biorbd model type.

Parameters:
  • markers (numpy.array) – The experimental markers.

  • kalman (biorbd.KalmanReconsMarkers) – The Kalman filter to use.

  • kalman_freq (int) – The frequency of the Kalman filter.

  • method (Union[InverseKinematicsMethods, str]) – The method to use to compute the inverse kinematics.

  • custom_function (callable) – Custom function to use.

  • initial_guess (Union[np.ndarray, list]) – Initial generalized coordinate, velocity and acceleration for the kalman filter

  • qdot_from_finite_difference (bool) – If true the velocity will be computed using a finite difference method.

Returns:

The joint angle and velocity.

Return type:

tuple

compute_joint_reaction_load(q: ndarray = None, qdot: ndarray = None, qddot: ndarray = None, muscle_activations: ndarray = None, act_from_static_optimisation: bool = False, kinetics_from_inverse_dynamics: bool = False, express_in_coordinate: str = None, apply_on_segment: str = 'all', from_distal: bool = True, application_point: list | tuple = None, external_loads: any = None)[source]
compute_static_optimization(q: ndarray = None, q_dot: ndarray = None, tau: ndarray = None, ocp_solver: any = None, compile_c_code: bool = True, use_residual_torque: bool = True, torque_tracking_as_objective: bool = True, muscle_torque_dynamics_func: any = None, scaling_factor: list | tuple = (1, 1), muscle_track_idx: list = None, emg: ndarray = None, weight: dict = None, x0: ndarray = None, data_from_inverse_dynamics: bool = False, solver_options: dict = None, compile_only_first_call: bool = True, print_optimization_status: bool = False)[source]

Compute the static optimization using the model kinematics and a biorbd model type. :param q: The joint position for each model joints :type q: np.ndarray :param q_dot: The joint velocities for each model joints :type q_dot: np.ndarray :param tau: The joint torques for each model joints :type tau: np.ndarray :param ocp_solver: The acados solver to use, if none is provided a new one will be created :type ocp_solver: AcadosOcpSolver :param compile_c_code: If true the c code will be compiled :type compile_c_code: bool :param use_residual_torque: If true the residual torque will be used :type use_residual_torque: bool :param torque_tracking_as_objective: If true the torque tracking will be used as objective, otherwise it will be a constraint :type torque_tracking_as_objective: bool :param muscle_torque_dynamics_func: The casadi function to use for the muscle torque dynamics (muscle activation -> muscle torque) :type muscle_torque_dynamics_func: ca.Function :param scaling_factor: The scaling factor to use for the muscle activation and muscle torque optimization variables :type scaling_factor: list :param muscle_track_idx: The index of the muscles to track :type muscle_track_idx: list :param emg: The emg data to track for the muscle_track_idx provided :type emg: np.ndarray :param weight: The weight to use for the objective function :type weight: dict :param x0: The initial guess for the optimization :type x0: np.ndarray :param data_from_inverse_dynamics: If true the data will be taken from the inverse dynamics (q, qdot, tau) instead of the provided data :type data_from_inverse_dynamics: bool :param solver_options: The solver options to use for the acados solver :type solver_options: dict :param compile_only_first_call: If true the c code will be compiled only for the first call :type compile_only_first_call: bool :param print_optimization_status: If true the status of the optimization will be printed :type print_optimization_status: bool

Returns:

The optimal activation and torque for each muscles

Return type:

tuple

get_activation_from_so()[source]
get_filtered_kinematics_from_id()[source]
get_jrf_from_external_load_analysis()[source]
get_kinematics_from_ik()[source]
get_mean_process_time()[source]

Get the mean process time.

Returns:

The mean process time.

Return type:

float

get_tau_from_id()[source]

Module contents