==================================================================== KIT general pipeline: source analysis, statistics and multivariate ==================================================================== Author: Hadi Zaatiti Finding the sources of activity in the brain from measured MEG/EEG data, in the literature is implemented either as: - a localization problem: which infers the contribution, and location of current-like dipoles from measured data, point-wise - an imaging representation problem: which computes a distribution of the current sources on the cortical surface Each approach is useful depending on the research question under investigation. The search for the sources is an inverse problem: - MEG and EEG measure the output of the "brain" system and noise - the forward problem attempt to compute the measurable output (MEG/EEG measurement) knowing the exact source of activity in the brain - the inverse problem attempts to compute the distribution or current-like dipoles in the brain given the output measurements (MEG/EEG measurement) This page covers the stages after the epochs and evoked responses of stage 10: the automatic template coregistration, the forward and inverse models (stages 16, 17 and 18), the group averages (stage 21), the cluster permutation tests (stage 22), the representational similarity and decoding analyses (stage 23), the source estimates browsed over time with a slider (stage 24), the dipole fits (stage 20) and the spectra of the long recordings (stage 12). Like every other stage they read one YAML config (``-c``) and write under ``/derivatives//`` with a timestamped copy of the config in ``configs/``, one merged summary table, a JSON record per output, PNG figures, and a refresh of the Quarto report (individual and group sections). Everything is computed with MNE-Python; the statistics with ``mne.stats``; the RSA with ``mne-rsa``; the classifiers with scikit-learn inside MNE's decoding module. Nothing is hand-coded that a library provides. .. contents:: :local: :depth: 2 Overview -------- .. list-table:: :header-rows: 1 :widths: 4 30 33 33 * - # - Script - Reads - Writes (under ``/derivatives/``) * - 13 - ``16-kit_auto_coreg.py`` - stage-2 ``.fif`` (digitisation), fsaverage - ``16-kit_auto_coreg/sub-*/sub-*_trans.fif`` + ``.json``, scaled template ``subjects/sub-*``, ``figures/*_desc-autocoregqa.png``, ``auto_coreg_summary.csv`` * - 14 - ``17-kit_forward_model.py`` - stage-15 or 16 transform, the anatomy it refers to, stage-10 ``*_ave.fif`` (sensor info of every recording) - ``17-kit_forward_model/sub-*/sub-*__src.fif``, ``sub-*_bem-sol.fif``, ``*_desc-forward_fwd.fif`` + ``.json``, ``figures/*_desc-forwardqa.png``, ``forward_summary.csv`` * - 15 - ``18-kit_inverse_solution.py`` - stage-10 ``*_epo.fif`` (noise covariance) and ``*_ave.fif``, stage-17 forward - ``18-kit_inverse_solution/sub-*/*_desc-noise_cov.fif``, ``*_inv.fif``, ``*_cond-_desc-_space-fsaverage-{lh,rh}.stc``, ``*_labels.tsv``, ``*_inverse.json``, ``figures/*.png``, ``inverse_summary.csv`` * - 16 - ``21-kit_group_source.py`` - stage-18 estimates and ROI time courses - ``21-kit_group_source/subjects_manifest.json``, ``group/cond-*``, ``group/contrast-*`` (STCs, label TSVs), ``group/figures/*.png``, ``group_source_summary.csv`` * - 17 - ``22-kit_cluster_stats.py`` - stage-10 evoked (sensor level), stage-18/21 estimates (source level) - ``22-kit_cluster_stats/group//contrast-*/clusters.json``, ``tobs.npz``, ``clusters_desc-summary_*.stc``, ``figures/*.png``, ``cluster_stats_summary.csv`` * - 18 - ``23-kit_rsa_decoding.py`` - stage-18/21 estimates or stage-10 evoked (RSA), stage-10 epochs (decoding) - ``23-kit_rsa_decoding/sub-*/*_desc-rdm.npz``, ``*_desc-rsa.tsv``, ``*_contrast-*_desc-decoding.tsv``, ``group/*``, ``figures/*.png``, ``rsa_decoding_summary.csv`` * - 19 - ``24-kit_source_movie.py`` - stage-18 estimates (individual), stage-21 averages and contrasts (group) - ``24-kit_source_movie/sub-*/*_frames/frame-NNN.jpg``, ``*_movie.json``, ``*_slider.html``, ``*_desc-peakframe.png``, ``group/cond-*`` and ``group/contrast-*`` likewise, ``source_movie_summary.csv`` * - 20 - ``20-kit_dipole_fit.py`` - stage-10 evoked, stage-18 covariance (or stage-10 epochs), stage-15/16 transform, stage-17 BEM - ``20-kit_dipole_fit/sub-*/*_desc-dipole.dip``, ``*_timecourse.tsv``, ``*_desc-dipole.json``, ``figures/*``, ``dipole_fit_summary.csv`` * - 21 - ``12-kit_sleep_spectra.py`` - stage-9 filtered FIF (or stage-2) of ``spectra.tasks``, ``scans.tsv`` - ``12-kit_sleep_spectra/sub-*/[ses-*/]*_desc-psd.tsv``, ``*_desc-bandpower_*.tsv``, ``*_desc-spectra.json``, ``figures/*``, ``spectra_summary.csv`` Run them in order after stage 10:: python 16-kit_auto_coreg.py -c pipeline_config_files/kidlang_config.yml python 17-kit_forward_model.py -c pipeline_config_files/kidlang_config.yml python 18-kit_inverse_solution.py -c pipeline_config_files/kidlang_config.yml python 21-kit_group_source.py -c pipeline_config_files/kidlang_config.yml python 22-kit_cluster_stats.py -c pipeline_config_files/kidlang_config.yml python 23-kit_rsa_decoding.py -c pipeline_config_files/kidlang_config.yml python 24-kit_source_movie.py -c pipeline_config_files/kidlang_config.yml python 20-kit_dipole_fit.py -c pipeline_config_files/epilepsy_project_config.yml # needs 14 (and 15 for the covariance) python 12-kit_sleep_spectra.py -c pipeline_config_files/epilepsy_project_config.yml # needs 7 only Stage 16 is optional when every subject has a reviewed stage-15 transform. Stages 17 and 18 run per recording and reuse their outputs while the inputs and settings are unchanged (``--overwrite`` recomputes); stages 21 to 23 are group stages and always recompute from what stages 10 and 18 currently list. The launch entries ``(Kidlang) ...`` in ``.vscode/launch.json`` run each stage in the standalone MNE environment. Conditions: ``events.conditions`` --------------------------------- Stages 18 and 21 to 23 and the report name the conditions through ``events.conditions``, a mapping from the event code (as written in the stage-3 ``.eve`` files) to a name and factor levels:: events: conditions: "18": {name: dialogue_phrase, task: dialogue, condition: phrase} "34": {name: dialogue_object, task: dialogue, condition: object} "66": {name: dialogue_baseline, task: dialogue, condition: baseline} ... Every key other than ``name`` is a factor. Factors give **selectors** that expand to every condition with that level, usable wherever a list of conditions is expected (``{condition: phrase}`` selects the four phrase conditions of KidLang), and they define the **model RDMs** of the RSA. Stage 10 is unaffected by the catalogue (it keeps writing one evoked per code). **Tasks that reuse the codes.** When a dataset has several tasks and each task gives the same codes a different meaning (the epilepsy project: 224 is ``tone_low`` in *twotones*, ``reversal_to_a`` in *flippedsquares*, ``cue_left`` in *buttonlr*), one flat catalogue cannot name them, and pooling by code across tasks (as the group and movie stages do) would mix unrelated responses. The catalogue can therefore be written **per task**:: events: conditions: twotones: {"224": tone_low, "225": tone_high, "226": tone_offset} flippedsquares: {"224": {name: reversal_to_a, kind: reversal}, "225": reversal_to_b} buttonlr: {"224": cue_left, "225": cue_right, "226": press_left, "227": press_right} name_conditions_by_task: auto The name of a condition of a recording is then resolved in this order: the per-task entry, else the flat entry, else the ``TrialType`` of the run's ``*_events.json`` (``TriggerChannels.KIT.TrialType``, written by the stimulus scripts), else ``code-``. Names that do not come from the flat catalogue are **prefixed with the task** (``twotones_tone_low``, ``buttonlr_press_left``), so two tasks sharing a code never pool in stages 21 to 24 and the report lists them apart. ``name_conditions_by_task: false`` turns the prefix off (single-task datasets, or when the per-task names are unique by construction), ``true`` prefixes every name, including the flat catalogue's. Per-task entries carry a ``task`` factor automatically, so ``{task: twotones}`` selects the conditions of that task. Renaming a condition changes the stage-18 output names; stage 18 notices (the names are part of its inputs), recomputes the recording and removes the estimates written under the old names. Stage 16: automatic coregistration ---------------------------------- ``16-kit_auto_coreg.py`` gives a head-to-MRI transform to every subject without opening a GUI, fitted either to the subject's **own** FreeSurfer reconstruction or to the **fsaverage template** (``coregistration.automatic.mri``): - ``template`` (default): fsaverage, scaled to the head shape (below). - ``individual``: the stage-13 reconstruction ``sub-