forked from MIC-DKFZ/nnDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
109ca2b
commit e8f3f5b
Showing
5 changed files
with
155 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# RibFrac | ||
**Disclaimer**: We are not the host of the data. | ||
Please make sure to read the requirements and usage policies of the data and **give credit to the authors of the dataset**! | ||
|
||
Please read the information from the homepage carefully and follow the rules and instructions provided by the original authors when using the data. | ||
- Homepage: https://ribfrac.grand-challenge.org/ | ||
- Subtask: Task 1 | ||
|
||
## Setup | ||
0. Follow the installation instructions of nnDetection and create a data directory name `Task020FG_RibFrac`. We added FG to the ID to indicate that we don't distinguish the different classes. (even if you prepare the data set with classes, the data needs to be placed inside that directory) | ||
1. Follow the instructions and usage policies to download the data and copy the data/labels/csv files to the following locations: | ||
data -> `Task020FG_RibFrac / raw / imagesTr`; labels -> `Task020FG_RibFrac / raw / labelsTr`; csv files -> `Task020FG_RibFrac / raw` | ||
2. Run `python prepare.py` in `projects / Task020FG_RibFrac / scripts` of the nnDetection repository. | ||
|
||
Note: If no manual split is created, nnDetection will create a random 5Fold split which we used for results. | ||
|
||
The data is now converted to the correct format and the instructions from the nnDetection README can be used to train the networks. |
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,106 @@ | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
import pandas as pd | ||
from tqdm import tqdm | ||
|
||
from nndet.io import save_json | ||
from nndet.utils.check import env_guard | ||
|
||
|
||
def create( | ||
image_source: Path, | ||
label_source: Path, | ||
image_target_dir: Path, | ||
label_target_dir: Path, | ||
df: pd.DataFrame, | ||
fg_only: bool = False, | ||
): | ||
image_target_dir.mkdir(parents=True, exist_ok=True) | ||
label_target_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
case_id = image_source.stem.rsplit('-', 1)[0] | ||
case_id_check = label_source.stem.rsplit('-', 1)[0] | ||
assert case_id == case_id_check, f"case ids not matching, found image {case_id} and label {case_id_check}" | ||
|
||
df_case = df.loc[df['public_id'] == case_id] | ||
instances = {} | ||
for row in df_case.itertuples(): | ||
_cls = int(row.label_code) | ||
if _cls == 0: # background has label code 0 and lab id 0 | ||
continue | ||
|
||
if fg_only: | ||
_cls = 1 | ||
elif _cls == -1: | ||
_cls = 5 | ||
|
||
instances[str(row.label_id)] = _cls - 1 # class range from 0 - 4 // if fg only 0 | ||
assert 0 < _cls < 6, f"Something strange happened {_cls}" | ||
save_json({"instances": instances}, label_target_dir / f"{case_id}.json") | ||
|
||
shutil.copy2(image_source, image_target_dir / f"{case_id}_0000.nii.gz") | ||
shutil.copy2(label_source, label_target_dir / f"{case_id}.nii.gz") | ||
|
||
|
||
@env_guard | ||
def main(): | ||
det_data_dir = Path(os.getenv('det_data')) | ||
task_data_dir = det_data_dir / "Task020_RibFrac" | ||
source_data_dir = task_data_dir / "raw" | ||
|
||
if not source_data_dir.is_dir(): | ||
raise RuntimeError(f"{source_data_dir} should contain the raw data but does not exist.") | ||
if not (p := source_data_dir / "imagesTr").is_dir(): | ||
raise ValueError(f"Expected data to be located at {p}") | ||
if not (p := source_data_dir / "labelsTr").is_dir(): | ||
raise ValueError(f"Expected labels to be located at {p}") | ||
if not (p := source_data_dir / "ribfrac-train-info-1.csv").is_file(): | ||
raise ValueError(f"Expected {p} to exist.") | ||
if not (p := source_data_dir / "ribfrac-train-info-2.csv").is_file(): | ||
raise ValueError(f"Expected {p} to exist.") | ||
if not (p := source_data_dir / "ribfrac-val-info.csv").is_file(): | ||
raise ValueError(f"Expected {p} to exist.") | ||
|
||
target_data_dir = task_data_dir / "raw_splitted" / "imagesTr" | ||
target_data_dir.mkdir(exist_ok=True, parents=True) | ||
target_label_dir = task_data_dir / "raw_splitted" / "labelsTr" | ||
target_label_dir.mkdir(exist_ok=True, parents=True) | ||
|
||
csv_fies = [source_data_dir / "ribfrac-train-info-1.csv", | ||
source_data_dir / "ribfrac-train-info-2.csv", | ||
source_data_dir / "ribfrac-val-info.csv"] | ||
df = pd.concat([pd.read_csv(f) for f in csv_fies]) | ||
|
||
image_paths = list((source_data_dir / "imagesTr").glob("*.nii.gz")) | ||
image_paths.sort() | ||
label_paths = list((source_data_dir / "labelsTr").glob("*.nii.gz")) | ||
label_paths.sort() | ||
|
||
print(f"Found {len(image_paths)} data files and {len(label_paths)} label files.") | ||
assert len(image_paths) == len(label_paths) | ||
|
||
meta = { | ||
"name": "RibFracFG", | ||
"task": "Task020FG_RibFrac", | ||
"target_class": None, | ||
"test_labels": False, | ||
"labels": {"0": "fracture"}, # since we are running FG vs BG this is not completely correct | ||
"modalities": {"0": "CT"}, | ||
"dim": 3, | ||
} | ||
save_json(meta, task_data_dir / "dataset.json") | ||
|
||
for ip, lp in tqdm(list(zip(image_paths, label_paths))): | ||
create(image_source=ip, | ||
label_source=lp, | ||
image_target_dir=target_data_dir, | ||
label_target_dir=target_label_dir, | ||
df=df, | ||
fg_only=True, | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |