Repository: nilearn/nilearn

Issue:
"UserWarning: It seems you have created more than 10 nilearn views." But no way to delete nilearn views
When more than 10 _html_view_ objects via `html_view = nilearn.plotting.view_img()` in a loop, you will see the warning:

> UserWarning: It seems you have created more than 10 nilearn views. As each view uses dozens of megabytes of RAM, you might want to delete some of them.

This is a helpful warning, but it doesn't give the user any way to deal with the problem, as there doesn't seem to be any documented way to free the memory associated with these objects. There is also no clean way (besides e.g. https://stackoverflow.com/questions/5644836/in-python-how-does-one-catch-warnings-as-if-they-were-exceptions/30368735) to disable the warning for users that are sure they have sufficient memory, and/or don't wish to have the console spammed.

It is also not clear to me from the [source code](https://github.com/nilearn/nilearn/blob/2df00ffb1826550b9c8e6497c7c67756e2188039/nilearn/plotting/js_plotting_utils.py#L102) how to work around this: I don't seem to be able to call `html_view._all_open_html_repr.clear()` , `html_view._all_open_html_repr.remove()`, `HTMLDocument._all_open_html_repr.clear()` or etc to manually shrink the size of the `WeakRef` object which has the length causing the error message, although this may be due to my lack of familiarity with these objects. 

Any workaround would be appreciated.


Discussion:
there are two things that can use some memory: the nilearn views themselves (the python objects), although I don't think these are very big, and the browser windows in which views are displayed, eg if you called `open_in_browser`, which can be quite big for surface plots. for the browser windows, you need to close them manually; nilearn provides no way to do it. for the python objects, as usual, if you don't keep a reference to them they will be garbage collected and you can also explicitly `del` them.

as you said, it is quite easy to silence a particular warning in python with `catch_warnings` or with filters:
https://docs.python.org/3.7/library/warnings.html#the-warnings-filter

but it is true that it would be better to be able to control the number of views that triggers the warning or disable it altogether; I'll open a PR and link it here

thanks!

Resolving patch:
diff --git a/nilearn/plotting/__init__.py b/nilearn/plotting/__init__.py
--- a/nilearn/plotting/__init__.py
+++ b/nilearn/plotting/__init__.py
@@ -45,6 +45,7 @@ def _set_mpl_backend():
 from .html_stat_map import view_img
 from .html_connectome import view_connectome, view_markers
 from .surf_plotting import plot_surf, plot_surf_stat_map, plot_surf_roi
+from .js_plotting_utils import set_max_img_views_before_warning
 
 __all__ = ['cm', 'plot_img', 'plot_anat', 'plot_epi',
            'plot_roi', 'plot_stat_map', 'plot_glass_brain',
@@ -54,4 +55,5 @@ def _set_mpl_backend():
            'view_img', 'view_connectome', 'view_markers',
            'find_parcellation_cut_coords', 'find_probabilistic_atlas_cut_coords',
            'plot_surf', 'plot_surf_stat_map', 'plot_surf_roi',
+           'set_max_img_views_before_warning'
            ]
diff --git a/nilearn/plotting/js_plotting_utils.py b/nilearn/plotting/js_plotting_utils.py
--- a/nilearn/plotting/js_plotting_utils.py
+++ b/nilearn/plotting/js_plotting_utils.py
@@ -24,6 +24,17 @@
 from .._utils.param_validation import check_threshold
 from .. import surface
 
+MAX_IMG_VIEWS_BEFORE_WARNING = 10
+
+
+def set_max_img_views_before_warning(new_value):
+    """Set the number of open views which triggers a warning.
+
+    If `None` or a negative number, disable the memory warning.
+    """
+    global MAX_IMG_VIEWS_BEFORE_WARNING
+    MAX_IMG_VIEWS_BEFORE_WARNING = new_value
+
 
 def add_js_lib(html, embed_js=True):
     """
@@ -99,11 +110,17 @@ def __init__(self, html, width=600, height=400):
 
     def _check_n_open(self):
         HTMLDocument._all_open_html_repr.add(self)
-        if len(HTMLDocument._all_open_html_repr) > 9:
-            warnings.warn('It seems you have created more than 10 '
+        if MAX_IMG_VIEWS_BEFORE_WARNING is None:
+            return
+        if MAX_IMG_VIEWS_BEFORE_WARNING < 0:
+            return
+        if len(HTMLDocument._all_open_html_repr
+               ) > MAX_IMG_VIEWS_BEFORE_WARNING - 1:
+            warnings.warn('It seems you have created more than {} '
                           'nilearn views. As each view uses dozens '
                           'of megabytes of RAM, you might want to '
-                          'delete some of them.')
+                          'delete some of them.'.format(
+                              MAX_IMG_VIEWS_BEFORE_WARNING))
 
     def resize(self, width, height):
         """Resize the plot displayed in a Jupyter notebook."""


Test patch:
diff --git a/nilearn/plotting/tests/test_js_plotting_utils.py b/nilearn/plotting/tests/test_js_plotting_utils.py
--- a/nilearn/plotting/tests/test_js_plotting_utils.py
+++ b/nilearn/plotting/tests/test_js_plotting_utils.py
@@ -327,6 +327,14 @@ def test_open_view_warning():
     # should raise a warning about memory usage
     assert_warns(UserWarning, _open_views)
     assert_no_warnings(_open_one_view)
+    js_plotting_utils.set_max_img_views_before_warning(15)
+    assert_no_warnings(_open_views)
+    js_plotting_utils.set_max_img_views_before_warning(-1)
+    assert_no_warnings(_open_views)
+    js_plotting_utils.set_max_img_views_before_warning(None)
+    assert_no_warnings(_open_views)
+    js_plotting_utils.set_max_img_views_before_warning(6)
+    assert_warns(UserWarning, _open_views)
 
 
 def test_to_color_strings():