Skip to content

Commit

Permalink
Tests for
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWilkie committed Jan 16, 2025
1 parent 5412327 commit 703c6e4
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 32 deletions.
8 changes: 0 additions & 8 deletions darwin/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,11 @@ def scale_coordinates(self, x_scale: float, y_scale: float) -> None:
y_scale : float
Scale factor for y coordinates
"""
if (
getattr(self, "annotation_class", None)
and self.annotation_class.name == "__raster_layer__"
):
return

annotation_type = (
self.annotation_class.annotation_type
if hasattr(self, "annotation_class")
else None
)
if not annotation_type:
return

if annotation_type == "bounding_box":
self.data["x"] *= x_scale
Expand Down
8 changes: 4 additions & 4 deletions darwin/importer/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2442,10 +2442,10 @@ def _get_remote_medical_file_transform_requirements(
elif primary_plane == "CORONAL":
pixdims = [pixdims[1], pixdims[2]]
slot_pixdim_map[slot_name] = pixdims
if slot_pixdim_map:
remote_files_that_require_pixel_to_mm_transform[
remote_file.full_path
] = slot_pixdim_map
if slot_pixdim_map:
remote_files_that_require_pixel_to_mm_transform[remote_file.full_path] = (
slot_pixdim_map
)
else:
slot_affine_map = {}
for slot in remote_file.slots:
Expand Down
20 changes: 0 additions & 20 deletions tests/darwin/dataset/remote_dataset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path
from typing import Any, Dict
from unittest.mock import MagicMock, patch
import numpy as np

import orjson as json
import pytest
Expand Down Expand Up @@ -1948,22 +1947,3 @@ def mock_remote_files(self):
current_workflow=None,
),
]

@patch.object(RemoteDatasetV2, "fetch_remote_files")
def test_get_remote_files_that_require_legacy_nifti_scaling(
self, mock_fetch_remote_files, mock_remote_files
):
mock_fetch_remote_files.return_value = mock_remote_files
remote_dataset = RemoteDatasetV2(
client=MagicMock(),
team="test-team",
name="test-dataset",
slug="test-dataset",
dataset_id=1,
)

result = remote_dataset._get_remote_files_that_require_legacy_nifti_scaling()
assert Path("/path/to/file/filename") in result
np.testing.assert_array_equal(
result[Path("/path/to/file/filename")]["0"], np.array([[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]]) # type: ignore
)
115 changes: 115 additions & 0 deletions tests/darwin/datatypes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
make_polygon,
parse_property_classes,
split_paths_by_metadata,
Annotation,
AnnotationClass,
)


Expand Down Expand Up @@ -188,3 +190,116 @@ def test_repr(self, object_store):
repr(object_store)
== "ObjectStore(name=test, prefix=test_prefix, readonly=False, provider=aws)"
)


class TestAnnotation:
def test_scale_coordinates_bounding_box(self):
annotation = Annotation(
annotation_class=AnnotationClass("test_bb", "bounding_box"),
data={"x": 10, "y": 20, "w": 30, "h": 40},
)
annotation.scale_coordinates(x_scale=2, y_scale=1.5)
assert annotation.data == {"x": 20, "y": 30, "w": 60, "h": 60}

def test_scale_coordinates_polygon(self):
annotation = Annotation(
annotation_class=AnnotationClass("test_polygon", "polygon"),
data={
"paths": [
[
{"x": 0, "y": 0},
{"x": 100, "y": 0},
{"x": 100, "y": 100},
{"x": 0, "y": 100},
{"x": 0, "y": 0},
],
[
{"x": 100, "y": 0},
{"x": 200, "y": 0},
{"x": 200, "y": 100},
{"x": 100, "y": 100},
{"x": 100, "y": 0},
],
]
},
)
annotation.scale_coordinates(x_scale=2, y_scale=1.5)
assert annotation.data == {
"paths": [
[
{"x": 0, "y": 0},
{"x": 200, "y": 0},
{"x": 200, "y": 150},
{"x": 0, "y": 150},
{"x": 0, "y": 0},
],
[
{"x": 200, "y": 0},
{"x": 400, "y": 0},
{"x": 400, "y": 150},
{"x": 200, "y": 150},
{"x": 200, "y": 0},
],
]
}

def test_scale_coordinates_ellipse(self):
annotation = Annotation(
annotation_class=AnnotationClass("test_ellipse", "ellipse"),
data={
"center": {"x": 0, "y": 0},
"radius": {"x": 100, "y": 50},
},
)
annotation.scale_coordinates(x_scale=2, y_scale=1.5)
assert annotation.data == {
"center": {"x": 0, "y": 0},
"radius": {"x": 200, "y": 75},
}

def test_scale_coordinates_line(self):
annotation = Annotation(
annotation_class=AnnotationClass("test_line", "line"),
data={"path": [{"x": 0, "y": 0}, {"x": 100, "y": 100}]},
)
annotation.scale_coordinates(x_scale=2, y_scale=1.5)
assert annotation.data == {"path": [{"x": 0, "y": 0}, {"x": 200, "y": 150}]}

def test_scale_coordinates_keypoint(self):
annotation = Annotation(
annotation_class=AnnotationClass("test_keypoint", "keypoint"),
data={"x": 50, "y": 100},
)
annotation.scale_coordinates(x_scale=2, y_scale=1.5)
assert annotation.data == {"x": 100, "y": 150}

def test_scale_coordinates_skeleton(self):
annotation = Annotation(
annotation_class=AnnotationClass("test_skeleton", "skeleton"),
data={
"nodes": [
{"x": 0, "y": 0},
{"x": 100, "y": 100},
]
},
)
annotation.scale_coordinates(x_scale=2, y_scale=1.5)
assert annotation.data == {"nodes": [{"x": 0, "y": 0}, {"x": 200, "y": 150}]}

def test_scale_coordinates_skips_raster_layer(self):
annotation = Annotation(
annotation_class=AnnotationClass("__raster_layer__", "raster_layer"),
data={"dense_rle": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]},
)
annotation.scale_coordinates(x_scale=2, y_scale=1.5)
assert annotation.data == {
"dense_rle": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
}

def test_scale_coordinates_skips_tag(self):
annotation = Annotation(
annotation_class=AnnotationClass("test_tag", "tag"),
data={},
)
annotation.scale_coordinates(x_scale=2, y_scale=1.5)
assert annotation.data == {}

0 comments on commit 703c6e4

Please sign in to comment.