osl_dynamics.analysis.static#

Functions to calculate static properties of time series data.

Functions#

functional_connectivity(data[, conn_type, demean, ...])

Calculate functional connectivity networks.

welch_spectra(data, sampling_frequency[, ...])

Calculate spectra using Welch's method.

multitaper_spectra(data, sampling_frequency[, ...])

Calculate multitaper spectra.

Module Contents#

osl_dynamics.analysis.static.functional_connectivity(data, conn_type='corr', demean=True, window_length=None, n_jobs=1)[source]#

Calculate functional connectivity networks.

This function uses the LedoitWolf estimator to calculate covariance. If another measure is requested it is calculated from this covariance matrix.

Parameters:
  • data (np.ndarray) – Time series data. Shape must be (n_sessions, n_samples, n_channels) or (n_samples, n_channels).

  • conn_type (str or func, optional) –

    What measure should we use? Must be one of:

    • "cov": covariance.

    • "pcov": partial covariance.

    • "corr": Pearson correlation.

    • "pcorr": partial correlation.

    • "prec": precision (inverse of the covariance matrix).

    Or can be a function that accepts a (n_samples, n_channels) array and returns a (n_channels, n_channels) array.

  • demean (bool, optional) – Should we demean the data before calculating the functional connectivity?

  • window_length (int, optional) – Window length (in samples) to split the data and average the functional connectivity network for. If None, no averaging is done. Windows are non-overlapping.

  • n_jobs (int, optional) – Number of jobs.

Returns:

fc – Functional connectivity network. Shape is (n_sessions, n_channels, n_channels) or (n_channels, n_channels).

Return type:

np.ndarray

osl_dynamics.analysis.static.welch_spectra(data, sampling_frequency, window_length=None, step_size=None, frequency_range=None, standardize=True, averaging='mean', calc_cpsd=False, calc_coh=False, return_weights=False, keepdims=False, n_jobs=1)[source]#

Calculate spectra using Welch’s method.

Wrapper for spectral.welch_spectra assuming only one state is active for all time points.

Parameters:
  • data (np.ndarray or list) – Time series data. Must have shape (n_sessions, n_samples, n_channels) or (n_samples, n_channels).

  • sampling_frequency (float) – Sampling frequency in Hz.

  • window_length (int, optional) – Length of the data segment to use to calculate spectra. If None, we use 2 * sampling_frequency.

  • step_size (int, optional) – Step size for shifting the window. If None, we use window_length // 2.

  • frequency_range (list, optional) – Minimum and maximum frequency to keep.

  • standardize (bool, optional) – Should we standardize the data before calculating the spectra?

  • averaging (str, optional) – Method used to average periodograms. Must be 'mean' or 'median'.

  • calc_cpsd (bool, optional) – Should we return the cross spectra for psd? If True, we force calc_coh to False.

  • calc_coh (bool, optional) – Should we also return the coherence spectra?

  • return_weights (bool, optional) – Should we return the weights for session-specific spectra? This is useful for calculating a group average.

  • keepdims (bool, optional) – Should we enforce a (n_sessions, …) array is returned for psd and coh? If False, we remove any dimension of length 1.

  • n_jobs (int, optional) – Number of parallel jobs.

Returns:

  • f (np.ndarray) – Frequencies of the power spectra and coherences. Shape is (n_freq,).

  • psd (np.ndarray) – Power spectra for each session. Shape is (n_sessions, n_channels, n_freq) if calc_cpsd=False, otherwise the shape is (n_sessions, n_channels, n_channels, n_freq). Any axis of length 1 is removed if keepdims=False.

  • coh (np.ndarray) – Coherences. Shape is (n_sessions, n_channels, n_channels, n_freq). Any axis of length 1 is removed if keepdims=False. Only returned is calc_coh=True.

  • w (np.ndarray) – Weighting for session-specific spectra. Only returned if return_weights=True. Shape is (n_sessions,).

osl_dynamics.analysis.static.multitaper_spectra(data, sampling_frequency, window_length=None, time_half_bandwidth=4, n_tapers=7, frequency_range=None, standardize=True, averaging='mean', calc_cpsd=False, calc_coh=False, return_weights=False, keepdims=False, n_jobs=1)[source]#

Calculate multitaper spectra.

Wrapper for spectral.multitaper_spectra assuming only one state is active for all time points.

Parameters:
  • data (np.ndarray or list) – Time series data. Must have shape (n_sessions, n_samples, n_channels) or (n_samples, n_channels).

  • sampling_frequency (float) – Sampling frequency in Hz.

  • window_length (int, optional) – Length of the data segment to use to calculate the multitaper.

  • time_half_bandwidth (float, optional) – Parameter to control the resolution of the spectra.

  • n_tapers (int, optional) – Number of tapers.

  • frequency_range (list, optional) – Minimum and maximum frequency to keep.

  • standardize (bool, optional) – Should we standardize the data before calculating the multitaper?

  • averaging (str, optional) – Method used to average periodograms. Must be 'mean' or 'median'.

  • calc_cpsd (bool, optional) – Should we return the cross spectra for psd? If True, we force calc_coh to False.

  • calc_coh (bool, optional) – Should we also return the coherence spectra?

  • return_weights (bool, optional) – Should we return the weights for session-specific PSDs? Useful for calculating the group average PSD.

  • keepdims (bool, optional) – Should we enforce a (n_sessions, …) array is returned for psd and coh? If False, we remove any dimension of length 1.

  • n_jobs (int, optional) – Number of parallel jobs.

Returns:

  • f (np.ndarray) – Frequencies of the power spectra and coherences. Shape is (n_freq,).

  • psd (np.ndarray) – Power spectra for each session. Shape is (n_sessions, n_channels, n_freq) if calc_cpsd=False, otherwise the shape is (n_sessions, n_channels, n_channels, n_freq). Any axis of length 1 is removed if keepdims=False.

  • coh (np.ndarray) – Coherences. Shape is (n_sessions, n_channels, n_channels, n_freq). Any axis of length 1 is removed if keepdims=False. Only returned is calc_coh=True.

  • w (np.ndarray) – Weighting for session-specific spectra. Only returned if return_weights=True. Shape is (n_sessions,).