{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "EEG leadfield computation - On regular grid\n", "===========================================\n", "\n", "The leadfield matrix can be computed on different source spaces in *shamo*. In this example, we compute it on a regular grid that can be processed as a NIFTI image.\n", "\n", "The first steps are the same as in the previous example." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import logging\n", "import sys\n", "\n", "logger = logging.getLogger(\"shamo\")\n", "handler = logging.StreamHandler(sys.stdout)\n", "handler.setFormatter(logging.Formatter(\"[{levelname}] {message}\", style=\"{\"))\n", "logger.addHandler(handler)\n", "logger.setLevel(logging.INFO)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shamo import FEM\n", "\n", "model = FEM.load(\"../../derivatives/fem_from_labels/fem_from_labels.json\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shamo.eeg import ProbEEGLeadfield\n", "\n", "problem = ProbEEGLeadfield()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "problem.sigmas.set(\"scalp\", 0.4137)\n", "problem.sigmas.set(\"gm\", 0.4660)\n", "problem.sigmas.set(\"wm\", 0.2126)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "problem.reference.add(\"IZ\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "problem.markers.adds([\"NZ\", \"LeftEar\", \"RightEar\"])" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Grid definition\n", "---------------\n", "\n", "The grid is stored as a :py:class:`~shamo.core.problems.single.components.grid_sampler.CompGridSampler` instance. It provides the :py:func:`~shamo.core.problems.single.components.grid_sampler.CompGridSampler.set` method which requires an affine matrix and a shape. An additional mask can be set to reduce the region of interest." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import nibabel as nib\n", "from nilearn.image import crop_img\n", "\n", "img = nib.load(model.nii_path)\n", "mask = img.get_fdata() == 2\n", "mask_img = nib.Nifti1Image(mask.astype(int), img.affine)\n", "mask_img = crop_img(mask_img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "problem.grid.set(\n", " mask_img.affine, \n", " mask_img.shape, \n", " mask=mask_img.get_fdata().astype(bool)\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "solution = problem.solve(\"single_ssp-grid\", \"../../derivatives/eeg_leadfield\", model)" ] } ], "metadata": { "kernelspec": { "display_name": "shamo", "language": "python", "name": "shamo" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }