13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
from copy import copy
16
+ from typing import IO
16
17
17
18
import siibra as bs
18
19
import json
@@ -535,6 +536,7 @@ def get_path_to_regional_map(query_id, roi, space_of_interest):
535
536
# cache fails, fetch from source
536
537
def save_new_nii (cached_fullpath ):
537
538
import nibabel as nib
539
+ import numpy as np
538
540
# fix regional_map if necessary
539
541
regional_map .image .header .set_xyzt_units ('mm' , 'sec' )
540
542
@@ -543,7 +545,21 @@ def save_new_nii(cached_fullpath):
543
545
544
546
# num channel
545
547
regional_map .image .header ['dim' ][5 ] = 1
546
- nib .save (regional_map .image , cached_fullpath )
548
+
549
+ if regional_map .image .header .get_data_dtype () == np .float64 :
550
+ fdata = regional_map .image .get_fdata ()
551
+ new_data = fdata .astype (np .float32 )
552
+ regional_map .image .set_data_dtype (np .float32 )
553
+
554
+ if regional_map .image .header ['sizeof_hdr' ] == 348 :
555
+ new_image = nib .Nifti1Image (new_data , regional_map .image .affine , regional_map .image .header )
556
+ elif regional_map .image .header ['sizeof_hdr' ] == 540 :
557
+ new_image = nib .Nifti2Image (new_data , regional_map .image .affine , regional_map .image .header )
558
+ else :
559
+ raise IOError ('regional map has incorrect sizeof_hdr' )
560
+ nib .save (new_image , cached_fullpath )
561
+ else :
562
+ nib .save (regional_map .image , cached_fullpath )
547
563
548
564
return get_cached_file (cached_filename , save_new_nii )
549
565
0 commit comments