From 398cf1b251184c6cff0340e125a26e8a12f81a6e Mon Sep 17 00:00:00 2001 From: Dom Batten Date: Wed, 12 Apr 2023 19:11:22 +0100 Subject: [PATCH 1/4] fix e-notation bug --- src/arraytex/api.py | 4 ++-- src/arraytex/utils.py | 2 +- tests/test_api.py | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/arraytex/api.py b/src/arraytex/api.py index 4705ef2..8490f98 100644 --- a/src/arraytex/api.py +++ b/src/arraytex/api.py @@ -82,8 +82,8 @@ def to_tabular( Raises: TooManyDimensionsError: when the supplied array has more than 2 dimensions - DimensionMismatchError: when there are mismatched column identifiers and - dimensions + DimensionMismatchError: when there is a mismatch between column items and number + of columns, or column index items and number of rows """ n_dims = len(arr.shape) diff --git a/src/arraytex/utils.py b/src/arraytex/utils.py index f6303e8..798dae7 100644 --- a/src/arraytex/utils.py +++ b/src/arraytex/utils.py @@ -61,7 +61,7 @@ def num_formatter(x: Union[np.int64, np.float64, np.float32]) -> str: ) if num_format and "e" in num_format: - pattern = r"e(-?\d+)" + pattern = r"e([\+-]?\d+)" replace = r"\\mathrm{e}{\g<1>}" if scientific_notation: replace = r" \\times 10^{\g<1>}" diff --git a/tests/test_api.py b/tests/test_api.py index 763cedf..c06003e 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -87,6 +87,20 @@ def test_e_notation(self) -> None: == r"""\begin{bmatrix} 1.00\mathrm{e}{-03} & 2.00\mathrm{e}{-03} & 3.00\mathrm{e}{-03} \\ 4.00\mathrm{e}{-03} & 5.00\mathrm{e}{-03} & 6.00\mathrm{e}{-03} \\ +\end{bmatrix}""" + ) + + def test_e_notation_positive(self) -> None: + """Scientific E notation can be selected with positive powers.""" + mat = np.arange(6).reshape(2, 3) + + out = to_matrix(mat, num_format=".2e") + + assert ( + out + == r"""\begin{bmatrix} +0.00\mathrm{e}{+00} & 1.00\mathrm{e}{+00} & 2.00\mathrm{e}{+00} \\ +3.00\mathrm{e}{+00} & 4.00\mathrm{e}{+00} & 5.00\mathrm{e}{+00} \\ \end{bmatrix}""" ) From a6702d764b4d4da130270de5dbcb4a686d9f0357 Mon Sep 17 00:00:00 2001 From: Dom Batten Date: Thu, 13 Apr 2023 10:12:36 +0100 Subject: [PATCH 2/4] add usage docs --- docs/index.md | 5 +- docs/usage.md | 144 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 143 insertions(+), 6 deletions(-) diff --git a/docs/index.md b/docs/index.md index 46f2fff..42456b0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -6,7 +6,8 @@ end-before: [license]: license [contributor guide]: contributing -[command-line reference]: usage + + ```{toctree} --- @@ -14,7 +15,7 @@ hidden: maxdepth: 1 --- - +usage reference contributing Code of Conduct diff --git a/docs/usage.md b/docs/usage.md index a967ba4..5c0a1c2 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -1,7 +1,143 @@ # Usage -```{eval-rst} -.. click:: arraytex.__main__:main - :prog: arraytex - :nested: full + + + + + + +Suppose you want to convert a `numpy.NDArray` object to a LaTeX representation: + +```python +>>> import numpy as np +>>> A = np.arange(6).reshape(2, 3) +>>> A +array([[0, 1, 2], + [3, 4, 5]]) +``` + +## To matrix + +First import the `to_matrix` function: + +```python +>>> from arraytex import to_matrix +``` + +Then run `to_matrix` with a `numpy.NDArray` object as the first argument: + +```python +>>> print(to_matrix(A)) +\begin{bmatrix} +0 & 1 & 2 \\ +3 & 4 & 5 \\ +\end{bmatrix} +``` + +Different matrix style environment delimiters can be used: + +```python +>>> print(to_matrix(A, style="p")) +\begin{pmatrix} +0 & 1 & 2 \\ +3 & 4 & 5 \\ +\end{pmatrix} +``` + +So can builtin number formatters: + +```python +>>> print(to_matrix(A, num_format=".2e")) +\begin{bmatrix} +0.00\mathrm{e}{+00} & 1.00\mathrm{e}{+00} & 2.00\mathrm{e}{+00} \\ +3.00\mathrm{e}{+00} & 4.00\mathrm{e}{+00} & 5.00\mathrm{e}{+00} \\ +\end{bmatrix} +``` + +Prefer scientific notation to e-notation? No problem: + +```python +>>> print(to_matrix(A, num_format=".2e", scientific_notation=True)) +\begin{bmatrix} +0.00 \times 10^{+00} & 1.00 \times 10^{+00} & 2.00 \times 10^{+00} \\ +3.00 \times 10^{+00} & 4.00 \times 10^{+00} & 5.00 \times 10^{+00} \\ +\end{bmatrix} +``` + +## To tabular + +First import the `to_tabular` function: + +```python +>>> from arraytex import to_tabular +``` + +Then run `to_tabular` with a `numpy.NDArray` as the first argument: + +```python +>>> print(to_tabular(A)) +\begin{tabular}{c c c} +\toprule +Col 1 & Col 2 & Col 3 \\ +\midrule +0 & 1 & 2 \\ +3 & 4 & 5 \\ +\bottomrule +\end{tabular} +``` + +The `num_format` and `scientific_notation` arguments are available to use: + +```python +>>> print(to_tabular(A, num_format=".2f")) +\begin{tabular}{c c c} +\toprule +Col 1 & Col 2 & Col 3 \\ +\midrule +0.00 & 1.00 & 2.00 \\ +3.00 & 4.00 & 5.00 \\ +\bottomrule +\end{tabular} +``` + +You can pass custom column names and column align identifiers: + +```python +>>> print(to_tabular(A, col_align=["l", "c", "r"], col_names=["Data", "More Data", "Even More Data"])) +\begin{tabular}{l c r} +\toprule +Data & More Data & Even More Data \\ +\midrule +0 & 1 & 2 \\ +3 & 4 & 5 \\ +\bottomrule +\end{tabular} +``` + +Pass a list of column indices: + +```python +>>> print(to_tabular(A, col_index=["Sample 1", "Sample 2"])) +\begin{tabular}{l c c c} +\toprule +Index & Col 1 & Col 2 & Col 3 \\ +\midrule +Sample 1 & 0 & 1 & 2 \\ +Sample 2 & 3 & 4 & 5 \\ +\bottomrule +\end{tabular} +``` + +Specify the name of the name of index column through `col_names`: + +```python +>>> print(to_tabular(A, col_index=["Sample 1", "Sample 2"], col_names=["Which Sample", "A", "B", "C"])) +\begin{tabular}{l c c c} +\toprule +Which Sample & A & B & C \\ +\midrule +Sample 1 & 0 & 1 & 2 \\ +Sample 2 & 3 & 4 & 5 \\ +\bottomrule +\end{tabular} ``` From f99c9685ff6c668200e879b330e72adc30816ea7 Mon Sep 17 00:00:00 2001 From: Dom Batten Date: Thu, 13 Apr 2023 10:16:44 +0100 Subject: [PATCH 3/4] bump version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 6952591..a55536d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "arraytex" -version = "0.0.7" +version = "0.0.8" description = "ArrayTeX" authors = ["Dom Batten "] license = "MIT" From 9916eaef06f432d11192d0d09f3c901bb47ebff5 Mon Sep 17 00:00:00 2001 From: Dom Batten Date: Thu, 13 Apr 2023 10:22:40 +0100 Subject: [PATCH 4/4] bump down codecov-actions dep --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9768abf..4a8919f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -149,4 +149,4 @@ jobs: nox --session=coverage -- xml - name: Upload coverage report - uses: codecov/codecov-action@3.1.2 + uses: codecov/codecov-action@v3