spectral_connectivity.wrapper.connectivity_to_xarray#

connectivity_to_xarray(m: Multitaper, method: str = 'coherence_magnitude', signal_names: Sequence[str] | None = None, squeeze: bool = False, **kwargs: Any) DataArray[source]#

Calculate connectivity measures and return as labeled xarray.

Computes the specified connectivity measure from multitaper spectral analysis and returns results in an xarray.DataArray with properly labeled dimensions.

Parameters:
  • m (Multitaper) – Multitaper object containing spectral transform results.

  • method (str, default="coherence_magnitude") – Name of connectivity method to compute (e.g., “coherence_magnitude”, “imaginary_coherence”, “phase_locking_value”).

  • signal_names (sequence of str, optional) – Names for signal channels used to label ‘source’ and ‘target’ dimensions. If None, uses integer indices.

  • squeeze (bool, default=False) – If True and only 2 signals, return connectivity between first and last signal only. Only meaningful for symmetric measures.

  • **kwargs (dict) – Additional keyword arguments passed to connectivity method.

Returns:

connectivity – Connectivity results with dimensions: - [‘time’, ‘frequency’, ‘source’, ‘target’] for pairwise measures - [‘time’, ‘frequency’, ‘source’] for power spectral density - [‘time’, ‘frequency’] if squeeze=True and n_signals=2

Return type:

xarray.DataArray

Raises:

NotImplementedError – If the requested method is not supported by xarray interface.

Examples

>>> import numpy as np
>>> from spectral_connectivity.transforms import Multitaper
>>> # Simulate data: (100 time points, 5 trials, 3 channels)
>>> data = np.random.randn(100, 5, 3)
>>> mt = Multitaper(data, sampling_frequency=1000)
>>> coherence = connectivity_to_xarray(mt, method="coherence_magnitude")
>>> coherence.dims
('time', 'frequency', 'source', 'target')