tips for interactive teaching using google colab

Dear all,

I am teaching a tutorial for which I use MNE. I would like to use google colab to make it interactive (or another tool of you have a suggestion), but I am new to that one.
Is it now possible to have source plots in colab?

If anyone could share some tips/ examples or a good tutorial on how to set up an environment for the class including notebooks, pre-installed packages (mne, mne bids), and the corresponding data, that would really help me save time.

Thanks!

1 Like

In theory yes doing mne.viz.set_3d_backend("notebook") and using an osmesa version of vtk (see this post if you use pip or with a command like mamba install -c conda-forge "vtk>=9.2=*osmesa*" "mesalib=21.2.5") or regular vtk with xvfb enabled (not sure if google collab allows this?) then 3D plotting functions / coreg etc. should work in a notebook.

3 Likes

Hi @larsoner, I got it to work using the links you provided. Now I am stuck when trying to plot the source spaces with what seems an error when the notebook plotting tries to read viewer.value, apparently from matplotlib’s widget.
If needed, I can share my notebook and data as it is already on gdrive.

src.plot(subjects_dir=bids_derivatives_mri)

Loading /content/gdrive/MyDrive/CNN_MEG_EEG_SHerbst_2023/BIDS_DATA/derivatives/mri/sub-01/surf/rh.white...
Mapping rh sub-01 -> oct (6) ...
    Triangle neighbors and vertex normals...
Loading geometry from /content/gdrive/MyDrive/CNN_MEG_EEG_SHerbst_2023/BIDS_DATA/derivatives/mri/sub-01/surf/rh.sphere...
Setting up the triangulation for the decimated surface...
loaded rh.white 4098/145589 selected to source space (oct = 6)

You are now one step closer to computing the gain matrix
Type a file name
/usr/local/lib/python3.10/dist-packages/pyvista/plotting/plotter.py:6665: UserWarning: Not within a jupyter notebook environment.
Ignoring ``jupyter_backend``.
  warnings.warn(
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-42-80dde18d978e> in <cell line: 14>()
     12                                 add_dist=False)
     13 # plot the source space
---> 14 src.plot(subjects_dir=bids_derivatives_mri)
     15 
     16 # # save the source space

<decorator-gen-408> in plot(self, head, brain, skull, subjects_dir, trans, verbose)

2 frames
<decorator-gen-280> in plot_alignment(info, trans, subject, subjects_dir, surfaces, coord_frame, meg, eeg, fwd, dig, ecog, src, mri_fiducials, bem, seeg, fnirs, show_axes, dbs, fig, interaction, sensor_colors, verbose)

/usr/local/lib/python3.10/dist-packages/mne/viz/backends/_notebook.py in show(self)
   1600                 # value taken from matplotlib's widget
   1601                 r" style='\1; border: 1px solid rgb(221,221,221);' scrolling='no'></iframe>",  # noqa: E501
-> 1602                 viewer.value,
   1603             )
   1604         rendering_row = list()

AttributeError: 'NoneType' object has no attribute 'value'
1 Like

Marginally related:
Maybe a bunch of us can team up and set up & maintain a publicly hosted Jupyter or Colab install of MNE that can be used in situations like this. I’d be willing to help out here.

A Docker image could make things much simpler.

I also switched all my development environments to using Dev Containers; MNE is still an exception b/c of Qt-based plotting. But I hadn’t considered that we do support Notebooks to some extent. So maybe I should just share my Dev Container and we can iterate from there?

@Sophie What’s the specific reason you want to use Colab instead of asking participants to bring their own pre-installed MNE? Is installation still too cumbersome, even with the installers? I know they still have some rough edges but I thought they work fine most of the time…

Thanks and best of luck!
Richard

Hi @richard, the students from last year suggested to use colab after the course. We had some problems getting everyones installations to work (disk size issues, plotting problems, lingering python installations). Although I was quite happy with it as I managed to get everyone from raw to source plots in 3h by myself :wink:

1 Like

Argh, that doesn’t sound too great. :frowning:

@cbrnr and I ran into some issues too esp. with Windows users, or a few folks who ran a very old version of macOS. Linux was mostly fine, I believe …

I think many of our problems with installing could be addressed by giving students some pre-course to-do list, e.g.

  • Make sure you have a supported OS (i.e. the minimum version or later).
  • Check your free disk space, you need at least e.g. 20GB (or whatever space we think is a good amount).
  • Windows and macOS: download and install MNE with the official installers.
  • Linux: use pip or conda.
  • Install Visual Studio Code (or anything else needed in the course that’s not included with the installers).

Providing some environment where everything works out of the box is certainly nice for a short workshop, but you’re completely missing out on teaching how to set up a working environment. They will need to do that later anyway, so I prefer to include this as part of the workshop. But yes, it usually takes way too long, but if they prepare before the workshop, it should be much faster.

1 Like

Another issue we ran into, IIRC, was that we didn’t tell students to download the sample data beforehand. And then the WiFi basically collapsed … and we started running around with USB thumb drives. Ouch.

True! So with some advance preparation, we would have avoided most issues (and could have used 3+ hours to showcase actual MNE content).

1 Like

For the longer, hackathon-like sessions, we always have people install MNE on their machine in advance, but the current course is embedded in a series of different methods, so I wanted to keep preparation minimal for the students. But I definitely overestimated colab.
Another argument for using it was that not every student has a laptop that allows to process a real dataset fast enough (1 run, M/EEG, MRI).

1 Like

Good points, in this case setting up an env might not be worth it.

Re laptop, in my limited experience with Colab, I found that it was pretty slow (at least in the free tier). If a student has a laptop which is even slower, it must be really really old. Ideally, you need to provide computers for students that don’t have laptops (or very old laptops), but I know that this is not always possible.

1 Like

This to me suggests there is some problem with the setup. Using a local jupyter notebook instance you locally get a simplified version of the script to run in notebook mode at least? If so then we can start to figure out what’s different in your collab env/setup compared to your local one

Thanks @larsoner – I have given up on colab as I also did not find a convenient way to share the data with write access for everyone.
I will have everyone install MNE on their machines. @richard I’ll let you know how it goes!

1 Like

I’ll keep my fingers crossed!!!

Be sure to tell your participants that the installation might take tens of minutes to complete, without any visible progress. They should just let it keep running :slight_smile: