Skip to content

Commit

Permalink
fixed formatting problems with folder names
Browse files Browse the repository at this point in the history
Former-commit-id: 437db52
  • Loading branch information
fishingguy456 committed Jun 10, 2022
1 parent 0de879b commit 1dde8cd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 43 deletions.
49 changes: 29 additions & 20 deletions examples/autotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def __init__(self,
show_progress=False,
warn_on_error=False,
overwrite=False,
generate_sparsemask=False,
nnUnet_info=None):

super().__init__(
Expand All @@ -53,9 +52,8 @@ def __init__(self,
self.output_directory = pathlib.Path(output_directory).as_posix()
self.spacing = spacing
self.existing = [None] #self.existing_patients()
self.generate_sparsemask = generate_sparsemask
self.nnUnet_info = nnUnet_info
if nnUnet_info:
self.nnUnet_info = nnUnet_info
self.nnUnet_info["modalities"] = {"CT": "0000"}
self.nnUnet_info["index"] = 0

Expand Down Expand Up @@ -107,7 +105,8 @@ def process_one_subject(self, subject_id):
metadata = {}
subject_modalities = set()
num_rtstructs = 0
self.nnUnet_info["index"] += 1
if self.nnUnet_info:
self.nnUnet_info["index"] += 1
for i, colname in enumerate(self.output_streams):
modality = colname.split("_")[0]
subject_modalities.add(modality)
Expand Down Expand Up @@ -195,7 +194,7 @@ def process_one_subject(self, subject_id):
sparse_mask = mask.generate_sparse_mask().mask_array
sparse_mask = sitk.GetImageFromArray(sparse_mask)
# save_path = pathlib.Path(self.output_directory, subject_id, "sparse_mask", "sparse_mask.nii.gz").as_posix()
self.output(subject_id, sparse_mask, output_stream, nnUnet_info=self.nnUnet_info, nnUnet_is_label=True)
self.output(subject_id, sparse_mask, output_stream, nnUnet_info=self.nnUnet_info, label_or_image="labels")
# sparse_mask_nifti = nib.Nifti1Image(sparse_mask.mask_array, affine=np.eye(4))
# nib.save(sparse_mask_nifti, save_path)
# self.output("sparse_mask", sparse_mask, output_stream, "sparse_mask")
Expand Down Expand Up @@ -282,33 +281,43 @@ def run(self):
print("Dataset already processed...")
shutil.rmtree(pathlib.Path(self.output_directory, ".temp").as_posix())
else:
Parallel(n_jobs=self.n_jobs, verbose=verbose)(
delayed(self._process_wrapper)(subject_id) for subject_id in subject_ids)
# Parallel(n_jobs=self.n_jobs, verbose=verbose)(
# delayed(self._process_wrapper)(subject_id) for subject_id in subject_ids)
for subject_id in subject_ids:
self._process_wrapper(subject_id)
self.save_data()


if __name__ == "__main__":
pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/datasetshort/manifest-1598890146597/NSCLC-Radiomics-Interobserver1",
output_directory="C:/Users/qukev/BHKLAB/autopipelineoutputshort",
modalities="CT,RTSTRUCT",
visualize=False,
overwrite=True,
generate_sparsemask=True,
nnUnet_info={"study name": "NSCLC-Radiomics-Interobserver1"})

# pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/hnscc_testing/HNSCC",
# output_directory="C:/Users/qukev/BHKLAB/hnscc_testing_output",
# pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/datasetshort/manifest-1598890146597/NSCLC-Radiomics-Interobserver1",
# output_directory="C:/Users/qukev/BHKLAB/autopipelineoutputshort",
# modalities="CT,RTSTRUCT",
# visualize=False,
# overwrite=True)
# pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/datasetshort/manifest-1598890146597/NSCLC-Radiomics-Interobserver1",
# output_directory="C:/Users/qukev/BHKLAB/autopipelineoutputshort",
# modalities="CT,RTSTRUCT",
# visualize=False,
# overwrite=True,
# generate_sparsemask=True)

# nnUnet_info={"study name": "NSCLC-Radiomics-Interobserver1"})
# pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/dataset/manifest-1598890146597/NSCLC-Radiomics-Interobserver1",
# output_directory="C:/Users/qukev/BHKLAB/autopipelineoutput",
# modalities="CT,RTSTRUCT",
# visualize=False,
# overwrite=True,
# generate_sparsemask=True)
# nnUnet_info={"study name": "NSCLC-Radiomics-Interobserver1"})

# pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/hnscc_testing/HNSCC",
# output_directory="C:/Users/qukev/BHKLAB/hnscc_testing_output",
# modalities="CT,RTSTRUCT",
# visualize=False,
# overwrite=True)

pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/dataset/manifest-1598890146597/NSCLC-Radiomics-Interobserver1",
output_directory="C:/Users/qukev/BHKLAB/autopipelineoutput",
modalities="CT,RTSTRUCT",
visualize=False,
overwrite=True)

# pipeline = AutoPipeline(input_directory="C:/Users/qukev/BHKLAB/hnscc_pet/PET",
# output_directory="C:/Users/qukev/BHKLAB/hnscc_pet_output",
Expand Down
34 changes: 18 additions & 16 deletions imgtools/io/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,35 @@ def __init__(self, root_directory, filename_format="{subject_id}.nii.gz", create
#delete the folder called {subject_id} that was made in the original BaseWriter


def put(self, subject_id, image, is_mask=False, nnUnet_info=None, nnUnet_is_label=False, mask_label="",**kwargs):
def put(self, subject_id, image, is_mask=False, nnUnet_info=None, label_or_image: str = "images", mask_label="", **kwargs):
if is_mask:
self.filename_format = mask_label+".nii.gz"
if nnUnet_info:
if nnUnet_is_label:
self.filename_format = f"{nnUnet_info['study name']}_{nnUnet_info['index']}.nii.gz"
if label_or_image == "labels":
filename = f"{nnUnet_info['study name']}_{nnUnet_info['index']}.nii.gz"
else:
# f"{nnUnet_info['study name']}_{nnUnet_info['index']}_{nnUnet_info['modalities'][nnUnet_info['current_modality']]}.nii.gz"
self.filename_format = self.filename_format.format(study_name=nnUnet_info['study name'], index=nnUnet_info['index'], modality_index=nnUnet_info['modalities'][nnUnet_info['current_modality']])
out_path = self._get_path_from_subject_id(subject_id, nnUnet_is_label=nnUnet_is_label, **kwargs)
filename = self.filename_format.format(study_name=nnUnet_info['study name'], index=nnUnet_info['index'], modality_index=nnUnet_info['modalities'][nnUnet_info['current_modality']])
out_path = self._get_path_from_subject_id(filename, label_or_image=label_or_image)
else:
out_path = self._get_path_from_subject_id(self.filename_format, subject_id=subject_id)
sitk.WriteImage(image, out_path, self.compress)

def _get_path_from_subject_id(self, subject_id, nnUnet_is_label=False, **kwargs):
def _get_path_from_subject_id(self, filename, **kwargs):
# out_filename = self.filename_format.format(subject_id=subject_id, **kwargs)
try:
self.root_directory = self.root_directory.format(subject_id=subject_id,
**kwargs)
except:
if nnUnet_is_label:
self.root_directory = self.root_directory.format(label_or_image="labels")
else:
self.root_directory = self.root_directory.format(label_or_image="images")
out_path = pathlib.Path(self.root_directory, self.filename_format).as_posix()
# print(subject_id, "asasa")
# print(self.root_directory)
# try:
root_directory = self.root_directory.format(**kwargs)
# except:
# if nnUnet_is_label:
# self.root_directory = self.root_directory.format(label_or_image="labels")
# else:
# self.root_directory = self.root_directory.format(label_or_image="images")
out_path = pathlib.Path(root_directory, filename).as_posix()
out_dir = os.path.dirname(out_path)
if self.create_dirs and not os.path.exists(out_dir):
os.makedirs(out_dir, exist_ok=True) # create subdirectories if specified in filename_format

return out_path


Expand Down
2 changes: 0 additions & 2 deletions imgtools/modules/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ def generate_sparse_mask(self) -> SparseMask:
# for e in res[1]:
# voxels_with_overlap.add(e)
sparsemask_arr = np.fmax(sparsemask_arr, slice) # elementwise maximum
print("A")
else:
sparsemask_arr = mask_arr
print("B")

sparsemask = SparseMask(sparsemask_arr, roi_names)
# if len(voxels_with_overlap) != 0:
Expand Down
10 changes: 5 additions & 5 deletions imgtools/ops/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class ImageSubjectFileOutput(BaseOutput):

def __init__(self,
root_directory: str,
filename_format: Optional[str] ="{subject_id}.nrrd",
filename_format: Optional[str] ="{subject_id}.nii.gz",
create_dirs: Optional[bool] =True,
compress: Optional[bool] =True):
self.root_directory = root_directory
Expand Down Expand Up @@ -347,11 +347,11 @@ def __call__(self,
img: sitk.Image,
output_stream,
is_mask: bool = False,
nnUnet_is_label: bool=False,
nnUnet_info: Dict=None,
mask_label: Optional[str] = ""):
mask_label: Optional[str] = "",
label_or_image: str="images",
nnUnet_info: Dict=None):

self.output[output_stream](subject_id, img, is_mask=is_mask, mask_label=mask_label, nnUnet_is_label=nnUnet_is_label, nnUnet_info=nnUnet_info)
self.output[output_stream](subject_id, img, is_mask=is_mask, mask_label=mask_label, label_or_image=label_or_image, nnUnet_info=nnUnet_info)

class NumpyOutput(BaseOutput):
"""NumpyOutput class processed images as NumPy files.
Expand Down

0 comments on commit 1dde8cd

Please sign in to comment.