From 582f78773b22ba4dc8bc2946e728a6bc7f8afdcd Mon Sep 17 00:00:00 2001 From: Dan Lurie Date: Mon, 17 Aug 2015 01:53:07 -0700 Subject: [PATCH 1/3] Added a hacky new script to convert parcellation data to a NIfTI image. --- nifti_gen_fix.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 nifti_gen_fix.py diff --git a/nifti_gen_fix.py b/nifti_gen_fix.py new file mode 100644 index 0000000..f7b30f2 --- /dev/null +++ b/nifti_gen_fix.py @@ -0,0 +1,34 @@ +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) From d3e39c25512b23abcacddf3996baca6306dc0fa0 Mon Sep 17 00:00:00 2001 From: Dan Lurie Date: Mon, 17 Aug 2015 01:55:54 -0700 Subject: [PATCH 2/3] NIfTI generator now has primitive documentation. --- nifti_gen_fix.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/nifti_gen_fix.py b/nifti_gen_fix.py index f7b30f2..5d11b58 100644 --- a/nifti_gen_fix.py +++ b/nifti_gen_fix.py @@ -1,7 +1,21 @@ +""" +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] From 2d95f1b42f2723dc10cf4d29a78816e00c8a875e Mon Sep 17 00:00:00 2001 From: Dan Lurie Date: Sat, 22 Aug 2015 03:10:59 -0400 Subject: [PATCH 3/3] Fixed dtype issue with make_image_from_bin and make_image_from_bin_renum. --- make_image_from_bin.py | 2 ++ make_image_from_bin_renum.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) 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)