Issue with mp.usetex

205 views
Skip to first unread message

Ramsey Lee Karim

unread,
Aug 25, 2021, 1:13:29 PM8/25/21
to PDR Toolbox
Hi all,

I am having an issue with the ModelPlot method usetex(True). This line runs fine on its own, but a subsequent mp.plot() command will raise a RuntimeError. If mp.usetex(False), the mp.plot() command runs fine. I copied a minimal code example below which also includes the version number (2.1.1). This code comes directly from the PDRT_Example_Model_Plotting.ipynb notebook, where the bug can also be reproduced for me.

Thanks! Excited to use this new version!

import pdrtpy
print(f"pdrtpy version {pdrtpy.version()}")

import matplotlib.pyplot as plt
from pdrtpy.modelset import ModelSet
from pdrtpy.plot.modelplot import ModelPlot

m = ModelSet(name="wk2006",z=1)
mp = ModelPlot(m)
mp.usetex(True)
mp.plot("CI_609",legend=False,contours=True,label=True,yaxis_unit="Draine")




The output is copied below. There is also a FutureWarning included in the output, but this appears unrelated and doesn't crash the script.
The bug appears to be latex-related, and I do not have latex installed on my computer. I wouldn't expect it to crash the script for that reason though. This also seems like it is on matplotlib's end, not pdrtpy's.


pdrtpy version 2.1.1
/home/ramsey/anaconda3/lib/python3.8/site-packages/astropy/table/column.py:1157: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  result = getattr(super(), op)(other)
Traceback (most recent call last):
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/texmanager.py", line 252, in _run_checked_subprocess
    report = subprocess.check_output(
  File "/home/ramsey/anaconda3/lib/python3.8/subprocess.py", line 411, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/home/ramsey/anaconda3/lib/python3.8/subprocess.py", line 489, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/home/ramsey/anaconda3/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/home/ramsey/anaconda3/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'latex'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "usetex_bug.py", line 17, in <module>
    mp.plot("CI_609",legend=False,contours=True,label=True,yaxis_unit="Draine")
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/pdrtpy/plot/modelplot.py", line 54, in plot
    self.intensity(identifier,**kwargs_opts)
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/pdrtpy/plot/modelplot.py", line 127, in intensity
    self._plot_no_wcs(model[identifier],**kwargs_opts)
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/pdrtpy/plot/modelplot.py", line 718, in _plot_no_wcs
    drawn = self._axis[axidx].clabel(contourset,contourset.levels,inline=True,fmt='%1.2e')
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 6463, in clabel
    return CS.clabel(levels, **kwargs)
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/contour.py", line 204, in clabel
    self.labels(inline, inline_spacing)
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/contour.py", line 543, in labels
    lw = self.get_label_width(lev, self.labelFmt, fsize)
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/contour.py", line 252, in get_label_width
    width = (text.Text(0, 0, lev, figure=fig,
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/text.py", line 903, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/text.py", line 306, in _get_layout
    _, lp_h, lp_d = renderer.get_text_width_height_descent(
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 229, in get_text_width_height_descent
    w, h, d = texmanager.get_text_width_height_descent(
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/texmanager.py", line 399, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/texmanager.py", line 291, in make_dvi
    self._run_checked_subprocess(
  File "/home/ramsey/anaconda3/lib/python3.8/site-packages/matplotlib/texmanager.py", line 256, in _run_checked_subprocess
    raise RuntimeError(
RuntimeError: Failed to process string with tex because latex could not be found


Marc William Pound

unread,
Aug 25, 2021, 2:02:37 PM8/25/21
to PDR Toolbox
Hi Ramsey,

TLDR:  You need to set mp.usetex(False) [the default]  or install latex on your system.  e.g. ,
sudo apt-get install texlive-latex-extra
sudo apt-get install dvipng

Long answer:   What PlotBase.usetex(True) does is set matplotlib.rcParams["text.usetex"] = True.  This in turn, tells matplotlib to delegate fancy text rendering to the system installation of LaTeX instead of handling it internally. See https://matplotlib.org/stable/tutorials/text/usetex.html.   There are some instances where delegating to LaTeX does a 'better' job than matplotlib but beauty is in the eye of the beholder.  

I could try to catch this Exception and issue a warning, but I think this may paper over the rather clear error message "Failed to process string with tex because latex could not be found."    Plus by the time the exception arises the plot() call has already failed, so there's no way to go back in a recover cleanly.  What I will do is add to the documentation in both readthedocs and in the notebook with this example a caveat that one has to have latex installed for this to work.

thanks,
Marc
Reply all
Reply to author
Forward
0 new messages