{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Adding Synthetic Hemodynamic Reponses to Data\n", "\n", "This example notebook illustrates the functionality in `cedalion.sim.synthetic_hrf`\n", "to create simulated datasets with added activations." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-06-06T16:11:46.996822Z", "iopub.status.busy": "2025-06-06T16:11:46.996386Z", "iopub.status.idle": "2025-06-06T16:11:47.002132Z", "shell.execute_reply": "2025-06-06T16:11:47.001732Z" } }, "outputs": [], "source": [ "# set this flag to True to enable interactive 3D plots\n", "INTERACTIVE_PLOTS = False" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-06-06T16:11:47.003942Z", "iopub.status.busy": "2025-06-06T16:11:47.003799Z", "iopub.status.idle": "2025-06-06T16:11:48.700483Z", "shell.execute_reply": "2025-06-06T16:11:48.700042Z" } }, "outputs": [], "source": [ "import os\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pyvista as pv\n", "import xarray as xr\n", "\n", "import cedalion\n", "import cedalion.dataclasses as cdc\n", "import cedalion.datasets\n", "import cedalion.geometry.landmarks as cd_landmarks\n", "import cedalion.imagereco.forward_model as fw\n", "import cedalion.models.glm as glm\n", "import cedalion.nirs\n", "import cedalion.plots\n", "\n", "import cedalion.sigproc.quality as quality\n", "import cedalion.sim.synthetic_hrf as synhrf\n", "import cedalion.xrutils as xrutils\n", "from cedalion import units\n", "from cedalion.imagereco.solver import pseudo_inverse_stacked\n", "\n", "xr.set_options(display_expand_data=False)\n", "\n", "if INTERACTIVE_PLOTS:\n", " pv.set_jupyter_backend('server')\n", "else:\n", " pv.set_jupyter_backend('static')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading and preprocessing the dataset\n", "\n", "This notebook uses a high-density, whole head resting state dataset recorded with a NinjaNIRS 22." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-06-06T16:11:48.703268Z", "iopub.status.busy": "2025-06-06T16:11:48.702590Z", "iopub.status.idle": "2025-06-06T16:11:55.941801Z", "shell.execute_reply": "2025-06-06T16:11:55.941302Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Downloading file 'nn22_resting_state.zip' from 'https://doc.ibs.tu-berlin.de/cedalion/datasets/nn22_resting_state.zip' to '/home/runner/.cache/cedalion'.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Unzipping contents of '/home/runner/.cache/cedalion/nn22_resting_state.zip' to '/home/runner/.cache/cedalion/nn22_resting_state.zip.unzip'\n" ] }, { "data": { "text/html": [ "
<xarray.DataArray (channel: 567, wavelength: 2, time: 3309)> Size: 30MB\n", "[V] 0.0238 0.02385 0.02375 0.02361 0.02364 ... 0.2887 0.2892 0.2897 0.291 0.2923\n", "Coordinates:\n", " * time (time) float64 26kB 0.0 0.1112 0.2225 ... 367.8 367.9 368.0\n", " samples (time) int64 26kB 0 1 2 3 4 5 ... 3303 3304 3305 3306 3307 3308\n", " * channel (channel) object 5kB 'S10D87' 'S10D94' ... 'S47D29' 'S5D137'\n", " source (channel) object 5kB 'S10' 'S10' 'S10' ... 'S56' 'S47' 'S5'\n", " detector (channel) object 5kB 'D87' 'D94' 'D89' ... 'D129' 'D29' 'D137'\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", "Attributes:\n", " data_type_group: unprocessed raw
<xarray.DataArray (label: 4, aligned: 3)> Size: 96B\n", "[mm] -0.04992 79.79 -36.14 1.756 -100.9 ... -16.12 -54.66 75.95 -13.04 -54.08\n", "Coordinates:\n", " * label (label) <U3 48B 'Nz' 'Iz' 'LPA' 'RPA'\n", " type (label) object 32B PointType.LANDMARK ... PointType.LANDMARK\n", "Dimensions without coordinates: aligned
<xarray.DataArray (vertex: 15002, chromo: 2)> Size: 240kB\n", "[M] 1.168e-19 -4.672e-20 1.648e-20 -6.593e-21 3.798e-21 ... 0.0 -0.0 0.0 -0.0\n", "Coordinates:\n", " * chromo (chromo) <U3 24B 'HbO' 'HbR'\n", "Dimensions without coordinates: vertex
<xarray.DataArray (trial_type: 2, vertex: 15002, chromo: 2)> Size: 480kB\n", "[M] 1.168e-19 -4.672e-20 1.648e-20 -6.593e-21 ... -2.275e-13 1.01e-19 -4.038e-20\n", "Coordinates:\n", " * chromo (chromo) <U3 24B 'HbO' 'HbR'\n", " * trial_type (trial_type) <U7 56B 'Stim C3' 'Stim C4'\n", "Dimensions without coordinates: vertex
<xarray.DataArray (channel: 545, vertex: 15002, wavelength: 2)> Size: 131MB\n", "[16352180 values with dtype=float64]\n", "Coordinates:\n", " is_brain (vertex) bool 15kB True True True True ... True True True True\n", " * channel (channel) object 4kB 'S10D87' 'S10D94' ... 'S47D29' 'S5D137'\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", " source (channel) object 4kB 'S10' 'S10' 'S10' ... 'S56' 'S47' 'S5'\n", " detector (channel) object 4kB 'D87' 'D94' 'D89' ... 'D129' 'D29' 'D137'\n", "Dimensions without coordinates: vertex
<xarray.DataArray (flat_channel: 1090, flat_vertex: 30004)> Size: 262MB\n", "[1/M] 5.859e-16 4.574e-15 3.6e-15 2.592e-15 ... 1.471e-11 0.0108 1.169e-10\n", "Coordinates:\n", " is_brain (flat_vertex) bool 30kB True True True True ... True True True\n", " chromo (flat_vertex) <U3 360kB 'HbO' 'HbO' 'HbO' ... 'HbR' 'HbR' 'HbR'\n", " vertex (flat_vertex) int64 240kB 0 1 2 3 4 ... 14998 14999 15000 15001\n", " wavelength (flat_channel) float64 9kB 760.0 760.0 760.0 ... 850.0 850.0\n", " channel (flat_channel) object 9kB 'S10D87' 'S10D94' ... 'S5D137'\n", " source (flat_channel) object 9kB 'S10' 'S10' 'S10' ... 'S56' 'S47' 'S5'\n", " detector (flat_channel) object 9kB 'D87' 'D94' 'D89' ... 'D29' 'D137'\n", "Dimensions without coordinates: flat_channel, flat_vertex
<xarray.DataArray (flat_vertex: 30004, flat_channel: 1090)> Size: 262MB\n", "[M] 2.581e-13 -7.848e-13 -5.654e-12 ... -2.363e-14 2.218e-14 -4.732e-15\n", "Coordinates:\n", " is_brain (flat_vertex) bool 30kB True True True True ... True True True\n", " chromo (flat_vertex) <U3 360kB 'HbO' 'HbO' 'HbO' ... 'HbR' 'HbR' 'HbR'\n", " vertex (flat_vertex) int64 240kB 0 1 2 3 4 ... 14998 14999 15000 15001\n", " wavelength (flat_channel) float64 9kB 760.0 760.0 760.0 ... 850.0 850.0\n", " channel (flat_channel) object 9kB 'S10D87' 'S10D94' ... 'S5D137'\n", " source (flat_channel) object 9kB 'S10' 'S10' 'S10' ... 'S56' 'S47' 'S5'\n", " detector (flat_channel) object 9kB 'D87' 'D94' 'D89' ... 'D29' 'D137'\n", "Dimensions without coordinates: flat_vertex, flat_channel" ], "text/plain": [ "
<xarray.DataArray (trial_type: 2, flat_vertex: 30004)> Size: 480kB\n", "[M] 1.168e-19 1.648e-20 3.798e-21 1.274e-19 ... -3.851e-12 -2.275e-13 -4.038e-20\n", "Coordinates:\n", " * trial_type (trial_type) <U7 56B 'Stim C3' 'Stim C4'\n", " * flat_vertex (flat_vertex) object 240kB MultiIndex\n", " * chromo (flat_vertex) <U3 360kB 'HbO' 'HbO' 'HbO' ... 'HbR' 'HbR' 'HbR'\n", " * vertex (flat_vertex) int64 240kB 0 1 2 3 4 ... 14998 14999 15000 15001" ], "text/plain": [ "
<xarray.DataArray (flat_channel: 1090, trial_type: 2)> Size: 17kB\n", "[] -1.963e-16 -6.505e-07 -5.609e-16 -6.902e-08 ... 1.986e-07 3.311e-15 7.887e-10\n", "Coordinates:\n", " wavelength (flat_channel) float64 9kB 760.0 760.0 760.0 ... 850.0 850.0\n", " channel (flat_channel) object 9kB 'S10D87' 'S10D94' ... 'S5D137'\n", " source (flat_channel) object 9kB 'S10' 'S10' 'S10' ... 'S56' 'S47' 'S5'\n", " detector (flat_channel) object 9kB 'D87' 'D94' 'D89' ... 'D29' 'D137'\n", " * trial_type (trial_type) <U7 56B 'Stim C3' 'Stim C4'\n", "Dimensions without coordinates: flat_channel" ], "text/plain": [ "
<xarray.DataArray (time: 1, trial_type: 2, wavelength: 2, channel: 545)> Size: 17kB\n", "[] -1.738e-14 -6.175e-13 -3.619e-13 -2.301e-15 ... 3.903e-06 3.561e-08 2.022e-08\n", "Coordinates:\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", " * channel (channel) object 4kB 'S10D112' 'S10D133' ... 'S9D94' 'S9D96'\n", " * trial_type (trial_type) <U7 56B 'Stim C3' 'Stim C4'\n", " source (channel) object 4kB 'S10' 'S10' 'S10' 'S10' ... 'S9' 'S9' 'S9'\n", " detector (channel) object 4kB 'D112' 'D133' 'D135' ... 'D92' 'D94' 'D96'\n", " * time (time) int64 8B 0
<xarray.DataArray 'concentration' ()> Size: 8B\n", "[] 17.98" ], "text/plain": [ "
<xarray.DataArray 'time' (time: 3309)> Size: 26kB\n", "0.0 0.1112 0.2225 0.3337 0.445 0.5562 ... 367.5 367.6 367.7 367.8 367.9 368.0\n", "Coordinates:\n", " * time (time) float64 26kB 0.0 0.1112 0.2225 0.3337 ... 367.8 367.9 368.0\n", " samples (time) int64 26kB 0 1 2 3 4 5 6 ... 3303 3304 3305 3306 3307 3308\n", "Attributes:\n", " units: second
\n", " | onset | \n", "duration | \n", "value | \n", "trial_type | \n", "
---|---|---|---|---|
0 | \n", "17.35 | \n", "10.0 | \n", "1.0 | \n", "Stim C3 | \n", "
1 | \n", "44.71 | \n", "10.0 | \n", "1.0 | \n", "Stim C4 | \n", "
2 | \n", "71.19 | \n", "10.0 | \n", "1.0 | \n", "Stim C4 | \n", "
3 | \n", "91.41 | \n", "10.0 | \n", "1.0 | \n", "Stim C4 | \n", "
4 | \n", "117.11 | \n", "10.0 | \n", "1.0 | \n", "Stim C4 | \n", "
<xarray.DataArray (trial_type: 2, wavelength: 2, channel: 545, time: 3309)> Size: 58MB\n", "[] -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", "Coordinates:\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", " * channel (channel) object 4kB 'S10D112' 'S10D133' ... 'S9D94' 'S9D96'\n", " * trial_type (trial_type) <U7 56B 'Stim C3' 'Stim C4'\n", " source (channel) object 4kB 'S10' 'S10' 'S10' 'S10' ... 'S9' 'S9' 'S9'\n", " detector (channel) object 4kB 'D112' 'D133' 'D135' ... 'D92' 'D94' 'D96'\n", " * time (time) float64 26kB 0.0 0.1112 0.2225 ... 367.8 367.9 368.0
<xarray.DataArray (wavelength: 2, channel: 545, time: 3309)> Size: 29MB\n", "[] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n", "Coordinates:\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", " * channel (channel) object 4kB 'S10D112' 'S10D133' ... 'S9D94' 'S9D96'\n", " source (channel) object 4kB 'S10' 'S10' 'S10' 'S10' ... 'S9' 'S9' 'S9'\n", " detector (channel) object 4kB 'D112' 'D133' 'D135' ... 'D92' 'D94' 'D96'\n", " * time (time) float64 26kB 0.0 0.1112 0.2225 ... 367.8 367.9 368.0" ], "text/plain": [ "
<xarray.DataArray (channel: 545, wavelength: 2, time: 3309)> Size: 29MB\n", "[] 0.002072 0.002634 0.00317 0.003662 ... 0.006999 0.006033 0.004973 0.00387\n", "Coordinates:\n", " * time (time) float64 26kB 0.0 0.1112 0.2225 ... 367.8 367.9 368.0\n", " * channel (channel) object 4kB 'S10D87' 'S10D94' ... 'S47D29' 'S5D137'\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", " samples (time) int64 26kB 0 1 2 3 4 5 ... 3303 3304 3305 3306 3307 3308\n", " source (channel) object 4kB 'S10' 'S10' 'S10' ... 'S56' 'S47' 'S5'\n", " detector (channel) object 4kB 'D87' 'D94' 'D89' ... 'D129' 'D29' 'D137'