From 827b5ec63ebe0b9ea833957637d6b60fdc2f3198 Mon Sep 17 00:00:00 2001 From: wpbonelli Date: Sun, 25 Feb 2024 12:38:57 -0500 Subject: [PATCH] refactor(latex): support path-like, add docstrings (#142) * add docs and support str or path-like for latex utils * also install nightly build and update flopy pkgs in ci --- .github/workflows/ci.yml | 10 ++++++- docs/conf.py | 2 +- modflow_devtools/latex.py | 62 ++++++++++++++++++++++++++++++++++----- 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 549431c..26cbc9e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -120,6 +120,11 @@ jobs: - name: Install executables uses: modflowpy/install-modflow-action@v1 + + - name: Install nightly build + uses: modflowpy/install-modflow-action@v1 + with: + repo: modflow6-nightly-build - name: Setup GNU Fortran ${{ env.GCC_V }} uses: awvwgk/setup-fortran@main @@ -137,7 +142,7 @@ jobs: run: | pip install . pip install ".[test]" - + - name: Cache modflow6 examples id: cache-examples uses: actions/cache@v3 @@ -152,6 +157,9 @@ jobs: pip install -r requirements.pip.txt pip install -r requirements.usgs.txt + - name: Update FloPy packages + run: python -m flopy.mf6.utils.generate_classes --ref develop --no-backup + - name: Build modflow6 example models if: steps.cache-examples.outputs.cache-hit != 'true' working-directory: modflow6-examples/autotest diff --git a/docs/conf.py b/docs/conf.py index 1d0f770..9fd38eb 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -8,7 +8,7 @@ project = "modflow-devtools" author = "MODFLOW Team" -release = '1.5.0.dev0' +release = "1.5.0.dev0" # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration diff --git a/modflow_devtools/latex.py b/modflow_devtools/latex.py index 5d6f5eb..bbe766c 100644 --- a/modflow_devtools/latex.py +++ b/modflow_devtools/latex.py @@ -1,13 +1,38 @@ -import os +from os import PathLike +from pathlib import Path +from typing import Iterable, Union -def build_table(caption, fpth, arr, headings=None, col_widths=None): +def build_table( + caption: str, + fpth: Union[str, PathLike], + arr, + headings: Iterable[str] = None, + col_widths: Iterable[float] = None, +): + """ + Build a LaTeX table from the given NumPy array. + + Parameters + ---------- + caption : str + The table's caption + fpth : str or path-like + The LaTeX file to create + arr : numpy recarray + The array + headings : iterable of str + The table headings + col_widths : iterable of float + The table's column widths + """ + + fpth = Path(fpth).expanduser().absolute().with_suffix(".tex") + if headings is None: headings = arr.dtype.names ncols = len(arr.dtype.names) - if not fpth.endswith(".tex"): - fpth += ".tex" - label = "tab:{}".format(os.path.basename(fpth).replace(".tex", "")) + label = "tab:{}".format(fpth.stem) line = get_header(caption, label, headings, col_widths=col_widths) @@ -29,8 +54,32 @@ def build_table(caption, fpth, arr, headings=None, col_widths=None): def get_header( - caption, label, headings, col_widths=None, center=True, firsthead=False + caption: str, + label: str, + headings: Iterable[str], + col_widths: Iterable[float] = None, + center: bool = True, + firsthead: bool = False, ): + """ + Build a LaTeX table header. + + Parameters + ---------- + caption : str + The table's caption + label : str + The table's label + headings : iterable of str + The table's heading + col_widths : iterable of float + The table's column widths + center : bool + Whether to center-align the table text + firsthead : bool + Whether to add first header + """ + ncol = len(headings) if col_widths is None: dx = 0.8 / float(ncol) @@ -85,7 +134,6 @@ def exp_format(v): s = f"{v:.2e}" s = s.replace("e-0", "e-") s = s.replace("e+0", "e+") - # s = s.replace("e", " \\times 10^{") + "}$" return s