Skip to content

Commit b09bf22

Browse files
authored
Merge pull request #57 from Hendrik-code/dicom_extreaction
Dicom extraction, Segmentation via TPTBox, new tutorial
2 parents 25cb8f9 + 6bb20a7 commit b09bf22

File tree

15 files changed

+764
-168
lines changed

15 files changed

+764
-168
lines changed

TPTBox/core/bids_files.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,31 @@ def validate_entities(key: str, value: str, name: str, verbose: bool):
7373
if key in entity_left_right and value not in ["L", "R"]:
7474
print(f"[!] value for {key} must be in {['L', 'R']}. This name '{name}' is invalid, with value {value}")
7575
return False
76-
parts = [
77-
"mag",
78-
"phase",
79-
"real",
80-
"imag",
81-
"inphase",
82-
"outphase",
83-
"fat",
84-
"water",
85-
"eco0-opp1",
86-
"eco0-opp1",
87-
"eco1-pip1",
88-
"eco2-opp2",
89-
"eco3-in1",
90-
"eco4-pop1",
91-
"eco5-arb1",
92-
"fat-outphase",
93-
"water-outphase",
94-
"water-fraction",
95-
"fat-fraction",
96-
"r2s",
97-
]
98-
if key in entity_parts and value not in parts:
99-
print(f'[!] value for {key} must be in {parts}. This name "{name}" is invalid, with value {value}')
100-
return False
76+
# parts = [
77+
# "mag",
78+
# "phase",
79+
# "real",
80+
# "imag",
81+
# "inphase",
82+
# "outphase",
83+
# "fat",
84+
# "water",
85+
# "eco0-opp1",
86+
# "eco0-opp1",
87+
# "eco1-pip1",
88+
# "eco2-opp2",
89+
# "eco3-in1",
90+
# "eco4-pop1",
91+
# "eco5-arb1",
92+
# "fat-outphase",
93+
# "water-outphase",
94+
# "water-fraction",
95+
# "fat-fraction",
96+
# "r2s",
97+
# ]
98+
# if key in entity_parts and value not in parts:
99+
# print(f'[!] value for {key} must be in {parts}. This name "{name}" is invalid, with value {value}')
100+
# return False
101101
else:
102102
return True
103103
except Exception as e:
@@ -942,15 +942,15 @@ def open_nii(self):
942942
raise ValueError(f"nii.gz not present. Found only {self.file.keys()}\t{self.file}\n\n{self}") from e
943943

944944
def get_grid_info(self):
945-
from TPTBox.core.dicom.dicom_extract import add_grid_info_to_json
945+
from TPTBox.core.dicom.dicom_extract import _add_grid_info_to_json
946946
from TPTBox.core.nii_poi_abstract import Grid
947947

948948
nii_file = self.get_nii_file()
949949
if nii_file is None:
950950
return None
951951
if not self.has_json():
952952
self.file["json"] = Path(str(nii_file).split(".")[0] + ".json")
953-
return Grid(**add_grid_info_to_json(nii_file, self.file["json"])["Grid"])
953+
return Grid(**_add_grid_info_to_json(nii_file, self.file["json"])["grid"])
954954

955955
def get_nii_file(self):
956956
for key in _supported_nii_files:

TPTBox/core/dicom/dicom2nii_utils.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,9 @@
4242
},
4343
"pd": {"rule": "acq", "iso": "PD_FS_SPC_COR"},
4444
}
45-
# [
46-
#
47-
#
48-
#
49-
# "PD_FS_SPC_COR",
50-
# "3D_GRE_TRA_F",
51-
#
52-
#
53-
#
54-
# ]
55-
56-
57-
def test_nii(path: Path | str | BIDS_FILE):
45+
46+
47+
def __test_nii(path: Path | str | BIDS_FILE):
5848
if isinstance(path, str):
5949
path = Path(path)
6050
if path.exists():
@@ -82,7 +72,7 @@ def find_all_broken(path: str = "/DATA/NAS/datasets_processed/NAKO/dataset-nako/
8272
q = subj.new_query(flatten=True)
8373
q.filter_filetype("nii.gz")
8474
for f in q.loop_list():
85-
if not test_nii(f):
75+
if not __test_nii(f):
8676
print("BROKEN:", f)
8777
brocken.append(f)
8878
with open("broken.pkl", "wb") as w:
@@ -97,8 +87,8 @@ def find_all_broken(path: str = "/DATA/NAS/datasets_processed/NAKO/dataset-nako/
9787
)
9888

9989

100-
def test_and_replace(out_folder="/media/data/NAKO/dataset-nako2"):
101-
from TPTBox.core.dicom.dicom_extract import extract_folder
90+
def __test_and_replace(out_folder="/media/data/NAKO/dataset-nako2"):
91+
from TPTBox.core.dicom.dicom_extract import extract_dicom_folder
10292

10393
with open("/run/user/1000/gvfs/smb-share:server=172.21.251.64,share=nas/tools/TPTBox/broken.pkl", "rb") as w:
10494
brocken = pickle.load(w)
@@ -127,7 +117,7 @@ def test_and_replace(out_folder="/media/data/NAKO/dataset-nako2"):
127117
except StopIteration:
128118
continue
129119
if f2.exists():
130-
out_files.update(extract_folder(f2, Path(out_folder), make_subject_chunks=3, verbose=False))
120+
out_files.update(extract_dicom_folder(f2, Path(out_folder), make_subject_chunks=3, verbose=False))
131121
else:
132122
print(f, f.exists())
133123
else:
@@ -140,11 +130,11 @@ def test_and_replace(out_folder="/media/data/NAKO/dataset-nako2"):
140130
print((source_folder_encrypted / source_folders[mod][sub_key]) / (f"{subj}*.zip"))
141131
continue
142132
## Call the extraction
143-
out_files = extract_folder(zip_file, Path(out_folder), make_subject_chunks=3, verbose=False)
133+
out_files = extract_dicom_folder(zip_file, Path(out_folder), make_subject_chunks=3, verbose=False)
144134
## -- Testing ---
145135
# Save over brocken...
146136
for o in out_files.values():
147-
if o is not None and not test_nii(o):
137+
if o is not None and not __test_nii(o):
148138
Print_Logger().on_fail("Still Broken ", out_files)
149139

150140

@@ -156,7 +146,6 @@ def clean_dicom_data(dcm_data) -> dict:
156146
# raise NoImageError()
157147
else:
158148
del py_dataset.PixelData
159-
print(type(py_dataset))
160149
py_dict = py_dataset.to_json_dict(suppress_invalid_tags=True)
161150
for tag in ["00291010", "00291020"]:
162151
if tag in py_dict and "InlineBinary" in py_dict[tag]:
@@ -204,7 +193,7 @@ def test_name_conflict(json_ob, file):
204193
return False
205194

206195

207-
def save_json(json_ob, file):
196+
def save_json(json_ob, file, check_exist=False):
208197
"""
209198
recieves a json object and a path and saves the object as a json file
210199
"""
@@ -214,7 +203,7 @@ def convert(obj):
214203
return int(obj)
215204
raise TypeError
216205

217-
if test_name_conflict(json_ob, file):
206+
if check_exist and test_name_conflict(json_ob, file):
218207
raise FileExistsError(file)
219208
if Path(file).exists():
220209
return True
@@ -229,7 +218,7 @@ def load_json(file):
229218

230219

231220
if __name__ == "__main__":
232-
test_and_replace()
221+
__test_and_replace()
233222
# find_all_broken()
234223
# test_and_replace(
235224
# "/DATA/NAS/datasets_processed/NAKO/dataset-nako/rawdata/101/101776/mevibe/sub-101776_sequ-94_acq-ax_part-eco0-opp1_mevibe.nii.gz",

0 commit comments

Comments
 (0)