osl_dynamics.analysis.spectral.Spectra#
- class osl_dynamics.analysis.spectral.Spectra(frequencies, arrays, n_channels, session_weights, sampling_frequency, method, settings, has_cross_spectra)#
Power spectra and cross spectra.
Base class of
StaticSpectra,StateSpectraandRegressionSpectra.load()loads any of them.The cross spectra are stored (as the upper triangle) and the PSDs and coherences are calculated from them. The first axis of each array is the session, except for a group average (see
group_average()). Cross spectra are \(\langle \bar{X}_i X_j \rangle\), where \(X_i\) is the Fourier transform of channel \(i\).- Attributes:
- frequenciesnp.ndarray
Frequency axis. Shape is (n_freq,).
- session_weightsnp.ndarray
Weight of each session in a group average (the number of samples in a session divided by the total). Shape is (n_sessions,).
Nonefor a group average.- n_channelsint
Number of channels.
- sampling_frequencyfloat
Sampling frequency in Hz.
- methodstr
Method used to calculate the spectra.
- settingsdict
Settings used to calculate the spectra.
- 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.
- property psd: numpy.ndarray#
Power spectral density.
Shape is (n_sessions, n_channels, n_freq).
- property cross_spectra: numpy.ndarray#
Cross spectral density.
Shape is (n_sessions, n_channels, n_channels, n_freq).
- property coherence: numpy.ndarray#
Coherence.
Shape is (n_sessions, n_channels, n_channels, n_freq).
- 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.