Skip to content

Commit

Permalink
ENH: Close all Matplotlib figures in relevant tests explicitly
Browse files Browse the repository at this point in the history
Close all `Matplotlib` figures in relevant tests explicitly: when the
`output_file` argument is `None` the `plot_carpet` method does not close
figures, and thus a warning is raised if the number of open figures
exceeds a given value.

Use `all` explicitly to guarantee all figures are closed. This change
fixes the warnings that have persisted despite commits 626dd35 and
15f4898 claiming to fix these issues.

Fixes:
```
  nireports/tests/test_reportlets.py::test_nifti_carpetplot
    /home/runner/work/nireports/nireports/nireports/reportlets/nuisance.py:326:
 RuntimeWarning: More than 20 figures have been opened.
 Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are
 retained until explicitly closed and may consume too much memory.
 (To control this warning, see the rcParam `figure.max_open_warning`).
 Consider using `matplotlib.pyplot.close()`.
      figure, allaxes = plt.subplots(figsize=(19.2, 10))
```

and similar warnings.

Raised for example in:
https://github.com/nipreps/nireports/actions/runs/13276183449/job/37066107605#step:14:341
  • Loading branch information
jhlegarreta committed Feb 15, 2025
1 parent 22f3d4f commit 771f87c
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions nireports/tests/test_reportlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_carpetplot(tr, sorting, outdir):
sort_rows=sorting,
)

plt.close()
plt.close("all")


@pytest.mark.parametrize(
Expand Down Expand Up @@ -320,7 +320,7 @@ def test_cifti_carpetplot(tmp_path, test_data_package, outdir):
cmap="paired",
)

plt.close()
plt.close("all")


def test_nifti_carpetplot(tmp_path, test_data_package, outdir):
Expand All @@ -343,6 +343,8 @@ def test_nifti_carpetplot(tmp_path, test_data_package, outdir):
drop_trs=0,
)

plt.close("all")


_views = list(permutations(("axial", "sagittal", "coronal", None), 3)) + [
(v, None, None) for v in ("axial", "sagittal", "coronal")
Expand Down

0 comments on commit 771f87c

Please sign in to comment.