.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/00_encodingdecoding/invert_model.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_invert_model.py: Invert encoding model ============================================= Here we invert different encoding models .. GENERATED FROM PYTHON SOURCE LINES 8-31 .. code-block:: Python import scipy.stats as ss import matplotlib.pyplot as plt import seaborn as sns import numpy as np plt.title('Hypothetical RF + response') x = np.linspace(0, 2*np.pi) dist = ss.vonmises(loc=.5*np.pi, kappa=1.) plt.plot(x, dist.pdf(x), c='k', ls='--', label='Receptive field') y =dist.pdf(7./8.*np.pi) plt.plot(x, np.ones_like(x)*y, c='k', ls='-') plt.fill_between(x, y-0.025, y+0.025, alpha=.25, color='k', label='Measured activity') plt.scatter(1./8.*np.pi, y, c='k') plt.scatter(7./8.*np.pi, y, c='k') plt.xticks([0.0, .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi], [r'0', r'$\frac{1}{2}\pi$', r'$\pi$', r'$\frac{3}{2}\pi$', r'$2\pi$']) sns.despine() plt.legend() .. image-sg:: /auto_examples/00_encodingdecoding/images/sphx_glr_invert_model_001.png :alt: Hypothetical RF + response :srcset: /auto_examples/00_encodingdecoding/images/sphx_glr_invert_model_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 32-46 .. code-block:: Python # We set up a simple VonMisesPRF model from braincoder.models import VonMisesPRF import pandas as pd import numpy as np # Set up six evenly spaced von Mises PRFs parameters = pd.DataFrame([{'mu':0.5*np.pi, 'kappa':1., 'amplitude':1.0, 'baseline':0.0}]).astype(np.float32) weights = np.array([[1]]).astype(np.float32) model = VonMisesPRF(parameters=parameters, weights=weights) omega = np.array([[0.1]]).astype(np.float32) data = pd.DataFrame([y]).astype(np.float32) .. GENERATED FROM PYTHON SOURCE LINES 47-60 .. code-block:: Python # Evaluate the likelihood of different possible orientations orientations = np.linspace(0.0, 2*np.pi).astype(np.float32) likelihood = model.likelihood(orientations, data, parameters, weights, omega) # And plot it.. plt.figure() plt.plot(orientations, likelihood.T, c='k') plt.xticks([0.0, .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi], [r'0', r'$\frac{1}{2}\pi$', r'$\pi$', r'$\frac{3}{2}\pi$', r'$2\pi$']) sns.despine() plt.xlabel('Orientation') plt.ylabel('Likelihood') .. image-sg:: /auto_examples/00_encodingdecoding/images/sphx_glr_invert_model_002.png :alt: invert model :srcset: /auto_examples/00_encodingdecoding/images/sphx_glr_invert_model_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(16.347222222222214, 0.5, 'Likelihood') .. GENERATED FROM PYTHON SOURCE LINES 61-87 .. code-block:: Python # Simulate two-RF model palette = sns.color_palette() plt.title('Hypothetical RF + response') x = np.linspace(0, 2*np.pi) dist1 = ss.vonmises(loc=.5*np.pi, kappa=.5) dist2 = ss.vonmises(loc=1.*np.pi, kappa=.5) plt.plot(x, dist1.pdf(x), ls='--', label='Receptive field 1', color=palette[0]) plt.plot(x, dist2.pdf(x), ls='--', label='Receptive field 2', color=palette[1]) y1 =dist1.pdf(7./8.*np.pi) y2 =dist2.pdf(7./8.*np.pi) plt.plot(x, np.ones_like(x)*y1, c=palette[0], ls='-') plt.plot(x, np.ones_like(x)*y2, c=palette[1], ls='-') plt.fill_between(x, y1-0.025, y1+0.025, alpha=.25, color=palette[0], label='Measured activity RF1') plt.fill_between(x, y2-0.025, y2+0.025, alpha=.25, color=palette[1], label='Measured activity RF2') plt.xticks([0.0, .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi], [r'0', r'$\frac{1}{2}\pi$', r'$\pi$', r'$\frac{3}{2}\pi$', r'$2\pi$']) sns.despine() plt.legend() .. image-sg:: /auto_examples/00_encodingdecoding/images/sphx_glr_invert_model_003.png :alt: Hypothetical RF + response :srcset: /auto_examples/00_encodingdecoding/images/sphx_glr_invert_model_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 88-112 .. code-block:: Python # Set up 2-dimensional model to invert parameters = pd.DataFrame([{'mu':0.5*np.pi, 'kappa':.5, 'amplitude':1.0, 'baseline':0.0}, {'mu':1.*np.pi, 'kappa':.5, 'amplitude':1.0, 'baseline':0.0}]).astype(np.float32) model = VonMisesPRF(parameters=parameters) omega = np.array([[0.05, 0.0], [0.0, 0.05]]).astype(np.float32) dist1 = ss.vonmises(loc=.5*np.pi, kappa=.5) dist2 = ss.vonmises(loc=1.*np.pi, kappa=.5) x = 7./8.*np.pi y1 =dist1.pdf(x) y2 =dist2.pdf(x) data = pd.DataFrame([[y1, y2]]).astype(np.float32) likelihood = model.likelihood(orientations, data, parameters, None, omega) plt.plot(orientations, likelihood.T, c='k') plt.xticks([0.0, .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi], [r'0', r'$\frac{1}{2}\pi$', r'$\pi$', r'$\frac{3}{2}\pi$', r'$2\pi$']) sns.despine() plt.xlabel('Orientation') plt.ylabel('Likelihood') # %% .. image-sg:: /auto_examples/00_encodingdecoding/images/sphx_glr_invert_model_004.png :alt: invert model :srcset: /auto_examples/00_encodingdecoding/images/sphx_glr_invert_model_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(25.097222222222214, 0.5, 'Likelihood') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.903 seconds) .. _sphx_glr_download_auto_examples_00_encodingdecoding_invert_model.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: invert_model.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: invert_model.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: invert_model.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_