21
21
22
22
from ctypes import ArgumentError
23
23
import numpy as np
24
- from nibabel import Nifti1Image
24
+ import nibabel
25
25
from cloudvolume .exceptions import OutOfBoundsError
26
26
from cloudvolume import CloudVolume
27
27
import os
@@ -170,7 +170,7 @@ class LocalNiftiVolume(ImageProvider):
170
170
171
171
volume_type = 'nii'
172
172
173
- def __init__ (self , name : str , img : Nifti1Image , space : Space ):
173
+ def __init__ (self , name : str , img : nibabel . Nifti1Image , space : Space ):
174
174
""" Create a new local nifti volume from a Nifti1Image object.
175
175
176
176
Args:
@@ -179,12 +179,19 @@ def __init__(self, name: str, img: Nifti1Image, space: Space):
179
179
space (Space): the reference space associated with the Image
180
180
"""
181
181
self .name = name
182
- if isinstance (img , Nifti1Image ):
182
+ if isinstance (img , nibabel . Nifti1Image ):
183
183
self .image = img
184
184
elif isinstance (img , str ):
185
- self .image = Nifti1Image . from_filename (img )
185
+ self .image = nibabel . load (img )
186
186
else :
187
187
raise ValueError (f"Cannot create local nifti volume from image parameter { img } " )
188
+
189
+ if np .isnan (self .image .get_fdata ()).any ():
190
+ logger .warn (f'Replacing NaN by 0 for { self .name } ' )
191
+ self .image = nibabel .Nifti1Image (
192
+ np .nan_to_num (self .image .get_fdata ()),
193
+ self .image .affine
194
+ )
188
195
self .space = space
189
196
190
197
def fetch (self , ** kwargs ):
@@ -249,7 +256,7 @@ def fetch(self, resolution_mm=None, voi=None, mapindex=None, clip=False):
249
256
f"Mapindex of { mapindex } provided for fetching from NiftiVolume, but its shape is { shape } ."
250
257
)
251
258
else :
252
- img = Nifti1Image (
259
+ img = nibabel . Nifti1Image (
253
260
dataobj = self .image .dataobj [:, :, :, mapindex ], affine = self .image .affine
254
261
)
255
262
assert img is not None
@@ -265,7 +272,7 @@ def fetch(self, resolution_mm=None, voi=None, mapindex=None, clip=False):
265
272
(x0 , y0 , z0 ), (x1 , y1 , z1 ) = bb_vox .minpoint , bb_vox .maxpoint
266
273
shift = np .identity (4 )
267
274
shift [:3 , - 1 ] = bb_vox .minpoint
268
- img = Nifti1Image (
275
+ img = nibabel . Nifti1Image (
269
276
dataobj = img .dataobj [x0 :x1 , y0 :y1 , z0 :z1 ],
270
277
affine = np .dot (img .affine , shift ),
271
278
)
@@ -526,7 +533,7 @@ def fetch(self, resolution_mm=None, voi=None, mapindex=None, clip=False):
526
533
if data .ndim == 2 :
527
534
data = data .reshape (list (data .shape ) + [1 ])
528
535
529
- return Nifti1Image (data , self .build_affine (resolution_mm = resolution_mm , voi = voi ))
536
+ return nibabel . Nifti1Image (data , self .build_affine (resolution_mm = resolution_mm , voi = voi ))
530
537
531
538
def get_shape (self , resolution_mm = None ):
532
539
mip = self ._resolution_to_mip (resolution_mm , voi = None )
0 commit comments