Animate a series of volumes as a GIFΒΆ

A convenient way to compare two flat maps (e.g., prediction performance or tuning weights) is to flip back and forth between them. This example shows how to make an animated gif in which each frame is a flatmap.

import cortex
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1234)

Create several pycortex Volumes

volumes = {'first': cortex.Volume.random(subject='S1', xfmname='fullhead', vmin=-2, vmax=2, cmap="RdBu_r"),
           'second': cortex.Volume.random(subject='S1', xfmname='fullhead', vmin=-2, vmax=2, cmap="RdBu_r")}

Plot flat maps individually

_ = cortex.quickflat.make_figure(volumes['first'], colorbar_location="right")
_ = cortex.quickflat.make_figure(volumes['second'], colorbar_location="right")
_ = plt.show()
  • plot make gif
  • plot make gif

Out:

Failed to get connection
** (inkscape:5327): CRITICAL **: 01:30:14.896: dbus_g_proxy_new_for_name: assertion 'connection != NULL' failed
** (inkscape:5327): CRITICAL **: 01:30:14.896: dbus_g_proxy_call: assertion 'DBUS_IS_G_PROXY (proxy)' failed
** (inkscape:5327): CRITICAL **: 01:30:14.896: dbus_g_connection_register_g_object: assertion 'connection != NULL' failed
Failed to get connection
** (inkscape:5332): CRITICAL **: 01:30:15.933: dbus_g_proxy_new_for_name: assertion 'connection != NULL' failed
** (inkscape:5332): CRITICAL **: 01:30:15.933: dbus_g_proxy_call: assertion 'DBUS_IS_G_PROXY (proxy)' failed
** (inkscape:5332): CRITICAL **: 01:30:15.933: dbus_g_connection_register_g_object: assertion 'connection != NULL' failed

Generate an animated gif that switches between frames every 1.5 seconds

filename = "./flatmap_comparison.gif"
cortex.quickflat.make_gif(filename, volumes, frame_duration=1.5, colorbar_location="right")

Out:

Failed to get connection
** (inkscape:5337): CRITICAL **: 01:30:19.261: dbus_g_proxy_new_for_name: assertion 'connection != NULL' failed
** (inkscape:5337): CRITICAL **: 01:30:19.261: dbus_g_proxy_call: assertion 'DBUS_IS_G_PROXY (proxy)' failed
** (inkscape:5337): CRITICAL **: 01:30:19.262: dbus_g_connection_register_g_object: assertion 'connection != NULL' failed
Failed to get connection
** (inkscape:5341): CRITICAL **: 01:30:21.048: dbus_g_proxy_new_for_name: assertion 'connection != NULL' failed
** (inkscape:5341): CRITICAL **: 01:30:21.049: dbus_g_proxy_call: assertion 'DBUS_IS_G_PROXY (proxy)' failed
** (inkscape:5341): CRITICAL **: 01:30:21.049: dbus_g_connection_register_g_object: assertion 'connection != NULL' failed

Display gif inline in an IPython notebook

import io
from IPython.display import Image

stream = io.BytesIO()
cortex.quickflat.make_gif(stream, volumes, frame_duration=1.5, colorbar_location="right")

Image(stream.read())

Out:

Failed to get connection
** (inkscape:5347): CRITICAL **: 01:30:23.595: dbus_g_proxy_new_for_name: assertion 'connection != NULL' failed
** (inkscape:5347): CRITICAL **: 01:30:23.595: dbus_g_proxy_call: assertion 'DBUS_IS_G_PROXY (proxy)' failed
** (inkscape:5347): CRITICAL **: 01:30:23.595: dbus_g_connection_register_g_object: assertion 'connection != NULL' failed
Failed to get connection
** (inkscape:5351): CRITICAL **: 01:30:25.381: dbus_g_proxy_new_for_name: assertion 'connection != NULL' failed
** (inkscape:5351): CRITICAL **: 01:30:25.381: dbus_g_proxy_call: assertion 'DBUS_IS_G_PROXY (proxy)' failed
** (inkscape:5351): CRITICAL **: 01:30:25.381: dbus_g_connection_register_g_object: assertion 'connection != NULL' failed

<IPython.core.display.Image object>
../../_images/flatmap_comparison.gif

Total running time of the script: ( 0 minutes 12.570 seconds)

Gallery generated by Sphinx-Gallery