Last active
January 11, 2021 01:09
-
-
Save cindeem/7347843 to your computer and use it in GitHub Desktop.
quick ecat to nifti
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys, os | |
import nibabel as ni | |
import nibabel.ecat as ecat | |
import numpy as np | |
def print_smoothed(infile): | |
""" prints smoothing kernel FWHM for each file to stdout""" | |
try: | |
img = ecat.load(infile) | |
shdrs = img.get_subheaders() | |
fwhm = shdrs.subheaders[0]['rfilter_resolution'].item() * 10 | |
print 'smoothed(%2.2f):'%fwhm | |
except: | |
print 'Error reading file %s'%infile | |
def print_header(infile): | |
""" prints header data for infile to std out""" | |
try: | |
img = ecat.load(infile) | |
hdr = img.get_header() | |
for k,v in hdr.items(): | |
print k,v | |
except: | |
print 'Error reading file %s'%infile | |
def get_framenumbers(infile, frame=0): | |
""" Returns the order of the frames in the file | |
Useful as sometimes Frames are not stored in the file in | |
chronological order, and can be used to extract frames | |
in correct order | |
returns | |
frameorder_in_file : [true_framenumber, mlist_id] | |
""" | |
img = ecat.load(infile) | |
mlist = img.get_mlist() | |
realframes = mlist.get_frame_order() | |
len_mlist = mlist._mlist[mlist._mlist[:,0] > 0].shape[0] | |
hdr = img.get_header() | |
nframes = hdr['num_frames'] | |
realframe = frame #realframes[frame][0] | |
framenumbers = np.arange(nframes - len_mlist, nframes) | |
if len(framenumbers) ==1: | |
return framenumbers[0] | |
else: | |
return framenumbers[realframe] | |
def check_dtypes(infile): | |
frame_numbers = get_framenumbers(infile) | |
img = ecat.load(infile) | |
shdrs = img.get_subheaders() | |
# get data_type | |
dtypes = [x['data_type'].item() for x in shdrs.subheaders] | |
if not all([x == dtypes[0] for x in dtypes]): | |
raise StandardError('warning: data frames have different datatypes') | |
if dtypes[0] == 6: | |
return np.uint16 | |
elif dtypes[0] == 5: | |
return np.float | |
else: | |
raise StandardError( 'Unknown datatype code %s'%dtypes[0]) | |
def convert2nifti(img, frame = 0, dtype=None): | |
if dtype: | |
tmpimg = ni.Nifti1Image(img.get_frame(frame, | |
orientation='neurological' | |
).astype(dtype), | |
img.get_frame_affine(frame)) | |
else: | |
tmpimg = ni.Nifti1Image(img.get_frame(frame, | |
orientation='neurological', | |
), | |
img.get_frame_affine(frame)) | |
return tmpimg | |
def save_file(image, filename): | |
""" saves image to filename""" | |
image.to_filename(filename) | |
def create_outfile(infile, inimg, frame, newname=None): | |
pth, nme_ext = os.path.split(infile) | |
if inimg.__class__ == ni.Nifti1Image: | |
ext = '.nii' | |
else: | |
raise IOError('Unknown Image format %s,unable to save'%(infile.__class__)) | |
if newname is None: | |
newname = os.path.splitext(nme_ext)[0] | |
correct_frame_number = get_framenumbers(infile, frame) | |
outname = newname + '_frame%04d'%(correct_frame_number + 1) + ext | |
outfile = os.path.join(pth, outname) | |
return outfile | |
if __name__ == '__main__': | |
try: | |
infile = sys.argv[1] | |
except: | |
raise IOError('Please pass file to convert') | |
eimg = ecat.load(infile) | |
mlist = eimg.get_mlist() | |
nframes = mlist.get_frame_order() | |
for realframe, index in nframes.items(): | |
print 'rf,', realframe, 'index', index[0] | |
niimg = convert2nifti(eimg, index[0]) | |
outf = create_outfile(infile, niimg, realframe) | |
niimg.to_filename(outf) | |
print outf | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hm... I'm trying this and it isn't working for me. I'm wondering if I'm perhaps rolling a newer version of nibabel?
It looks like img.get_mlist() returns an ndarray now, so it doesn't have attributes like _mlist and or get_frame_order(). I think I can make it go; if so I'll try and post an update.