diff --git a/make_image_from_bin.py b/make_image_from_bin.py index 2c438f7..dcd3655 100755 --- a/make_image_from_bin.py +++ b/make_image_from_bin.py @@ -82,6 +82,7 @@ def make_image_from_bin( image, binfile, mask ): imdat=nim.get_data() print "shape",shape(a) print "sum",sum(imdat) + imdat=imdat.astype('int16') # map the binary data to mask mask_voxels=(imdat.flatten()>0).sum() @@ -91,6 +92,7 @@ def make_image_from_bin( image, binfile, mask ): # write out the image as nifti thdr=nim.get_header() thdr['scl_slope']=1 + thdr.set_data_dtype('int16') nim_aff = nim.get_affine() diff --git a/make_image_from_bin_renum.py b/make_image_from_bin_renum.py index 3c5f0e9..7a83350 100755 --- a/make_image_from_bin_renum.py +++ b/make_image_from_bin_renum.py @@ -89,12 +89,17 @@ def make_image_from_bin_renum( image, binfile, mask ): b[a==unique_a[i]]=i+1 imdat=nim.get_data() + imdat=imdat.astype('int16') # map the binary data to mask imdat[imdat>0]=1 imdat[imdat>0]=short(b[0:sum(imdat)].flatten()) + # Get the mask header and change the dtype + nim_head = nim.get_header() + nim_head.set_data_dtype('int16') + # write out the image as nifti - nim_out = nb.Nifti1Image(imdat, nim.get_affine(), nim.get_header()) + nim_out = nb.Nifti1Image(imdat, nim.get_affine(), nim_head) #nim_out.set_data_dtype('int16') nim_out.to_filename(image) diff --git a/nifti_gen_fix.py b/nifti_gen_fix.py new file mode 100644 index 0000000..5d11b58 --- /dev/null +++ b/nifti_gen_fix.py @@ -0,0 +1,48 @@ +""" +Temporary band-aid script to generate NIfTI images from NumPY parcellation data. + +Written by Dan Lurie (danjlurie@gmail.com). + +Usage: + +python nifti_gen_fix.py /path/to/parcel_data.npy /path/to/mask.nii.gz /path/to/new_atlas_image.nii.gz + +""" + + +import sys +import numpy as np +import nibabel as nib + + + +# Read in command line arguments. +parcel_path, mask_path, out_path = sys.argv[1:4] + +# Load parcellation data. +parcel_data = np.load(parcel_path) + +# Load the mask file. +mask_file = nib.load(mask_path) + +# Get the mask data. +mask_data = mask_file.get_data() + +# Recast the mask data as float 64 to match parcellation data. +atlas_data = mask_data.astype('float64') + +# Copy the parcellation data to the within-mask voxels. +atlas_data[atlas_data == 1] = parcel_data.ravel() + +# Get the affine and header from the mask. +mask_affine = mask_file.get_affine() +mask_header = mask_file.get_header() + +# Set the data type for the header to float64. +mask_header.set_data_dtype('float64') + +# Create a NIfTI image from the atlas data. +atlas_image = nib.Nifti1Image(atlas_data, mask_affine, mask_header) + +# Write the NIfTI file. +atlas_image.to_filename(out_path)