{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Channel Quality Assessment and Pruning\n", "\n", "This notebook sketches how to prune bad channels " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-04-14T12:56:34.118113Z", "iopub.status.busy": "2025-04-14T12:56:34.117951Z", "iopub.status.idle": "2025-04-14T12:56:35.693697Z", "shell.execute_reply": "2025-04-14T12:56:35.693157Z" } }, "outputs": [], "source": [ "import matplotlib.pyplot as p\n", "import numpy as np\n", "from matplotlib.colors import LinearSegmentedColormap\n", "\n", "import cedalion\n", "import cedalion.datasets as datasets\n", "import cedalion.nirs\n", "import cedalion.plots as plots\n", "import cedalion.sigproc.quality as quality\n", "import cedalion.xrutils as xrutils\n", "from cedalion import units\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Loading raw CW-NIRS data from a SNIRF file and converting it to OD and CONC\n", "\n", "This notebook uses a finger-tapping dataset in BIDS layout provided by Rob Luke that is automatically fetched. \n", "You can also find it [here](https://github.com/rob-luke/BIDS-NIRS-Tapping)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-04-14T12:56:35.696174Z", "iopub.status.busy": "2025-04-14T12:56:35.695811Z", "iopub.status.idle": "2025-04-14T12:56:36.127995Z", "shell.execute_reply": "2025-04-14T12:56:36.127430Z" } }, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (channel: 28, wavelength: 2, time: 23239)> Size: 10MB\n", "<Quantity([[[0.0913686 0.0909875 0.0910225 ... 0.0941083 0.0940129 0.0944882]\n", " [0.1856806 0.186377 0.1836514 ... 0.1856486 0.1850836 0.1842172]]\n", "\n", " [[0.227516 0.2297024 0.2261366 ... 0.2264519 0.2271665 0.226713 ]\n", " [0.6354927 0.637668 0.6298023 ... 0.6072068 0.6087293 0.6091066]]\n", "\n", " [[0.1064704 0.1066212 0.1053444 ... 0.121114 0.1205022 0.1205441]\n", " [0.2755033 0.2761615 0.2727006 ... 0.2911952 0.2900544 0.2909847]]\n", "\n", " ...\n", "\n", " [[0.2027881 0.1996586 0.2004866 ... 0.2318743 0.2311941 0.2330808]\n", " [0.4666358 0.4554404 0.4561614 ... 0.4809749 0.4812827 0.4862896]]\n", "\n", " [[0.4885007 0.4802285 0.4818338 ... 0.6109142 0.6108118 0.613845 ]\n", " [0.8457658 0.825988 0.8259648 ... 0.975894 0.9756599 0.9826459]]\n", "\n", " [[0.6304559 0.6284427 0.6287045 ... 0.6810626 0.6809573 0.6818709]\n", " [1.2285622 1.2205907 1.2190002 ... 1.2729124 1.2727222 1.2755645]]], 'volt')>\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\n", "Attributes:\n", " data_type_group: unprocessed raw
<xarray.DataArray (channel: 24, wavelength: 2, time: 23239)> Size: 9MB\n", "<Quantity([[[0.0913686 0.0909875 0.0910225 ... 0.0941083 0.0940129 0.0944882]\n", " [0.1856806 0.186377 0.1836514 ... 0.1856486 0.1850836 0.1842172]]\n", "\n", " [[0.227516 0.2297024 0.2261366 ... 0.2264519 0.2271665 0.226713 ]\n", " [0.6354927 0.637668 0.6298023 ... 0.6072068 0.6087293 0.6091066]]\n", "\n", " [[0.1064704 0.1066212 0.1053444 ... 0.121114 0.1205022 0.1205441]\n", " [0.2755033 0.2761615 0.2727006 ... 0.2911952 0.2900544 0.2909847]]\n", "\n", " ...\n", "\n", " [[0.187484 0.1868235 0.1866562 ... 0.1735965 0.1736705 0.1738339]\n", " [0.2424386 0.241503 0.2408491 ... 0.22303 0.2229887 0.2234081]]\n", "\n", " [[0.2027881 0.1996586 0.2004866 ... 0.2318743 0.2311941 0.2330808]\n", " [0.4666358 0.4554404 0.4561614 ... 0.4809749 0.4812827 0.4862896]]\n", "\n", " [[0.6304559 0.6284427 0.6287045 ... 0.6810626 0.6809573 0.6818709]\n", " [1.2285622 1.2205907 1.2190002 ... 1.2729124 1.2727222 1.2755645]]], 'volt')>\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 192B 'S1D1' 'S1D2' 'S1D3' ... 'S8D7' 'S8D16'\n", " source (channel) object 192B 'S1' 'S1' 'S1' 'S1' ... 'S7' 'S8' 'S8'\n", " detector (channel) object 192B 'D1' 'D2' 'D3' 'D9' ... 'D15' 'D7' 'D16'\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", "Attributes:\n", " data_type_group: unprocessed raw
<xarray.DataArray (channel: 20, wavelength: 2, time: 23239)> Size: 7MB\n", "<Quantity([[[0.0913686 0.0909875 0.0910225 ... 0.0941083 0.0940129 0.0944882]\n", " [0.1856806 0.186377 0.1836514 ... 0.1856486 0.1850836 0.1842172]]\n", "\n", " [[0.227516 0.2297024 0.2261366 ... 0.2264519 0.2271665 0.226713 ]\n", " [0.6354927 0.637668 0.6298023 ... 0.6072068 0.6087293 0.6091066]]\n", "\n", " [[0.1064704 0.1066212 0.1053444 ... 0.121114 0.1205022 0.1205441]\n", " [0.2755033 0.2761615 0.2727006 ... 0.2911952 0.2900544 0.2909847]]\n", "\n", " ...\n", "\n", " [[0.2225884 0.2187791 0.2195495 ... 0.2564863 0.2551258 0.2560233]\n", " [0.3994258 0.3917637 0.389261 ... 0.4304597 0.430814 0.4331249]]\n", "\n", " [[0.2027881 0.1996586 0.2004866 ... 0.2318743 0.2311941 0.2330808]\n", " [0.4666358 0.4554404 0.4561614 ... 0.4809749 0.4812827 0.4862896]]\n", "\n", " [[0.4885007 0.4802285 0.4818338 ... 0.6109142 0.6108118 0.613845 ]\n", " [0.8457658 0.825988 0.8259648 ... 0.975894 0.9756599 0.9826459]]], 'volt')>\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 160B 'S1D1' 'S1D2' 'S1D3' ... 'S8D7' 'S8D8'\n", " source (channel) object 160B 'S1' 'S1' 'S1' 'S2' ... 'S7' 'S8' 'S8'\n", " detector (channel) object 160B 'D1' 'D2' 'D3' 'D1' ... 'D7' 'D7' 'D8'\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", "Attributes:\n", " data_type_group: unprocessed raw
<xarray.DataArray (channel: 23, wavelength: 2, time: 23239)> Size: 9MB\n", "<Quantity([[[0.227516 0.2297024 0.2261366 ... 0.2264519 0.2271665 0.226713 ]\n", " [0.6354927 0.637668 0.6298023 ... 0.6072068 0.6087293 0.6091066]]\n", "\n", " [[0.1064704 0.1066212 0.1053444 ... 0.121114 0.1205022 0.1205441]\n", " [0.2755033 0.2761615 0.2727006 ... 0.2911952 0.2900544 0.2909847]]\n", "\n", " [[0.5512474 0.5510672 0.5476283 ... 0.6179242 0.6188702 0.6187721]\n", " [1.125532 1.1238331 1.1119423 ... 1.1817728 1.1819598 1.1832658]]\n", "\n", " ...\n", "\n", " [[0.2027881 0.1996586 0.2004866 ... 0.2318743 0.2311941 0.2330808]\n", " [0.4666358 0.4554404 0.4561614 ... 0.4809749 0.4812827 0.4862896]]\n", "\n", " [[0.4885007 0.4802285 0.4818338 ... 0.6109142 0.6108118 0.613845 ]\n", " [0.8457658 0.825988 0.8259648 ... 0.975894 0.9756599 0.9826459]]\n", "\n", " [[0.6304559 0.6284427 0.6287045 ... 0.6810626 0.6809573 0.6818709]\n", " [1.2285622 1.2205907 1.2190002 ... 1.2729124 1.2727222 1.2755645]]], 'volt')>\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 184B 'S1D2' 'S1D3' 'S2D1' ... 'S8D8' 'S8D16'\n", " source (channel) object 184B 'S1' 'S1' 'S2' 'S2' ... 'S8' 'S8' 'S8'\n", " detector (channel) object 184B 'D2' 'D3' 'D1' 'D3' ... 'D7' 'D8' 'D16'\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", "Attributes:\n", " data_type_group: unprocessed raw
<xarray.DataArray (channel: 13, wavelength: 2, time: 23239)> Size: 5MB\n", "<Quantity([[[0.227516 0.2297024 0.2261366 ... 0.2264519 0.2271665 0.226713 ]\n", " [0.6354927 0.637668 0.6298023 ... 0.6072068 0.6087293 0.6091066]]\n", "\n", " [[0.1064704 0.1066212 0.1053444 ... 0.121114 0.1205022 0.1205441]\n", " [0.2755033 0.2761615 0.2727006 ... 0.2911952 0.2900544 0.2909847]]\n", "\n", " [[0.5512474 0.5510672 0.5476283 ... 0.6179242 0.6188702 0.6187721]\n", " [1.125532 1.1238331 1.1119423 ... 1.1817728 1.1819598 1.1832658]]\n", "\n", " ...\n", "\n", " [[0.3463254 0.3424951 0.3408207 ... 0.3929267 0.3941368 0.3945422]\n", " [0.6978315 0.6875081 0.6857653 ... 0.7259991 0.7271688 0.7292138]]\n", "\n", " [[0.2225884 0.2187791 0.2195495 ... 0.2564863 0.2551258 0.2560233]\n", " [0.3994258 0.3917637 0.389261 ... 0.4304597 0.430814 0.4331249]]\n", "\n", " [[0.2027881 0.1996586 0.2004866 ... 0.2318743 0.2311941 0.2330808]\n", " [0.4666358 0.4554404 0.4561614 ... 0.4809749 0.4812827 0.4862896]]], 'volt')>\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 104B 'S1D2' 'S1D3' 'S2D1' ... 'S7D7' 'S8D7'\n", " source (channel) object 104B 'S1' 'S1' 'S2' 'S2' ... 'S6' 'S7' 'S8'\n", " detector (channel) object 104B 'D2' 'D3' 'D1' 'D3' ... 'D7' 'D7' 'D7'\n", " * wavelength (wavelength) float64 16B 760.0 850.0\n", "Attributes:\n", " data_type_group: unprocessed raw