Utils

Module of utility functions used throughout the package, for things like common preprocessing steps many models are likely to use, or useful for multiple points of analysis.

CSV utilities


source

determine_header_rows_and_delimiter

 determine_header_rows_and_delimiter (filename:pathlib.Path|str)

Given a filename pointing at a CSV files, decides: how many header lines there are (based on first line starting with a digit) * the delimiter– right now tries whitespace and comma

Returns one of: - (number of header rows, column delimiter), - (number of header rows, None) if the delimiter could not be inferred, - (None, None) if CSV has no numerical rows,

:param filename: CSV Path or filepath literal :return: header and delimiter information, if possible.*

Activity counts

/home/runner/.local/lib/python3.10/site-packages/fastcore/docscrape.py:230: UserWarning: potentially wrong underline length... 
Parameters 
--- in 
ADS algorithm for activity counts, developed by Arcascope with support from the NHRC.
...
  else: warn(msg)

source

build_ADS

 build_ADS (time_xyz:numpy.ndarray, sampling_hz:float=50.0,
            bin_size_seconds:float=15, prefix:str='')

*ADS algorithm for activity counts, developed by Arcascope with support from the NHRC.

Parameters

  • time_xyz: numpy array with shape (N_samples, 4) where the 4 coordinates are: [time, x, y, z]
  • sampling_hz: float sampling frequency of thetime_xyz*

source

build_activity_counts_te_Lindert_et_al

 build_activity_counts_te_Lindert_et_al (time_xyz, axis:int=3,
                                         prefix:str='')

*Implementation of the reverse-engineered activity count algorithm from te Lindert BH, Van Someren EJ. Sleep. 2013 Sleep estimates using microelectromechanical systems (MEMS). doi: 10.5665/sleep.2648

:param time_xyz: np.ndarray loaded from timestamped triaxial accelerometer CSV. Shape (N, 4) :return: (time, activity counts with 15 second epoch)*


source

build_activity_counts

 build_activity_counts (data, axis:int=3, prefix:str='',
                        algorithm:__main__.ActivityCountAlgorithm=<Activit
                        yCountAlgorithm.ADS: 2>)

source

ActivityCountAlgorithm

 ActivityCountAlgorithm (value, names=None, module=None, qualname=None,
                         type=None, start=1)

An enumeration.

Plotting utilities


source

plot_scores_PDF

 plot_scores_PDF (scores:List[float], ax:matplotlib.axes._axes.Axes=None)

Plot the probability dist function (PDF) of the scores.


source

plot_scores_CDF

 plot_scores_CDF (scores:List[float], ax:matplotlib.axes._axes.Axes=None)

Plot the cumulative dist function (CDF) of the scores.

/home/runner/.local/lib/python3.10/site-packages/fastcore/docscrape.py:230: UserWarning: potentially wrong underline length... 
Returns 
--- in 
Computes average of step functions.
...
  else: warn(msg)

source

avg_steps

 avg_steps (xs:List[List[float]], ys:List[List[float]])

*Computes average of step functions.

Each ys[j] is thought of as a right-continuous step function given by

ys[j](x) = xs[j][i] for xs[j][i] <= x < xs[j][i+1]

This function returns two NumPy arrays, (inputs, outputs), giving the pointwise average (see below) of these functions, one for inputs and one for outputs. These output arrays can be considered to give another step function.

For a list of functions [f_1, f_2, ..., f_n], their pointwise average is the function f_bar defined by

f_bar(x) = (1/n)(f_1(x) + f_2(x) + ... + f_n(x))

Returns

inputs: np.ndaray The union of all elements of all vectors in xs; this is the mutual domain of the average function. outputs: np.ndarray The pointwise average of the ys[j]s, considered as step functions extended to the full real line by assuming constant values for x < min(xs[j]) or x > max(xs[j])*


source

constant_interp

 constant_interp (x:numpy.ndarray, xp:numpy.ndarray, yp:numpy.ndarray,
                  side:str='right')

source

add_rocs

 add_rocs (fprs:List[numpy.ndarray], tprs:List[numpy.ndarray],
           x_class:str='SLEEP', y_class:str='WAKE', min_auc:float=0.0,
           avg_curve_color:str='tab:blue',
           specific_curve_color:str='tab:orange', roc_group_name:str='',
           ax:matplotlib.axes._axes.Axes|None=None)

*Adds ROC curves to the given plot, or makes a new plot if ax is None.

if ax is None, we are making a new plot. We do additional formatting in this case, such as adding the legend and showing the plot.

When ax is provided, we expect the call site to do formatting.*

Data utilities


source

pad_to_hat

 pad_to_hat (y:numpy.ndarray, y_hat:numpy.ndarray)

*Adds zeros to the end of y to match the length of y_hat.

Useful when the inputs had to be padded with zeros to match shape requirements for dense layers.*

/home/runner/.local/lib/python3.10/site-packages/fastcore/docscrape.py:230: UserWarning: potentially wrong underline length... 
Parameters 
--- in 
Computes Mean Absolute Error (MAE) for the numerical function `func` on the given lists.
...
  else: warn(msg)
/home/runner/.local/lib/python3.10/site-packages/fastcore/docscrape.py:230: UserWarning: potentially wrong underline length... 
Returns 
--- in 
Computes Mean Absolute Error (MAE) for the numerical function `func` on the given lists.
...
  else: warn(msg)

source

mae_func

 mae_func (func:Callable[[numpy.ndarray],float],
           trues:List[numpy.ndarray], preds:List[numpy.ndarray])

*Computes Mean Absolute Error (MAE) for the numerical function func on the given lists.

This function is useful for computing MAE of statistical functions giving a single float for every NumPy array.

Parameters

func: callable (np.ndarray) -> float The statistic we are computing for truth/prediction arrays. It is called on each element of the lists of NumPy arrays, then MAE of the resulting statistic lists is computed. trues: list of np.ndarray The “True” labels, eg. This function is symmetric in trues and preds, and isn’t specific to classifiers, so the argument names are just mnemonics. preds: list of np.ndarray The “Predicted” labels, eg.

Returns

MAE of func applied to elements of trues and preds.*
## Sleep metrics

source

SleepMetricsCalculator

 SleepMetricsCalculator ()

Initialize self. See help(type(self)) for accurate signature.


source

Constants

 Constants ()

Initialize self. See help(type(self)) for accurate signature.

Split analysis tools


source

split_analysis

 split_analysis (y, y_hat_sleep_proba, sleep_accuracy:float=0.93,
                 balancing:bool=True)