Guide for AI coding assistants#
This page is for AI coding assistants (and the people prompting them) writing
code that uses spectral_connectivity. It covers the one workflow to use, the
conventions that are easy to get wrong, and where to look things up. Every code
block runs as a doctest in the test suite (tests/test_cookbook.py).
The workflow#
Use multitaper_connectivity. It takes the time series and returns a labeled
xarray object; you do not need Multitaper or Connectivity unless you need
something the wrapper does not expose.
Input shape is
(n_time_samples, n_trials, n_signals), or(n_time_samples, n_signals)for a single trial. Anxarray.DataArraywith named dimensions also works.sampling_frequencyis in Hz. All durations are in seconds.One
methodname returns aDataArray; a list of names, or nomethod, returns aDatasetwith one variable per measure.
>>> import numpy as np
>>> from spectral_connectivity import list_measures, multitaper_connectivity
>>> rng = np.random.default_rng(0)
>>> leader = rng.standard_normal((1003, 20))
>>> # Signal "b" is signal "a" delayed by 3 samples (6 ms at 500 Hz), plus noise.
>>> time_series = np.stack([leader[3:], leader[:-3]], axis=-1)
>>> time_series += 0.5 * rng.standard_normal(time_series.shape)
>>> time_series.shape # (n_time_samples, n_trials, n_signals)
(1000, 20, 2)
>>> coherence = multitaper_connectivity(
... time_series,
... sampling_frequency=500,
... method="magnitude_squared_coherence",
... signal_names=["a", "b"],
... )
>>> coherence.dims
('time', 'frequency', 'source', 'target')
>>> bool(coherence.sel(source="a", target="b").mean() > 0.5)
True
Find measures with list_measures()#
Do not guess method names: list_measures() returns one MeasureInfo per
valid method, with what its values mean.
>>> granger = next(
... m for m in list_measures() if m.name == "pairwise_spectral_granger_prediction"
... )
>>> granger.value_range, granger.units, granger.dims
((0.0, inf), '1', ('time', 'frequency', 'source', 'target'))
>>> granger.is_directed
True
>>> [m.name for m in list_measures(directed=True)][:3]
['pairwise_spectral_granger_prediction', 'directed_phase_lag_index', 'subset_pairwise_spectral_granger_prediction']
interpretation says how to read the values, and
Connectivity Metric Ranges tabulates all of
it. An unknown name raises an error listing the closest valid names.
Direction#
In the wrapper’s results, result.sel(source="a", target="b") is always the
influence of a on b, and for phase measures, positive means a leads b.
>>> granger = multitaper_connectivity(
... time_series,
... sampling_frequency=500,
... method="pairwise_spectral_granger_prediction",
... signal_names=["a", "b"],
... ).mean("frequency")
>>> bool(granger.sel(source="a", target="b") > granger.sel(source="b", target="a"))
True
The lower-level Connectivity methods return plain arrays in the same order:
result[..., i, j] is i -> j for every directed measure, and for
directed_phase_lag_index, phase_slope_index, delay, and group_delay a
positive result[..., i, j] (above 0.5 for the directed phase lag index)
means i leads j.
Prefer the wrapper’s labeled results so you never index these by hand.
Choosing parameters#
time_window_durationdefaults to the whole recording (one window). Set it, andtime_window_step, for a time-resolved result.The common settings
time_window_duration,time_window_step,time_halfbandwidth_product,n_tapers, anddetrend_typeare named arguments ofmultitaper_connectivity. Put any otherMultitapersetting intransform_kwargs={...}and measure settings inconnectivity_kwargs={...}; a misspelled key raises instead of being ignored. Passing those as plain keyword arguments is the deprecated 2.x form.Multitaper frequency resolution is the full DPSS concentration bandwidth,
2 * time_halfbandwidth_product / time_window_durationHz. It describes spectral smoothing; FFT bin spacing also depends on the FFT length and zero-padding. There are2 * time_halfbandwidth_product - 1requested tapers (rounded down). Low-bias filtering drops any requested taper whose spectral concentration is at or below 0.9, with a warning, andn_tapersreports the count used: a largern_tapersneeds a largertime_halfbandwidth_product.suggest_parameterspicks these from the sampling rate, recording length, and the resolution you need:
>>> from spectral_connectivity import suggest_parameters
>>> parameters = suggest_parameters(
... sampling_frequency=500, signal_duration=10.0, frequency_resolution=2.0
... )
>>> parameters["time_window_duration"], parameters["time_halfbandwidth_product"]
(3.0, 3.0)
>>> parameters["n_tapers"]
5
Pitfalls#
Read the scientific pitfalls first. Choosing a measure says which measure answers which question, and its Pitfalls cover the mistakes common to every measure.
Use the shared result schema. Common attrs include
transform,sampling_frequencywhen known,n_observations, independence flags, andoutput_schema_version=1. Remaining settings are intransform_parameters_json, withestimator/executionrecords. The 15 releasedmt_*attributes are compatibility copies scheduled for removal in 4.0. Multitaper’sfrequency_resolutionproperty and helper names remain supported. See the schema reference.Rate-adjusting spike-field coherence. Call
statistics.coherence_rate_adjustmentwithConnectivity.power()of the spike train andis_one_sided=True(that spectrum is one-sided), the spikes given to the transform as counts per sample, anddt = 1 / sampling_frequency. Leave out 0 Hz (and, with the default mean removal, the bins within a taper bandwidth of it); the Nyquist bin is valid only with a two-sided spectrum. For a two-sided spectrum (Connectivity.power(one_sided=False); the wrappers do not return one) passis_one_sided=False. Omitting the keyword warns and assumes one-sided.Group measures need labels. Pass
group_labels=...(one label per signal, e.g.["CA1", "CA1", "PFC"]) directly tomultitaper_connectivityorfourier_connectivityforcanonical_coherence,blockwise_spectral_granger_prediction, and the othergroup_pairwiseandmultivariate_componentsmeasures. The labels reach only the measures that take them, so one call may mix group and pairwise measures.Band summaries. Use
frequency_bands={"theta": (4, 8)}rather than averaging frequency bins by hand: phase measures get a circular mean and complex measures a complex mean.Frequency ranges include both endpoints.
frequency_range=(8, 12)keeps the bins at 8 and 12 Hz. Pass it to the wrapper itself, also forphase_slope_index,group_delay, anddelay, which then consume only those bins. Their oldfrequencies_of_interestexcluded the edges and is deprecated; do not write new code with it.Avoid the deprecated 2.x spellings in new code: use
magnitude_squared_coherence(notcoherence_magnitude, and index default results by the new name),Connectivity.from_transform(notfrom_multitaper),power_confidence_intervals(n_observations=..., confidence_level=...)(notn_tapers=orci=),suggest_parameters(frequency_resolution=...)(notdesired_freq_resolution=), and thetransform_kwargs/connectivity_kwargsmappings for advanced settings. Each old form still works through 3.x with aDeprecationWarning.GPU. Set
SPECTRAL_CONNECTIVITY_ENABLE_GPU=truebefore importing the package; setting it later has no effect.
>>> bands = multitaper_connectivity(
... time_series,
... sampling_frequency=500,
... method=["magnitude_squared_coherence", "coherence_phase"],
... signal_names=["a", "b"],
... frequency_bands={"theta": (4, 8), "gamma": (30, 50)},
... )
>>> bands.coherence_phase.dims
('time', 'band', 'source', 'target')
>>> bool(bands.coherence_phase.sel(band="gamma", source="a", target="b") > 0)
True
Uncertainty#
Connectivity.jackknife(method) returns a bias-corrected estimate, standard
error, and confidence interval by leaving out one trial or taper at a time:
>>> from spectral_connectivity import Connectivity, Multitaper
>>> connectivity = Connectivity.from_transform(
... Multitaper(time_series, sampling_frequency=500, time_halfbandwidth_product=2)
... )
>>> result = connectivity.jackknife("magnitude_squared_coherence")
>>> result.estimate.shape == result.standard_error.shape
True
Where to look next#
help(spectral_connectivity.multitaper_connectivity)for every wrapper option (frequency cropping, decimation,xarrayinput dimensions).Each
Connectivitymeasure’s docstring has a runnable example.help(spectral_connectivity.SpectralTransform)for the interface a custom transform needs to work withConnectivity.from_transform.The Cookbook has recipes for common tasks.