diff --git a/darwin/datatypes.py b/darwin/datatypes.py index c0b080fad..4525af9f2 100644 --- a/darwin/datatypes.py +++ b/darwin/datatypes.py @@ -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 diff --git a/darwin/importer/importer.py b/darwin/importer/importer.py index 6d0675a63..dc7892268 100644 --- a/darwin/importer/importer.py +++ b/darwin/importer/importer.py @@ -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: diff --git a/tests/darwin/dataset/remote_dataset_test.py b/tests/darwin/dataset/remote_dataset_test.py index e134fbb85..b15c55fff 100644 --- a/tests/darwin/dataset/remote_dataset_test.py +++ b/tests/darwin/dataset/remote_dataset_test.py @@ -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 @@ -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 - ) diff --git a/tests/darwin/datatypes_test.py b/tests/darwin/datatypes_test.py index e2fb6d662..d01a39108 100644 --- a/tests/darwin/datatypes_test.py +++ b/tests/darwin/datatypes_test.py @@ -15,6 +15,8 @@ make_polygon, parse_property_classes, split_paths_by_metadata, + Annotation, + AnnotationClass, ) @@ -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 == {}