nirspipe.pipeline.hyper.isc#

Inter-subject correlation: the same question as wavelet coherence, in the time domain.

compute_isc_pairs               One correlation per channel pair between two members,
                                optionally after a lag search, as the matrix a report
                                draws and as a long table, with the scrambled null
                                beside it.
roi_mean_of_isc                 Groups those correlations into ROIs.

Module Contents#

Functions#

compute_isc_pairs

The ISC matrix and the same numbers as one row per channel pair, ranked against a null.

roi_mean_of_isc

Average the channel-level ISC inside each ROI pair: the ROI number beside the ROI WTC.

roi_mean_of_homologous_isc

roi_mean_of_isc() over the same-channel pairs only, the ISC beside the homologous ROI coherence.

Data#

API#

nirspipe.pipeline.hyper.isc.logger = 'get_logger(...)'#
nirspipe.pipeline.hyper.isc.compute_isc_pairs(aligned_raws: dict[str, mne.io.Raw], subject_ids: list[str], ch_type: str = 'hbo', sep_bands=None, window: tuple[float, float] | None = None, max_lag_s: float = 0.0, n_null: int = 0, seed: int | list[int] | None = None, band: tuple[float | None, float | None] | None = None, cross: bool = True, on_draws: Callable[[pd.DataFrame], None] | None = None, skip_s: float = 0.0, excluded: numpy.ndarray | None = None, shared_phases: bool = False, draw_order: list[str] | None = None) → tuple[np.ndarray, list[str], pd.DataFrame, np.ndarray | None] | tuple[None, None, None, None]#

The ISC matrix and the same numbers as one row per channel pair, ranked against a null.

a 3 x 3 matrix  ->  9 rows of sub1, sub2, label, label2, r, r_z

The matrix is what the report draws; the frame is what a group analysis reads, and it is the form the extra columns fit in. r_z is the Fisher r-to-z of r. Each surrogate is correlated over the same kept samples as the real pair.

matrix[i, j] = Pearson r between sub1_ch_i and sub2_ch_j. Diagonal = same-channel ISC.

window restricts it to (tstart, tstop) on the aligned clock, which is how a condition gets a correlation of its own. Unlike the wavelet coherence this really is a cut and not a slice of a whole-record computation: a correlation has no frequency axis and band is applied over the whole record before the cut (see _band_limit()), so a window carries no edge that the whole record would not have had. Both sides are z-scored inside the window, because the correlation over a stretch is against that stretch’s mean, not the recording’s. The wavelet coherence is windowed instead; see window_result().

Both axes are the montage’s long channels, rejected ones included, so every dyad’s matrix has one shape and a group analysis can stack them however their rejections differ. That is the convention nirspipe.pipeline.restingstate.compute_fc() follows for the same reason. A rejected channel of sub1 leaves a blank row and one of sub2 a blank column -- never both, since sub1’s copy of a channel is not needed to correlate sub2’s against everything else.

Position is not a safe key: a participant with one more rejected channel than the other shifts every channel after it, so column j would hold a different pair than its label claims. Everything here is looked up by S-D label.

Rejections arrive on raw.info["bads"], which is where nirspipe.pipeline.hyper.group_io.load_group_haemo() puts them and the only place the WTC path reads them from.

Raises ValueError if the members were recorded at different sampling rates, which is the refusal WTC makes: alignment equalises duration, not rate.

Whitening is the caller’s: hand in the copies whiten_raws() made of the whole record, with skip_s past their zeroed transient, so every channel of both members carries one order. Whitening shrinks r, so a whitened matrix and an unwhitened one are not comparable and the sidecar records which was written.

Samples under either member’s BAD_ spans are left out of every r; excluded, the excluded samples on the pair’s clock, stands in for those spans when given.

n_null phase-scrambles the second member and recomputes; scrambling preserves each signal’s own power spectrum and so its autocorrelation. It adds null_mean, null_sd, null_p95 and percentile, the share of a cell’s surrogate draws its real value beat. All four are on |r|, since a surrogate is as likely to land either side of zero. shared_phases rotates all of the second member’s channels by one set of random phases per draw (phase_rotate()) instead of scrambling each channel on its own.

draw_order is the order, by label, in which the surrogates draw the second member’s channels from the stream seed starts; ch_names when left at None. A group passes its own axis, so one member on one stream is one surrogate in every pairing it closes, whatever the montage of the member it is paired with.

On whitened recordings the null scrambles the residuals, as the coherence’s does.

on_draws receives every surrogate as rows, one per pair and draw, signed r and r_z, so a test that averages before ranking can be rebuilt from them.

cross False keeps each channel against the other member’s copy of it only, as an uncrossed coherence does: the matrix keeps its shape with every off-diagonal cell NaN, and the frame holds the diagonal’s rows.

nirspipe.pipeline.hyper.isc.roi_mean_of_isc(isc_mat, ch_names: list[str], roi_map: dict[str, list[str]], min_channels: int = ROI_MIN_CHANNELS) → tuple[np.ndarray, list[str]] | tuple[None, None]#

Average the channel-level ISC inside each ROI pair: the ROI number beside the ROI WTC.

4x4 r matrix over S1_D1..S4_D4 + {"L": ["S1_D1", "S2_D2"], "R": [...]}
  -> 2x2 r matrix over ["L", "R"]

Built from the channel values rather than from an ROI-averaged signal, which is how roi_mean_of_channels() builds the ROI coherence, so the two ROI numbers a page prints rest on the same channels.

Cell (i, j) is the first member’s ROI i against the other’s ROI j, so the matrix is crossed and asymmetric exactly as the channel one is.

Averaged in Fisher z and returned as r, where the coherence path averages its values directly: a correlation is signed and bounded and these are, unlike coherences, spread across zero.

Parameters:
  • isc_mat (array, shape (n_ch, n_ch)) -- The matrix compute_isc_pairs() returned, NaN where a member lost a channel.

  • ch_names (list of str) -- The axis of isc_mat, both sides.

  • roi_map (dict) -- {region: [channel label, ...]}. Its key order is the returned axis.

  • min_channels (int) -- Least channels with a value each member must contribute to a cell; thinner cells come back NaN. Each side is counted on its own, as the ROI coherence counts them.

Returns:

(None, None) when there is nothing to average.

Return type:

(matrix, labels) or (None, None)

nirspipe.pipeline.hyper.isc.roi_mean_of_homologous_isc(isc_mat, ch_names: list[str], roi_map: dict[str, list[str]], min_channels: int = ROI_MIN_CHANNELS) → tuple[np.ndarray, list[str]] | tuple[None, None]#

roi_mean_of_isc() over the same-channel pairs only, the ISC beside the homologous ROI coherence.

4x4 r matrix + {"L": ["S1_D1", "S2_D2"], ...}
  -> L x L is the Fisher z mean of (S1_D1, S1_D1) and (S2_D2, S2_D2); off the
     diagonal NaN

The counterpart of roi_mean_of_homologous(): the crossed ROI diagonal averages every pairing inside a region, this averages only a channel against the other member’s copy of it.