nirspipe.pipeline.hyper.wtc#
Wavelet transform coherence: the transform, and the numbers a finished map collapses to.
compute_wtc Coherence resolved in both time and frequency, per channel pair, so a
pair that only synchronises during part of the task still shows it.
Backed by pycwt.
window_result Cuts a finished map to a stretch of it, which is not the same number
as transforming that stretch on its own.
condition_cone Marks a condition's own edges as cone, where its cells carry the
neighbouring condition.
wtc_band_mean Collapses a map to one value per channel pair, the form a group
analysis wants.
wtc_phase_by_scale The same collapse kept per scale, for the phase.
The callers live in pipeline/hyper/__init__.py, which handles the dyad bookkeeping these functions assume has already happened: recordings loaded, aligned, trimmed to a common length and normalised. All of them read long channels only.
Module Contents#
Classes#
Pairwise wavelet transform coherence per HbO channel. |
Functions#
The wavelet grid the maps sit on, for a sidecar: fixed, but reportable. |
|
Compute pairwise WTC per long channel of one chromophore, using a Morlet wavelet. |
|
The same WTC restricted to a time window, so a band mean over it describes one condition. |
|
The same WTC with the cone a cut at |
|
Per map column, whether it lies outside the pairing’s excluded time. |
|
Collapse each WTC map to one number per pair and label: the band mean. |
|
The same circular statistic as |
Data#
API#
- nirspipe.pipeline.hyper.wtc.logger = 'get_logger(...)'#
- class nirspipe.pipeline.hyper.wtc.WTCResult#
Pairwise wavelet transform coherence per HbO channel.
pairs[(sub1, sub2)][ch_name] = {"wtc": ndarray(n_freqs, n_times), "coi": ndarray(n_times), "phase": ndarray(n_freqs, n_times)}
freqs: ascending Hz. times: decimated aligned time axis (seconds). excluded:
{(sub1, sub2): ndarray(n_times) of bool}, the columns under either member’sBAD_spans, for a pairing that has any; averages leave those columns out.- freqs: numpy.ndarray = None#
- times: numpy.ndarray = None#
- nirspipe.pipeline.hyper.wtc.WTC_W0 = 6.0#
- nirspipe.pipeline.hyper.wtc.WTC_DJ = None#
- nirspipe.pipeline.hyper.wtc.wtc_grid_params(raws: dict) dict#
The wavelet grid the maps sit on, for a sidecar: fixed, but reportable.
- wtc_grid_params(raws_at_10_Hz)
- -> {“wtc_w0”: 6.0, “wtc_dj”: 0.0833, “wtc_time_step_s”: 1.0,
“wtc_scale_smooth_dj0”: 0.6}
None is configurable, and all of them change what a band mean is an average over, so a reader comparing two studies’ coherences needs them on the file. Read off the recordings rather than passed in, so they cannot disagree with what ran.
- nirspipe.pipeline.hyper.wtc.compute_wtc(raws: dict[str, mne.io.Raw], fmin: float = 0.004, fmax: float = 0.2, significance: bool = False, seed: int | None = None, mc_count: int = 300, cross: bool = False, limit_scales: bool = True, ch_type: str = 'hbo', sep_bands=None, device=None) nirspipe.pipeline.hyper.wtc.WTCResult#
Compute pairwise WTC per long channel of one chromophore, using a Morlet wavelet.
Channels are matched by S-D label across subjects; time axis decimated to ~1 Hz. Short-distance channels are excluded (see long_channel_picks), as are bads. significance adds a Monte Carlo significance level per pair (slow; see _wtc_over_pairs), and seed makes it reproducible.
ch_typeis one chromophore, “hbo”, “hbr” or “hbt”. Running several is this function called once each, at the cost of one call each: nothing about the statistic changes, and they never mix, since a member’s HbO pairs only with the other member’s HbO.crosscrosses every channel with every other rather than pairing like with like, so n channels give n**2 results keyed by(label_sub1, label_sub2)instead of n keyed by the label. The cost is quadratic in the channel count, and it is also whatroi_mean_of_channels()needs to build the cross-ROI matrix.deviceis the card to run on (open_device()), None for the CPU.
- nirspipe.pipeline.hyper.wtc.window_result(result: nirspipe.pipeline.hyper.wtc.WTCResult, tstart: float, tstop: float) nirspipe.pipeline.hyper.wtc.WTCResult#
The same WTC restricted to a time window, so a band mean over it describes one condition.
a 1200 s result + (300, 600) -> the same maps holding only those 300 s
This is how a condition is read out of a whole-record transform, and it is not the same number as transforming that condition on its own: a cut window has two edges of its own, so more of its band cells fall outside the cone. Windowing carries the whole record’s cone instead, which only reaches into the ends of the recording.
condition_cone()adds the condition’s own.sigis carried through unchanged: a Monte Carlo level is per frequency and constant over time, so a window of it is itself.
- nirspipe.pipeline.hyper.wtc.condition_cone(result: nirspipe.pipeline.hyper.wtc.WTCResult, tstart: float, tstop: float) nirspipe.pipeline.hyper.wtc.WTCResult#
The same WTC with the cone a cut at
[tstart, tstop]would have laid over its own.a window of 300 s read out of a whole-run transform + (3580, 3880) -> at 0.02 Hz, the first and last 68 s of it marked as cone
A cell of a whole-run transform is built from the signal on either side of it, so near a condition’s start or end it carries the neighbouring condition. Those cells are marked as the cone of the condition itself, where \(d(t)\) is the distance to the nearer of
tstartandtstop:\[\mathrm{coi}(t) = \min\left(\mathrm{coi}_\mathrm{run}(t),\; \frac{\lambda}{\sqrt{2}}\, d(t)\right)\]with \(\lambda\) the Morlet’s Fourier factor, the reach pycwt gives the cone of a series of its own.
tstartandtstopare the condition’s, which for a window cut out of a condition are the condition’s edges rather than the window’s.
- nirspipe.pipeline.hyper.wtc.kept_columns(result: nirspipe.pipeline.hyper.wtc.WTCResult, pair_key, n_times: int) numpy.ndarray#
Per map column, whether it lies outside the pairing’s excluded time.
- nirspipe.pipeline.hyper.wtc.wtc_band_mean(result: nirspipe.pipeline.hyper.wtc.WTCResult, fmin: float, fmax: float, mask_coi: bool = True) pandas.DataFrame#
Collapse each WTC map to one number per pair and label: the band mean.
A time-frequency map is what you look at; a single number per channel is what enters a group analysis. This averages \(R^2(f, t)\) over the frequencies of
[fmin, fmax]and over time,\[\overline{R^2} = \frac{1}{|V|} \sum_{(f, t) \in V} R^2(f, t),\]where \(V\) is the set of cells inside the band and, with
mask_coi, outside the cone of influence. pycwt reports the COI as the longest period still free of edge effects at each time point, so a cell is kept when \(f \ge 1 / \mathrm{coi}(t)\). Cells inside the cone are wavelet coefficients padded against the edges of the record: near 1 whatever the data does, and enough of them at the low-frequency end to carry a whole row.mask_coiis on by default; cells inside the cone are padding. Masking discards more of a short segment than of a long one, son_valid_fracis reported either way.--no-wtc-mask-coiaverages the whole band.n_valid_fracis the share of band cells that lie outside the cone of influence and outside the pairing’s excluded time (result.excluded), and are defined (a member with no power leaves a cell NaN). It is reported whether or not the cone mask is applied, so the share is visible as a quality number even when every cell was averaged. Excluded columns leave the averages under either setting.coherence_zis the Fisher r-to-z ofcoherence(\(\operatorname{arctanh}\), clipped just below 1).phase_angleis the circular mean of the relative phase over the same band, in degrees, positive meaning the first member leads;phase_sdis its circular standard deviation, also in degrees and unbounded above;phase_nis how many cells they were taken over. Coherence says whether a pair is locked, the angle says which of them leads, and the two are independent: a pair can be fully coherent at any fixed lag. The cells are chosen by_phase_cells(), which is a stricter mask than the coherence uses, and the angle is only readable wherephase_sdis small. It is a band mean, so it cannot be divided by the frequency to get a lag in seconds;wtc_phase_by_scale()is that number.Returns one row per (pair, label) with columns sub1, sub2, label, coherence, coherence_z, n_valid_frac, phase_angle, phase_sd, phase_n. A crossed result is keyed by a label pair rather than one label, and gains a
label2column afterlabel:labelis what sub1 contributed,label2what sub2 did, and the homologous rows are the ones where they agree. A label with no map behind it keeps its row, with NaN in every measured column. That covers both a pairing one member had no usable channel at and one the transform failed on.n_valid_fracis NaN there rather than 0: the share of band cells outside the cone is undefined when there are no cells, and a 0 would be averaged as a real share byroi_mean_of_channels(), pulling an ROI’s reported share down by however many of its channels were rejected.
- nirspipe.pipeline.hyper.wtc.wtc_phase_by_scale(result: nirspipe.pipeline.hyper.wtc.WTCResult, fmin: float, fmax: float, mask_coi: bool = True) pandas.DataFrame#
The same circular statistic as
wtc_band_mean(), but one row per frequency.a pair coherent at 0.1 Hz with the first member 1.25 s ahead -> freq 0.1, phase_angle +45.0, lag_s +1.25
A band mean collapses scales whose phase differs, so its angle belongs to no particular frequency and cannot be read as a delay. Resolved per scale it can: a phase of \(\theta\) at frequency \(f\) is \(\theta / 2 \pi f\) seconds, which is what
lag_scarries, positive meaning the first member leads.lag_swraps. An angle is only known modulo a turn, so a delay longer than half the period at that frequency comes back as a short one of the other sign, and only alag_sthat agrees across neighbouring scales is a delay rather than an artefact of where it folded. That agreement is the check Grinsted et al. (2004) describe.Returns one row per (pair, label, freq), the label pair and a
label2column on a crossed result, as inwtc_band_mean(). A pairing with no map behind it contributes no rows rather than a NaN one: there is no frequency axis to hang them on.