---------------------- KIT to FIFF conversion ---------------------- Author: Hadi Zaatiti What & why ---------- ``kit2fiff_from_config.py`` converts NYUAD-KIT ``.con`` recordings into MNE ``.fif`` files while **preserving all BIDS entities** in the output filenames, pairing **head-position marker** (``.mrk``) files deterministically, `headshape` head surface laser digitization and `headshape` laser points digitization and storing results and logs under BIDS **derivatives**. Use this after BIDS-structuring your KIT dataset to obtain analysis-ready ``.fif`` per run/split/processing, with clear provenance for which **CON**, **MRK**, **HSP** (headshape) and **ELP** (digitizer points) were used. Key features ------------ - **Entity preservation:** output names include *all* present entities (``sub``, ``ses``, ``task``, ``acq``, ``run``, **``split``**, ``rec``, ``space``, **``proc``**) plus ``desc-rawkit``. - **Deterministic MRK pairing:** MRKs are ordered by ``acq-``; for run-group *i*, use MRK[*i*] (before) and, if present, MRK[*i+1*] (after). If only MRK[*i*] exists, use it alone. **Never** reuse “way-before” MRKs for later runs. - **Run/split ordering:** CONs are grouped by **run** and ordered by **split**; missing entities default to 0, so split-less data works seamlessly. - **Session-aware HSP/ELP:** points and headshape are applied **per subject / session**; if session-specific files are absent, fallback to subject-level. These files **do not** include task/run in names, so they are never filtered by task/run. - **Edited points once per scope:** creates a ``*_edited.txt`` (drops last 3 cols) and reuses it for all CONs in that subject/session. - **Reproducible logging:** a root summary CSV and per-subject text logs recording, for each generated ``.fif``, the exact CON / MRK / HSP / ELP used. What you need to provide ------------------------ A BIDS-like KIT dataset named following the BIDS standard and the additional NYUAD-naming constraints found at :ref:`data_naming`. Configuration file ------------------ The script reads by default the template config: .. code-block:: text pipeline/mne_pipelines/kit_general_pipelines/pipeline_config_files/config_template.yml Provide your own configuration for your own dataset by providing the script with the argument `--config PATH_TO_YOUR_CONFIG` Example configuration ^^^^^^^^^^^^^^^^^^^^^ .. code-block:: yaml project: name: script-testing-dataset root_env: MEG_DATA # or set project.root_override to an absolute path subjects: include: [] # [] → discover all subjects exclude: [] bids_selection: sessions: [] # [] / "" / null => not specified tasks: [] runs: [] splits: [] processings: [] Environment ----------- Set the base directory for your datasets: .. code-block:: bash export MEG_DATA=/path/to/datasets_parent The BIDS root resolves to: .. code-block:: text $MEG_DATA/ How to run ---------- Basic command ^^^^^^^^^^^^^ .. code-block:: bash python pipeline/mne_pipelines/kit_general_pipelines/kit_con_to_fif.py \ --config pipeline/mne_pipelines/kit_general_pipelines/pipeline_config_files/config_template.yml What the script does -------------------- 1. **Load config & locate BIDS root.** 2. **Discover subjects** (``include`` empty ⇒ all; ``exclude`` removed). 3. **Select runs** using optional ``sessions/tasks/runs/splits/processings``; empty arrays are treated as *not specified*. 4. **Order recordings**: group CONs by **run** (missing ⇒ 0), sort within group by **split** (missing ⇒ 0). 5. **Order MRKs**: sort by **``acq``**; pair per run-group: - group *i* → [MRK[*i*], MRK[*i+1*]] if both exist, - else [MRK[*i*]] if only one exists, - else skip group (no MRK for that group). 6. **Resolve HSP/ELP** per subject/session (never by task/run). If session-scoped files are absent, fallback to subject-level. 7. **Create edited points** once per (subject, session, source points) at: .. code-block:: text /derivatives/kit2fiff/sub-/[ses-/]/*_edited.txt 8. **Read KIT and save FIFF** using the resolved MRK/HSP/ELP. 9. **Preserve entities in output names** (see below). 10. **Log** each conversion row into a root CSV and append a subject log. File naming (entity preservation) --------------------------------- All present entities are serialized in a stable order into the output name, including **split** and **proc**, followed by ``desc-rawkit``: .. code-block:: text sub-[_ses-][_task-][_acq-