Skip to content

[DAR-5371] Removed NifTI export pixdim scaling #990

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

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 1 addition & 3 deletions darwin/cli_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ def set_file_status(dataset_slug: str, status: str, files: List[str]) -> None:
files: List[str]
Names of the files we want to update.
"""
available_statuses = ["archived", "clear", "new", "restore-archived", "complete"]
available_statuses = ["archived", "new", "restore-archived", "complete"]
if status not in available_statuses:
_error(
f"Invalid status '{status}', available statuses: {', '.join(available_statuses)}"
Expand All @@ -1075,8 +1075,6 @@ def set_file_status(dataset_slug: str, status: str, files: List[str]) -> None:
)
if status == "archived":
dataset.archive(items)
elif status == "clear":
dataset.reset(items)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the unrelated change to remove a deprecated method

elif status == "new":
dataset.move_to_new(items)
elif status == "restore-archived":
Expand Down
31 changes: 10 additions & 21 deletions darwin/dataset/remote_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,68 +467,57 @@ def fetch_remote_files(
"""

@abstractmethod
def archive(self, items: Iterator[DatasetItem]) -> None:
def archive(self, items: Iterable[DatasetItem]) -> None:
"""
Archives (soft-deletion) the given ``DatasetItem``\\s belonging to this ``RemoteDataset``.

Parameters
----------
items : Iterator[DatasetItem]
items : Iterable[DatasetItem]
The ``DatasetItem``\\s to be archived.
"""

@abstractmethod
def restore_archived(self, items: Iterator[DatasetItem]) -> None:
def restore_archived(self, items: Iterable[DatasetItem]) -> None:
"""
Restores the archived ``DatasetItem``\\s that belong to this ``RemoteDataset``.

Parameters
----------
items : Iterator[DatasetItem]
items : Iterable[DatasetItem]
The ``DatasetItem``\\s to be restored.
"""

@abstractmethod
def move_to_new(self, items: Iterator[DatasetItem]) -> None:
def move_to_new(self, items: Iterable[DatasetItem]) -> None:
"""
Changes the given ``DatasetItem``\\s status to ``new``.

Parameters
----------
items : Iterator[DatasetItem]
items : Iterable[DatasetItem]
The ``DatasetItem``\\s whose status will change.
"""

@abstractmethod
def reset(self, items: Iterator[DatasetItem]) -> None:
"""
Resets the given ``DatasetItem``\\s.

Parameters
----------
items : Iterator[DatasetItem]
The ``DatasetItem``\\s to be reset.
"""

@abstractmethod
def complete(self, items: Iterator[DatasetItem]) -> None:
def complete(self, items: Iterable[DatasetItem]) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the unrelated change to remove a deprecated method

"""
Completes the given ``DatasetItem``\\s.

Parameters
----------
items : Iterator[DatasetItem]
items : Iterable[DatasetItem]
The ``DatasetItem``\\s to be completed.
"""

@abstractmethod
def delete_items(self, items: Iterator[DatasetItem]) -> None:
def delete_items(self, items: Iterable[DatasetItem]) -> None:
"""
Deletes the given ``DatasetItem``\\s.

Parameters
----------
items : Iterator[DatasetItem]
items : Iterable[DatasetItem]
The ``DatasetItem``\\s to be deleted.
"""

Expand Down
33 changes: 11 additions & 22 deletions darwin/dataset/remote_dataset_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Sequence,
Tuple,
Union,
Iterable,
)
import numpy as np
from pydantic import ValidationError
Expand Down Expand Up @@ -362,13 +363,13 @@ def fetch_remote_files(
else:
return

def archive(self, items: Iterator[DatasetItem]) -> None:
def archive(self, items: Iterable[DatasetItem]) -> None:
"""
Archives (soft-deletion) the given ``DatasetItem``\\s belonging to this ``RemoteDataset``.

Parameters
----------
items : Iterator[DatasetItem]
items : Iterable[DatasetItem]
The ``DatasetItem``\\s to be archived.
"""
payload: Dict[str, Any] = {
Expand All @@ -379,13 +380,13 @@ def archive(self, items: Iterator[DatasetItem]) -> None:
}
self.client.api_v2.archive_items(payload, team_slug=self.team)

def restore_archived(self, items: Iterator[DatasetItem]) -> None:
def restore_archived(self, items: Iterable[DatasetItem]) -> None:
"""
Restores the archived ``DatasetItem``\\s that belong to this ``RemoteDataset``.

Parameters
----------
items : Iterator[DatasetItem]
items : Iterable[DatasetItem]
The ``DatasetItem``\\s to be restored.
"""
payload: Dict[str, Any] = {
Expand All @@ -396,13 +397,13 @@ def restore_archived(self, items: Iterator[DatasetItem]) -> None:
}
self.client.api_v2.restore_archived_items(payload, team_slug=self.team)

def move_to_new(self, items: Iterator[DatasetItem]) -> None:
def move_to_new(self, items: Iterable[DatasetItem]) -> None:
"""
Changes the given ``DatasetItem``\\s status to ``new``.

Parameters
----------
items : Iterator[DatasetItem]
items : Iterable[DatasetItem]
The ``DatasetItem``\\s whose status will change.
"""

Expand All @@ -417,25 +418,13 @@ def move_to_new(self, items: Iterator[DatasetItem]) -> None:
team_slug=self.team,
)

def reset(self, items: Iterator[DatasetItem]) -> None:
"""
Deprecated
Resets the given ``DatasetItem``\\s.

Parameters
----------
items : Iterator[DatasetItem]
The ``DatasetItem``\\s to be resetted.
"""
raise ValueError("Reset is deprecated for version 2 datasets")

def complete(self, items: Iterator[DatasetItem]) -> None:
def complete(self, items: Iterable[DatasetItem]) -> None:
"""
Completes the given ``DatasetItem``\\s.

Parameters
----------
items : Iterator[DatasetItem]
items : Iterable[DatasetItem]
The ``DatasetItem``\\s to be completed.
"""
(workflow_id, stages) = self._fetch_stages("complete")
Expand All @@ -449,13 +438,13 @@ def complete(self, items: Iterator[DatasetItem]) -> None:
team_slug=self.team,
)

def delete_items(self, items: Iterator[DatasetItem]) -> None:
def delete_items(self, items: Iterable[DatasetItem]) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing the type of items from Iterator to Iterable makes these methods more flexible

"""
Deletes the given ``DatasetItem``\\s.

Parameters
----------
items : Iterator[DatasetItem]
items : Iterable[DatasetItem]
The ``DatasetItem``\\s to be deleted.
"""
self.client.api_v2.delete_items(
Expand Down
5 changes: 2 additions & 3 deletions darwin/exporter/formats/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import re
from dataclasses import dataclass
from enum import Enum
from numbers import Number
from pathlib import Path
from typing import Dict, Iterable, List, Optional, Tuple, Union

Expand Down Expand Up @@ -538,7 +537,7 @@ def _get_reoriented_nifti_image(


def shift_polygon_coords(
polygon: List[Dict], pixdim: List[Number], legacy: bool = False
polygon: List[Dict], pixdim: List[float], legacy: bool = False
) -> List:
if legacy:
# Need to make it clear that we flip x/y because we need to take the transpose later.
Expand All @@ -549,7 +548,7 @@ def shift_polygon_coords(
else:
return [{"x": p["y"], "y": p["x"]} for p in polygon]
else:
return [{"x": p["y"] // pixdim[1], "y": p["x"] // pixdim[0]} for p in polygon]
return [{"x": p["y"], "y": p["x"]} for p in polygon]
Comment on lines -552 to +551
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the consequential change in this PR



def get_view_idx(frame_idx: int, groups: List) -> int:
Expand Down
19 changes: 0 additions & 19 deletions tests/darwin/cli_functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,25 +289,6 @@ def test_calls_dataset_archive(
)
mock.assert_called_once_with(fetch_remote_files_mock.return_value)

def test_calls_dataset_clear(
self, dataset_identifier: str, remote_dataset: RemoteDataset
):
with patch.object(
Client, "get_remote_dataset", return_value=remote_dataset
) as get_remote_dataset_mock:
with patch.object(
RemoteDatasetV2, "fetch_remote_files"
) as fetch_remote_files_mock:
with patch.object(RemoteDatasetV2, "reset") as mock:
set_file_status(dataset_identifier, "clear", ["one.jpg", "two.jpg"])
get_remote_dataset_mock.assert_called_once_with(
dataset_identifier=dataset_identifier
)
fetch_remote_files_mock.assert_called_once_with(
{"item_names": "one.jpg,two.jpg"}
)
mock.assert_called_once_with(fetch_remote_files_mock.return_value)

Comment on lines -292 to -310
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the unrelated change to remove a deprecated method

def test_calls_dataset_new(
self, dataset_identifier: str, remote_dataset: RemoteDataset
):
Expand Down
55 changes: 55 additions & 0 deletions tests/darwin/exporter/formats/export_nifti_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,58 @@ def test_export_creates_file_for_polygons_and_masks(
# Empty the directory for the next test
for output_file in video_annotation_files[video_annotation_file]:
(Path(tmpdir) / output_file).unlink()


def test_shift_polygon_coords_legacy():
"""Test the `shift_polygon_coords` function in legacy mode with different pixdim ratios."""
# Case 1: pixdim[1] > pixdim[0]
polygon = [{"x": 10, "y": 20}, {"x": 30, "y": 40}, {"x": 50, "y": 60}]
pixdim = [1.0, 2.0, 1.0]
result = nifti.shift_polygon_coords(polygon, pixdim, legacy=True)
expected = [
{"x": 20, "y": 20},
{"x": 40, "y": 60},
{"x": 60, "y": 100},
]
assert result == expected

# Case 2: pixdim[1] < pixdim[0]
polygon = [{"x": 10, "y": 20}, {"x": 30, "y": 40}, {"x": 50, "y": 60}]
pixdim = [2.0, 1.0, 1.0]
result = nifti.shift_polygon_coords(polygon, pixdim, legacy=True)
expected = [
{"x": 40, "y": 10},
{"x": 80, "y": 30},
{"x": 120, "y": 50},
]
assert result == expected

# Case 3: pixdim[1] == pixdim[0]
polygon = [{"x": 10, "y": 20}, {"x": 30, "y": 40}, {"x": 50, "y": 60}]
pixdim = [1.0, 1.0, 1.0]
result = nifti.shift_polygon_coords(polygon, pixdim, legacy=True)
expected = [
{"x": 20, "y": 10},
{"x": 40, "y": 30},
{"x": 60, "y": 50},
]
assert result == expected


def test_shift_polygon_coords_no_legacy():
"""Test the `shift_polygon_coords` function in non-legacy mode."""
polygon = [{"x": 10, "y": 20}, {"x": 30, "y": 40}, {"x": 50, "y": 60}]
pixdim = [2.0, 1.0, 1.0]
result = nifti.shift_polygon_coords(polygon, pixdim, legacy=False)
expected = [{"x": 20, "y": 10}, {"x": 40, "y": 30}, {"x": 60, "y": 50}]
assert result == expected


def test_shift_polygon_coords_empty_polygon():
"""Test the `shift_polygon_coords` function with an empty polygon."""
empty_polygon = []
pixdim = [1.0, 1.0, 1.0]
result = nifti.shift_polygon_coords(empty_polygon, pixdim, legacy=True)
assert result == []
result = nifti.shift_polygon_coords(empty_polygon, pixdim, legacy=False)
assert result == []