Skip to content

Commit

Permalink
Add checks / tests for AFIDs coord array shape
Browse files Browse the repository at this point in the history
Check to make sure the number of afids (shape[0]) and the spatial
dimensions (shape[1]) match what is expected from the template. This
commit additionally adds tests to check for the invalid cases by drawing
from invalid integers and asserting a substring of the error message is
raised.
  • Loading branch information
kaitj committed Jul 17, 2023
1 parent 0f68fea commit 9d911aa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
11 changes: 11 additions & 0 deletions afids_utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ def afids_to_fcsv(
)
fcsv = list(reader)

# Check to make sure shape of AFIDs array matches expected template
if afid_coords.shape[0] != len(fcsv):
raise TypeError(
f"Expected {len(fcsv)} AFIDs, but received {afid_coords.shape[0]}."
)
if afid_coords.shape[1] != 3:
raise TypeError(
"Expected 3 spatial dimensions (x, y, z),"
f"but received {afid_coords.shape[1]}."
)

# Loop over fiducials and update with fiducial spatial coordinates
for idx, row in enumerate(fcsv):
row["x"] = afid_coords[idx][0]
Expand Down
16 changes: 15 additions & 1 deletion afids_utils/tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,24 @@ def afid_coords(
min_value: float = -50.0,
max_value: float = 50.0,
width: int = 16,
bad_range: bool = False,
bad_dims: bool = False,
) -> NDArray[np.single]:

# Set (in)valid dimensions for array containing AFID coords
num_afids, spatial_dims = 32, 3
if bad_range:
num_afids = draw(
st.integers(min_value=0, max_value=100).filter(lambda x: x != 32)
)
if bad_dims:
spatial_dims = draw(
st.integers(min_value=0, max_value=10).filter(lambda x: x != 3)
)

return draw(
arrays(
shape=(32, 3),
shape=(num_afids, spatial_dims),
dtype=np.single,
elements=st.floats(
min_value=min_value, max_value=max_value, width=width
Expand Down
22 changes: 22 additions & 0 deletions afids_utils/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ def test_invalid_template(self, afids_coords: NDArray[np.single]) -> None:
"/invalid/fcsv/path",
)

@given(afids_coords=afid_coords(bad_range=True))
def test_invalid_num_afids(self, afids_coords: NDArray[np.single]) -> None:
out_fcsv_file = tempfile.NamedTemporaryFile(
mode="w", delete=False, prefix="sub-test_afids.fcsv"
)
out_fcsv_path = Path(out_fcsv_file.name)
with pytest.raises(TypeError) as err:
afids_to_fcsv(afids_coords, out_fcsv_path)

assert "AFIDs, but received" in str(err.value)

@given(afids_coords=afid_coords(bad_dims=True))
def test_invalid_afids_dims(self, afids_coords: NDArray[np.single]) -> None:
out_fcsv_file = tempfile.NamedTemporaryFile(
mode="w", delete=False, prefix="sub-test_afids.fcsv"
)
out_fcsv_path = Path(out_fcsv_file.name)
with pytest.raises(TypeError) as err:
afids_to_fcsv(afids_coords, out_fcsv_path)

assert "Expected 3 spatial dimensions" in str(err.value)

@given(afids_coords=afid_coords())
@settings(
suppress_health_check=[HealthCheck.function_scoped_fixture],
Expand Down

0 comments on commit 9d911aa

Please sign in to comment.