.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_tutorials/plot_06_time_varying.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_tutorials_plot_06_time_varying.py: =================================== Connectivity that changes over time =================================== Brain networks do not stay the same: an influence can switch on when a task starts and off when it ends. A single estimate over a whole recording averages over such changes. This tutorial uses a simulation in which one influence switches on half-way through, and shows three ways to follow it: a time-varying model (ADTF and TPDC), one model per window or segment (WDTF), and a reduction of a time-resolved result over segments. .. GENERATED FROM PYTHON SOURCE LINES 14-18 Setup and data -------------- The scenario ``switch_on`` ships with dirconn. It lasts 60 s: region A drives region B from 30 s on (and not before), and region B drives region C all the time, as a control that does not change. .. GENERATED FROM PYTHON SOURCE LINES 18-49 .. code-block:: Python import os import warnings import matplotlib.pyplot as plt import numpy as np import pandas as pd import dirconn from dirconn.simulation.scenario import load_scenario QUICK = os.environ.get("DIRCONN_QUICK") == "1" # the tests set this; readers get the full version SEED = 0 print(f"dirconn {dirconn.__version__}, numpy {np.__version__}") spec = load_scenario("switch_on") A, B, C = spec.network.names SWITCH_S = 30.0 sim = dirconn.simulate( spec.network, sfreq=spec.sfreq, n_times=int(spec.duration_s * spec.sfreq), seed=SEED, dynamics=spec.dynamics, snr_db=spec.snr_db, n_background=0, node_scale="unit", ) data = sim.roi_observations(snr_db=spec.snr_db, seed=SEED + 1) print(spec.description) print(data) .. rst-class:: sphx-glr-script-out .. code-block:: none dirconn 0.2.0, numpy 2.5.5 A -> B switches on in the middle of the recording (30 s of 60 s); B -> C is a constant control edge. A, B, C are the first three regions of the pool oscillating at 10, 20 and 15 Hz, coupling 0.4 with a delay of one sample (10 ms at 100 Hz). Tests whether a time-resolved method sees an edge appear, and how long it takes. TimeSeries(n_trials=1, n_channels=3, n_times=6000, sfreq=100, segmentation='continuous') .. GENERATED FROM PYTHON SOURCE LINES 50-55 What one estimate over everything gives --------------------------------------- DTF over the whole recording (tutorial 3) gives one value per pair and frequency. Averaged over frequency, the influence of A on B comes out between "absent" and "present", because it is present for half of the time. The estimate cannot say that it was off and then on. .. GENERATED FROM PYTHON SOURCE LINES 55-60 .. code-block:: Python static = dirconn.dtf(data, order=3, n_freqs=41, fmax=40.0) print("whole recording, mean over frequency") print(" A -> B:", np.nanmean(static.values[0, 1]).round(3)) print(" B -> C:", np.nanmean(static.values[1, 2]).round(3)) .. rst-class:: sphx-glr-script-out .. code-block:: none whole recording, mean over frequency A -> B: 0.144 B -> C: 0.147 .. GENERATED FROM PYTHON SOURCE LINES 61-68 A model whose coefficients follow the data ------------------------------------------ ``adtf`` fits a vector autoregressive model whose coefficients are allowed to change from one time point to the next (a Kalman filter) and computes the DTF at every time point; ``tpdc`` does the same for partial directed coherence. ``time_step`` is the spacing of the output in seconds. The result has a time axis in addition to the frequency axis, and ``kind="time_frequency"`` draws a pair as a panel. .. GENERATED FROM PYTHON SOURCE LINES 68-85 .. code-block:: Python TIME_STEP = 2.0 if QUICK else 1.0 def fit_adtf(label, **options): """Run ``adtf`` and print the warnings the library gives instead of letting them scroll by.""" with warnings.catch_warnings(record=True) as caught: warnings.simplefilter("always") result = dirconn.adtf(data, order=3, fmax=40.0, n_freqs=41, time_step=TIME_STEP, **options) print(f"{label}: {len(caught)} warning(s) from the library") for item in caught: print(" -", str(item.message)) return result adtf = fit_adtf("default settings") print(adtf) .. rst-class:: sphx-glr-script-out .. code-block:: none default settings: 1 warning(s) from the library - 9 coefficients per equation are estimated from 23 observations (ratio 0.391; the rule of thumb is at most 0.1). Estimates will be unreliable; use more data, a lower order, fewer channels or a ridge penalty. Connectivity(method='adtf', shape=3x3x41x59, complex=False) .. GENERATED FROM PYTHON SOURCE LINES 86-93 The library warns, and the warning matters. The filter forgets old data, so the coefficients at any time point are effectively estimated from a short stretch of the recording (the measured length is in ``adtf.provenance.details["memory_samples"]``); the model has several coefficients per equation to estimate from that stretch, and the library says that this is thin. Read the curves below as a description of how the estimate behaves in this simulation. A longer memory or a lower order reduces the problem and makes the estimate follow changes more slowly; the trade is explained in ``docs/timevarying.md``. .. GENERATED FROM PYTHON SOURCE LINES 95-104 .. code-block:: Python fig, axes = plt.subplots(1, 2, sharey=True, figsize=(11.0, 3.8), layout="constrained") top = max(np.nanmax(adtf.values[0, 1]), np.nanmax(adtf.values[1, 2])) adtf.plot(kind="time_frequency", source=A, target=B, ax=axes[0], vmin=0.0, vmax=top, colorbar=False) adtf.plot(kind="time_frequency", source=B, target=C, ax=axes[1], vmin=0.0, vmax=top) for ax in axes: ax.axvline(SWITCH_S, color="k", linestyle=":") axes[0].set_title("ADTF, A → B (switches on at the dotted line)") axes[1].set_title("ADTF, B → C (constant)") .. image-sg:: /auto_tutorials/images/sphx_glr_plot_06_time_varying_001.png :alt: ADTF, A → B (switches on at the dotted line), ADTF, B → C (constant) :srcset: /auto_tutorials/images/sphx_glr_plot_06_time_varying_001.png, /auto_tutorials/images/sphx_glr_plot_06_time_varying_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 105-110 The influence of A on B appears after the dotted line and the constant control stays where it was. Averaged over frequency, the same picture as curves, together with the single value over the whole recording from above (dashed). The estimate does not jump at the switch: it needs some time to reach the new level, which the printout measures as the first output time after the switch at which the influence has covered half of the way between its two levels. .. GENERATED FROM PYTHON SOURCE LINES 110-127 .. code-block:: Python mean_ab = np.nanmean(adtf.values[0, 1], axis=0) mean_bc = np.nanmean(adtf.values[1, 2], axis=0) before = mean_ab[adtf.times < SWITCH_S - 2.0].mean() after = mean_ab[adtf.times > SWITCH_S + 10.0].mean() half = adtf.times[(adtf.times > SWITCH_S) & (mean_ab >= (before + after) / 2)][0] print(f"A -> B: {before:.3f} before, {after:.3f} well after; half-way at {half:g} s") fig, ax = plt.subplots(layout="constrained") ax.plot(adtf.times, mean_ab, label="A → B") ax.plot(adtf.times, mean_bc, linestyle="--", label="B → C") ax.axhline( np.nanmean(static.values[0, 1]), color="0.5", linestyle=":", label="A → B, whole recording" ) ax.axvline(SWITCH_S, color="k", linestyle=":") ax.set_xlabel("time (s)") ax.set_ylabel("DTF, mean over 0-40 Hz") ax.legend() .. image-sg:: /auto_tutorials/images/sphx_glr_plot_06_time_varying_002.png :alt: plot 06 time varying :srcset: /auto_tutorials/images/sphx_glr_plot_06_time_varying_002.png, /auto_tutorials/images/sphx_glr_plot_06_time_varying_002_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none A -> B: 0.014 before, 0.339 well after; half-way at 31.8 s .. GENERATED FROM PYTHON SOURCE LINES 128-133 How fast the model may change ----------------------------- The ``forgetting`` argument sets how fast the coefficients are allowed to move. The default follows slowly; a small value follows quickly. Following quickly is not free: the estimate then moves with the noise too. Below, the same data with ``forgetting=0.99``: .. GENERATED FROM PYTHON SOURCE LINES 133-149 .. code-block:: Python fast = fit_adtf("forgetting=0.99", forgetting=0.99) mean_fast = np.nanmean(fast.values[0, 1], axis=0) mask = fast.times < SWITCH_S - 2.0 print("A -> B before the switch, where the truth is 0:") print(f" default forgetting: mean {mean_ab[mask].mean():.3f}, spread {mean_ab[mask].std():.3f}") print( f" forgetting=0.99: mean {mean_fast[mask].mean():.3f}, spread {mean_fast[mask].std():.3f}" ) fig, ax = plt.subplots(layout="constrained") ax.plot(adtf.times, mean_ab, label="default forgetting") ax.plot(fast.times, mean_fast, label="forgetting=0.99") ax.axvline(SWITCH_S, color="k", linestyle=":") ax.set_xlabel("time (s)") ax.set_ylabel("A → B, DTF mean over 0-40 Hz") ax.legend() .. image-sg:: /auto_tutorials/images/sphx_glr_plot_06_time_varying_003.png :alt: plot 06 time varying :srcset: /auto_tutorials/images/sphx_glr_plot_06_time_varying_003.png, /auto_tutorials/images/sphx_glr_plot_06_time_varying_003_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none forgetting=0.99: 2 warning(s) from the library - 9 coefficients per equation are estimated from 2 observations (ratio 4.5; the rule of thumb is at most 0.1). Estimates will be unreliable; use more data, a lower order, fewer channels or a ridge penalty. - 10 of 59 time points (17%) have a companion spectral radius of 1 or more. The coefficient trajectory left the stationary region and the spectral transforms of those slices are unreliable; consider a larger forgetting factor, a longer window or a lower order. A -> B before the switch, where the truth is 0: default forgetting: mean 0.014, spread 0.011 forgetting=0.99: mean 0.133, spread 0.085 .. GENERATED FROM PYTHON SOURCE LINES 150-154 The fast filter shows an influence of A on B *before* the switch, where there is none, and its curve is much more irregular. What "forgetting" means for this recursion, and why its value is not a memory length in samples, is explained in ``docs/timevarying.md``; the library records the memory it measured in ``result.provenance.details["memory_samples"]``. .. GENERATED FROM PYTHON SOURCE LINES 154-162 .. code-block:: Python print( "measured memory (samples):", { "default": round(adtf.provenance.details["memory_samples"], 1), "forgetting=0.99": round(fast.provenance.details["memory_samples"], 1), }, ) .. rst-class:: sphx-glr-script-out .. code-block:: none measured memory (samples): {'default': 22.9, 'forgetting=0.99': 1.7} .. GENERATED FROM PYTHON SOURCE LINES 163-169 One model per window or per segment ----------------------------------- ``wdtf`` takes the opposite approach: it cuts the recording into windows (or into segments that you name) and fits an ordinary model to each. Nothing is assumed about how the coefficients move, but a window must be long enough to fit a model, and an influence that switches inside a window is averaged. With ``segments`` you decide where the cuts are: .. GENERATED FROM PYTHON SOURCE LINES 169-173 .. code-block:: Python segments = [(5.0, 25.0), (35.0, 55.0)] # before and after the switch, away from the transition per_segment = dirconn.wdtf(data, segments=segments, order=3, fmax=40.0, n_freqs=41) print(per_segment) .. rst-class:: sphx-glr-script-out .. code-block:: none Connectivity(method='wdtf', shape=3x3x41x2, complex=False) .. GENERATED FROM PYTHON SOURCE LINES 174-176 The time-resolved result can be reduced over the same segments, with ``reduce_time``. The two ways to the same question give similar numbers here: .. GENERATED FROM PYTHON SOURCE LINES 176-192 .. code-block:: Python averaged = adtf.reduce_time(segments) rows = [] for label, result in (("ADTF, then reduce_time", averaged), ("WDTF per segment", per_segment)): ab = np.nanmean(result.values[0, 1], axis=0) bc = np.nanmean(result.values[1, 2], axis=0) rows.append( { "method": label, "A -> B before": ab[0], "A -> B after": ab[1], "B -> C before": bc[0], "B -> C after": bc[1], } ) print(pd.DataFrame(rows).round(3).to_string(index=False)) .. rst-class:: sphx-glr-script-out .. code-block:: none method A -> B before A -> B after B -> C before B -> C after ADTF, then reduce_time 0.014 0.335 0.142 0.134 WDTF per segment 0.010 0.339 0.138 0.136 .. GENERATED FROM PYTHON SOURCE LINES 193-196 The two can differ in how they are made, and only one of them has a test: segment means of a continuous ADTF are descriptive, with no p-value; a comparison of two segments with a permutation test needs epoched data and is shown in ``docs/timevarying.md``. .. GENERATED FROM PYTHON SOURCE LINES 198-203 The network before and after ---------------------------- ``kind="network_graph"`` draws the nodes on a circle with an arrow for every edge you choose, here every pair with a DTF of at least 0.1, averaged over 1 to 40 Hz. The edges are always chosen by you, with ``threshold`` or ``mask``, because the picture changes with that choice. .. GENERATED FROM PYTHON SOURCE LINES 203-208 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(10.0, 4.8), layout="constrained") for ax, time_s, label in zip(axes, (15.0, 45.0), ("before", "after"), strict=True): averaged.plot(kind="network_graph", threshold=0.1, band=(1.0, 40.0), time=time_s, ax=ax) ax.set_title(f"{label} the switch (segment centred at {time_s:g} s)") .. image-sg:: /auto_tutorials/images/sphx_glr_plot_06_time_varying_004.png :alt: before the switch (segment centred at 15 s), after the switch (segment centred at 45 s) :srcset: /auto_tutorials/images/sphx_glr_plot_06_time_varying_004.png, /auto_tutorials/images/sphx_glr_plot_06_time_varying_004_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 209-213 Before the switch there is an arrow from B to C; afterwards there is one from A to B as well. A third arrow, from A to C, appears after the switch, although this scenario has no direct influence from A on C: tutorial 5 shows how a path through a third region can produce an influence between the other two. .. GENERATED FROM PYTHON SOURCE LINES 215-229 Limitations ----------- * The influence switched on at one moment, abruptly, in one scenario, with the other influence constant. How these methods behave with gradual changes, with several changes, or when the regions' own rhythms change is not shown here. * The library warned about the sampling adequacy of the time-varying fit, see above; the curves are descriptions of this simulation, not measurements with an error bar. * The curve of a time-varying estimate follows the truth with a delay and, with a fast filter, with noise. Where a value in time begins and ends is not read off a curve without looking at that delay. * No method is recommended here. ADTF and WDTF answered the same question with similar numbers in this scenario, which says nothing about other settings. * Every result lists the assumptions it rests on; ``not_checked`` means that the library did not test an assumption, not that it holds: .. GENERATED FROM PYTHON SOURCE LINES 229-232 .. code-block:: Python for check in adtf.provenance.assumptions.checks: print(f"{check.name:24s} {check.status:12s} {check.detail}") .. rst-class:: sphx-glr-script-out .. code-block:: none stationarity pass 0 of 59 time points (0%) have a companion spectral radius of 1 or more. linearity not_checked sampling_adequacy warn 9 coefficients per equation are estimated from 23 observations (ratio 0.391; the rule of thumb is at most 0.1). instantaneous_coupling not_checked causal_sufficiency not_checked filtering not_checked filter_health pass no update was skipped and no state was reset. order_source not_checked order 3 was fixed by the caller, not selected. .. GENERATED FROM PYTHON SOURCE LINES 233-235 * Not tested here: significance of a change over time, and anything about real recordings, whose changes are neither abrupt nor known beforehand. .. GENERATED FROM PYTHON SOURCE LINES 235-236 .. code-block:: Python plt.close("all") .. _sphx_glr_download_auto_tutorials_plot_06_time_varying.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_06_time_varying.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_06_time_varying.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_06_time_varying.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_