Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for make_image_from_bin scripts. #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions make_image_from_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand Down
7 changes: 6 additions & 1 deletion make_image_from_bin_renum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
48 changes: 48 additions & 0 deletions nifti_gen_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Temporary band-aid script to generate NIfTI images from NumPY parcellation data.

Written by Dan Lurie ([email protected]).

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)