osl_dynamics.analysis.spectral.StateSpectra#
- class osl_dynamics.analysis.spectral.StateSpectra(frequencies, arrays, n_channels, session_weights, sampling_frequency, method, settings, has_cross_spectra)#
Bases:
SpectraSpectra for each state, from Welch’s method or a multitaper.
Returned by
welch_spectra()andmultitaper_spectra()withalpha. SeeSpectrafor the attributes shared by all spectra.The static spectrum is the average of the state spectra weighted by the fractional occupancies.
- Attributes:
- fractional_occupanciesnp.ndarray
Mean of
alphafor each state, normalized to sum to one. Shape is (n_sessions, n_states).
- property n_states: int#
Number of states.
- property fractional_occupancies: numpy.ndarray#
- property psd: numpy.ndarray#
Power spectral density of each state, \(P_j\).
Shape is (n_sessions, n_states, n_channels, n_freq).
- 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.
Nonefor 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 fromdecompose()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
bandsis 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 fromdecompose()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.
97keeps 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 unlessbandsis 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
bandstopower_maps()andcoherence_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
filenameandaxareNone.- axmatplotlib.axes.Axes
Axis. Only returned if
filenameandaxareNone.
- save(filename)#
Save the spectra to a
.npzfile.- Parameters:
- filenamestr
Output filename.
.npzis appended if it is missing.