Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate AfidSet for testing #24

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion afids_utils/afids.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AfidPosition:
x: float = attrs.field()
y: float = attrs.field()
z: float = attrs.field()
desc: str = attrs.field()
desc: str = attrs.field(default="")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not have a default value that's invalid here (even if you want to leave the invalid descs in the test for now)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I think alternatively I can load in the valid descs and any invalid ones set to "Unknown".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me, or you can just leave it without a default until we implement the validation



@attrs.define
Expand Down
21 changes: 15 additions & 6 deletions afids_utils/tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@
from hypothesis.extra.numpy import arrays
from numpy.typing import NDArray

from afids_utils.afids import AfidPosition
from afids_utils.afids import AfidPosition, AfidSet


@st.composite
def afid_coords(
def afid_set(
kaitj marked this conversation as resolved.
Show resolved Hide resolved
draw: st.DrawFn,
min_value: float = -50.0,
max_value: float = 50.0,
width: int = 16,
bad_range: bool = False,
) -> list[AfidPosition]:
# Set (in)valid number of Afid coordinates for array containing AFID coords
slicer_version = draw(st.from_regex(r"\d+\.\d+"))
coord_system = draw(st.sampled_from(["LPS", "RAS", "0", "1"]))

# Set (in)valid number of Afid coordinates in a list
afid_pos = []
num_afids = (
32
if not bad_range
else draw(
st.integers(min_value=0, max_value=100).filter(lambda x: x != 32)
)
)

afid_pos = []
for afid in range(num_afids):
afid_pos.append(
AfidPosition(
Expand All @@ -49,7 +51,14 @@ def afid_coords(
)
)

return afid_pos
# Create AfidSet
st_afid_set = AfidSet(
slicer_version=slicer_version,
coord_system=coord_system,
afids=afid_pos,
)

return st_afid_set


@st.composite
Expand Down
35 changes: 18 additions & 17 deletions afids_utils/tests/test_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
from hypothesis import HealthCheck, assume, given, settings
from hypothesis import strategies as st

from afids_utils.afids import AfidPosition
from afids_utils.afids import AfidPosition, AfidSet
from afids_utils.exceptions import InvalidFileError
from afids_utils.ext.fcsv import (
FCSV_FIELDNAMES,
_get_afids,
_get_metadata,
save_fcsv,
)
from afids_utils.tests.strategies import afid_coords
from afids_utils.tests.strategies import afid_set


@pytest.fixture
Expand Down Expand Up @@ -164,32 +164,29 @@ def test_valid_get_afids(self, valid_fcsv_file: PathLike[str], label: int):


class TestSaveFcsv:
@given(afids_coords=afid_coords())
@given(afids_set=afid_set())
kaitj marked this conversation as resolved.
Show resolved Hide resolved
@settings(
suppress_health_check=[HealthCheck.function_scoped_fixture],
)
def test_save_fcsv_invalid_template(
self,
afids_coords: list[AfidPosition],
valid_fcsv_file: PathLike[str],
afids_set: AfidSet,
):
with pytest.raises(FileNotFoundError):
save_fcsv(afids_coords, "/invalid/template/path.fcsv")
save_fcsv(afids_set.afids, "/invalid/template/path.fcsv")

@given(afids_coords=afid_coords())
@given(afids_set=afid_set())
@settings(
suppress_health_check=[HealthCheck.function_scoped_fixture],
)
def test_save_fcsv_valid_template(
self,
afids_coords: list[AfidPosition],
valid_fcsv_file: PathLike[str],
self, afids_set: list[AfidPosition], valid_fcsv_file: PathLike[str]
):
with tempfile.NamedTemporaryFile(
mode="w", prefix="sub-test_desc-", suffix="_afids.fcsv"
) as out_fcsv_file:
# Create and check output file
save_fcsv(afids_coords, out_fcsv_file.name)
save_fcsv(afids_set.afids, out_fcsv_file.name)

# Load files
with open(
Expand All @@ -211,17 +208,21 @@ def test_save_fcsv_valid_template(
# Check contents
for idx, row in enumerate(output_fcsv):
assert (row["x"], row["y"], row["z"]) == (
str(afids_coords[idx].x),
str(afids_coords[idx].y),
str(afids_coords[idx].z),
str(afids_set.afids[idx].x),
str(afids_set.afids[idx].y),
str(afids_set.afids[idx].z),
)

@given(afids_coords=afid_coords(bad_range=True))
def test_invalid_num_afids(self, afids_coords: list[AfidPosition]) -> None:
# Check to see if file can be loaded with afids-utils
test_load = AfidSet.load(out_fcsv_file.name)
assert isinstance(test_load, AfidSet)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could even assert test_load == afids_set

Copy link
Contributor Author

@kaitj kaitj Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yeah, this test can be simplified quite a bit with the new afid_sets strategy (and slight modification since it also randomly generates the slicer version by default)


@given(afids_set=afid_set(bad_range=True))
def test_invalid_num_afids(self, afids_set: list[AfidPosition]) -> None:
with tempfile.NamedTemporaryFile(
mode="w", prefix="sub-test_desc-", suffix="_afids.fcsv"
) as out_fcsv_file:
with pytest.raises(TypeError) as err:
save_fcsv(afids_coords, out_fcsv_file)
save_fcsv(afids_set.afids, out_fcsv_file)

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