AlexsLemonade / OpenPBTA-analysis

The analysis repository for the Open Pediatric Brain Tumor Atlas Project
Other
100 stars 67 forks source link

Add dependency: `python-tk` #1361

Closed sjspielman closed 2 years ago

sjspielman commented 2 years ago

The tp53_nf1_score module relies heavily on Python libraries for running the classifier. In addition, this module makes some plots using matplotlib.

It turns out this module can only run if it's not being run in interactive mode, e.g. from the rocker IDE, because of a matplotlib dependency.. It runs perfectly fine directly from the command line with docker run, but not from within a server session. The problem occurs when importing matplotlib -

import matplotlib.pyplot as plt

Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/__init__.py", line 36, in <module>
    import _tkinter
ImportError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 2372, in <module>
    switch_backend(rcParams["backend"])
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 207, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 1, in <module>
    from . import _backend_tk
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/_backend_tk.py", line 5, in <module>
    import tkinter as Tk
  File "/usr/lib/python3.5/tkinter/__init__.py", line 38, in <module>
    raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package

According to the internet, this can be pretty easily addressed with a ubuntu installation of python3-tk.

Therefore, for the tp53_nf1_score module to run without errors in an interactive session, we will need to add a line installing python3-tk to the Dockerfile.

sjspielman commented 2 years ago

After testing this out, it seems that adding python-tk to the Docker image is not sufficient for the solution. It seems to be necessary to install this ubuntu dependency to avoid import errors, but a new error now occurs:

_tkinter.TclError: couldn't connect to display "unix:0"

Therefore, even with this dependency installed, there are still errors that matplotlib triggers because of how graphics generally work within RStudio Server.

A different workaround is possible, however, as described in this stackoverflow post -

you can use

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

if you dont want to use tkinter at all.

Also dont forget to use %matplotlib inline at the top of your notebook if using one.

EDIT: agg is a different backend like tkinter for matplotlib.

Adding matplotlib.use('agg') to a python script seems to be the way to go.

Note I tested matplotlib.use('agg') a Docker image that does NOT have python-tk installed, and the original tk error returned. Therefore, the solution here requires BOTH: we need to both a) adding the dependency to Docker, and b) setting matplotlib.use('agg') in the relevant python scripts.