|
| 1 | +""" |
| 2 | +Shared pytest fixtures for DeepLens test suite. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | + |
| 8 | +import pytest |
| 9 | +import torch |
| 10 | + |
| 11 | +# Add deeplens to path |
| 12 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 13 | + |
| 14 | + |
| 15 | +# ============================================================================= |
| 16 | +# Device fixtures |
| 17 | +# ============================================================================= |
| 18 | +@pytest.fixture(scope="session") |
| 19 | +def device(): |
| 20 | + """Return CUDA device if available, otherwise CPU.""" |
| 21 | + if torch.cuda.is_available(): |
| 22 | + return torch.device("cuda") |
| 23 | + else: |
| 24 | + pytest.skip("CUDA not available, skipping GPU tests") |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(scope="session") |
| 28 | +def device_cpu(): |
| 29 | + """Return CPU device.""" |
| 30 | + return torch.device("cpu") |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="session") |
| 34 | +def device_auto(): |
| 35 | + """Return CUDA if available, otherwise CPU (for tests that should run on either).""" |
| 36 | + if torch.cuda.is_available(): |
| 37 | + return torch.device("cuda") |
| 38 | + return torch.device("cpu") |
| 39 | + |
| 40 | + |
| 41 | +# ============================================================================= |
| 42 | +# Lens fixtures |
| 43 | +# ============================================================================= |
| 44 | +@pytest.fixture(scope="function") |
| 45 | +def sample_singlet_lens(device_auto): |
| 46 | + """Load a simple singlet lens for testing.""" |
| 47 | + from deeplens import GeoLens |
| 48 | + |
| 49 | + lens_path = os.path.join( |
| 50 | + os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
| 51 | + "datasets/lenses/singlet/example1.json", |
| 52 | + ) |
| 53 | + lens = GeoLens(filename=lens_path) |
| 54 | + lens.to(device_auto) |
| 55 | + return lens |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(scope="function") |
| 59 | +def sample_cellphone_lens(device_auto): |
| 60 | + """Load a cellphone lens with aspheric surfaces for testing.""" |
| 61 | + from deeplens import GeoLens |
| 62 | + |
| 63 | + lens_path = os.path.join( |
| 64 | + os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
| 65 | + "datasets/lenses/cellphone/cellphone68deg.json", |
| 66 | + ) |
| 67 | + lens = GeoLens(filename=lens_path) |
| 68 | + lens.to(device_auto) |
| 69 | + return lens |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture(scope="function") |
| 73 | +def sample_camera_lens(device_auto): |
| 74 | + """Load a camera lens for testing.""" |
| 75 | + from deeplens import GeoLens |
| 76 | + |
| 77 | + lens_path = os.path.join( |
| 78 | + os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
| 79 | + "datasets/lenses/camera/ef50mm_f1.8.json", |
| 80 | + ) |
| 81 | + lens = GeoLens(filename=lens_path) |
| 82 | + lens.to(device_auto) |
| 83 | + return lens |
| 84 | + |
| 85 | + |
| 86 | +# ============================================================================= |
| 87 | +# Image fixtures |
| 88 | +# ============================================================================= |
| 89 | +@pytest.fixture(scope="function") |
| 90 | +def sample_image(device_auto): |
| 91 | + """Create a simple test image tensor [B, C, H, W].""" |
| 92 | + # Create a gradient image for testing |
| 93 | + H, W = 256, 256 |
| 94 | + x = torch.linspace(0, 1, W, device=device_auto) |
| 95 | + y = torch.linspace(0, 1, H, device=device_auto) |
| 96 | + yy, xx = torch.meshgrid(y, x, indexing="ij") |
| 97 | + |
| 98 | + img = torch.stack([xx, yy, (xx + yy) / 2], dim=0) # [3, H, W] |
| 99 | + img = img.unsqueeze(0) # [1, 3, H, W] |
| 100 | + return img |
| 101 | + |
| 102 | + |
| 103 | +@pytest.fixture(scope="function") |
| 104 | +def sample_image_small(device_auto): |
| 105 | + """Create a small test image tensor for fast tests.""" |
| 106 | + H, W = 64, 64 |
| 107 | + img = torch.rand(1, 3, H, W, device=device_auto) |
| 108 | + return img |
| 109 | + |
| 110 | + |
| 111 | +# ============================================================================= |
| 112 | +# Ray fixtures |
| 113 | +# ============================================================================= |
| 114 | +@pytest.fixture(scope="function") |
| 115 | +def sample_ray(device_auto): |
| 116 | + """Create a sample ray for testing.""" |
| 117 | + from deeplens.optics.ray import Ray |
| 118 | + |
| 119 | + o = torch.tensor([[0.0, 0.0, -100.0]], device=device_auto) |
| 120 | + d = torch.tensor([[0.0, 0.0, 1.0]], device=device_auto) |
| 121 | + ray = Ray(o, d, wvln=0.55, device=device_auto) |
| 122 | + return ray |
| 123 | + |
| 124 | + |
| 125 | +@pytest.fixture(scope="function") |
| 126 | +def sample_rays_batch(device_auto): |
| 127 | + """Create a batch of rays for testing.""" |
| 128 | + from deeplens.optics.ray import Ray |
| 129 | + |
| 130 | + # Create 100 rays in a grid pattern |
| 131 | + n = 10 |
| 132 | + x = torch.linspace(-1, 1, n, device=device_auto) |
| 133 | + y = torch.linspace(-1, 1, n, device=device_auto) |
| 134 | + yy, xx = torch.meshgrid(y, x, indexing="ij") |
| 135 | + |
| 136 | + o = torch.stack([xx.flatten(), yy.flatten(), torch.full((n*n,), -100.0, device=device_auto)], dim=-1) |
| 137 | + d = torch.zeros_like(o) |
| 138 | + d[..., 2] = 1.0 |
| 139 | + |
| 140 | + ray = Ray(o, d, wvln=0.55, device=device_auto) |
| 141 | + return ray |
| 142 | + |
| 143 | + |
| 144 | +# ============================================================================= |
| 145 | +# Path helpers |
| 146 | +# ============================================================================= |
| 147 | +@pytest.fixture(scope="session") |
| 148 | +def project_root(): |
| 149 | + """Return the project root directory.""" |
| 150 | + return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 151 | + |
| 152 | + |
| 153 | +@pytest.fixture(scope="session") |
| 154 | +def lenses_dir(project_root): |
| 155 | + """Return the lenses dataset directory.""" |
| 156 | + return os.path.join(project_root, "datasets/lenses") |
| 157 | + |
| 158 | + |
| 159 | +@pytest.fixture(scope="session") |
| 160 | +def test_output_dir(project_root): |
| 161 | + """Return a directory for test outputs.""" |
| 162 | + output_dir = os.path.join(project_root, "test/test_outputs") |
| 163 | + os.makedirs(output_dir, exist_ok=True) |
| 164 | + return output_dir |
0 commit comments