{ "cells": [ { "cell_type": "markdown", "id": "e0cdf1b6", "metadata": {}, "source": [ "# Image Reconstruction" ] }, { "cell_type": "code", "execution_count": 1, "id": "cb3f6968", "metadata": { "execution": { "iopub.execute_input": "2024-09-02T16:02:32.392873Z", "iopub.status.busy": "2024-09-02T16:02:32.392721Z", "iopub.status.idle": "2024-09-02T16:02:32.907389Z", "shell.execute_reply": "2024-09-02T16:02:32.906901Z" } }, "outputs": [], "source": [ "import pyvista as pv\n", "#pv.set_jupyter_backend('html')\n", "pv.set_jupyter_backend('static')\n", "#pv.OFF_SCREEN=True" ] }, { "cell_type": "code", "execution_count": 2, "id": "7e869404", "metadata": { "execution": { "iopub.execute_input": "2024-09-02T16:02:32.909595Z", "iopub.status.busy": "2024-09-02T16:02:32.909362Z", "iopub.status.idle": "2024-09-02T16:02:33.882652Z", "shell.execute_reply": "2024-09-02T16:02:33.882131Z" } }, "outputs": [], "source": [ "import time\n", "\n", "import matplotlib.pyplot as p\n", "import numpy as np\n", "import xarray as xr\n", "from IPython.display import Image\n", "\n", "import cedalion\n", "import cedalion.dataclasses as cdc\n", "import cedalion.datasets\n", "import cedalion.geometry.registration\n", "import cedalion.geometry.segmentation\n", "import cedalion.imagereco.forward_model as fw\n", "import cedalion.imagereco.tissue_properties\n", "import cedalion.io\n", "import cedalion.plots\n", "from cedalion.imagereco.solver import pseudo_inverse_stacked\n", "\n", "xr.set_options(display_expand_data=False);" ] }, { "cell_type": "markdown", "id": "bf3bab37", "metadata": {}, "source": [ "## Load a finger-tapping dataset \n", "\n", "For this demo we load an example finger-tapping recording through `cedalion.datasets.get_fingertapping`. The file contains a single NIRS element with one block of raw amplitude data. " ] }, { "cell_type": "code", "execution_count": 3, "id": "a1425f28", "metadata": { "execution": { "iopub.execute_input": "2024-09-02T16:02:33.885268Z", "iopub.status.busy": "2024-09-02T16:02:33.884763Z", "iopub.status.idle": "2024-09-02T16:02:33.994170Z", "shell.execute_reply": "2024-09-02T16:02:33.993682Z" } }, "outputs": [], "source": [ "rec = cedalion.datasets.get_fingertapping()" ] }, { "cell_type": "markdown", "id": "842b9f72", "metadata": {}, "source": [ "The location of the probes is obtained from the snirf metadata (i.e. /nirs0/probe/)\n", "\n", "Note that units ('m') are adopted and the coordinate system is named 'digitized'." ] }, { "cell_type": "code", "execution_count": 4, "id": "1e5c3430", "metadata": { "execution": { "iopub.execute_input": "2024-09-02T16:02:33.996576Z", "iopub.status.busy": "2024-09-02T16:02:33.996261Z", "iopub.status.idle": "2024-09-02T16:02:34.006868Z", "shell.execute_reply": "2024-09-02T16:02:34.006428Z" } }, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (label: 55, digitized: 3)> Size: 1kB\n", "[m] -0.04161 0.0268 0.1299 -0.06477 0.05814 ... 0.06258 0.08226 0.01751 0.06619\n", "Coordinates:\n", " type (label) object 440B PointType.SOURCE ... PointType.LANDMARK\n", " * label (label) <U3 660B 'S1' 'S2' 'S3' 'S4' 'S5' ... '25' '26' '27' '28'\n", "Dimensions without coordinates: digitized
\n", " | sourceIndex | \n", "detectorIndex | \n", "wavelengthIndex | \n", "wavelengthActual | \n", "wavelengthEmissionActual | \n", "dataType | \n", "dataUnit | \n", "dataTypeLabel | \n", "dataTypeIndex | \n", "sourcePower | \n", "detectorGain | \n", "moduleIndex | \n", "sourceModuleIndex | \n", "detectorModuleIndex | \n", "channel | \n", "source | \n", "detector | \n", "wavelength | \n", "chromo | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1 | \n", "1 | \n", "1 | \n", "None | \n", "None | \n", "1 | \n", "None | \n", "None | \n", "1 | \n", "None | \n", "None | \n", "None | \n", "None | \n", "None | \n", "S1D1 | \n", "S1 | \n", "D1 | \n", "760.0 | \n", "None | \n", "
1 | \n", "1 | \n", "1 | \n", "2 | \n", "None | \n", "None | \n", "1 | \n", "None | \n", "None | \n", "1 | \n", "None | \n", "None | \n", "None | \n", "None | \n", "None | \n", "S1D1 | \n", "S1 | \n", "D1 | \n", "850.0 | \n", "None | \n", "
2 | \n", "1 | \n", "2 | \n", "1 | \n", "None | \n", "None | \n", "1 | \n", "None | \n", "None | \n", "1 | \n", "None | \n", "None | \n", "None | \n", "None | \n", "None | \n", "S1D2 | \n", "S1 | \n", "D2 | \n", "760.0 | \n", "None | \n", "
3 | \n", "1 | \n", "2 | \n", "2 | \n", "None | \n", "None | \n", "1 | \n", "None | \n", "None | \n", "1 | \n", "None | \n", "None | \n", "None | \n", "None | \n", "None | \n", "S1D2 | \n", "S1 | \n", "D2 | \n", "850.0 | \n", "None | \n", "
4 | \n", "1 | \n", "3 | \n", "1 | \n", "None | \n", "None | \n", "1 | \n", "None | \n", "None | \n", "1 | \n", "None | \n", "None | \n", "None | \n", "None | \n", "None | \n", "S1D3 | \n", "S1 | \n", "D3 | \n", "760.0 | \n", "None | \n", "
\n", " | onset | \n", "
---|---|
trial_type | \n", "\n", " |
15.0 | \n", "2 | \n", "
Tapping/Left | \n", "30 | \n", "
Tapping/Right | \n", "30 | \n", "
control | \n", "30 | \n", "
<xarray.DataArray (channel: 28, wavelength: 2, time: 23239)> Size: 10MB\n", "[] 0.04042 0.0446 0.04422 0.05065 0.04511 ... -0.0113 -0.01109 -0.01094 -0.01317\n", "Coordinates:\n", " * time (time) float64 186kB 0.0 0.128 0.256 ... 2.974e+03 2.974e+03\n", " samples (time) int64 186kB 0 1 2 3 4 5 ... 23234 23235 23236 23237 23238\n", " * channel (channel) object 224B 'S1D1' 'S1D2' 'S1D3' ... 'S8D8' 'S8D16'\n", " source (channel) object 224B 'S1' 'S1' 'S1' 'S1' ... 'S8' 'S8' 'S8'\n", " detector (channel) object 224B 'D1' 'D2' 'D3' 'D9' ... 'D7' 'D8' 'D16'\n", " * wavelength (wavelength) float64 16B 760.0 850.0
<xarray.DataArray (segmentation_type: 5, i: 255, j: 255, k: 189)> Size: 61MB\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 0 0\n", "Coordinates:\n", " * segmentation_type (segmentation_type) <U5 100B 'csf' 'gm' ... 'skull' 'wm'\n", "Dimensions without coordinates: i, j, k
<xarray.DataArray (label: 4, ijk: 3)> Size: 96B\n", "[] 127.0 206.8 57.86 128.8 26.05 39.14 55.05 110.9 39.34 202.9 114.0 39.92\n", "Coordinates:\n", " * label (label) <U3 48B 'Nz' 'Iz' 'LPA' 'RPA'\n", " type (label) object 32B PointType.LANDMARK ... PointType.LANDMARK\n", "Dimensions without coordinates: ijk" ], "text/plain": [ "
<xarray.DataArray (aligned: 4, ijk: 4)> Size: 128B\n", "[mm] 1.0 0.0 0.0 -127.0 0.0 1.0 0.0 -127.0 0.0 0.0 1.0 -94.0 0.0 0.0 0.0 1.0\n", "Dimensions without coordinates: aligned, ijk
<xarray.DataArray (label: 55, ijk: 3)> Size: 1kB\n", "[] 85.61 111.3 166.8 66.62 149.0 129.9 ... 200.3 135.0 99.23 204.1 117.9 100.9\n", "Coordinates:\n", " type (label) object 440B PointType.SOURCE ... PointType.LANDMARK\n", " * label (label) <U3 660B 'S1' 'S2' 'S3' 'S4' 'S5' ... '25' '26' '27' '28'\n", "Dimensions without coordinates: ijk
<xarray.DataArray (label: 24, wavelength: 2, i: 255, j: 255, k: 189)> Size: 5GB\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 0.0\n", "Coordinates:\n", " * label (label) <U3 288B 'S1' 'S2' 'S3' 'S4' ... 'D13' 'D14' 'D15' 'D16'\n", " type (label) object 192B PointType.SOURCE ... PointType.DETECTOR\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", "Dimensions without coordinates: i, j, k
<xarray.DataArray (optode1: 24, optode2: 24, wavelength: 2)> Size: 9kB\n", "0.06158 0.06158 2.043e-08 2.043e-08 ... 1.492e-08 1.492e-08 0.2147 0.2147\n", "Coordinates:\n", " * optode1 (optode1) <U3 288B 'S1' 'S2' 'S3' 'S4' ... 'D14' 'D15' 'D16'\n", " * optode2 (optode2) <U3 288B 'S1' 'S2' 'S3' 'S4' ... 'D14' 'D15' 'D16'\n", " * wavelength (wavelength) float64 16B 760.0 850.0" ], "text/plain": [ "
<xarray.DataArray (channel: 28, vertex: 120237, wavelength: 2)> Size: 54MB\n", "0.0 0.0 0.0 0.0 2.977e-15 ... 3.159e-20 5.023e-20 5.023e-20 7.523e-19 7.523e-19\n", "Coordinates:\n", " * channel (channel) <U5 560B 'S1D1' 'S1D2' 'S1D3' ... 'S8D8' 'S8D16'\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", " is_brain (vertex) bool 120kB True True True True ... False False False\n", "Dimensions without coordinates: vertex
<xarray.DataArray (flat_channel: 56, flat_vertex: 240474)> Size: 108MB\n", "0.0 0.0 4.016e-13 0.0 2.163e-13 ... 2.035e-15 0.0 5.028e-18 7.996e-18 1.198e-16\n", "Dimensions without coordinates: flat_channel, flat_vertex" ], "text/plain": [ "
<xarray.DataArray (flat_vertex: 240474, flat_channel: 56)> Size: 108MB\n", "0.0 0.0 0.0 0.0 0.0 0.0 ... 1.766e-24 -1.063e-24 -6.122e-24 3.769e-25 3.738e-24\n", "Coordinates:\n", " * chromo (flat_vertex) <U3 3MB 'HbO' 'HbO' 'HbO' 'HbO' ... 'HbR' 'HbR' 'HbR'\n", "Dimensions without coordinates: flat_vertex, flat_channel
<xarray.DataArray (trial_type: 2, channel: 28, wavelength: 2, reltime: 196)> Size: 176kB\n", "[] -0.002354 -0.002351 -0.002334 -0.002295 ... -0.0004654 -0.0005121 -0.0005609\n", "Coordinates:\n", " * channel (channel) object 224B 'S1D1' 'S1D2' 'S1D3' ... 'S8D8' 'S8D16'\n", " source (channel) object 224B 'S1' 'S1' 'S1' 'S1' ... 'S8' 'S8' 'S8'\n", " detector (channel) object 224B 'D1' 'D2' 'D3' 'D9' ... 'D7' 'D8' 'D16'\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", " * reltime (reltime) float64 2kB -4.992 -4.864 -4.736 ... 19.71 19.84 19.97\n", " * trial_type (trial_type) object 16B 'Tapping/Left' 'Tapping/Right'
<xarray.DataArray (trial_type: 2, reltime: 196, flat_channel: 56)> Size: 176kB\n", "[] -0.002354 -0.001524 -0.001609 -0.0005054 ... -0.002499 -0.008621 -0.0005609\n", "Coordinates:\n", " source (flat_channel) object 448B 'S1' 'S1' 'S1' ... 'S8' 'S8' 'S8'\n", " detector (flat_channel) object 448B 'D1' 'D2' 'D3' ... 'D7' 'D8' 'D16'\n", " * reltime (reltime) float64 2kB -4.992 -4.864 -4.736 ... 19.84 19.97\n", " * trial_type (trial_type) object 16B 'Tapping/Left' 'Tapping/Right'\n", " * flat_channel (flat_channel) object 448B MultiIndex\n", " * wavelength (flat_channel) float64 448B 760.0 760.0 760.0 ... 850.0 850.0\n", " * channel (flat_channel) object 448B 'S1D1' 'S1D2' ... 'S8D8' 'S8D16'
<xarray.DataArray (flat_vertex: 240474, trial_type: 2, reltime: 196)> Size: 754MB\n", "[] 0.0 0.0 0.0 0.0 0.0 ... -9.449e-27 -8.761e-27 -8.353e-27 -8.166e-27 -8.13e-27\n", "Coordinates:\n", " * chromo (flat_vertex) <U3 3MB 'HbO' 'HbO' 'HbO' ... 'HbR' 'HbR' 'HbR'\n", " * reltime (reltime) float64 2kB -4.992 -4.864 -4.736 ... 19.71 19.84 19.97\n", " * trial_type (trial_type) object 16B 'Tapping/Left' 'Tapping/Right'\n", "Dimensions without coordinates: flat_vertex