osl_dynamics.analysis.spectral.RegressionSpectra#

class osl_dynamics.analysis.spectral.RegressionSpectra(frequencies, arrays, n_channels, session_weights, sampling_frequency, method, settings, has_cross_spectra)#

Bases: StateSpectra

Spectra for each state/mode from regressing a spectrogram on alpha.

Returned by regression_spectra(). See Spectra and StateSpectra for the attributes shared by all spectra and the User Guide for the definitions.

Attributes:
sigmanp.ndarray

Group-level standard deviation of the windowed alpha, \(\sigma_j\). Shape is (n_modes,).

property n_states: int#

Number of states/modes.

property sigma: numpy.ndarray#
property psd: numpy.ndarray#

Power spectral density of each state/mode, \(P_j = S + \beta_j / \sigma_j\).

Shape is (n_sessions, n_modes, n_channels, n_freq).

property psd_coefs: numpy.ndarray#

Regression coefficients of the PSD, \(\beta_j = \sigma_j (P_j - S)\): the change in the PSD for a one standard deviation increase in a mode’s alpha.

Shape is (n_sessions, n_modes, n_channels, n_freq).

property cross_spectra_coefs: numpy.ndarray#

Regression coefficients of the cross spectra.

Shape is (n_sessions, n_modes, n_channels, n_channels, n_freq).

property coherence_coefs: numpy.ndarray#

Change in coherence for a one standard deviation increase in a mode’s alpha.

The change in the cross spectrum in the direction (phase) of the static cross spectrum, normalized by the static power. The diagonal is the relative change in power.

Shape is (n_sessions, n_modes, n_channels, n_channels, n_freq).

property fractional_occupancies: numpy.ndarray#
property cross_spectra: numpy.ndarray#

Cross spectral density of each state.

Shape is (n_sessions, n_states, n_channels, n_channels, n_freq).

property coherence: numpy.ndarray#

Coherence of each state.

Shape is (n_sessions, n_states, n_channels, n_channels, n_freq).

property static_psd: numpy.ndarray#

Static power spectral density, \(S\).

Shape is (n_sessions, n_channels, n_freq).

property static_cross_spectra: numpy.ndarray#

Static cross spectral density.

Shape is (n_sessions, n_channels, n_channels, n_freq).

property static_coherence: numpy.ndarray#

Static coherence.

Shape is (n_sessions, n_channels, n_channels, n_freq).

property psd_change: numpy.ndarray#

Change in PSD from the static PSD, \(P_j - S\).

Shape is (n_sessions, n_states, n_channels, n_freq).

property coherence_change: numpy.ndarray#

Change in coherence from the static coherence.

Shape is (n_sessions, n_states, n_channels, n_channels, n_freq).

frequencies#
n_channels#
session_weights = None#
sampling_frequency#
method#
settings#
has_cross_spectra#
property is_group: bool#

Is this a group average (no session axis)?

property n_sessions: int | None#

Number of sessions. None for a group average.

property n_freq: int#

Number of frequency bins.

group_average()#

Group average of the spectra.

The cross spectra (or PSDs) are averaged over sessions with session_weights, ignoring NaN values. Coherences are calculated from the group-averaged cross spectra.

Returns:
spectraSpectra

Group-averaged spectra, an object of the same class without a session axis.

power_maps(values=None, bands=None, frequency_range=None, method='mean')#

Power maps: PSDs averaged over frequency bands.

Parameters:
valuesstr, optional

Which PSDs to use. Defaults to the first of: 'psd_coefs' (regression spectra), 'psd_change' (state spectra, the change from the static PSD), 'psd' (static spectra). State and regression spectra also accept 'psd' and 'static_psd'.

bandslist or np.ndarray, optional

Frequency ranges, e.g. [[1, 4], [4, 8]], or spectral components from decompose() with shape (n_components, n_freq).

frequency_rangelist, optional

A single frequency range. Defaults to all frequencies.

methodstr, optional

For frequency ranges: take the 'mean', 'sum' or 'integral' of the PSD. Use 'integral' for the variance.

Returns:
power_mapsnp.ndarray

Shape is (n_sessions, n_bands, n_states, n_channels). There is no session axis for a group average, no band axis unless bands is passed and no state axis for static spectra.

coherence_networks(values=None, bands=None, frequency_range=None, percentile=None, vectorize=False)#

Coherence networks: coherences averaged over frequency bands.

Parameters:
valuesstr, optional

Which coherences to use. Defaults to the first of: 'coherence_coefs' (regression spectra), 'coherence_change' (state spectra, the change from the static coherence), 'coherence' (static spectra). State and regression spectra also accept 'coherence' and 'static_coherence'.

bandslist or np.ndarray, optional

Frequency ranges, e.g. [[1, 4], [4, 8]], or spectral components from decompose() with shape (n_components, n_freq).

frequency_rangelist, optional

A single frequency range. Defaults to all frequencies.

percentilefloat, optional

Only keep the edges with an absolute value above this percentile of each network, e.g. 97 keeps the 3% of edges with the largest (absolute) values. Other edges and the diagonal are set to zero. Defaults to keeping all edges.

vectorizebool, optional

Return the upper triangle (excluding the diagonal) of each network, e.g. for statistical tests.

Returns:
networksnp.ndarray

Shape is (n_sessions, n_bands, n_states, n_channels, n_channels), or (…, n_channels * (n_channels - 1) / 2) if vectorize=True. There is no session axis for a group average, no band axis unless bands is passed and no state axis for static spectra.

decompose(n_components, values=None, **kwargs)#

Spectral components from non-negative matrix factorization.

See decompose_spectra().

Parameters:
n_componentsint

Number of spectral components.

valuesstr, optional

Which (non-negative) coherences to decompose. Defaults to 'coherence', or 'static_coherence' for regression spectra.

**kwargs

Keyword arguments for decompose_spectra().

Returns:
componentsnp.ndarray

Spectral components, which can be passed as bands to power_maps() and coherence_networks(). Shape is (n_components, n_freq).

plot_psd(values=None, states=None, channels=None, static=False, labels=None, frequency_range=None, filename=None, ax=None)#

Plot the group-average PSDs, averaged over channels.

The shaded area is the standard error over channels.

Parameters:
valuesstr, optional

Which PSDs to plot. See power_maps().

stateslist of int, optional

States to plot. Defaults to all states.

channelslist of int, optional

Channels to average over. Defaults to all channels.

staticbool, optional

Also plot the static PSD (dashed black line).

labelslist of str, optional

Legend label for each state.

frequency_rangelist, optional

Shade this frequency range.

filenamestr, optional

Output filename. If None, the figure is returned.

axmatplotlib.axes.Axes, optional

Axis to plot on.

Returns:
figmatplotlib.figure.Figure

Figure. Only returned if filename and ax are None.

axmatplotlib.axes.Axes

Axis. Only returned if filename and ax are None.

save(filename)#

Save the spectra to a .npz file.

Parameters:
filenamestr

Output filename. .npz is appended if it is missing.

classmethod load(filename)#

Load spectra saved with save().

Parameters:
filenamestr

Path to the .npz file.

Returns:
spectraSpectra

The spectra, with the class they were saved with.