Skip to content

Commit

Permalink
test segmentation metrics modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ziw-liu committed Dec 7, 2024
1 parent c6ac8ef commit f86e3d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ def small_hcs_dataset(tmp_path_factory: TempPathFactory) -> Path:
def labels_hcs_dataset(tmp_path_factory: TempPathFactory) -> Path:
"""Provides a small, not preprocessed HCS OME-Zarr dataset."""
dataset_path = tmp_path_factory.mktemp("labels.zarr")
_build_hcs(dataset_path, ["DAPI"], (2, 16, 16), np.uint16, 3)
_build_hcs(dataset_path, ["DAPI", "GFP"], (2, 16, 16), np.uint16, 3)
return dataset_path
33 changes: 33 additions & 0 deletions tests/translation/test_evaluation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np
import pandas as pd
import pytest
from lightning.pytorch.loggers import CSVLogger
from numpy.testing import assert_array_equal

from viscy.data.segmentation import SegmentationDataModule
from viscy.trainer import Trainer
from viscy.translation.evaluation import SegmentationMetrics2D


@pytest.mark.parametrize("pred_channel", ["DAPI", "GFP"])
def test_segmentation_metrics_2d(pred_channel, labels_hcs_dataset, tmp_path) -> None:
dm = SegmentationDataModule(
pred_dataset=labels_hcs_dataset,
target_dataset=labels_hcs_dataset,
target_channel="DAPI",
pred_channel=pred_channel,
pred_z_slice=0,
target_z_slice=0,
batch_size=1,
num_workers=0,
)
lm = SegmentationMetrics2D()
trainer = Trainer(logger=CSVLogger(tmp_path, name="", version=""))
trainer.test(lm, datamodule=dm)
metrics = pd.read_csv(tmp_path / "metrics.csv")
if pred_channel == "DAPI":
assert_array_equal(
metrics["accuracy"].to_numpy(), np.ones_like(metrics["accuracy"])
)
else:
assert 0 < metrics["accuracy"].mean() < 1

0 comments on commit f86e3d5

Please sign in to comment.