Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Fix grid during validation callback #108

Closed
wants to merge 1 commit 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
13 changes: 8 additions & 5 deletions ahcore/utils/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ def __init__(
self._annotations = _validate_annotations(data_description, annotations)
self._mask = _validate_annotations(data_description, mask)

self._reader_grid_offset = self._reader.metadata["grid_offset"]
self._grid = Grid.from_tiling(
(0, 0),
self._reader_grid_offset,
reader.size,
tile_size=self._region_size,
tile_overlap=(0, 0),
Expand Down Expand Up @@ -166,12 +167,14 @@ def _read_and_pad_region(self, coordinates: tuple[int, int]) -> pyvips.Image:
width, height = self._region_size
new_width = min(width, self._reader.size[0] - x)
new_height = min(height, self._reader.size[1] - y)
clipped_region = self._reader.read_region((x, y), 0, (new_width, new_height))

prediction = pyvips.Image.black(self._region_size[0], self._region_size[1])
prediction = prediction.insert(clipped_region, 0, 0)

return prediction
# The new width and height can be negative if the coordinates x or y are outside the image.
if new_width > 0 and new_height > 0:
clipped_region = self._reader.read_region((x, y), 0, (new_width, new_height))
prediction = prediction.insert(clipped_region, 0, 0)

return prediction.numpy()

def _get_annotation_data(
self, coordinates: tuple[int, int]
Expand Down
7 changes: 6 additions & 1 deletion ahcore/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ def consume(self, batch_generator: Generator[tuple[GenericNumberArray, GenericNu

def init_writer(self, first_coordinates: GenericNumberArray, first_batch: GenericNumberArray, file: Any) -> Any:
"""Initializes the image_dataset based on the first tile."""
self._grid_offset = (int(first_coordinates[0][0]), int(first_coordinates[0][1]))
if self._grid is None:
# If the grid is not given to the constructor, we take the first element of the coordinates as the offset.
self._grid_offset = (int(first_coordinates[0][0]), int(first_coordinates[0][1]))
else:
# If the grid is given to the constructor, we take the first element of the grid as the offset.
self._grid_offset = tuple(self._grid[0])

writer_metadata = self.get_writer_metadata(first_batch)

Expand Down