.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/00_encodingdecoding/decode_visual.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_visual.py: Fit a 2D PRF model ============================================= Here we fit a 2D PRF model to data from the Szinte (2024)-dataset. .. GENERATED FROM PYTHON SOURCE LINES 8-35 .. code-block:: Python import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from IPython.display import HTML, display from braincoder.utils.data import load_szinte2024 import numpy as np import pandas as pd # Load data data = load_szinte2024() stimulus = data['stimulus'] grid_coordinates = data['grid_coordinates'] # Set up a function to draw a single frame def update(frame): plt.clf() # Clear the current figure plt.imshow(stimulus[frame, :, :].T, cmap='viridis') plt.title(f"Frame {frame}") # Create the animation fig = plt.figure() ani = FuncAnimation(fig, update, frames=range(stimulus.shape[0]), interval=100) # Convert to HTML for easy display HTML(ani.to_html5_video()) .. image-sg:: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_visual_001.png :alt: Frame 149 :srcset: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_visual_001.png :class: sphx-glr-single-img .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 36-56 .. code-block:: Python # Set up a PRF model # Now we will set up fake PRFs just to show how the data is structured # We make a 9-by-9 grid of simulated PRFs x, y = np.meshgrid(np.linspace(-6, 6, 3), np.linspace(-4, 4, 3)) # Set them up in a parameter table # All PRFs have the same baseline and amplitude from braincoder.models import GaussianPRF2DWithHRF from braincoder.hrf import SPMHRFModel parameters = pd.DataFrame({'x':x.ravel(), 'y':y.ravel(), 'sd':2.5, 'baseline':0.0, 'amplitude':1.}).astype(np.float32) model = GaussianPRF2DWithHRF(grid_coordinates=grid_coordinates, paradigm=stimulus, parameters=parameters, hrf_model=SPMHRFModel(tr=data['tr'])) .. GENERATED FROM PYTHON SOURCE LINES 57-67 .. code-block:: Python # Let's plot all the RFs rfs = model.get_rf(as_frame=True) for i, rf in rfs.groupby(level=0): plt.subplot(3, 3, i+1) plt.title(f'RF {i+1}') plt.imshow(rf.unstack('y').loc[i].T) plt.axis('off') .. image-sg:: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_visual_002.png :alt: RF 1, RF 2, RF 3, RF 4, RF 5, RF 6, RF 7, RF 8, RF 9 :srcset: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_visual_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-76 .. code-block:: Python # We simulate data for the given paradigm and parameters and plot the resulting time series import seaborn as sns data = model.simulate(noise=1.) data.columns.set_names('voxel', inplace=True) tmp = data.stack().to_frame('activity') sns.relplot(x='frame', y='activity', data=tmp.reset_index(), hue='voxel', kind='line', palette=sns.color_palette('tab10', n_colors=parameters.shape[0]), aspect=2.) .. image-sg:: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_visual_003.png :alt: decode visual :srcset: /auto_examples/00_encodingdecoding/images/sphx_glr_decode_visual_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 77-121 .. code-block:: Python # We can also fit parameters back to data from braincoder.optimize import ParameterFitter # We set up a parameter fitter par_fitter = ParameterFitter(model, data, stimulus) # We set up a grid of parameters to search over x = np.linspace(-8, 8, 20) y = np.linspace(-4, 4, 20) sd = np.linspace(1, 5, 10) # For now, we only use one amplitude and baseline, because we # use a correlation cost function, which is indifferent to # the overall scaling of the model # We can easily estimate these later using OLS amplitudes = [1.0] baseline = [0.0] # Note that the grids should be given in the correct order (can be found back in # model.parameter_labels) grid_pars = par_fitter.fit_grid(x, y, sd, baseline, amplitudes, use_correlation_cost=True) # Once we have the best parameters from the grid, we can optimize the baseline # and amplitude refined_grid_pars = par_fitter.refine_baseline_and_amplitude(grid_pars) # We get the explained variance of these parameters from braincoder.utils import get_rsq refined_grid_r2 = get_rsq(data, model.predict(parameters=refined_grid_pars)) # Now we use gradient descent to further optimize the parameters pars = par_fitter.fit(init_pars=refined_grid_pars, learning_rate=1e-2, max_n_iterations=5000, min_n_iterations=100, r2_atol=0.0001) fitted_r2 = get_rsq(data, model.predict(parameters=pars)) # The fitted R2s tend to be a bit better than the grid R2s display(refined_grid_r2.to_frame('r2').join(fitted_r2.to_frame('r2'), lsuffix='_grid', rsuffix='_fitted')) # The real parameters are very similar to the estimated parameters display(pars.join(parameters, lsuffix='_fit', rsuffix='_true')) .. rst-class:: sphx-glr-script-out .. code-block:: none Working with chunk size of 493827 Using correlation cost! 0%| | 0/1 [00:00

.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 48.202 seconds) .. _sphx_glr_download_auto_examples_00_encodingdecoding_decode_visual.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: decode_visual.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: decode_visual.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_