mne-tools / mne-tools/mne-python

Error showing plot with reticulate

Open
#9,833 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

BUG
Dominant language
Python
Stars
3.5k
Forks
1.6k
Avg merge
1d 6h
Merged PRs (30d)
100

Description

I'm using the reticulate R package to create an R Markdown file with Python code cells. Here's a minimal test.Rmd file that shows how to produce a Matplotlib figure:

---
title: Test
output: html_document
---

```{python}
import matplotlib.pyplot as plt

plt.plot([0, 1, 2], "x")
```

However, I get an error when I try to plot sensor locations:

---
title: Test
output: html_document
---

```{python}
import mne

mne.channels.make_standard_montage("easycap-M1").plot()
```
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  IndexError: list index out of range

Detailed traceback:
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/mne/channels/montage.py", line 201, in plot
    return plot_montage(self, scale_factor=scale_factor,
  File "<decorator-gen-166>", line 24, in plot_montage
  File "/usr/local/lib/python3.9/site-packages/mne/viz/montage.py", line 74, in plot_montage
    collection = fig.axes[0].collections[0]
Calls: <Anonymous> ... py_capture_output -> force -> <Anonymous> -> py_call_impl
Execution halted

It seems like this is related to show=True (the default), because the following modification works just fine and produces the figure:

---
title: Test
output: html_document
---

```{python}
import mne
import matplotlib.pyplot as plt

fig = mne.channels.make_standard_montage("easycap-M1").plot(show=False)
plt.show(fig)
```

Any ideas if there could be something wrong with the way we are forcing figures to show? Or is this rather a problem related to reticulate? I have no idea what fig.axes[0].collections[0] is actually doing, why it is necessary here, and why it fails when used with reticulate (but works otherwise).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the supplied test.Rmd examples, then inspect mne/channels/montage.py and mne/viz/montage.py around plot_montage and the fig.axes[0].collections[0] access. Compare the show=True and show=False paths under reticulate; done means the easycap-M1 montage renders from the direct plot call without the workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.