Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write one run_metadata.json file per run #100

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
cd ..
docker run --rm --shm-size=512m --volume $(pwd):/work --workdir /work wsinferimage run \
--wsi-dir slides/ --results-dir results/ --model resnet34 --weights TCGA-BRCA-v1
test -f results/run_metadata.json
test -f results/run_metadata_*.json
test -f results/patches/JP2K-33003-1.h5
test -f results/model-outputs/JP2K-33003-1.csv
test $(wc -l < results/model-outputs/JP2K-33003-1.csv) -eq 653
Expand All @@ -85,7 +85,7 @@ jobs:
wget -q https://openslide.cs.cmu.edu/download/openslide-testdata/Aperio/JP2K-33003-1.svs
cd ..
wsinfer run --wsi-dir slides/ --results-dir results/ --model resnet34 --weights TCGA-BRCA-v1
test -f results/run_metadata.json
test -f results/run_metadata_*.json
test -f results/patches/JP2K-33003-1.h5
test -f results/model-outputs/JP2K-33003-1.csv
test $(wc -l < results/model-outputs/JP2K-33003-1.csv) -eq 653
Expand Down
48 changes: 47 additions & 1 deletion tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ def test_cli_run_regression(
assert np.allclose(df.loc[:, col], col_prob)

# Test that metadata path exists.
metadata_path = results_dir / "run_metadata.json"
metadata_paths = list(results_dir.glob("run_metadata_*.json"))
assert len(metadata_paths) == 1
metadata_path = metadata_paths[0]
assert metadata_path.exists()
with open(metadata_path) as f:
meta = json.load(f)
Expand Down Expand Up @@ -1019,3 +1021,47 @@ def test_issue_94(tmp_path: Path, tiff_image: Path):
assert result.exit_code == 0
assert results_dir.joinpath("model-outputs").joinpath("purple.csv").exists()
assert not results_dir.joinpath("model-outputs").joinpath("bad.csv").exists()


def test_issue_97(tmp_path: Path, tiff_image: Path):
"""Write a run_metadata file per run."""
from wsinfer.cli.cli import cli

runner = CliRunner()
results_dir = tmp_path / "inference"
result = runner.invoke(
cli,
[
"run",
"--wsi-dir",
str(tiff_image.parent),
"--model",
"resnet34",
"--weights",
"TCGA-BRCA-v1",
"--results-dir",
str(results_dir),
],
)
assert result.exit_code == 0
metas = list(results_dir.glob("run_metadata_*.json"))
assert len(metas) == 1

# Run again...
result = runner.invoke(
cli,
[
"run",
"--wsi-dir",
str(tiff_image.parent),
"--model",
"resnet34",
"--weights",
"TCGA-BRCA-v1",
"--results-dir",
str(results_dir),
],
)
assert result.exit_code == 0
metas = list(results_dir.glob("run_metadata_*.json"))
assert len(metas) == 2
6 changes: 3 additions & 3 deletions wsinfer/cli/infer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Detect cancerous regions in a whole slide image."""

from datetime import datetime
import getpass
import json
import os
Expand Down Expand Up @@ -44,8 +45,6 @@ def _inside_container() -> str:


def _get_timestamp() -> str:
from datetime import datetime

dt = datetime.now().astimezone()
# Thu Aug 25 23:32:17 2022 EDT
return dt.strftime("%c %Z")
Expand Down Expand Up @@ -354,7 +353,8 @@ def run(
)
click.secho("\n".join(failed_inference), fg="yellow")

run_metadata_outpath = results_dir / "run_metadata.json"
timestamp = datetime.now().astimezone().strftime("%Y%m%dT%H%M%S")
run_metadata_outpath = results_dir / f"run_metadata_{timestamp}.json"
click.echo(f"Saving metadata about run to {run_metadata_outpath}")
run_metadata = _get_info_for_save(weights_obj)
with open(run_metadata_outpath, "w") as f:
Expand Down