Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make check for latex more robust #74

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,34 @@ jobs:

- name: New CLI
run: mud examples mud-paper

texless-integration-tests:
name: texless cli examples
strategy:
matrix:
python-version: ["3.9"]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 1

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install Python dependencies
run: |
pip install --upgrade pip
pip install --upgrade mud-examples
pip install .[examples]

- name: Old CLI
continue-on-error: true
run: mud_run_all -v

- name: New CLI
continue-on-error: true
run: mud examples mud-paper
54 changes: 30 additions & 24 deletions src/mud/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
---------

"""
from logging import getLogger
from pathlib import Path

import numpy as np
Expand All @@ -16,7 +17,34 @@

from mud.util import null_space

_logger = getLogger(__name__)


def _check_latex():
"""check latex installation"""

path = Path.cwd() / ".test_fig.png"
try: # minimal example to trip up matplotlib
plt.rcParams.update({"text.usetex": True})
plt.plot([0], [1], label=r"$a_\text{foo} = \lambda$")
plt.plot([0, 1], [1, 2], label="Something")
plt.savefig(str(path), bbox_inches="tight")
path.unlink(missing_ok=True)
_logger.info("USING TEX")
return True
except (RuntimeError, FileNotFoundError):
_logger.warning("NOT USING TEX")
return False


# Matplotlib plotting options
HAS_LATEX = _check_latex()
PREAMBLE = ""
if HAS_LATEX:
PREAMBLE = " ".join(
[r"\usepackage{bm}", r"\usepackage{amsfonts}", r"\usepackage{amsmath}"]
)

mud_plot_params = {
"mathtext.fontset": "stix",
"font.family": "STIXGeneral",
Expand All @@ -29,31 +57,12 @@
"axes.labelpad": 1,
"font.size": 16,
"savefig.facecolor": "white",
"text.usetex": True,
"text.latex.preamble": " ".join(
[r"\usepackage{bm}", r"\usepackage{amsfonts}", r"\usepackage{amsmath}"]
),
"text.usetex": HAS_LATEX,
"text.latex.preamble": PREAMBLE,
}
plt.rcParams.update(mud_plot_params)


def _check_latex():
"""check latex installation"""
global mud_plot_params

path = Path.cwd() / ".test_fig.png"
plt.plot([0], [1], label=r"$a_\text{foo} = \lambda$")
try:
plt.legend()
plt.savefig(str(path))
path.unlink(missing_ok=True)
except RuntimeError:
print("NOT USING TEX")
mud_plot_params["text.usetex"] = False
mud_plot_params["text.latex.preamble"] = ""
plt.rcParams.update(mud_plot_params)


def save_figure(
fname: str, save_path: str = "figures", close_fig: bool = True, **kwargs
):
Expand Down Expand Up @@ -227,6 +236,3 @@ def plot_vert_line(ax, x_loc, ylim=None, **kwargs):
ylims[1] = ylim if ylim is not None else ylims[1]
ax.plot([x_loc, x_loc], [ylims[0], ylims[1]], **kwargs)
ax.set_ylim(ylims)


_check_latex()