|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import h5py |
| 6 | +import numpy as np |
| 7 | +import pytest |
| 8 | +import torch |
| 9 | +from huggingface_hub.errors import GatedRepoError |
| 10 | + |
| 11 | +from stamp.cache import download_file |
| 12 | +from stamp.preprocessing import ExtractorName, Microns, TilePixels, extract_ |
| 13 | + |
| 14 | + |
| 15 | +def test_if_feature_extraction_crashes(extractor=ExtractorName.CTRANSPATH) -> None: |
| 16 | + example_slide_path = download_file( |
| 17 | + url="https://github.com/KatherLab/STAMP/releases/download/2.0.0.dev14/TCGA-G4-6625-01Z-00-DX1.0fa26667-2581-4f96-a891-d78dbc3299b4.svs", |
| 18 | + file_name="TCGA-G4-6625-01Z-00-DX1.0fa26667-2581-4f96-a891-d78dbc3299b4.svs", |
| 19 | + sha256sum="9b7d2b0294524351bf29229c656cc886af028cb9e7463882289fac43c1347525", |
| 20 | + ) |
| 21 | + with tempfile.TemporaryDirectory(prefix="stamp_test_preprocessing_") as tmp_dir: |
| 22 | + dir = Path(tmp_dir) |
| 23 | + wsi_dir = dir / "wsis" |
| 24 | + wsi_dir.mkdir() |
| 25 | + (wsi_dir / "slide.svs").symlink_to(example_slide_path) |
| 26 | + |
| 27 | + try: |
| 28 | + extract_( |
| 29 | + wsi_dir=wsi_dir, |
| 30 | + output_dir=dir / "output", |
| 31 | + extractor=extractor, |
| 32 | + cache_dir=None, |
| 33 | + tile_size_px=TilePixels(224), |
| 34 | + tile_size_um=Microns(256.0), |
| 35 | + max_workers=min(os.cpu_count() or 1, 16), |
| 36 | + brightness_cutoff=224, |
| 37 | + canny_cutoff=0.02, |
| 38 | + device="cuda" if torch.cuda.is_available() else "cpu", |
| 39 | + ) |
| 40 | + except ModuleNotFoundError: |
| 41 | + pytest.skip(f"dependencies for {extractor} not installed") |
| 42 | + except GatedRepoError: |
| 43 | + pytest.skip(f"cannot access gated repo for {extractor}") |
| 44 | + |
| 45 | + # Check if the file has any contents |
| 46 | + with h5py.File(next((dir / "output").glob("*/*.h5"))) as h5_file: |
| 47 | + just_extracted_feats = np.array(h5_file["feats"][:]) # pyright: ignore[reportIndexIssue] |
| 48 | + |
| 49 | + assert len(just_extracted_feats) > 0 |
| 50 | + |
| 51 | + |
| 52 | +def test_if_conch_feature_extraction_crashes() -> None: |
| 53 | + test_if_feature_extraction_crashes(ExtractorName.CONCH) |
| 54 | + |
| 55 | + |
| 56 | +def test_if_uni_feature_extraction_crashes() -> None: |
| 57 | + test_if_feature_extraction_crashes(ExtractorName.UNI) |
| 58 | + |
| 59 | + |
| 60 | +def test_if_dino_bloom_feature_extraction_crashes() -> None: |
| 61 | + test_if_feature_extraction_crashes(ExtractorName.DINO_BLOOM) |
| 62 | + |
| 63 | + |
| 64 | +def test_if_virchow2_feature_extraction_crashes() -> None: |
| 65 | + test_if_feature_extraction_crashes(ExtractorName.VIRCHOW2) |
| 66 | + |
| 67 | + |
| 68 | +def test_if_empty_feature_extraction_crashes() -> None: |
| 69 | + test_if_feature_extraction_crashes(ExtractorName.EMPTY) |
| 70 | + |
| 71 | + |
| 72 | +def check_backward_compatability(extractor=ExtractorName.CTRANSPATH) -> None: |
| 73 | + example_slide_path = download_file( |
| 74 | + url="https://github.com/KatherLab/STAMP/releases/download/2.0.0.dev14/TCGA-G4-6625-01Z-00-DX1.0fa26667-2581-4f96-a891-d78dbc3299b4.svs", |
| 75 | + file_name="TCGA-G4-6625-01Z-00-DX1.0fa26667-2581-4f96-a891-d78dbc3299b4.svs", |
| 76 | + sha256sum="9b7d2b0294524351bf29229c656cc886af028cb9e7463882289fac43c1347525", |
| 77 | + ) |
| 78 | + with tempfile.TemporaryDirectory(prefix="stamp_test_preprocessing_") as tmp_dir: |
| 79 | + dir = Path(tmp_dir) |
| 80 | + wsi_dir = dir / "wsis" |
| 81 | + wsi_dir.mkdir() |
| 82 | + (wsi_dir / "slide.svs").symlink_to(example_slide_path) |
| 83 | + |
| 84 | + try: |
| 85 | + extract_( |
| 86 | + wsi_dir=wsi_dir, |
| 87 | + output_dir=dir / "output", |
| 88 | + extractor=extractor, |
| 89 | + cache_dir=None, |
| 90 | + tile_size_px=TilePixels(224), |
| 91 | + tile_size_um=Microns(256.0), |
| 92 | + max_workers=min(os.cpu_count() or 1, 16), |
| 93 | + brightness_cutoff=224, |
| 94 | + canny_cutoff=0.02, |
| 95 | + device="cuda" if torch.cuda.is_available() else "cpu", |
| 96 | + ) |
| 97 | + except ModuleNotFoundError: |
| 98 | + pytest.skip(f"dependencies for {extractor} not installed") |
| 99 | + |
| 100 | + reference_feature_path = download_file( |
| 101 | + url="https://github.com/KatherLab/STAMP/releases/download/2.0.0.dev14/ctranspath-TCGA-G4-6625-01Z-00-DX1.0fa26667-2581-4f96-a891-d78dbc3299b4.h5", |
| 102 | + file_name="ctranspath-TCGA-G4-6625-01Z-00-DX1.0fa26667-2581-4f96-a891-d78dbc3299b4.h5", |
| 103 | + sha256sum="f3f33b069c3ed860d2bdb7d65ca5db64936d7acee3ba1061a457a8cdb1bc67e3", |
| 104 | + ) |
| 105 | + |
| 106 | + with h5py.File(reference_feature_path) as h5_file: |
| 107 | + reference_feats = h5_file["feats"][:] # pyright: ignore[reportIndexIssue] |
| 108 | + reference_version = h5_file.attrs["stamp_version"] |
| 109 | + |
| 110 | + with h5py.File(next((dir / "output").glob("*/*.h5"))) as h5_file: |
| 111 | + just_extracted_feats = h5_file["feats"][:] # pyright: ignore[reportIndexIssue] |
| 112 | + |
| 113 | + assert torch.allclose( |
| 114 | + torch.tensor(just_extracted_feats), torch.tensor(reference_feats) |
| 115 | + ), ( |
| 116 | + f"extracted {extractor} features differ from those made with stamp version {reference_version}" |
| 117 | + ) |
0 commit comments