Matplotlib Error

Hi AFNI team,

I’m trying to switch from using the basic html_review_style to pythonic. I am getting some errors during that portion of the processing though and I’m not sure why. Here is the terminal output:


++ FOR: motion_AS1108_enorm.1D: 1 arrays with 512 pts
++ FOR: outcount_rall.1D: 1 arrays with 512 pts
++ Lists of names and yvars match length
*+ No input x-axis; making values based on length of data: [0..511]
++ Plotting: enorm (~mm)
++ Plotting: outlier frac
Traceback (most recent call last):
  File "/nfs/pkg64/afni_ubuntu_16_20230101/1dplot.py", line 65, in <module>
    ok_fig  = lpod.make_1dplot_figure(bigfig)
  File "/nfs/pkg64/afni_ubuntu_16_20230101/afnipy/lib_plot_1D.py", line 497, in make_1dplot_figure
    plt.savefig( bf.fname, dpi=bf.dpi, facecolor=bf.bkgd_color,
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 722, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 2180, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 2021, in print_figure
    canvas = self._get_output_canvas(format)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 1961, in _get_output_canvas
    raise ValueError(
ValueError: Format 'jpg' is not supported (supported formats: eps, pdf, pgf, png, ps, raw, rgba, svg, svgz)
++ FOR: dfile_rall.1D: 6 arrays with 512 pts
++ Lists of names and yvars match length
*+ No input x-axis; making values based on length of data: [0..511]
++ Plotting: dP
(mm)
++ Plotting: dL
(mm)
++ Plotting: dS
(mm)
++ Plotting: yaw
(deg)
++ Plotting: pitch
(deg)
++ Plotting: roll
(deg)
Traceback (most recent call last):
  File "/nfs/pkg64/afni_ubuntu_16_20230101/1dplot.py", line 65, in <module>
    ok_fig  = lpod.make_1dplot_figure(bigfig)
  File "/nfs/pkg64/afni_ubuntu_16_20230101/afnipy/lib_plot_1D.py", line 497, in make_1dplot_figure
    plt.savefig( bf.fname, dpi=bf.dpi, facecolor=bf.bkgd_color,
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 722, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 2180, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 2021, in print_figure
    canvas = self._get_output_canvas(format)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 1961, in _get_output_canvas
    raise ValueError(
ValueError: Format 'jpg' is not supported (supported formats: eps, pdf, pgf, png, ps, raw, rgba, svg, svgz)

And I am also attaching my afni_system_check output:


architecture:         64bit ELF
cpu type:             x86_64
system:               Linux
release:              5.4.0-139-generic
version:              #156-Ubuntu SMP Fri Jan 20 17:27:18 UTC 2023
distribution:         ubuntu 20.04 focal
number of CPUs:       96
apparent login shell: bash
shell RC file:        .bashrc (exists)

--------------------- AFNI and related program tests ---------------------
which afni           : /nfs/pkg64/afni_ubuntu_16_20230101/afni
afni version         : Precompiled binary linux_ubuntu_16_64: Jan 18 2023 
                     : AFNI_23.0.01 'Commodus'
AFNI_version.txt     : AFNI_23.0.01, linux_ubuntu_16_64, Jan 18 2023
which python         : /bin/python
python version       : 3.8.10
which R              : /bin/R
R version            : R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting"
which tcsh           : /bin/tcsh

instances of various programs found in PATH:
    afni    : 1   (/nfs/pkg64/afni_ubuntu_16_20230101/afni)
    R       : 1   (/usr/bin/R)
    python  : 1   (/usr/bin/python3.8)
    python2 : 1   (/usr/bin/python2.7)
    python3 : 1   (/usr/bin/python3.8)


testing ability to start various programs...
    afni                 : success
    suma                 : success
    3dSkullStrip         : success
    uber_subject.py      : success
    3dAllineate          : success
    3dRSFC               : success
    SurfMesh             : success
    3dClustSim           : success
    3dMVM                : success

checking for R packages...
    rPkgsInstall -pkgs ALL -check : success

R RHOME : /usr/lib/R

checking for $HOME files...
    .afnirc                   : found
    .sumarc                   : found
    .afni/help/all_progs.COMP : found

------------------------------ python libs -------------------------------
** failed to load module PyQt4
-- PyQt4 is no longer needed for an AFNI bootcamp

++ module loaded: matplotlib.pyplot
   module file : /usr/lib/python3/dist-packages/matplotlib/pyplot.py
   matplotlib version : 3.1.2

-------------------------------- env vars --------------------------------
PATH = /nfs/pkg64/afni_ubuntu_16_20230101:/home/farnsworth/bin:/home/farnsworth/.local/bin:/usr/local/cuda/bin:/bin:/usr/bin:/opt/thinlinc/bin:/usr/local/bin:/usr/bin/X11:/sbin:/usr/sbin:/usr/local/sbin:/snap/bin

PYTHONPATH = 
R_LIBS = /home/farnsworth/R

LD_LIBRARY_PATH = /usr/local/cuda/lib64:/opt/thinlinc/lib64:/opt/thinlinc/lib

DYLD_LIBRARY_PATH = 
DYLD_FALLBACK_LIBRARY_PATH = 

------------------------------ data checks -------------------------------
data dir : missing AFNI_data6
data dir : missing AFNI_demos
data dir : missing suma_demo
data dir : missing afni_handouts
atlas    : found TT_N27+tlrc  under /nfs/pkg64/afni_ubuntu_16_20230101

------------------------------ OS specific -------------------------------
which apt-get        : /bin/apt-get
apt-get version      : apt 2.0.9 (amd64)

have Ubuntu system: ubuntu 20.04 focal
have Ubuntu afni  : Precompiled binary linux_ubuntu_16_64: Jan 18 2023 

=========================  summary, please fix:  =========================
*  just be aware: login shell 'bash', but our code examples use 'tcsh'
*  'afni' executable is owned by root
*  insufficient data for AFNI bootcamp
   (see "Prepare for Bootcamp" on install pages)

Thanks in advance for your help!

Sincerely,
Ian

Hi, Ian-

I think you will be happier with the full pythonic version, yes! But indeed, we need to fix that error message.

So, the default package manager in Ubuntu 20.04 (Aptitude) distributes a version of Matplotlib, 3.1.2, that contains this error when trying to make JPGs:
https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/1897283
I don’t know why they haven’t fixed it, and it is the root cause of the error here.

The good news: there is a fix. Can you please run the following on your computer (requires the admin/sudo password):


sudo apt-get install pip
sudo apt-get remove python3-matplotlib
pip install matplotlib

That should get you matplotlib ver=3.6.2 (or so), and Python can now generate JPG images. (Why Ubuntu didn’t make the bug-free version of matplotlib its default… I don’t know.)

If you want to test this fix without rerunning the full afni_proc.py command, you can do this:
Go into an afni_proc.py (AP) results directory, and do:


curl -O [https://raw.githubusercontent.com/afni/afni/master/src/ptaylor/supplement/redo_apqc.tcsh](https://raw.githubusercontent.com/afni/afni/master/src/ptaylor/supplement/redo_apqc.tcsh)
tcsh redo_apqc.tcsh

… to regenerate the QC directory there. Under the ‘mot’ block, you should now see lots of line plots.

Please let me know how that goes.

–pt

Hi Paul,

So, our server admin didn’t want to change any of the Python/Matplotlib versions tied to the server Linux distributions. However, they recommended a very simple fix to create a virtual environment using Python’s venv module to install a newer matplotlib version, which worked perfectly! Thanks for your help and info about that specific Matplotlib version being bugged.

Sincerely,
Ian

Hi, Ian-

Glad it got sorted.

I’ll just note that using conda/miniconda to make a local environment (I use this to manage Python on my own systems) is useful, modular and doesn’t require sudo/admin privileges. There are other ways to go, certainly, and it sounds like you are set, but I note this for anyone with similar constraints reading this thread. Here are notes about using miniconda specifically, with recipes for AFNI dependencies:
https://afni.nimh.nih.gov/pub/dist/doc/htmldoc/background_install/miniconda.html

–pt