.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/00_encodingdecoding/decode.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_00_encodingdecoding_decode.py: Decoding of stimuli from neural data ============================================= Here we will simulate neural data given a ground truth encoding model and try to decode the stimulus from the data. .. GENERATED FROM PYTHON SOURCE LINES 8-33 .. code-block:: Python # Set up a neural model from braincoder.models import GaussianPRF import numpy as np import pandas as pd import scipy.stats as ss # Set up 100 random of PRF parameters n = 20 n_trials = 50 noise = 1. mu = np.random.rand(n) * 100 sd = np.random.rand(n) * 45 + 5 amplitude = np.random.rand(n) * 5 baseline = np.random.rand(n) * 2 - 1 parameters = pd.DataFrame({'mu':mu, 'sd':sd, 'amplitude':amplitude, 'baseline':baseline}) # We have a paradigm of random numbers between 0 and 100 paradigm = np.ceil(np.random.rand(n_trials) * 100) model = GaussianPRF(parameters=parameters) data = model.simulate(paradigm=paradigm, noise=noise) .. GENERATED FROM PYTHON SOURCE LINES 34-53 .. code-block:: Python # Now we fit back the PRF parameters from braincoder.optimize import ParameterFitter, ResidualFitter fitter = ParameterFitter(model, data, paradigm) mu_grid = np.arange(0, 100, 5) sd_grid = np.arange(5, 50, 5) grid_pars = fitter.fit_grid(mu_grid, sd_grid, [1.0], [0.0], use_correlation_cost=True, progressbar=False) grid_pars = fitter.refine_baseline_and_amplitude(grid_pars) for par in ['mu', 'sd', 'amplitude', 'baseline']: print(f'Correlation grid-fitted parameter and ground truth for *{par}*: {ss.pearsonr(grid_pars[par], parameters[par])[0]:0.2f}') gd_pars = fitter.fit(init_pars=grid_pars, progressbar=False) for par in ['mu', 'sd', 'amplitude', 'baseline']: print(f'Correlation gradient descent-fitted parameter and ground truth for *{par}*: {ss.pearsonr(grid_pars[par], parameters[par])[0]:0.2f}') .. rst-class:: sphx-glr-script-out .. code-block:: none Working with chunk size of 666666 Using correlation cost! 0%| | 0/1 [00:00 .. GENERATED FROM PYTHON SOURCE LINES 91-126 .. code-block:: Python # Let's look at the summary statistics of the posteriors posteriors def get_posterior_stats(posterior, normalize=True): posterior = posterior.copy() posterior = posterior.div(np.trapezoid(posterior, posterior.columns,axis=1), axis=0) # Take integral over the posterior to get to the expectation (mean posterior) E = np.trapezoid(posterior*posterior.columns.values[np.newaxis,:], posterior.columns, axis=1) # Take the integral over the posterior to get the expectation of the distance to the # mean posterior (i.e., standard deviation) sd = np.trapezoid(np.abs(E[:, np.newaxis] - posterior.columns.astype(float).values[np.newaxis, :]) * posterior, posterior.columns, axis=1) stats = pd.DataFrame({'E':E, 'sd':sd}, index=posterior.index) return stats posterior_stats = get_posterior_stats(posterior) # Let's see how far the posterior mean is from the ground truth plt.errorbar(test_paradigm, posterior_stats['E'],posterior_stats['sd'], fmt='o',) plt.plot([0, 100], [0,100], c='k', ls='--') plt.xlabel('Ground truth') plt.ylabel('Mean posterior') # Let's see how the error depends on the standard deviation of the posterior error = test_paradigm - posterior_stats['E'] error_abs = np.abs(error) error_abs.name = 'error' sns.lmplot(x='sd', y='error', data=posterior_stats.join(error_abs)) plt.xlabel('Standard deviation of posterior') plt.ylabel('Objective error') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_002.png :alt: decode :srcset: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_003.png :alt: decode :srcset: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(29.0, 0.5, 'Objective error') .. GENERATED FROM PYTHON SOURCE LINES 127-136 .. code-block:: Python # Now, let's try to find the MAP estimate using gradient descent from braincoder.optimize import StimulusFitter stimulus_fitter = StimulusFitter(model=model, data=test_data, omega=omega) # We start with a very coarse grid search, so we are sure we are in the right ballpark estimated_stimuli_grid = stimulus_fitter.fit_grid(np.arange(1, 100, 5)) .. GENERATED FROM PYTHON SOURCE LINES 137-148 .. code-block:: Python # We can then refine the estimate using gradient descent estimated_stimuli_gd = stimulus_fitter.fit(init_pars=estimated_stimuli_grid, progressbar=False) # Let's see how well we did plt.scatter(test_paradigm, estimated_stimuli_grid, alpha=.5, label='MAP (grid search)') plt.scatter(test_paradigm, estimated_stimuli_gd, alpha=.5, label='MAP (gradient descent)') plt.scatter(test_paradigm, posterior_stats['E'], alpha=.5, label='Mean posterior') plt.plot([0, 100], [0,100], c='k', ls='--', label='Identity line') plt.legend() # %% .. image-sg:: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_004.png :alt: decode :srcset: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/1000 [00:00 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 32.790 seconds) .. _sphx_glr_download_auto_examples_00_encodingdecoding_decode.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: decode.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: decode.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: decode.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_