Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving image io #2079

Merged
merged 1 commit into from
Jul 2, 2024
Merged

Improving image io #2079

merged 1 commit into from
Jul 2, 2024

Conversation

ancestor-mithril
Copy link
Contributor

  • reducing the number of allocations when reading images and writing segmentations
  • segmentation are casted to np.uint8 only if they are not already np.uint8
  • images are stacked directly into a float32 array, without additional casts

* reducing the number of allocations when reading images and writing segmentations
* segmentation are casted to np.uint8 only if they are not already np.uint8
* images are stacked directly into a float32 array, without additional casts
@FabianIsensee FabianIsensee self-assigned this Apr 11, 2024
@@ -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)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you comment on the use of 'unsafe', any specific reason this is picked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've chosen 'unsafe' for backwards compatibility as astype uses 'unsafe' casting (https://numpy.org/doc/stable/reference/generated/numpy.ndarray.astype.html).
Moreover, 'unsafe' casting is needed in this case because numpy raises an exception when casting from int32 to float32:

>>> import numpy as np
>>> x = np.array([1, 2, 3], dtype=np.int32)
>>> x.astype(np.float32)
array([1., 2., 3.], dtype=float32)
>>> x.astype(np.float32, casting='safe') 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot cast array data from dtype('int32') to dtype('float32') according to the rule 'safe'

@FabianIsensee
Copy link
Member

Thanks!

@FabianIsensee FabianIsensee merged commit fee8c2d into MIC-DKFZ:master Jul 2, 2024
1 check passed
@ancestor-mithril ancestor-mithril deleted the dev-6 branch July 9, 2024 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants