Skip to content

Commit

Permalink
Merge pull request #37 from dbatten5/index-name
Browse files Browse the repository at this point in the history
`col_index` to `index`
  • Loading branch information
dbatten5 committed Apr 14, 2023
2 parents 3e8b7a6 + 8be7b82 commit 66dec5a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ Convert a `numpy.NDArray` to various LaTeX forms.
1 & 2 & 3 \\
4 & 5 & 6 \\
\end{bmatrix}
>>> print(atx.to_tabular(A))
\begin{tabular}{c c c}
\toprule
Col 1 & Col 2 & Col 3 \\
\midrule
1 & 2 & 3 \\
4 & 5 & 6 \\
\bottomrule
\end{tabular}
```

Inspired by [@josephcslater](https://github.com/josephcslater)'s
[array_to_latex](https://github.com/josephcslater/array_to_latex).

## Features

- Support for different matrix environment delimiters, (`bmatrix`, `pmatrix`, etc.)
- Support for matrix environments with different delimiters (`bmatrix`, `pmatrix`, etc.).
- Support for tabular environments.
- Support for builtin number formats (`:.2f`, `:.3e`, etc.).
- Fully tested and typed.
Expand Down
20 changes: 10 additions & 10 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ Different matrix style environment delimiters can be used:
So can builtin number formatters:

```python
>>> print(to_matrix(A, num_format=".2e"))
>>> print(to_matrix((A + 1) * 1e3, 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} \\
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}
```

Prefer scientific notation to e-notation? No problem:

```python
>>> print(to_matrix(A, num_format=".2e", scientific_notation=True))
>>> print(to_matrix((A + 1) * 1e3, 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} \\
1.00 \times 10^{+03} & 2.00 \times 10^{+03} & 3.00 \times 10^{+03} \\
4.00 \times 10^{+03} & 5.00 \times 10^{+03} & 6.00 \times 10^{+03} \\
\end{bmatrix}
```

Expand Down Expand Up @@ -114,10 +114,10 @@ Data & More Data & Even More Data \\
\end{tabular}
```

Pass a list of column indices:
Pass a list of row identifiers to be used as a table index:

```python
>>> print(to_tabular(A, col_index=["Sample 1", "Sample 2"]))
>>> print(to_tabular(A, index=["Sample 1", "Sample 2"]))
\begin{tabular}{l c c c}
\toprule
Index & Col 1 & Col 2 & Col 3 \\
Expand All @@ -128,10 +128,10 @@ Sample 2 & 3 & 4 & 5 \\
\end{tabular}
```

Specify the name of the name of index column through `col_names`:
Specify the name of the index column through `col_names`:

```python
>>> print(to_tabular(A, col_index=["Sample 1", "Sample 2"], col_names=["Which Sample", "A", "B", "C"]))
>>> print(to_tabular(A, index=["Sample 1", "Sample 2"], col_names=["Which Sample", "A", "B", "C"]))
\begin{tabular}{l c c c}
\toprule
Which Sample & A & B & C \\
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "arraytex"
version = "0.0.8"
version = "0.0.9"
description = "ArrayTeX"
authors = ["Dom Batten <[email protected]>"]
license = "MIT"
Expand Down
16 changes: 8 additions & 8 deletions src/arraytex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def to_tabular(
scientific_notation: bool = False,
col_align: Union[List[str], str] = "c",
col_names: Optional[List[str]] = None,
col_index: Optional[List[str]] = None,
index: Optional[List[str]] = None,
to_clp: bool = False,
) -> str:
"""Convert a numpy.NDArray to LaTeX tabular environment.
Expand All @@ -74,7 +74,7 @@ def to_tabular(
is provided then each item will be assigned to each column, list size and
number of columns must match
col_names: an optional list of column names, otherwise generic names will be assigned
col_index: an optional list of column indices, i.e. row identifiers
index: an optional table index, i.e. row identifiers
to_clp: copy the output to the system clipboard
Returns:
Expand All @@ -96,7 +96,7 @@ def to_tabular(
else:
raise TooManyDimensionsError

if not col_index:
if not index:
if isinstance(col_align, list) and len(col_align) != n_cols:
raise DimensionMismatchError(
f"Number of `col_align` items ({len(col_align)}) "
Expand All @@ -110,7 +110,7 @@ def to_tabular(
)

if (
col_index
index
and col_names
and isinstance(col_align, list)
and len(col_names) != len(col_align)
Expand All @@ -128,10 +128,10 @@ def to_tabular(

lines = _parse_lines(arr, num_format, scientific_notation)

if col_index:
if len(col_index) != len(lines):
if index:
if len(index) != len(lines):
raise DimensionMismatchError(
f"Number of `col_index` items ({len(col_index)}) "
f"Number of `index` items ({len(index)}) "
+ f"doesn't match number of rows ({len(lines)})"
)

Expand All @@ -142,7 +142,7 @@ def to_tabular(
col_names.insert(0, "Index")

for idx, line in enumerate(lines):
lines[idx] = f"{col_index[idx]} & " + line.strip()
lines[idx] = f"{index[idx]} & " + line.strip()

rv = [f"\\begin{{tabular}}{{{' '.join(col_align)}}}"]
rv += [r"\toprule"]
Expand Down
34 changes: 17 additions & 17 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,15 @@ def test_0_d(self) -> None:
\end{tabular}"""
)

class TestColIndex:
"""Tests for the `col_index` support."""
class TestIndex:
"""Tests for the `index` support."""

def test_default(self) -> None:
"""`col_index` forms the row names."""
col_index = ["Row 1", "Row 2"]
"""`index` forms the row names."""
index = ["Row 1", "Row 2"]
mat = np.arange(1, 5).reshape(2, 2)

out = to_tabular(mat, col_index=col_index)
out = to_tabular(mat, index=index)

assert (
out
Expand All @@ -315,24 +315,24 @@ def test_default(self) -> None:
)

def test_bad_dimensions(self) -> None:
"""An error is raised if wrong dimension of `col_index`."""
col_index = ["Row 1", "Row 2"]
"""An error is raised if wrong dimension of `index`."""
index = ["Row 1", "Row 2"]
mat = np.arange(1, 4).reshape(3, 1)

with pytest.raises(DimensionMismatchError) as exc:
to_tabular(mat, col_index=col_index)
to_tabular(mat, index=index)

assert str(exc.value) == (
"Number of `col_index` items (2) doesn't match number of rows (3)"
"Number of `index` items (2) doesn't match number of rows (3)"
)

def test_given_col_names(self) -> None:
"""A given index name as part of `col_names` is used."""
col_index = ["Row 1", "Row 2"]
index = ["Row 1", "Row 2"]
col_names = ["My Index", "Col 1", "Col 2"]
mat = np.arange(1, 5).reshape(2, 2)

out = to_tabular(mat, col_index=col_index, col_names=col_names)
out = to_tabular(mat, index=index, col_names=col_names)

assert (
out
Expand All @@ -348,10 +348,10 @@ def test_given_col_names(self) -> None:

def test_given_col_align(self) -> None:
"""A given col align char can be used for the col index."""
col_index = ["Row 1", "Row 2"]
index = ["Row 1", "Row 2"]
mat = np.arange(1, 5).reshape(2, 2)

out = to_tabular(mat, col_index=col_index, col_align=["r", "c", "c"])
out = to_tabular(mat, index=index, col_align=["r", "c", "c"])

assert (
out
Expand All @@ -367,7 +367,7 @@ def test_given_col_align(self) -> None:

def test_given_col_name_and_align(self) -> None:
"""A given col index name and align can be used for the index."""
col_index = ["Row 1", "Row 2"]
index = ["Row 1", "Row 2"]
col_names = ["My Index", "Col 1", "Col 2"]
col_align = ["r", "c", "c"]
mat = np.arange(1, 5).reshape(2, 2)
Expand All @@ -376,7 +376,7 @@ def test_given_col_name_and_align(self) -> None:
mat,
col_align=col_align,
col_names=col_names,
col_index=col_index,
index=index,
)

assert (
Expand All @@ -393,7 +393,7 @@ def test_given_col_name_and_align(self) -> None:

def test_col_align_bad_dimensions(self) -> None:
"""Bad dimensions of `col_align` is caught."""
col_index = ["Row 1", "Row 2"]
index = ["Row 1", "Row 2"]
col_names = ["My Index", "Col 1", "Col 2"]
col_align = ["r", "c"]
mat = np.arange(1, 5).reshape(2, 2)
Expand All @@ -403,7 +403,7 @@ def test_col_align_bad_dimensions(self) -> None:
mat,
col_align=col_align,
col_names=col_names,
col_index=col_index,
index=index,
)

assert str(exc.value) == (
Expand Down

0 comments on commit 66dec5a

Please sign in to comment.