Skip to content

ENH: Use Qt backend for doc build - #10359

Merged
larsoner merged 16 commits into
mne-tools:mainfrom
larsoner:pg_scraper
Feb 20, 2022
Merged

larsoner merged 16 commits into
mne-tools:mainfrom
larsoner:pg_scraper

Conversation

@larsoner

Copy link
Copy Markdown
Member

@marsipu I can see the gc failure with the following in the doc folder:

$ make clean && PATTERN=15_inplace MNE_SKIP_INSTANCE_ASSERTIONS="brain,_Renderer,Plotter,BackgroundPlotter,vtkPolyData" make html_dev-pattern-memory
loading intersphinx inventory from https://www.fatiando.org/pooch/latest/objects.inv...
intersphinx inventory has moved: https://dipy.org/documentation/latest/objects.inv/ -> https://dipy.org/documentation/1.4.1./objects.inv/
generating gallery...
Using Sphinx-Gallery to convert rst text blocks to markdown for .ipynb files.
Clean    8.8 s : 442.5 MBto_tutorials/intro... [ 28%] 15_inplace.py                                                                                                                 

Traceback (most recent call last):
  File "/home/larsoner/python/sphinx/sphinx/events.py", line 101, in emit
    results.append(listener.handler(self.app, *args))
  File "/home/larsoner/python/sphinx-gallery/sphinx_gallery/gen_gallery.py", line 464, in generate_gallery_rst
    generate_dir_rst(src_dir, target_dir, gallery_conf,
  File "/home/larsoner/python/sphinx-gallery/sphinx_gallery/gen_rst.py", line 399, in generate_dir_rst
    intro, title, cost = generate_file_rst(
  File "/home/larsoner/python/sphinx-gallery/sphinx_gallery/gen_rst.py", line 1030, in generate_file_rst
    clean_modules(gallery_conf, fname, 'after')
  File "/home/larsoner/python/sphinx-gallery/sphinx_gallery/scrapers.py", line 596, in clean_modules
    reset_module(gallery_conf, fname, when=when)
  File "/home/larsoner/python/mne-python/doc/conf.py", line 346, in __call__
    _assert_no_instances(PyQtGraphBrowser, when)
  File "/home/larsoner/python/mne-python/mne/utils/misc.py", line 368, in _assert_no_instances
    assert n == 0, f'\n{n} {cls.__name__} @ {when}:\n' + '\n'.join(ref)
AssertionError: 
1 PyQtGraphBrowser @ mne/conf.py:Resetter.__call__:after:15_inplace.py:
dict: len=16, 8 referrers: dict/frame/list/module
dict: len=2, 4 referrers: LoadThread/dict/list
method: <bound method PyQtGraphBrowser.change_duration of <mne_qt_browser._pg_figure.PyQtGraphBrowser object
method: <bound method PyQtGraphBrowser.change_duration of <mne_qt_browser._pg_figure.PyQtGraphBrowser object
method: <bound method PyQtGraphBrowser.change_nchan of <mne_qt_browser._pg_figure.PyQtGraphBrowser object at
method: <bound method PyQtGraphBrowser.change_nchan of <mne_qt_browser._pg_figure.PyQtGraphBrowser object at
method: <bound method PyQtGraphBrowser.scale_all of <mne_qt_browser._pg_figure.PyQtGraphBrowser object at 0x
method: <bound method PyQtGraphBrowser.scale_all of <mne_qt_browser._pg_figure.PyQtGraphBrowser object at 0x
dict: len=5, 4 referrers: AnnotationDock/dict/list
...

It gives us at least some debugging leads on what could be causing gc not to be able to collect the PyQtGraphBrowser instances.

@marsipu

marsipu commented Feb 17, 2022

Copy link
Copy Markdown
Member

Oh, this seems complicated. There is no immediate idea I get when looking at this.
PyQtBrowser has references of all items either stored as attributes or as attributes of BrowserParams (self.mne). Actually I never asked you what the initial design idea of this BrowserParams as self.mne was. Sometimes I set attributes like LoadThread directly because I thougt self.mne is only attributes common to pyqtgraph and matplotlib.

I thought, when a PyQtBrowser-object is garbage-collected all attributes should be garbace-collected too. Or what does this mean with the methods above?

@larsoner

Copy link
Copy Markdown
Member Author

I thought, when a PyQtBrowser-object is garbage-collected all attributes should be garbace-collected too. Or what does this mean with the methods above?

Yes in theory this should happen, the python garbage collector should recognize when all references are gone, or when the only remaining ones are circular. But once you start interacting with C++ libraries it becomes harder. @GuillaumeFavelier for example has had to do a bunch of gymnastics with the Brain class to take care of the interactions with VTK. I think we might end up having to do the same thing with mne-qt-browser.

The easiest way to get to the bottom of it is to first come up with some minimal example, which we more or less have with the command above. Then, if it's possible to make the PyQtGraphBrowser object do as little as possible by removing almost all functionality and attributes, that's the best first step. Hopefully that doesn't show the problem. If that's the case, then functionality / attributes / connections can be restored little by little to see where the problem is. Any problematic things can be dealt with in a cleanup that explicitly removes references, e.g.:

@safe_event
def _clean(self):
# resolve the reference cycle
self.clear_glyphs()
self.remove_annotations()
# clear init actors
for hemi in self._hemis:
self._layered_meshes[hemi]._clean()
self._clear_callbacks()
self._clear_widgets()
if getattr(self, 'mpl_canvas', None) is not None:
self.mpl_canvas.clear()
if getattr(self, 'act_data_smooth', None) is not None:
for key in list(self.act_data_smooth.keys()):
self.act_data_smooth[key] = None
# XXX this should be done in PyVista
for renderer in self._renderer._all_renderers:
renderer.RemoveAllLights()
# app_window cannot be set to None because it is used in __del__
for key in ('lighting', 'interactor', '_RenderWindow'):
setattr(self.plotter, key, None)
# Qt LeaveEvent requires _Iren so we use _FakeIren instead of None
# to resolve the ref to vtkGenericRenderWindowInteractor
self.plotter._Iren = _FakeIren()
if getattr(self.plotter, 'picker', None) is not None:
self.plotter.picker = None
# XXX end PyVista
for key in ('plotter', 'window', 'dock', 'tool_bar', 'menu_bar',
'interactor', 'mpl_canvas', 'time_actor',
'picked_renderer', 'act_data_smooth', '_scalar_bar',
'actions', 'widgets', 'geo', '_data'):
setattr(self, key, None)
self._cleaned = True

@GuillaumeFavelier

GuillaumeFavelier commented Feb 18, 2022 •

Copy link
Copy Markdown
Contributor

Just adding that the _clean() function above is called in MainWindow.closeEvent() (if that can help).

@larsoner

Copy link
Copy Markdown
Member Author

@marsipu I'm working on this a little bit now. FYI you'll need to be on the branch from this PR to properly test:

sphinx-gallery/sphinx-gallery#917

@larsoner

Copy link
Copy Markdown
Member Author

Possible solution in mne-tools/mne-qt-browser#61 -- @marsipu I'm out of time for the day, but if you merge that, feel free to push a commit here with [circle full] and we can see if it builds successfully (CircleCI should use latest mne-qt-browser:main)!

Comment thread .circleci/config.yml Resolved
Comment thread .circleci/config.yml Resolved
Comment thread .circleci/config.yml Resolved
Comment thread tools/circleci_dependencies.sh Resolved
Comment thread tools/circleci_dependencies.sh Resolved
Comment thread .circleci/config.yml Resolved
@larsoner larsoner changed the title WIP: Expose the Qt scraping problem ENH: Use Qt backend for doc build Feb 20, 2022
@larsoner
larsoner marked this pull request as ready for review February 20, 2022 19:15
@larsoner larsoner added this to the 1.0 milestone Feb 20, 2022
@larsoner

Copy link
Copy Markdown
Member Author

@larsoner

Copy link
Copy Markdown
Member Author

I'm actually going to put this in since @marsipu is happy already. That way, we can get things built and have tomorrow and Tues to look them over. But @agramfort or others if you have comments, feel free to make them here and I can make a follow-up PR!

@larsoner
larsoner merged commit 01b4bf2 into mne-tools:main Feb 20, 2022
@larsoner
larsoner deleted the pg_scraper branch February 20, 2022 22:46
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.

3 participants