|
| 1 | +""" |
| 2 | +Tests for crop_generator.py utility functions. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from virtual_stain_flow.datasets.base_dataset import BaseImageDataset |
| 8 | +from virtual_stain_flow.datasets.ds_engine.crop_generator import ( |
| 9 | + _compute_center_crop, |
| 10 | + generate_center_crops, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class TestComputeCenterCrop: |
| 15 | + """Tests for _compute_center_crop function.""" |
| 16 | + |
| 17 | + def test_computes_correct_center_for_even_dimensions(self): |
| 18 | + """Should compute correct top-left for centered crop.""" |
| 19 | + x, y = _compute_center_crop(100, 100, 50) |
| 20 | + assert (x, y) == (25, 25) |
| 21 | + |
| 22 | + def test_computes_correct_center_for_odd_remainder(self): |
| 23 | + """Should floor division for odd remainder.""" |
| 24 | + x, y = _compute_center_crop(10, 10, 5) |
| 25 | + assert (x, y) == (2, 2) |
| 26 | + |
| 27 | + def test_computes_correct_center_for_rectangular_image(self): |
| 28 | + """Should handle non-square images.""" |
| 29 | + x, y = _compute_center_crop(100, 50, 20) |
| 30 | + assert (x, y) == (40, 15) |
| 31 | + |
| 32 | + def test_raises_error_when_crop_exceeds_width(self): |
| 33 | + """Should raise ValueError when crop_size > width.""" |
| 34 | + with pytest.raises(ValueError, match="exceeds image dimensions"): |
| 35 | + _compute_center_crop(10, 100, 20) |
| 36 | + |
| 37 | + def test_raises_error_when_crop_exceeds_height(self): |
| 38 | + """Should raise ValueError when crop_size > height.""" |
| 39 | + with pytest.raises(ValueError, match="exceeds image dimensions"): |
| 40 | + _compute_center_crop(100, 10, 20) |
| 41 | + |
| 42 | + |
| 43 | +class TestGenerateCenterCrops: |
| 44 | + """Tests for generate_center_crops function.""" |
| 45 | + |
| 46 | + def test_generates_center_crops_for_all_samples(self, basic_dataset): |
| 47 | + """Should generate one center crop per sample.""" |
| 48 | + crop_specs = generate_center_crops(basic_dataset, crop_size=4) |
| 49 | + |
| 50 | + # Should have 3 samples (from file_index fixture) |
| 51 | + assert len(crop_specs) == 3 |
| 52 | + assert set(crop_specs.keys()) == {0, 1, 2} |
| 53 | + |
| 54 | + def test_crop_specs_have_correct_format(self, basic_dataset): |
| 55 | + """Should return crop specs in ((x, y), width, height) format.""" |
| 56 | + crop_specs = generate_center_crops(basic_dataset, crop_size=4) |
| 57 | + |
| 58 | + for idx, crops in crop_specs.items(): |
| 59 | + assert len(crops) == 1 # One center crop per sample |
| 60 | + (x, y), w, h = crops[0] |
| 61 | + assert w == 4 |
| 62 | + assert h == 4 |
| 63 | + # For 10x10 images with crop_size=4: center is at (3, 3) |
| 64 | + assert (x, y) == (3, 3) |
| 65 | + |
| 66 | + def test_raises_error_for_non_positive_crop_size(self, basic_dataset): |
| 67 | + """Should raise ValueError for crop_size <= 0.""" |
| 68 | + with pytest.raises(ValueError, match="crop_size must be positive"): |
| 69 | + generate_center_crops(basic_dataset, crop_size=0) |
| 70 | + |
| 71 | + with pytest.raises(ValueError, match="crop_size must be positive"): |
| 72 | + generate_center_crops(basic_dataset, crop_size=-1) |
| 73 | + |
| 74 | + def test_raises_error_when_no_active_channels(self, file_index): |
| 75 | + """Should raise ValueError when no channels configured.""" |
| 76 | + dataset = BaseImageDataset( |
| 77 | + file_index=file_index, |
| 78 | + pil_image_mode="I;16", |
| 79 | + input_channel_keys=None, |
| 80 | + target_channel_keys=None, |
| 81 | + ) |
| 82 | + with pytest.raises(ValueError, match="No active channels"): |
| 83 | + generate_center_crops(dataset, crop_size=4) |
| 84 | + |
| 85 | + def test_raises_error_when_crop_too_large(self, basic_dataset): |
| 86 | + """Should raise ValueError when crop_size exceeds image dimensions.""" |
| 87 | + # Images are 10x10, so crop_size=20 should fail |
| 88 | + with pytest.raises(ValueError, match="exceeds image dimensions"): |
| 89 | + generate_center_crops(basic_dataset, crop_size=20) |
0 commit comments