-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨Fully support decathlon datalist (#63)
* refactor dataset, add decathlon support * fix wrong import of lightning, test_kfold_crossval still fails * fixes needed for metatensor changes in monai * remove python38 support * increase version number --------- Co-authored-by: Bryn Lloyd <lloyd@itis.swiss>
- Loading branch information
Showing
32 changed files
with
785 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
import SimpleITK as sitk | ||
import typer | ||
|
||
from segmantic.utils.file_iterators import find_matching_files | ||
|
||
|
||
def check_training_data( | ||
image_dir: Path, labels_dir: Path, copy_image_information: bool = False | ||
): | ||
matches = find_matching_files([image_dir / "*.nii.gz", labels_dir / "*.nii.gz"]) | ||
for p in matches: | ||
img = sitk.ReadImage(p[0]) | ||
lbl = sitk.ReadImage(p[1]) | ||
if img.GetSize() != lbl.GetSize(): | ||
print(f"Size mismatch {p[0].name}: {img.GetSize()} != {lbl.GetSize()}") | ||
continue | ||
if copy_image_information: | ||
lbl.CopyInformation(img) | ||
sitk.WriteImage(sitk.Cast(lbl, sitk.sitkUInt8), p[1]) | ||
elif img.GetSpacing() != lbl.GetSpacing() or img.GetOrigin() != lbl.GetOrigin(): | ||
np.testing.assert_almost_equal( | ||
img.GetSpacing(), lbl.GetSpacing(), decimal=2 | ||
) | ||
np.testing.assert_almost_equal(img.GetOrigin(), lbl.GetOrigin(), decimal=2) | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(check_training_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
import SimpleITK as sitk | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
import typer | ||
|
||
|
||
def extract_unet(input_file: Path, output_file: Optional[Path] = None): | ||
"""Load segmantic unet lightning module and export inner monai UNet""" | ||
import torch | ||
|
||
from segmantic.seg.monai_unet import Net | ||
|
||
if output_file is None: | ||
output_file = input_file.with_suffix(".pth") | ||
if output_file.exists() and output_file.samefile(input_file): | ||
raise RuntimeError("Input and output file are identical") | ||
net = Net.load_from_checkpoint(input_file) | ||
torch.save(net._model.state_dict(), output_file) | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(extract_unet) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import json | ||
import random | ||
from pathlib import Path | ||
|
||
import typer | ||
|
||
from segmantic.image.labels import load_tissue_list | ||
from segmantic.utils.file_iterators import find_matching_files | ||
|
||
|
||
def make_datalist( | ||
data_dir: Path = typer.Option( | ||
..., | ||
help="root data directory. Paths in datalist will be relative to this directory", | ||
), | ||
image_dir: Path = typer.Option(..., help="Directory containing images"), | ||
labels_dir: Path = typer.Option(None, help="Directory containing labels"), | ||
datalist_path: Path = typer.Option(..., help="Filename of output datalist"), | ||
num_channels: int = 1, | ||
num_classes: int = -1, | ||
tissuelist_path: Path = None, | ||
percent: float = 1.0, | ||
description: str = "", | ||
image_glob: str = "*.nii.gz", | ||
labels_glob: str = "*.nii.gz", | ||
test_only: bool = False, | ||
seed: int = 104, | ||
) -> int: | ||
# add labels | ||
if tissuelist_path is not None: | ||
tissuelist = load_tissue_list(tissuelist_path) | ||
labels = {str(id): n for n, id in tissuelist.items() if id != 0} | ||
elif num_classes > 0: | ||
labels = {str(id): f"tissue{id:02d}" for id in range(1, num_classes + 1)} | ||
else: | ||
raise ValueError("Either specify 'tissuelist_path' or 'num_classes'") | ||
|
||
data_config = { | ||
"description": description, | ||
"num_channels": num_channels, | ||
"labels": labels, | ||
} | ||
|
||
# add all files as test files | ||
if test_only: | ||
test_files = (data_dir / image_dir).glob(image_glob) | ||
data_config["training"] = [] | ||
data_config["validation"] = [] | ||
data_config["test"] = [str(f.relative_to(data_dir)) for f in test_files] | ||
|
||
# build proper datalist with training/validation/test split | ||
else: | ||
matches = find_matching_files( | ||
[data_dir / image_dir / image_glob, data_dir / labels_dir / labels_glob] | ||
) | ||
pairs = [ | ||
(p[0].relative_to(data_dir), p[1].relative_to(data_dir)) for p in matches | ||
] | ||
|
||
random.Random(seed).shuffle(pairs) | ||
test, pairs = pairs[:10], pairs[10:] | ||
num_valid = int(percent * 0.2 * len(pairs)) | ||
num_training = len(pairs) - num_valid if percent >= 1.0 else 4 * num_valid | ||
|
||
data_config["training"] = [ | ||
{"image": str(im), "label": str(lbl)} for im, lbl in pairs[:num_training] | ||
] | ||
data_config["validation"] = [ | ||
{"image": str(im), "label": str(lbl)} for im, lbl in pairs[-num_valid:] | ||
] | ||
data_config["test"] = ([str(im) for im, _ in test],) | ||
|
||
return datalist_path.write_text(json.dumps(data_config, indent=2)) | ||
|
||
|
||
def main(): | ||
typer.run(make_datalist) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
""" ML-based segmentation for medical images | ||
""" | ||
|
||
__version__ = "0.3.0" | ||
__version__ = "0.4.0" |
Oops, something went wrong.