gaitutils package

Subpackages

Submodules

gaitutils.autoprocess module

gaitutils.c3d module

C3D reader functions.

NB: do not use the data readers from this file directly. They are intended to be called via the read_data module.

@author: Jussi (jnu@iki.fi)

gaitutils.c3d.get_analysis(c3dfile, condition='unknown')[source]

Get ANALYSIS values from a c3d file.

Usually these are the time-distance parameters, but other values may be stored as well.

Parameters:
  • c3dfile (str | Path) – Path to the file.

  • condition (str, optional) – The condition, by default ‘unknown’. The condition is only used to annotate the output dictionary.

Returns:

A nested dict of the analysis values. Keys are condition, variable, and context/unit. For example, di[‘unknown’][‘Step Width’][‘Right’] would contain the step width for the right foot.

Return type:

dict

gaitutils.config module

Handles gaitutils config files.

@author: Jussi (jnu@iki.fi)

gaitutils.eclipse module

Created on Fri Sep 23 10:59:28 2016

Eclipse (database) hacks.

@author: Jussi (jnu@iki.fi)

class gaitutils.eclipse.FileFilter(fname)[source]

Bases: object

Filter class for configobj.

File-like class that will replace strings according to the replace dict below. This is needed for prefiltering before configobj parsing, since configobj will not tolerate lines with neither key nor value (which seem to occasionally appear in Eclipse files). Also deduplicates successive identical lines for same reason.

close()[source]
read()[source]

Read data.

ConfigObj seems to use only this method.

replace = {'\n=\n': '\n'}
gaitutils.eclipse.get_eclipse_keys(fname_enf, return_empty=False)[source]

Read key/value pairs from enf file.

Currently, only keys in the TRIAL_INFO section will be read.

Parameters:
  • fname_enf (str) – The filename.

  • return_empty (bool, optional) – If True, return also keys without value

Returns:

Dict of the eclipse keys and values.

Return type:

dict

gaitutils.eclipse.set_eclipse_keys(fname_enf, eclipse_dict, update_existing=False)[source]

Set key/value pairs in enf file.

Keys will be written into the TRIAL_INFO section.

Parameters:
  • fname_enf (str) – The filename.

  • eclipse_dict (dict) – A dict containing the new key/value pairs.

  • update_existing (bool, optional) – If True, overwrite existing keys.

gaitutils.emg module

gaitutils.envutils module

Created on Tue Mar 17 14:41:31 2015

Stuff related to Python environment

@author: Jussi (jnu@iki.fi)

exception gaitutils.envutils.GaitDataError[source]

Bases: Exception

Custom exception class to indicate gait data related errors

gaitutils.envutils.lru_cache_checkfile(fun)[source]

Cache function results, unless the argument file has changed.

A lru_cache -style decorator for functions that take a file name argument. Makes sense for functions that read a file and take a long time to process the data (anything that takes significantly longer than the md5 digest). Works by computing the md5 digest for the input file and passing that on to lru_cache, so the cache is invalidated if file contents have changed.

Parameters:

fun (function) – The function to cache.

Returns:

The cached function.

Return type:

function

gaitutils.models module

gaitutils.nexus module

gaitutils.normaldata module

gaitutils.numutils module

Created on Fri Sep 23 11:17:10 2016

Misc numerical utils

@author: Jussi (jnu@iki.fi)

gaitutils.numutils.center_of_pressure(F, M, dz)[source]

Compute center of pressure from forceplate data.

Computes CoP according to AMTI formula. The results differ slightly (typically few mm) from those given by Nexus, for unknown reasons (different filter?) See http://health.uottawa.ca/biomech/courses/apa6903/amticalc.pdf

Parameters:
  • F (ndarray) – The force vector (Nx3). N is the number of observations.

  • M (ndarray) – The moment vector (Nx3)

  • dz (float) – The thickness of the plate, or the distance from moment origin to physical origin.

Returns:

The center of pressure (Nx3)

Return type:

ndarray

gaitutils.numutils.digitize_array(v, b)[source]

Replace all elements of v with their closest matches in b.

Uses abs(x-y) as the distance metric. This function is similar to np.digitize(), but simpler in usage and implementation.

Parameters:
  • v (array_like) – Array to make replacements in.

  • b (array_like) – Array to pick replacements from.

Returns:

The result.

Return type:

ndarray

gaitutils.numutils.envelope(data, sfrate=None, axis=None)[source]

Calculate an envelope for data using the configured method

gaitutils.numutils.falling_zerocross(x)[source]

Find indices of falling zero crossings in array.

The indices are defined as n for which x[n] <= 0 and x[n-1] > 0.

Parameters:

x (array_like) – The data.

Returns:

The indices (np.where output)

Return type:

tuple

gaitutils.numutils.kabsch_rotation(P, Q)[source]

Calculate a rotation matrix P->Q using the Kabsch algorithm.

gaitutils.numutils.mad(data, axis=None, scale=1.4826, keepdims=False)[source]

Median absolute deviation (MAD).

Defined as the median absolute deviation from the median of the data. A robust alternative to stddev. Results should be identical to scipy.stats.median_absolute_deviation(), which does not take a keepdims argument.

Parameters:
  • data (array_like) – The data.

  • scale (float, optional) – Scaling of the result. By default, it is scaled to give a consistent estimate of the standard deviation of values from a normal distribution.

  • axis (numpy axis spec, optional) – Axis or axes along which to compute MAD.

  • keepdims (bool, optional) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one.

Returns:

The MAD.

Return type:

ndarray

gaitutils.numutils.modified_zscore(data, axis=None, single_mad=None)[source]

Modified Z-score.

Z-score analogue computed using robust (median-based) statistics.

Parameters:
  • data (array_like) – The data

  • axis (numpy axis spec, optional) – Axis or axes along which to compute the statistic.

  • single_mad (bool, optional) – Use a single MAD estimate computed all over the data. If False, MAD will be computed along given axis (e.g. separately for each variable).

Returns:

The modified Z-score.

Return type:

ndarray

gaitutils.numutils.outliers(x, axis=0, single_mad=None, p_threshold=0.001)[source]

Robustly detect outliers assuming a normal distribution.

A modified Z-score is first computed based on the data. Then a threshold value Zlim is computed from p_threshold, and values that exceed Zlim are rejected. p_threshold is the probability of rejection assuming strictly normally distributed data, i.e. probability for “false outlier”

Parameters:
  • data (array_like) – The data.

  • axis (numpy axis spec, optional) – Axis or axes along which to compute the Z scores. E.g. axis=0 computes row-wise Z scores and rejects based on those.

  • single_mad (bool) – Use a single MAD estimate computed all over the data. If False, the MAD will be computed along given axis (e.g. separately for each variable).

  • p_threshold (float) – The P threshold for rejection.

Returns:

Indexes of rejected values (np.where output)

Return type:

tuple

gaitutils.numutils.rising_zerocross(x)[source]

Find indices of rising zero crossings in array.

The indices are defined as n for which x[n] >= 0 and x[n-1] < 0.

Parameters:

x (array_like) – The data.

Returns:

The indices (np.where output)

Return type:

tuple

gaitutils.numutils.rms(data, win, axis=None, pad_mode=None)[source]

Calculate rolling window RMS.

Parameters:
  • data (ndarray) – The data.

  • win (int) – Window length.

  • axis (numpy axis spec, optional) – Axis along which to compute rms.

  • pad_mode (str or function, optional) – Padding mode. See np.pad for details.

Returns:

The rms data.

Return type:

ndarray

gaitutils.read_data module

gaitutils.sessionutils module

gaitutils.stats module

gaitutils.timedist module

gaitutils.trial module

gaitutils.utils module

Utility functions for processing gait data.

@author: Jussi (jnu@iki.fi)

gaitutils.utils.automark_events(source, mkrdata=None, events_range=None, vel_thresholds=None, roi=None, plot=False)[source]

Automatically mark foot strike and toeoff events.

Events are marked based on velocity thresholding. Absolute thresholds can be specified as arguments. Otherwise, relative thresholds will be calculated based on the data. Optimal results will be obtained when thresholds are predetermined based on forceplate data, but it is not necessary.

Before running automark, run reconstruct, label, gap fill and filter pipelines. Filtering is important to get reasonably smooth derivatives.

Parameters:
  • source (str | ViconNexus) – The data source, either c3d filename or ViconNexus connection. For Nexus connections, the events can automatically be inserted into Nexus. For c3d files, the events are returned but not actually written to the c3d file.

  • mkrdata (dict, optional) – The marker data dict. If not given, it will be read from the source. If given, it must include foot markers and subject tracking markers (see cfg).

  • events_range (array_like, optional) – If specified, the events will be restricted to given coordinate range in the principal gait direction. E.g. [-1000, 1000]

  • vel_thresholds (dict, optional) – Absolute velocity thresholds for identifying events. These can be obtained from forceplate data (utils.check_forceplate_contact). If None, relative thresholds will be computed based on marker data.

  • roi (array_like, optional) – If not None, specifies a ROI (in frames) inside which to mark events.

  • plot (bool, optional) – Plot velocity curves and events using matplotlib. Mostly for debug purposes.

gaitutils.utils.avg_markerdata(mkrdata, markers, roi=None, fail_on_gaps=True, avg_velocity=False)[source]

Average marker data.

Parameters:
  • mkrdata (dict) – The markerdata dict.

  • markers (list) – Markers to average.

  • roi (array-like) – If given, specified a ROI. Gaps outside the ROI will be ignored.

  • fail_on_gaps (bool, optional) – If True, raise an exception on ANY gaps. Otherwise, markers with gaps will not be included in the average.

  • avg_velocity (bool, optional) – If True, return averaged marker velocity data instead of position.

Returns:

The averaged data (Nx3).

Return type:

ndarray

gaitutils.utils.detect_forceplate_events(source, marker_data=None, eclipse_fp_info=None, roi=None, return_nplates=False)[source]

Detect frames where valid forceplate strikes and toeoffs occur.

Uses forceplate data and estimated foot shape.

If supplied, marker_data must include foot and pelvis markers. Otherwise the marker data will be read from source.

If the fp_info dict is supplied, no marker-based checks will be done; instead the Eclipse forceplate info will be used to determine the foot. Eclipse info is written e.g. as {FP1: ‘Left’}, where plate indices start from 1 and the value can be ‘Auto’, ‘Left’, ‘Right’ or ‘Invalid’. NB: even if Eclipse info is used, foot strike and toeoff frames must be determined from forceplate data.

If roi is given e.g. [100, 300], all marker data checks will be restricted to roi.

gaitutils.utils.get_contexts(right_first=False)[source]

Return the usual contexts and their names as pairs.

Our default is to return left size first.

gaitutils.utils.is_plugingait_set(mkrdata)[source]

Check whether marker data set corresponds to Plug-in Gait (full body or lower body only). Extra markers are accepted.

gaitutils.utils.marker_gaps(mdata, ignore_edge_gaps=True)[source]

Find gaps for a marker.

Parameters:
  • mdata (ndarray) – Marker position data. See read_data.get_marker_data()

  • ignore_edge_gaps (bool, optional) – If True, leading and trailing gaps are ignored.

Returns:

The indices of frames where gaps occur.

Return type:

ndarray

gaitutils.videos module

Module contents