Spaces:
Sleeping
Sleeping
File size: 11,076 Bytes
4780d8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
"""Shared fixtures and utilities for UI and CLI tests.
This module provides reusable fixtures for testing the Mosaic Gradio UI and CLI,
including mock file objects, settings DataFrames, cancer subtype mappings, and
utility functions for test setup/teardown.
"""
import tempfile
from pathlib import Path
from unittest.mock import Mock
import pandas as pd
import numpy as np
import pytest
from PIL import Image
# ============================================================================
# File and Path Fixtures
# ============================================================================
@pytest.fixture
def test_slide_path():
"""Path to actual test slide for integration tests."""
return Path("tests/testdata/948176.svs")
@pytest.fixture
def temp_output_dir():
"""Temporary directory for test outputs."""
with tempfile.TemporaryDirectory(prefix="mosaic_test_") as tmpdir:
yield Path(tmpdir)
@pytest.fixture
def mock_user_dir(temp_output_dir):
"""Mock user directory (same as temp_output_dir for simplicity)."""
return temp_output_dir
# ============================================================================
# Mock File Upload Fixtures
# ============================================================================
@pytest.fixture
def sample_files_single():
"""Mock single file upload."""
mock_file = Mock()
mock_file.name = "test_slide_1.svs"
return [mock_file]
@pytest.fixture
def sample_files_multiple():
"""Mock multiple file uploads (3 files)."""
files = []
for i in range(1, 4):
mock_file = Mock()
mock_file.name = f"test_slide_{i}.svs"
files.append(mock_file)
return files
def create_mock_file(filename):
"""Create a mock file object with specified filename.
Args:
filename: Name for the mock file
Returns:
Mock object with .name attribute
"""
mock_file = Mock()
mock_file.name = filename
return mock_file
# ============================================================================
# Settings DataFrame Fixtures
# ============================================================================
@pytest.fixture
def sample_settings_df():
"""Sample settings DataFrame with 3 slides."""
return pd.DataFrame(
{
"Slide": ["slide1.svs", "slide2.svs", "slide3.svs"],
"Site Type": ["Primary", "Metastatic", "Primary"],
"Sex": ["Unknown", "Female", "Male"],
"Tissue Site": ["Lung", "Liver", "Unknown"],
"Cancer Subtype": ["Unknown", "Lung Adenocarcinoma (LUAD)", "Unknown"],
"IHC Subtype": ["", "", ""],
"Segmentation Config": ["Biopsy", "Resection", "TCGA"],
}
)
def create_settings_df(n_rows, **kwargs):
"""Generate a test settings DataFrame with specified number of rows.
Args:
n_rows: Number of rows to generate
**kwargs: Column overrides (e.g., site_type="Metastatic")
Returns:
DataFrame with SETTINGS_COLUMNS
"""
defaults = {
"Slide": [f"slide_{i}.svs" for i in range(1, n_rows + 1)],
"Site Type": ["Primary"] * n_rows,
"Sex": ["Unknown"] * n_rows,
"Tissue Site": ["Unknown"] * n_rows,
"Cancer Subtype": ["Unknown"] * n_rows,
"IHC Subtype": [""] * n_rows,
"Segmentation Config": ["Biopsy"] * n_rows,
}
# Override with any provided kwargs
for key, value in kwargs.items():
column_name = key.replace("_", " ").title()
if column_name in defaults:
if isinstance(value, list):
defaults[column_name] = value
else:
defaults[column_name] = [value] * n_rows
return pd.DataFrame(defaults)
# ============================================================================
# CSV File Fixtures
# ============================================================================
@pytest.fixture
def sample_csv_valid():
"""Temporary CSV file with valid settings."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as f:
f.write(
"Slide,Site Type,Sex,Tissue Site,Cancer Subtype,IHC Subtype,Segmentation Config\n"
)
f.write("slide1.svs,Primary,Unknown,Lung,Unknown,,Biopsy\n")
f.write(
"slide2.svs,Metastatic,Female,Liver,Lung Adenocarcinoma (LUAD),,Resection\n"
)
f.write("slide3.svs,Primary,Male,Unknown,Unknown,,TCGA\n")
f.flush()
yield f.name
Path(f.name).unlink(missing_ok=True)
@pytest.fixture
def sample_csv_invalid():
"""Temporary CSV file with invalid values (for validation testing)."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as f:
f.write(
"Slide,Site Type,Sex,Tissue Site,Cancer Subtype,IHC Subtype,Segmentation Config\n"
)
f.write(
"slide1.svs,InvalidSite,InvalidSex,InvalidTissue,InvalidSubtype,InvalidIHC,InvalidConfig\n"
)
f.write(
"slide2.svs,Primary,Unknown,Lung,BRCA,HR+/HER2+,Biopsy\n"
) # Valid breast cancer
f.flush()
yield f.name
Path(f.name).unlink(missing_ok=True)
@pytest.fixture
def sample_csv_minimal():
"""Temporary CSV file with only required columns (missing optional columns)."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as f:
f.write("Slide,Site Type,Cancer Subtype\n")
f.write("slide1.svs,Primary,Unknown\n")
f.write("slide2.svs,Metastatic,LUAD\n")
f.flush()
yield f.name
Path(f.name).unlink(missing_ok=True)
# ============================================================================
# Cancer Subtype Mapping Fixtures
# ============================================================================
@pytest.fixture
def mock_cancer_subtype_maps():
"""Mock cancer subtype mappings for testing."""
cancer_subtype_name_map = {
"Unknown": "UNK",
"Lung Adenocarcinoma (LUAD)": "LUAD",
"Breast Invasive Carcinoma (BRCA)": "BRCA",
"Colorectal Adenocarcinoma (COAD)": "COAD",
"Prostate Adenocarcinoma (PRAD)": "PRAD",
}
reversed_cancer_subtype_name_map = {
"UNK": "Unknown",
"LUAD": "Lung Adenocarcinoma (LUAD)",
"BRCA": "Breast Invasive Carcinoma (BRCA)",
"COAD": "Colorectal Adenocarcinoma (COAD)",
"PRAD": "Prostate Adenocarcinoma (PRAD)",
}
cancer_subtypes = ["LUAD", "BRCA", "COAD", "PRAD"]
return cancer_subtype_name_map, reversed_cancer_subtype_name_map, cancer_subtypes
# ============================================================================
# Mock Analysis Results Fixtures
# ============================================================================
@pytest.fixture
def mock_analyze_slide_results():
"""Mock results from analyze_slide function."""
# Create a simple test mask image
mask = Image.new("RGB", (100, 100), color="red")
# Create Aeon results DataFrame
aeon_results = pd.DataFrame(
{
"Cancer Subtype": ["LUAD"],
"Confidence": [0.95],
}
)
# Create Paladin results DataFrame (NOTE: No "Slide" column - that gets added by CLI/UI)
paladin_results = pd.DataFrame(
{
"Cancer Subtype": ["LUAD", "LUAD", "LUAD"],
"Biomarker": ["TP53", "KRAS", "EGFR"],
"Score": [0.85, 0.72, 0.63],
}
)
return (mask, aeon_results, paladin_results)
@pytest.fixture
def mock_model_cache():
"""Mock ModelCache with test models."""
from unittest.mock import Mock
cache = Mock()
cache.ctranspath_model = Mock()
cache.optimus_model = Mock()
cache.marker_classifier = Mock()
cache.aeon_model = Mock()
cache.paladin_models = {}
cache.device = Mock()
cache.cleanup = Mock()
return cache
# ============================================================================
# CLI Argument Fixtures
# ============================================================================
@pytest.fixture
def cli_args_single():
"""Mock argparse Namespace for single-slide mode."""
from argparse import Namespace
return Namespace(
debug=False,
server_name="0.0.0.0",
server_port=None,
share=False,
slide_path="tests/testdata/948176.svs",
slide_csv=None,
output_dir="test_output",
site_type="Primary",
sex="Unknown",
tissue_site="Unknown",
cancer_subtype="Unknown",
ihc_subtype="",
segmentation_config="Biopsy",
num_workers=4,
)
@pytest.fixture
def cli_args_batch(sample_csv_valid):
"""Mock argparse Namespace for batch mode."""
from argparse import Namespace
return Namespace(
debug=False,
server_name="0.0.0.0",
server_port=None,
share=False,
slide_path=None,
slide_csv=sample_csv_valid,
output_dir="test_output",
site_type="Primary",
sex="Unknown",
tissue_site="Unknown",
cancer_subtype="Unknown",
ihc_subtype="",
segmentation_config="Biopsy",
num_workers=4,
)
# ============================================================================
# Utility Functions
# ============================================================================
def verify_csv_output(path, expected_columns):
"""Validate CSV file structure.
Args:
path: Path to CSV file
expected_columns: List of expected column names
Returns:
DataFrame loaded from CSV
Raises:
AssertionError: If CSV is invalid or missing columns
"""
assert Path(path).exists(), f"CSV file not found: {path}"
df = pd.read_csv(path)
assert not df.empty, f"CSV file is empty: {path}"
missing_cols = set(expected_columns) - set(df.columns)
assert not missing_cols, f"Missing columns in CSV: {missing_cols}"
return df
def mock_gradio_components():
"""Context manager to mock Gradio component classes.
Usage:
with mock_gradio_components() as mocks:
# Gradio components are mocked
result = function_that_returns_gr_components()
# Verify mocks
assert mocks['Dataframe'].called
"""
from unittest.mock import patch, Mock
mocks = {
"Dataframe": Mock(return_value=Mock()),
"File": Mock(return_value=Mock()),
"DownloadButton": Mock(return_value=Mock()),
"Dropdown": Mock(return_value=Mock()),
"Gallery": Mock(return_value=Mock()),
"Error": Exception, # gr.Error is an exception
"Warning": Mock(),
}
patches = []
for name, mock_obj in mocks.items():
patch_obj = patch(f"mosaic.ui.app.gr.{name}", mock_obj)
patches.append(patch_obj)
# Start all patches
for p in patches:
p.start()
try:
yield mocks
finally:
# Stop all patches
for p in patches:
p.stop()
|