Skip to content

Commit 66dec5a

Browse files
authored
Merge pull request #37 from dbatten5/index-name
`col_index` to `index`
2 parents 3e8b7a6 + 8be7b82 commit 66dec5a

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,23 @@ Convert a `numpy.NDArray` to various LaTeX forms.
3232
1 & 2 & 3 \\
3333
4 & 5 & 6 \\
3434
\end{bmatrix}
35+
>>> print(atx.to_tabular(A))
36+
\begin{tabular}{c c c}
37+
\toprule
38+
Col 1 & Col 2 & Col 3 \\
39+
\midrule
40+
1 & 2 & 3 \\
41+
4 & 5 & 6 \\
42+
\bottomrule
43+
\end{tabular}
3544
```
3645

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

4049
## Features
4150

42-
- Support for different matrix environment delimiters, (`bmatrix`, `pmatrix`, etc.)
51+
- Support for matrix environments with different delimiters (`bmatrix`, `pmatrix`, etc.).
4352
- Support for tabular environments.
4453
- Support for builtin number formats (`:.2f`, `:.3e`, etc.).
4554
- Fully tested and typed.

docs/usage.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ Different matrix style environment delimiters can be used:
4747
So can builtin number formatters:
4848

4949
```python
50-
>>> print(to_matrix(A, num_format=".2e"))
50+
>>> print(to_matrix((A + 1) * 1e3, num_format=".2e"))
5151
\begin{bmatrix}
52-
0.00\mathrm{e}{+00} & 1.00\mathrm{e}{+00} & 2.00\mathrm{e}{+00} \\
53-
3.00\mathrm{e}{+00} & 4.00\mathrm{e}{+00} & 5.00\mathrm{e}{+00} \\
52+
1.00\mathrm{e}{+03} & 2.00\mathrm{e}{+03} & 3.00\mathrm{e}{+03} \\
53+
4.00\mathrm{e}{+03} & 5.00\mathrm{e}{+03} & 6.00\mathrm{e}{+03} \\
5454
\end{bmatrix}
5555
```
5656

5757
Prefer scientific notation to e-notation? No problem:
5858

5959
```python
60-
>>> print(to_matrix(A, num_format=".2e", scientific_notation=True))
60+
>>> print(to_matrix((A + 1) * 1e3, num_format=".2e", scientific_notation=True))
6161
\begin{bmatrix}
62-
0.00 \times 10^{+00} & 1.00 \times 10^{+00} & 2.00 \times 10^{+00} \\
63-
3.00 \times 10^{+00} & 4.00 \times 10^{+00} & 5.00 \times 10^{+00} \\
62+
1.00 \times 10^{+03} & 2.00 \times 10^{+03} & 3.00 \times 10^{+03} \\
63+
4.00 \times 10^{+03} & 5.00 \times 10^{+03} & 6.00 \times 10^{+03} \\
6464
\end{bmatrix}
6565
```
6666

@@ -114,10 +114,10 @@ Data & More Data & Even More Data \\
114114
\end{tabular}
115115
```
116116

117-
Pass a list of column indices:
117+
Pass a list of row identifiers to be used as a table index:
118118

119119
```python
120-
>>> print(to_tabular(A, col_index=["Sample 1", "Sample 2"]))
120+
>>> print(to_tabular(A, index=["Sample 1", "Sample 2"]))
121121
\begin{tabular}{l c c c}
122122
\toprule
123123
Index & Col 1 & Col 2 & Col 3 \\
@@ -128,10 +128,10 @@ Sample 2 & 3 & 4 & 5 \\
128128
\end{tabular}
129129
```
130130

131-
Specify the name of the name of index column through `col_names`:
131+
Specify the name of the index column through `col_names`:
132132

133133
```python
134-
>>> print(to_tabular(A, col_index=["Sample 1", "Sample 2"], col_names=["Which Sample", "A", "B", "C"]))
134+
>>> print(to_tabular(A, index=["Sample 1", "Sample 2"], col_names=["Which Sample", "A", "B", "C"]))
135135
\begin{tabular}{l c c c}
136136
\toprule
137137
Which Sample & A & B & C \\

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "arraytex"
3-
version = "0.0.8"
3+
version = "0.0.9"
44
description = "ArrayTeX"
55
authors = ["Dom Batten <[email protected]>"]
66
license = "MIT"

src/arraytex/api.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def to_tabular(
5959
scientific_notation: bool = False,
6060
col_align: Union[List[str], str] = "c",
6161
col_names: Optional[List[str]] = None,
62-
col_index: Optional[List[str]] = None,
62+
index: Optional[List[str]] = None,
6363
to_clp: bool = False,
6464
) -> str:
6565
"""Convert a numpy.NDArray to LaTeX tabular environment.
@@ -74,7 +74,7 @@ def to_tabular(
7474
is provided then each item will be assigned to each column, list size and
7575
number of columns must match
7676
col_names: an optional list of column names, otherwise generic names will be assigned
77-
col_index: an optional list of column indices, i.e. row identifiers
77+
index: an optional table index, i.e. row identifiers
7878
to_clp: copy the output to the system clipboard
7979
8080
Returns:
@@ -96,7 +96,7 @@ def to_tabular(
9696
else:
9797
raise TooManyDimensionsError
9898

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

112112
if (
113-
col_index
113+
index
114114
and col_names
115115
and isinstance(col_align, list)
116116
and len(col_names) != len(col_align)
@@ -128,10 +128,10 @@ def to_tabular(
128128

129129
lines = _parse_lines(arr, num_format, scientific_notation)
130130

131-
if col_index:
132-
if len(col_index) != len(lines):
131+
if index:
132+
if len(index) != len(lines):
133133
raise DimensionMismatchError(
134-
f"Number of `col_index` items ({len(col_index)}) "
134+
f"Number of `index` items ({len(index)}) "
135135
+ f"doesn't match number of rows ({len(lines)})"
136136
)
137137

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

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

147147
rv = [f"\\begin{{tabular}}{{{' '.join(col_align)}}}"]
148148
rv += [r"\toprule"]

tests/test_api.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,15 @@ def test_0_d(self) -> None:
292292
\end{tabular}"""
293293
)
294294

295-
class TestColIndex:
296-
"""Tests for the `col_index` support."""
295+
class TestIndex:
296+
"""Tests for the `index` support."""
297297

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

303-
out = to_tabular(mat, col_index=col_index)
303+
out = to_tabular(mat, index=index)
304304

305305
assert (
306306
out
@@ -315,24 +315,24 @@ def test_default(self) -> None:
315315
)
316316

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

322322
with pytest.raises(DimensionMismatchError) as exc:
323-
to_tabular(mat, col_index=col_index)
323+
to_tabular(mat, index=index)
324324

325325
assert str(exc.value) == (
326-
"Number of `col_index` items (2) doesn't match number of rows (3)"
326+
"Number of `index` items (2) doesn't match number of rows (3)"
327327
)
328328

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

335-
out = to_tabular(mat, col_index=col_index, col_names=col_names)
335+
out = to_tabular(mat, index=index, col_names=col_names)
336336

337337
assert (
338338
out
@@ -348,10 +348,10 @@ def test_given_col_names(self) -> None:
348348

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

354-
out = to_tabular(mat, col_index=col_index, col_align=["r", "c", "c"])
354+
out = to_tabular(mat, index=index, col_align=["r", "c", "c"])
355355

356356
assert (
357357
out
@@ -367,7 +367,7 @@ def test_given_col_align(self) -> None:
367367

368368
def test_given_col_name_and_align(self) -> None:
369369
"""A given col index name and align can be used for the index."""
370-
col_index = ["Row 1", "Row 2"]
370+
index = ["Row 1", "Row 2"]
371371
col_names = ["My Index", "Col 1", "Col 2"]
372372
col_align = ["r", "c", "c"]
373373
mat = np.arange(1, 5).reshape(2, 2)
@@ -376,7 +376,7 @@ def test_given_col_name_and_align(self) -> None:
376376
mat,
377377
col_align=col_align,
378378
col_names=col_names,
379-
col_index=col_index,
379+
index=index,
380380
)
381381

382382
assert (
@@ -393,7 +393,7 @@ def test_given_col_name_and_align(self) -> None:
393393

394394
def test_col_align_bad_dimensions(self) -> None:
395395
"""Bad dimensions of `col_align` is caught."""
396-
col_index = ["Row 1", "Row 2"]
396+
index = ["Row 1", "Row 2"]
397397
col_names = ["My Index", "Col 1", "Col 2"]
398398
col_align = ["r", "c"]
399399
mat = np.arange(1, 5).reshape(2, 2)
@@ -403,7 +403,7 @@ def test_col_align_bad_dimensions(self) -> None:
403403
mat,
404404
col_align=col_align,
405405
col_names=col_names,
406-
col_index=col_index,
406+
index=index,
407407
)
408408

409409
assert str(exc.value) == (

0 commit comments

Comments
 (0)