Skip to content

Commit

Permalink
changes made to add new error unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nooshin Ghavami authored and Nooshin Ghavami committed May 5, 2023
1 parent cf1e0dc commit 7d0efa2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions tests/darwin/importer/formats/import_nifti_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from darwin.importer.formats.nifti import parse_path


from tests.fixtures import *


Expand Down Expand Up @@ -72,6 +73,38 @@ def test_image_annotation_nifti_import_multi_slot(team_slug: str):
assert output_json_string["annotations"][0]["frames"] == expected_json_string["annotations"][0]["frames"]


def test_image_annotation_nifti_import_incorrect_number_slot(team_slug: str):
with tempfile.TemporaryDirectory() as tmpdir:
with ZipFile("tests/data.zip") as zfile:
zfile.extractall(tmpdir)
label_path = (
Path(tmpdir) / team_slug / "nifti" / "releases" / "latest" / "annotations" / "vol0_brain.nii.gz"
)
input_dict = {
"data": [
{
"image": "vol0 (1).nii",
"label": str(label_path),
"class_map": {"1": "brain"},
"mode": "video",
"is_mpr": True,
"slot_names": ["0.3", "0.2"],
}
]
}
upload_json = Path(tmpdir) / "annotations.json"
upload_json.write_text(json.dumps(input_dict, indent=4, sort_keys=True, default=str))
annotation_files = parse_path(path=upload_json)
annotation_file = annotation_files[0]
output_json_string = json.loads(serialise_annotation_file(annotation_file, as_dict=False))
expected_json_string = json.load(
open(Path(tmpdir) / team_slug / "nifti" / "vol0_annotation_file_incorrect_number_slot.json", "r")
)
json.dump(output_json_string, open("test_output_for_nifti_import_test_incorrect_number_slot.json", "w"), indent=4)

assert len(input_dict["data"][0]["slot_names"]) != 3, "Slot length needs to be 3"


def serialise_annotation_file(annotation_file: AnnotationFile, as_dict) -> Union[str, dict]:
"""
Serialises an ``AnnotationFile`` into a string.
Expand Down Expand Up @@ -181,26 +214,27 @@ def serialise_sub_annotation(sub_annotation: SubAnnotation, as_dict: bool = True
"data": [
{
"image": "vol0 (1).nii",
"label": "tests/v7/v7-darwin-json-v1/nifti/releases/latest/annotations/vol0_brain.nii.gz",
"class_map": {
"1": "brain"
},
"is_mpr": true,
"slot_names": ["0.3", "0.2", "0.1"],
"mode": "video"
"label": str(label_path),
"class_map": {"1": "brain"},
"mode": "video",
"is_mpr": True,
"slot_names": ["0.3", "0.2"],
}
]
}
"""
with tempfile.TemporaryDirectory() as tmp_dir:
print(tmp_dir)
path = Path(tmp_dir) / "annotations.json"
path.write_text(input_json_string)
print(path)
annotation_files = parse_path(path=path)
annotation_files = parse_path(path=path)
if isinstance(annotation_files, list):
annotation_file = annotation_files[0]
output_json_string = serialise_annotation_file(annotation_file, as_dict=False)
with open(
Path("tests") / "v7" / "v7-darwin-json-v1" / "nifti" / "vol0_annotation_file_multi_slot.json", "w"
#with open(
# Path("tests") / "v7" / "v7-darwin-json-v1" / "nifti" / "vol0_annotation_file_incorrect_number_slot.json", "w"
with open("Users" /"nooshinghavami" / "Desktop" / "nooshin" / "darwin-py" / "tests" / "data" / "v7-darwin-json-v1" / "nifti" / "vol0_annotation_file_incorrect_number_slot.json", "w"

This comment has been minimized.

Copy link
@tomvars

tomvars May 5, 2023

Contributor

Best to remove your name from the directory here @NooshinGhavami

) as f:
f.write(output_json_string)

Binary file modified tests/data.zip
Binary file not shown.

0 comments on commit 7d0efa2

Please sign in to comment.