Python UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure

A sine or cosine is the ABC of plotting. If we can’t plot it, plotting is not possible. Actually, my message (it’s not an error) looks like this:

>>> plt.show()
__main__:1: UserWarning: Matplotlib is
currently using agg, which is a non-GUI backend,
so cannot show the figure.

I’m trying here to browse thought these python matplotlib snippets to see if I can build up my app using anything from them. In plain words, looking for code to steal. But my ABC plot doesn’t work. Allow me to show you how to make it work, as usual, in my CentOS 7.X machine. StackOverflow has this post about the message. What I did then:

## > yum install python3-tk
No package python3-tk available.
Error: Nothing to do
## > pip3 install PyQt5==5.9.2
Collecting PyQt5==5.9.2
Downloading PyQt5-5.9.2-5.9.3-cp35.cp36.cp37-abi3-manylinux1_x86_64.whl (105.3 MB)
|XXXXX| 105.3 MB 152 kB/s
Collecting sip<4.20,>=4.19.4
Downloading sip-4.19.8-cp36-cp36m-manylinux1_x86_64.whl (66 kB)
|XXXXX| 66 kB 18.7 MB/s
Installing collected packages: sip, PyQt5
Successfully installed PyQt5-5.9.2 sip-4.19.8

After the pip3 install, I don’t get anymore the message of the title and my wished sample plots appear in a separated window. Nice!

HOWTO: add Java and CSS to your python dash application

This is a pretty sad first post for February. A lot of things have been going on, so many that I’ve not been able to approach to a computer in a relaxed way. Anyway, here’s my tip of the day. To apply CSS styles and use java on your dash applications, place the files in a specific folder called assets. They will be found and used. Hopefully!

---app.py
---assets/style.css
---assets/picture.gif
---data/data.csv

For Flask you have a link here. That’s all for the moment.

A jupyter notebook in a docker: Jupyter Docker Stacks

Again I’m with python. I want to run as package independent as possible, so I want to run the external python apps in a docker. If we have a jupyter notebook, we can easily run it this way if we have it in a “test” folder. Note that the “test” folde needs to be readable for everyone (777) or you’ll get this error :

tornado.web.HTTPError: HTTP 403: Forbidden

Anyway, this is what I get when I run my docker mapping the test folder:

# > docker run -p 8888:8888 -v "${PWD}"/test:/home/jovyan/work 
jupyter/scipy-notebook
Entered start.sh with args: jupyter lab
Executing the command: jupyter lab
[I DATE ServerApp] jupyter_server_terminals
| extension was successfully linked.
[I DATE ServerApp] jupyterlab
| extension was successfully linked.
[W DATE NotebookApp] 'ip' has moved from NotebookApp
to ServerApp. This config will be passed to ServerApp.
Be sure to update your config before our next release.
[W DATE NotebookApp] 'ip' has moved from NotebookApp
to ServerApp. This config will be passed to ServerApp.
Be sure to update your config before our next release.
[I DATE ServerApp] nbclassic
| extension was successfully linked.
[I DATe ServerApp] Writing Jupyter server cookie secret
to /home/jovyan/.local/share/jupyter/runtime/jupyter_cookie_secret
[I DATE ServerApp] notebook_shim
| extension was successfully linked.
[I DATE ServerApp] notebook_shim
| extension was successfully loaded.
[I DATE ServerApp] jupyter_server_terminals
| extension was successfully loaded.
[I DATE LabApp] JupyterLab extension loaded from
/opt/conda/lib/python3.10/site-packages/jupyterlab
[I DATE LabApp] JupyterLab application directory
is /opt/conda/share/jupyter/lab
[I DATE ServerApp] jupyterlab
| extension was successfully loaded.
[I DATE ServerApp] nbclassic
| extension was successfully loaded.
[I DATE ServerApp] Serving notebooks
from local directory: /home/jovyan
[I DATE ServerApp] Jupyter Server 2.1.0 is running at:
[I DATE ServerApp] http://XXX:8888/lab?token=XXX
[I DATE ServerApp] or http://127.0.0.1:8888/lab?token=XXX
[I DATE ServerApp] Use Control-C to stop this server
and shut down all kernels (twice to skip confirmation).
[C DATE ServerApp]

To access the server, open this file in a browser:
file:///home/jovyan/.local/share/jupyter/runtime/jpserver-7-open.html
Or copy and paste one of these URLs:
http://XXX:8888/lab?token=XXX
or http://127.0.0.1:8888/lab?token=XXX

The Jupyter Docker stack documentation is available here. Happy docking!

A python dash web multiplot example reading CSV data

I previously used plotty to get a python plot on a website. It’s time to elaborate. Now we want to have two plots with data from a CSV file. It’s not as complicated as it sounds. Allow me to show and explain.

import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd

app = dash.Dash(__name__)

df = pd.read_csv('data3colums.csv')
##columns are named here and you need to use these names
df.columns=['Time','data','Size(T)']
fig = px.line(df, x="Time",y="data",markers=True)
fig2 = px.line(df, x="Time",y="Size(T)",markers=True)
app.layout = html.Div(children=[
# All elements from the top of the page
html.Div([
html.H1(children='Time series one'),
html.Div(children='''Title one'''),
dcc.Graph(id='graph1',figure=fig),
]),
# New Div for the new 'row' of the page
html.Div([
html.H1(children='Time Series two'),
html.Div(children='''Title two'''),
dcc.Graph(id='graph2',figure=fig2),
]),
])
if __name__ == '__main__':
app.run_server(debug=True,host='IP-OF-MY-MACHINE')

If you want to know more about how to plot data from CSV here you have the plotly documetation. It can be useful also to know about plotting time series, since at least in my CSV I have time tags. And here you have the stackoverflow post about this topic. Because we can’t forget about stackoverflow.

EDIT: this post has been backdated to “yesterday” because I forgot to publish it, so another post may come later. Hopefully…this will mean I had time for my things πŸ˜‰

Python Import Error: can’t import name gcd from fractions

Deprecation is a big issue in python. I’m in need of Molecular Dynamic (MD) simulations tools. The error above comes from one tool I already posted about, called LipIDens, more specifically, it’s a complain thrown away by vermouth. Vermouth (for VERsatile, MOdular, and Universal Tranformation Helper) isΒ also a drink and the python library that powers Martinize2. The vermouth source comes here. It is supposed to be used to apply transformation on molecular structures. Which means I don’t really know what it does! Anyway, my error reads

Installed 
/usr/local/lib/python3.9/site-packages/lipidens-1.0.0-py3.9.egg
Processing dependencies for lipidens==1.0.0
error: networkx 3.0 is installed but
networkx~=2.0 is required by {'vermouth'}

What to do here? I found the solution and the explanation once more on StackOverflow. It’s very interesting to know that the the grammar for a mathematical library changed after Python 3.5. So then, why on the LipIDens documentation it is recommended to use a python above 3.9? I’m going to leave the answer to this question open (old developer environments with remnants or insufficient tests) and show you my solution. We install a specific python package. I choose pip to install it instead of conda because it goes to my python site-packages, which I personally consider a more elegant solution. Here you have my output:

bash-5.1# pip install networkx==2.5
Collecting networkx==2.5
Downloading networkx-2.5-py3-none-any.whl (1.6 MB)
|XXXXXXX| 1.6 MB 4.3 MB/s
Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.9/site-packages (from networkx==2.5) (5.1.1)
Installing collected packages: networkx
Attempting uninstall: networkx
Found existing installation: networkx 2.0
Uninstalling networkx-2.0:
Successfully uninstalled networkx-2.0
Successfully installed networkx-2.5
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
bash-5.1# python setup.py install

After the pip install over my local python I run again the LipIDens installer and it works. Another issue is to get meaningful results from the program! BTW, I decided to keep writing my bits thanks to some good feedback that I was missing before… thank you guys. I appreciate it.

ModuleNotFoundError: No module named ‘absl’ while using python

This is a very specific error. We have quite a mess-up python setup with multiple versions, network modules and local, a SLURM cluster, and the option to install your own, so it’s quite tricky to track the origin of a module. But it helps if I have two servers with the same kernel and packages, one of them the program runs, but not in the other. I do have logs, but they are not very clear. Message says

ModuleCmd_Load.c(213):ERROR:105: Unable to locate a modulefile for 'python-3.7.3'
Traceback (most recent call last):
File "/XXX/run_docker.py", line 22, in <module>
from absl import app
ModuleNotFoundError: No module named 'absl'

So what is happening here I believe is that the program is trying to load the module, it fails and goes to the local install. As an user I can install the missing absl-py module, but I can’t run it in this case because of the special process. What to do then? All the solutions on stackoverflow were not suitable, since I don’t know which python is getting what. But I have a hint: the program seems to be loading python 3.7.3, that is not the default. So I look for the python modules stored and I found them on the server one. A simple sync

@ server-one ## > rsync -av /usr/local/lib/python3.6/site-packages/ root@server-two:/usr/local/lib/python3.6/site-packages/

and then the “program” works. What is going on? It looks like python, since it didn’t manage to find the modules for the requested python version, took the closest ones available (3.6). But who knows? I’m not a python expert, I’m just passing by πŸ˜”.

HOWTO: run a GUI in a docker

I’m trying to have this ChimeraX docker running on my CentOS 7.9 and the latest ChimeraX. It turned out I can’t, since I don’t have Qt6 and the support for the above CentOS choice has been dumped, but it has been an interesting experiment, enough to log it. The ChimeraX docker image from the docker builds when you bring it, so in principle it looks like it should work. The documentation, unfortunately, doesn’t tell how to start a sample docker. I will tell you:

docker run -i -t --name chimeraXtest \
--net=host --privileged -e DISPLAY \
-v /tmp/.X11-unix:/tmp/.X11-unix:ro \
chimerax:latest /bin/bash

If you have been paying attention to my docker notes, this will deliver you to a bash shell inside the ChimeraX docker, that seems to be Ubuntu 20 based. One can install and run GUIs inside the bash shell (for example try apt-get install nedit), but the very thing we want to run crashes. Like this:

ImportError: libQt6Core.so.6: cannot open shared object file: No such file or
directory

BUG: ImportError: libQt6Core.so.6: cannot open shared object file: No such file or
directory

File "/usr/lib/ucsf-chimerax/lib/python3.9/site-packages/Qt/__init__.py", line
64, in
from PyQt6.QtCore import PYQT_VERSION_STR as PYQT6_VERSION

_See log for complete Python traceback._

There’s no obvious solution for this import error. Maybe I will investigate how to run on a Qt6 docker container for CI. Or do you have a better suggestion maybe? Check this post: docker x11 fails to open display. Tomorrow more dockers, maybe. If I have time πŸ˜‰.

HOWTO: Install nvitop, a tool for enriching the output of nvidia-smi

A sample output. Taken from the github page.

Yes I still look for THE solution to monitor GPU usage. In my search I found nvitop, a top-like command that will allow you to see, in real time, what your GPUs are doing. Then you will just need to open 10 shells (or 20, or as many GPU as servers you have) to check your usage πŸ˜‰. So it’s not for me but still it deserves to be mentioned.

Installing it following the github howto goes like this:

## > pip3 install --upgrade nvitop
WARNING: Running pip install with root privileges
is generally not a good idea.
Try `pip3 install --user` instead.
Collecting nvitop
Downloading ../nvitop-0.8.1-py3-none-any.whl (148kB)
100% |XXXX| 153kB 3.6MB/s
Collecting cachetools>=1.0.1 (from nvitop)
Downloading ../cachetools-4.2.4-py3-none-any.whl
Requirement already up-to-date: psutil>=5.6.6 in
/usr/local/lib64/python3.6/site-packages (from nvitop)
Requirement already up-to-date:
nvidia-ml-py<11.500.0a0,>=11.450.51 in
/usr/local/lib/python3.6/site-packages (from nvitop)
Collecting termcolor>=1.0.0 (from nvitop)
Downloading ../termcolor-1.1.0.tar.gz
Installing collected packages: cachetools,
termcolor, nvitop
Running setup.py install for termcolor ... done
Successfully installed cachetools-4.2.4
nvitop-0.8.1 termcolor-1.1.0

After this, I’m able to run the command without issues ❀️. NOTE: there’s a similar tool, NVTOP, that requires compilation. You can find NVTOP github here. Unfortunately I didn’t manage to install it on my CentOS 7.X test machine…

Installing python3 on macOS 12.6 Monterey

Yeah I don’t know why I’m doing this, I rarely use python on my mac. I guess it’s just in case. I have followed this guide. I do have brew installed, but just in case, I will copy you the procedure.

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Now we install python with brew:

$ brew install python

And that’s it. Note that it took quite some time to complete in my 2017 macbook pro, but maybe because the install triggered quite some brew updates. Please be patient πŸ˜‰.