Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Close all Matplotlib figures in relevant tests explicitly #167

Merged
merged 1 commit into from
Feb 16, 2025

Conversation

jhlegarreta
Copy link
Collaborator

@jhlegarreta jhlegarreta commented Feb 12, 2025

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

Copy link

codecov bot commented Feb 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 66.41%. Comparing base (22f3d4f) to head (771f87c).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #167      +/-   ##
==========================================
+ Coverage   66.04%   66.41%   +0.37%     
==========================================
  Files          25       25              
  Lines        2677     2677              
  Branches      421      421              
==========================================
+ Hits         1768     1778      +10     
+ Misses        793      788       -5     
+ Partials      116      111       -5     
Flag Coverage Δ
unittests 61.37% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jhlegarreta
Copy link
Collaborator Author

Oh, does not fix issues, and the ones in 15f4898 are still there:
https://github.com/nipreps/nireports/actions/runs/13277357141/job/37069296326?pr=167#step:14:343

Not sure how to fix this otherwise. Marking as draft.

@jhlegarreta jhlegarreta marked this pull request as draft February 12, 2025 03:29
@jhlegarreta jhlegarreta force-pushed the CloseFiguresExplicitly3 branch 2 times, most recently from 524458c to cb97b95 Compare February 15, 2025 08:05
@jhlegarreta jhlegarreta marked this pull request as ready for review February 15, 2025 08:05
@jhlegarreta jhlegarreta changed the title ENH: Close figures in test explicitly ENH: Close all Matplotlib figures in relevant tests explicitly Feb 15, 2025
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
@oesteban
Copy link
Member

Perhaps a safer way of doing this is a fixture that ensures figures are closed after every test.

That said, this one effectively resolves the issue. I'll merge and leave the safer option for our future selves.

Thanks!

@oesteban oesteban merged commit 8a69b48 into nipreps:main Feb 16, 2025
16 checks passed
@jhlegarreta jhlegarreta deleted the CloseFiguresExplicitly3 branch February 16, 2025 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants