Skip to content

Commit

Permalink
Merge remote-tracking branch 'ancestor-mithril/dev-6'
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianIsensee committed Jul 2, 2024
2 parents efab718 + f6e0248 commit fee8c2d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions nnunetv2/imageio/natural_image_reader_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def read_images(self, image_fnames: Union[List[str], Tuple[str, ...]]) -> Tuple[
print('Image files:')
print(image_fnames)
raise RuntimeError()
return np.vstack(images).astype(np.float32), {'spacing': (999, 1, 1)}
return np.vstack(images, dtype=np.float32, casting='unsafe'), {'spacing': (999, 1, 1)}

def read_seg(self, seg_fname: str) -> Tuple[np.ndarray, dict]:
return self.read_images((seg_fname, ))

def write_seg(self, seg: np.ndarray, output_fname: str, properties: dict) -> None:
io.imsave(output_fname, seg[0].astype(np.uint8), check_contrast=False)
io.imsave(output_fname, seg[0].astype(np.uint8, copy=False), check_contrast=False)


if __name__ == '__main__':
Expand Down
8 changes: 3 additions & 5 deletions nnunetv2/imageio/nibabel_reader_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ def read_images(self, image_fnames: Union[List[str], Tuple[str, ...]]) -> Tuple[
print(image_fnames)
raise RuntimeError()

stacked_images = np.vstack(images)
dict = {
'nibabel_stuff': {
'original_affine': original_affines[0],
},
'spacing': spacings_for_nnunet[0]
}
return stacked_images.astype(np.float32), dict
return np.vstack(images, dtype=np.float32, casting='unsafe'), dict

def read_seg(self, seg_fname: str) -> Tuple[np.ndarray, dict]:
return self.read_images((seg_fname, ))
Expand Down Expand Up @@ -158,22 +157,21 @@ def read_images(self, image_fnames: Union[List[str], Tuple[str, ...]]) -> Tuple[
print(image_fnames)
raise RuntimeError()

stacked_images = np.vstack(images)
dict = {
'nibabel_stuff': {
'original_affine': original_affines[0],
'reoriented_affine': reoriented_affines[0],
},
'spacing': spacings_for_nnunet[0]
}
return stacked_images.astype(np.float32), dict
return np.vstack(images, dtype=np.float32, casting='unsafe'), dict

def read_seg(self, seg_fname: str) -> Tuple[np.ndarray, dict]:
return self.read_images((seg_fname, ))

def write_seg(self, seg: np.ndarray, output_fname: str, properties: dict) -> None:
# revert transpose
seg = seg.transpose((2, 1, 0)).astype(np.uint8)
seg = seg.transpose((2, 1, 0)).astype(np.uint8, copy=False)

seg_nib = nibabel.Nifti1Image(seg, affine=properties['nibabel_stuff']['reoriented_affine'])
seg_nib_reoriented = seg_nib.as_reoriented(io_orientation(properties['nibabel_stuff']['original_affine']))
Expand Down
5 changes: 2 additions & 3 deletions nnunetv2/imageio/simpleitk_reader_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def read_images(self, image_fnames: Union[List[str], Tuple[str, ...]]) -> Tuple[
print(image_fnames)
raise RuntimeError()

stacked_images = np.vstack(images)
dict = {
'sitk_stuff': {
# this saves the sitk geometry information. This part is NOT used by nnU-Net!
Expand All @@ -110,7 +109,7 @@ def read_images(self, image_fnames: Union[List[str], Tuple[str, ...]]) -> Tuple[
# are returned x,y,z but spacing is returned z,y,x. Duh.
'spacing': spacings_for_nnunet[0]
}
return stacked_images.astype(np.float32), dict
return np.vstack(images, dtype=np.float32, casting='unsafe'), dict

def read_seg(self, seg_fname: str) -> Tuple[np.ndarray, dict]:
return self.read_images((seg_fname, ))
Expand All @@ -122,7 +121,7 @@ def write_seg(self, seg: np.ndarray, output_fname: str, properties: dict) -> Non
if output_dimension == 2:
seg = seg[0]

itk_image = sitk.GetImageFromArray(seg.astype(np.uint8))
itk_image = sitk.GetImageFromArray(seg.astype(np.uint8, copy=False))
itk_image.SetSpacing(properties['sitk_stuff']['spacing'])
itk_image.SetOrigin(properties['sitk_stuff']['origin'])
itk_image.SetDirection(properties['sitk_stuff']['direction'])
Expand Down
6 changes: 3 additions & 3 deletions nnunetv2/imageio/tif_reader_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ def read_images(self, image_fnames: Union[List[str], Tuple[str, ...]]) -> Tuple[
print(image_fnames)
raise RuntimeError()

return np.vstack(images).astype(np.float32), {'spacing': spacing}
return np.vstack(images, dtype=np.float32, casting='unsafe'), {'spacing': spacing}

def write_seg(self, seg: np.ndarray, output_fname: str, properties: dict) -> None:
# not ideal but I really have no clue how to set spacing/resolution information properly in tif files haha
tifffile.imwrite(output_fname, data=seg.astype(np.uint8), compression='zlib')
tifffile.imwrite(output_fname, data=seg.astype(np.uint8, copy=False), compression='zlib')
file = os.path.basename(output_fname)
out_dir = os.path.dirname(output_fname)
ending = file.split('.')[-1]
Expand All @@ -97,4 +97,4 @@ def read_seg(self, seg_fname: str) -> Tuple[np.ndarray, dict]:
print(f'WARNING no spacing file found for segmentation {seg_fname}\nAssuming spacing (1, 1, 1).')
spacing = (1, 1, 1)

return seg.astype(np.float32), {'spacing': spacing}
return seg.astype(np.float32, copy=False), {'spacing': spacing}

0 comments on commit fee8c2d

Please sign in to comment.