DLICV 0.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- DLICV/VERSION +1 -0
- DLICV/__init__.py +0 -0
- DLICV/__main__.py +73 -0
- DLICV/compute_icv.py +63 -0
- DLICV-0.0.0.dist-info/LICENSE +3 -0
- DLICV-0.0.0.dist-info/METADATA +107 -0
- DLICV-0.0.0.dist-info/RECORD +10 -0
- DLICV-0.0.0.dist-info/WHEEL +5 -0
- DLICV-0.0.0.dist-info/entry_points.txt +2 -0
- DLICV-0.0.0.dist-info/top_level.txt +1 -0
DLICV/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.0.0
|
DLICV/__init__.py
ADDED
|
File without changes
|
DLICV/__main__.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import argparse
|
|
3
|
+
import shutil
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import tempfile
|
|
6
|
+
from DLICV.compute_icv import compute_volume
|
|
7
|
+
|
|
8
|
+
def validate_path(parser, arg):
|
|
9
|
+
"""Ensure the provided path exists."""
|
|
10
|
+
if not Path(arg).exists():
|
|
11
|
+
parser.error(f"The path {arg} does not exist.")
|
|
12
|
+
sys.exit(1)
|
|
13
|
+
return arg
|
|
14
|
+
|
|
15
|
+
def copy_and_rename_inputs(input_path, destination_dir):
|
|
16
|
+
"""Copy and rename input files according to nnUNet convention."""
|
|
17
|
+
input_path = Path(input_path)
|
|
18
|
+
destination_dir = Path(destination_dir)
|
|
19
|
+
|
|
20
|
+
if input_path.is_dir():
|
|
21
|
+
for filepath in input_path.glob('*.nii.gz'):
|
|
22
|
+
new_filename = filepath.stem.split('.')[0] + '_0000.nii.gz'
|
|
23
|
+
shutil.copy(filepath, destination_dir / new_filename)
|
|
24
|
+
else:
|
|
25
|
+
new_filename = input_path.stem + '_0000.nii.gz'
|
|
26
|
+
shutil.copy(input_path, destination_dir / new_filename)
|
|
27
|
+
|
|
28
|
+
def main():
|
|
29
|
+
"""Main function to manage the prediction workflow."""
|
|
30
|
+
parser = argparse.ArgumentParser(description="DLICV - Deep Learning Intra Cranial Volume")
|
|
31
|
+
parser.add_argument("-i","--input", required=True, type=lambda x: validate_path(parser, x), help="Input .nii.gz image or folder path.")
|
|
32
|
+
parser.add_argument("-o","--output", required=True, help="Output image or folder path. If folder path, it should exist.")
|
|
33
|
+
parser.add_argument("-m","--model", required=True, type=lambda x: validate_path(parser, x), help="Model path.")
|
|
34
|
+
parser.add_argument("-v","--version", action="version", version=Path(__file__).with_name("VERSION").read_text().strip(), help="Show program's version number and exit.")
|
|
35
|
+
parser.add_argument("--kwargs", nargs=argparse.REMAINDER, help="Additional keyword arguments to pass to compute_icv.py")
|
|
36
|
+
|
|
37
|
+
args = parser.parse_args()
|
|
38
|
+
|
|
39
|
+
kwargs = {}
|
|
40
|
+
if args.kwargs:
|
|
41
|
+
for kwarg in args.kwargs:
|
|
42
|
+
key, value = kwarg.split('=', 1)
|
|
43
|
+
kwargs[key] = value
|
|
44
|
+
|
|
45
|
+
input_path, output_path, model_path = args.input, args.output, args.model
|
|
46
|
+
|
|
47
|
+
# Create cross-platform temp dir, and child dirs that nnUNet needs
|
|
48
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
|
49
|
+
temp_input_dir = Path(temp_dir) / "nnUNet_raw_data"
|
|
50
|
+
temp_preprocessed_dir = Path(temp_dir) / "nnUNet_preprocessed"
|
|
51
|
+
temp_output_dir = Path(temp_dir) / "nnUNet_out"
|
|
52
|
+
temp_input_dir.mkdir()
|
|
53
|
+
temp_output_dir.mkdir()
|
|
54
|
+
|
|
55
|
+
copy_and_rename_inputs(input_path, temp_input_dir)
|
|
56
|
+
compute_volume(
|
|
57
|
+
str(temp_input_dir),
|
|
58
|
+
str(temp_output_dir),
|
|
59
|
+
model_path,
|
|
60
|
+
**kwargs
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
# Move results to the specified output location, including only .nii.gz files
|
|
64
|
+
for file in temp_output_dir.iterdir():
|
|
65
|
+
if file.suffixes == ['.nii', '.gz']:
|
|
66
|
+
shutil.move(str(file), output_path)
|
|
67
|
+
print()
|
|
68
|
+
print()
|
|
69
|
+
print()
|
|
70
|
+
print(f"Prediction complete. Results saved to {output_path}")
|
|
71
|
+
|
|
72
|
+
if __name__ == "__main__":
|
|
73
|
+
main()
|
DLICV/compute_icv.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
def compute_volume(input_path, output_path, model_path, **kwargs):
|
|
2
|
+
"""
|
|
3
|
+
Compute the intracranial volume from the input image using the nnUNet model.
|
|
4
|
+
:param input_path: Path to the input image (single image or directory of images).
|
|
5
|
+
:param output_path: Path for the output image (single image or directory of images).
|
|
6
|
+
:param model_path: Path to the model to be applied, or 'default'.
|
|
7
|
+
:param kwargs: Additional keyword arguments for the predict_from_folder function.
|
|
8
|
+
:return: The output image with the computed volume or a list of output images.
|
|
9
|
+
"""
|
|
10
|
+
from nnunet.inference.predict import predict_from_folder
|
|
11
|
+
|
|
12
|
+
# FROM nnUNet docs: (https://github.com/MIC-DKFZ/nnUNet/blob/nnunetv1/nnunet/inference/predict.py#L614)
|
|
13
|
+
# predict_from_folder( model: str, "model output folder. Will automatically discover the folds "
|
|
14
|
+
# input_folder: str, "Must contain all modalities for each patient in the correct order (same as training). Files must be named CASENAME_XXXX.nii.gz where XXXX is the modality identifier (0000, 0001, etc)", required=True
|
|
15
|
+
# output_folder: str, "folder for saving predictions"
|
|
16
|
+
# folds: Union[Tuple[int], List[int]],"folds to use for prediction. Default is None which means that folds will be detected automatically in the model output folder"
|
|
17
|
+
# save_npz: bool, "use this if you want to ensemble these predictions with those of other models. Softmax probabilities will be saved as compresed numpy arrays in output_folder and can be merged between output_folders with merge_predictions.py"
|
|
18
|
+
# num_threads_preprocessing: int, "Determines many background processes will be used for data preprocessing. Reduce this if you run into out of memory (RAM) problems. Default: 6"
|
|
19
|
+
# num_threads_nifti_save: int, "Determines many background processes will be used for segmentation export. Reduce this if you run into out of memory (RAM) problems. Default: 6"
|
|
20
|
+
# lowres_segmentations: Union[str, None], "if model is the highres stage of the cascade then you need to use -l to specify where the segmentations of the corresponding lowres unet are. Here they are required to do a prediction"
|
|
21
|
+
# part_id: int, "Used to parallelize the prediction of the folder over several GPUs. If you want to use n GPUs to predict this folder you need to run this command n times with --part_id=0, ... n-1 and --num_parts=n (each with a different GPU (for example via CUDA_VISIBLE_DEVICES=X)"
|
|
22
|
+
# num_parts: int, "Used to parallelize the prediction of the folder over several GPUs. If you want to use n GPUs to predict this folder you need to run this command n times with --part_id=0, ... n-1 and --num_parts=n (each with a different GPU (for example via CUDA_VISIBLE_DEVICES=X)"
|
|
23
|
+
# tta: bool, "Set to 0 to disable test time data augmentation (speedup of factor 4(2D)/8(3D)), lower quality segmentations"
|
|
24
|
+
# mixed_precision: bool = True, 'Predictions are done with mixed precision by default. This improves speed and reduces the required vram. If you want to disable mixed precision you can set this flag. Note that this is not recommended (mixed precision is ~2x faster!)'
|
|
25
|
+
# overwrite_existing: bool = True, "Set this to 0 if you need to resume a previous prediction. Default: 1 (=existing segmentations in output_folder will be overwritten)"
|
|
26
|
+
# mode: str = 'normal', "'normal' or 'fast' or 'fastest'"
|
|
27
|
+
# overwrite_all_in_gpu: bool = None, "can be None, False or True"
|
|
28
|
+
# step_size: float = 0.5, "don't touch"
|
|
29
|
+
# checkpoint_name: str = "model_final_checkpoint",
|
|
30
|
+
# segmentation_export_kwargs: dict = None,
|
|
31
|
+
# disable_postprocessing: bool = False):
|
|
32
|
+
|
|
33
|
+
# FOR THE PRETRAINED MODEL WITH TASK_ID = 802 AKA DLICV
|
|
34
|
+
# Default values for predict_from_folder parameters
|
|
35
|
+
default_params = {
|
|
36
|
+
'folds': [1],
|
|
37
|
+
'save_npz': False,
|
|
38
|
+
'num_threads_preprocessing': 6,
|
|
39
|
+
'num_threads_nifti_save': 6,
|
|
40
|
+
'lowres_segmentations': None,
|
|
41
|
+
'part_id': 0, # Change this if multiple GPUs are present
|
|
42
|
+
'num_parts': 1,
|
|
43
|
+
'tta': 0,
|
|
44
|
+
'mixed_precision': True,
|
|
45
|
+
'overwrite_existing': False,
|
|
46
|
+
'mode': 'fastest',
|
|
47
|
+
'overwrite_all_in_gpu': 1,
|
|
48
|
+
'step_size': 0.5,
|
|
49
|
+
'checkpoint_name': "model_final_checkpoint",
|
|
50
|
+
'segmentation_export_kwargs': None,
|
|
51
|
+
'disable_postprocessing': False
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
# Update default parameters with any additional keyword arguments provided
|
|
55
|
+
params = {**default_params, **kwargs}
|
|
56
|
+
|
|
57
|
+
# Call predict_from_folder with updated parameters
|
|
58
|
+
predict_from_folder(model_path,
|
|
59
|
+
input_path,
|
|
60
|
+
output_path,
|
|
61
|
+
**params)
|
|
62
|
+
|
|
63
|
+
return
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: DLICV
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: DLICV - Deep Learning Intra Cranial Volume
|
|
5
|
+
Home-page: https://github.com/georgeaidinis/DLICV/
|
|
6
|
+
Download-URL: https://github.com/georgeaidinis/DLICV/
|
|
7
|
+
Author: Ashish Singh, Guray Erus, Vishnu Bashyam, George Aidinis
|
|
8
|
+
Author-email: software@cbica.upenn.edu
|
|
9
|
+
Maintainer: George Aidinis
|
|
10
|
+
Maintainer-email: aidinisg@pennmedicine.upenn.edu
|
|
11
|
+
License: By installing/using DeepMRSeg, the user agrees to the following license: See https://www.med.upenn.edu/cbica/software-agreement-non-commercial.html
|
|
12
|
+
Keywords: deep learning,image segmentation,semantic segmentation,medical image analysis,medical image segmentation,nnU-Net,nnunet
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Intended Audience :: Healthcare Industry
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Image Processing
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: nnunet >=1.7.0
|
|
23
|
+
Requires-Dist: torch <2.1,>2.0
|
|
24
|
+
Provides-Extra: test
|
|
25
|
+
Requires-Dist: nnunet >=1.7.0 ; extra == 'test'
|
|
26
|
+
Requires-Dist: torch <2.1,>2.0 ; extra == 'test'
|
|
27
|
+
Requires-Dist: pytest >=7.0.0 ; extra == 'test'
|
|
28
|
+
|
|
29
|
+
# DLICV - Deep Learning Intra Cranial Volume
|
|
30
|
+
|
|
31
|
+
## Overview
|
|
32
|
+
|
|
33
|
+
DLICV uses a trained [nnUNet](https://github.com/MIC-DKFZ/nnUNet/tree/nnunetv1) model to compute the intracranial volume from structural MRI scans in the nifti image format, oriented in _**LPS**_ orientation.
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
### As a python package
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install dlicv
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Directly from this repository
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
git clone https://github.com/georgeaidinis/DLICV
|
|
47
|
+
cd DLICV
|
|
48
|
+
conda create -n DLICV -y python=3.8 && conda activate DLICV
|
|
49
|
+
pip install .
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
A pre-trained nnUNet model can be found in the [DLICV-0.0.0 release](https://github.com/georgeaidinis/DLICV/releases/tag/v0.0.0) as an [artifact](https://github.com/georgeaidinis/DLICV/releases/download/v0.0.0/model.zip). Feel free to use it under the package's [license](LICENSE).
|
|
55
|
+
|
|
56
|
+
### Import as a python package
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from dlicv.compute_icv import compute_volume
|
|
60
|
+
|
|
61
|
+
# Assuming your nifti file is named 'input.nii.gz'
|
|
62
|
+
volume_image = compute_volume("input.nii.gz", "output.nii.gz", "path/to/model/")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### From the terminal
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
DLICV --input input.nii.gz --output output.nii.gz --model path/to/model
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Replace the `input.nii.gz` with the path to your input nifti file, as well as the model path.
|
|
72
|
+
|
|
73
|
+
Example:
|
|
74
|
+
|
|
75
|
+
Assuming a file structure like so:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
.
|
|
79
|
+
├── in
|
|
80
|
+
│ ├── input1.nii.gz
|
|
81
|
+
│ ├── input2.nii.gz
|
|
82
|
+
│ └── input3.nii.gz
|
|
83
|
+
├── model
|
|
84
|
+
│ ├── fold_0
|
|
85
|
+
│ ├── fold_1
|
|
86
|
+
│ │ ├── debug.json
|
|
87
|
+
│ │ ├── model_final_checkpoint.model
|
|
88
|
+
│ │ ├── model_final_checkpoint.model.pkl
|
|
89
|
+
│ │ ├── model_latest.model
|
|
90
|
+
│ │ ├── model_latest.model.pkl
|
|
91
|
+
│ └── plans.pkl
|
|
92
|
+
└── out
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
An example command might be:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
DLICV --input path/to/input/ --output path/to/output/ --model path/to/model/
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Using the docker container
|
|
102
|
+
|
|
103
|
+
In the docker container, the.
|
|
104
|
+
|
|
105
|
+
## Contact
|
|
106
|
+
|
|
107
|
+
For more information, please contact [CBICA Software](mailto:software@cbica.upenn.edu).
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
DLICV/VERSION,sha256=8LjHfZeNe0rr6x31osCmqnA5NomBndQGCCarbTa16pA,5
|
|
2
|
+
DLICV/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
DLICV/__main__.py,sha256=aJngXrpRjL9CrnLtAC7QPsEOAF--DdHq-zqpQVbwyro,2962
|
|
4
|
+
DLICV/compute_icv.py,sha256=7e0s1b64M1OhwLU8-51AzxjdrOMqx901xGs3JcmpvWM,5072
|
|
5
|
+
DLICV-0.0.0.dist-info/LICENSE,sha256=-vjfDd2oSVzJBPpv4IADEAhW70fsXA-gLXd8jrQDgXE,148
|
|
6
|
+
DLICV-0.0.0.dist-info/METADATA,sha256=I_t7XIb6DYYDxgWuMLCVwE4i0EPf41oeL--sspY-Nqk,3412
|
|
7
|
+
DLICV-0.0.0.dist-info/WHEEL,sha256=Xo9-1PvkuimrydujYJAjF7pCkriuXBpUPEjma1nZyJ0,92
|
|
8
|
+
DLICV-0.0.0.dist-info/entry_points.txt,sha256=eQTD_qMyJnEQU4mtVAIGeGDfaMbQWx2IuTks7sC8AUE,46
|
|
9
|
+
DLICV-0.0.0.dist-info/top_level.txt,sha256=2c2nlHGf6aIDyGc-mxALhM41Y9d7Y6fYsZ8S0_9VVx4,6
|
|
10
|
+
DLICV-0.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
DLICV
|