{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "EEG parametric leadfield sensitivity analysis\n", "=============================================\n", "\n", "As for the surrogate model in the previous example, we have to build a simple model from the parametric solution generated before. Since the goal of this example is to compute the Sobol sensitivity indices, we have to produce a scalar value representing the full matrix and then build the surrogate model.\n", "\n", "The first steps are the same as before." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shamo.eeg import SolParamEEGLeadfield\n", "\n", "solution = SolParamEEGLeadfield.load(\"../../derivatives/eeg_leadfield/parametric_ssp-grid/parametric_ssp-grid.json\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shamo.eeg import SolEEGLeadfield\n", "\n", "reference = SolEEGLeadfield.load(\"../../derivatives/eeg_leadfield/single_ssp-grid/single_ssp-grid.json\")" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "We then define the metric function that takes both a reference matrix and another matrix to compare to. This function must return a single scalar value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "def metric(ref, mat):\n", " return np.linalg.norm(ref - mat, \"fro\")" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Next, we use the :py:class:`~shamo.eeg.leadfield.surrogate.SurrEEGLeadfieldToRef` class to build such model and fit it with the metric function as a parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shamo.eeg import SurrEEGLeadfieldToRef\n", "\n", "surrogate = SurrEEGLeadfieldToRef.fit(\"eeg_param_leadfield_ssp-dist_surr-toref\", \"../../derivatives/eeg_leadfield/\", solution, ref=reference, metric=metric)" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "In the end, we simply have to compute the Sobol indices for this model. They are both returned as a dictionary and stored as a JSON file in the directory of the object.\n", "\n", ".. note::\n", "\n", " This method computes the first, second and total Sobol sensitivity indices for the surrogate model with regard to all the varying parameters." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s_i = surrogate.gen_sobol()" ] } ], "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 }