@@ -507,6 +507,8 @@ def get_path_to_regional_map(query_id, roi, space_of_interest):
507
507
# cache fails, fetch from source
508
508
def save_new_nii (cached_fullpath ):
509
509
import nibabel as nib
510
+ import numpy as np
511
+
510
512
# fix regional_map if necessary
511
513
regional_map .image .header .set_xyzt_units ('mm' , 'sec' )
512
514
@@ -515,7 +517,22 @@ def save_new_nii(cached_fullpath):
515
517
516
518
# num channel
517
519
regional_map .image .header ['dim' ][5 ] = 1
518
- nib .save (regional_map .image , cached_fullpath )
520
+
521
+ # cast type float64 to float32
522
+ if regional_map .image .header .get_data_dtype () == np .float64 :
523
+ fdata = regional_map .image .get_fdata ()
524
+ new_data = fdata .astype (np .float32 )
525
+ regional_map .image .set_data_dtype (np .float32 )
526
+
527
+ if regional_map .image .header ['sizeof_hdr' ] == 348 :
528
+ new_image = nib .Nifti1Image (new_data , regional_map .image .affine , regional_map .image .header )
529
+ elif regional_map .image .header ['sizeof_hdr' ] == 540 :
530
+ new_image = nib .Nifti2Image (new_data , regional_map .image .affine , regional_map .image .header )
531
+ else :
532
+ raise IOError ('regional map has incorrect sizeof_hdr' )
533
+ nib .save (new_image , cached_fullpath )
534
+ else :
535
+ nib .save (regional_map .image , cached_fullpath )
519
536
520
537
return get_cached_file (cached_filename , save_new_nii )
521
538
0 commit comments