Skip to content

Commit

Permalink
#11 wip reading multiple matrices from same file
Browse files Browse the repository at this point in the history
  • Loading branch information
joshkamm committed Nov 19, 2024
1 parent 472d48f commit 4bfea6d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
51 changes: 33 additions & 18 deletions examples/test_output.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
from itertools import pairwise
import os
from pathlib import Path
import numpy as np
import pytest

@pytest.mark.parametrize("filename", ["A", "Ciap", "pVp"])
def test_output(filename):
# convert from script location to where the output files should be located
# Convert from the script location to where the output files should be located.
script_dir = Path(os.path.abspath(__file__)).parent
example_output_dir = script_dir.parent / "build" / "examples" / "geom_1"

assert example_output_dir.exists()

with open(example_output_dir / filename) as output, open(
example_output_dir / f"{filename}_ref"
) as reference_output:
output = list(output)
for i, line in enumerate(reference_output):
if line.endswith(":"):
continue
assert np.fromstring(output[i], sep=" ") == pytest.approx(
np.fromstring(line, sep=" "), rel=1e-6, abs=1e-6
)
# matrix_boundaries = [line for line in file if line.endswith(':')] + [-1]
# for idx in range(len(matrix_boundaries)-1):

# output = np.loadtxt(example_output_dir / filename, skiprows=matrix_boundaries[idx])
# reference_output = np.loadtxt(example_output_dir / f"{filename}_ref", skiprows=matrix_boundaries[idx]+1, max_rows=matrix_boundaries[idx+1]-matrix_boundaries[idx])
# assert output.shape == reference_output.shape
# assert output == pytest.approx(reference_output, rel=1e-6, abs=1e-6)
with open(example_output_dir / f"{filename}_ref") as reference_output:
# output = list(output)
# for i, line in enumerate(reference_output):
# if line.endswith(":"):
# continue
# assert np.fromstring(output[i], sep=" ") == pytest.approx(
# np.fromstring(line, sep=" "), rel=1e-6, abs=1e-6
# )
matrix_boundaries = [
line_num
for line_num, line in enumerate(reference_output)
# Get header lines for matrices and blank last line.
if line.strip().endswith(":")
] + [len(reference_output)] # JOSH - RESUME HERE
print(f"{matrix_boundaries = }")
for header_row, end_row in pairwise(matrix_boundaries):
start_row = header_row + 1
num_rows = end_row - start_row
output = np.loadtxt(
example_output_dir / filename,
skiprows=start_row,
max_rows=num_rows,
)
reference_output = np.loadtxt(
example_output_dir / f"{filename}_ref",
skiprows=start_row,
max_rows=num_rows,
)
assert output.shape == reference_output.shape
assert output + 1 == pytest.approx(reference_output, rel=1e-6, abs=1e-6)
assert False
2 changes: 1 addition & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@ numpy = ">=2.1.3,<3"
[environments]
athena = {features = ["athena"]}
linux-64 = {features = ["linux-64"]}
pytest = {features = ["pytest"]}
pytest = {features = ["pytest", "athena"]}

0 comments on commit 4bfea6d

Please sign in to comment.