def loadvol(filename): ''' Load a volumetric array stored in a Nifti or analyze file with a specific filename. It returns an array with the data (arr), the voxel size (voxsz) and a transformation matrix (affine). This function is using volumeimages. Example: arr,voxsz,aff=loadvol('test.nii') ''' if os.path.isfile(filename)==False: print('File does not exist') return [],[],[] img = vi.load(filename) arr = np.array(img.get_data()) voxsz = img.get_metadata().get_zooms() aff = img.get_affine() return arr,voxsz,aff
def loadnifti(fname,info=0): ''' Returns two arguments eg. arr,voxsz=loadnifti(filename) filename should be ending in nii or nii.gz ''' fname=fname.encode() #change from unicode if volumeloading=='ni': img = ni.NiftiImage(fname) ''' problems with C1 from adam's data 256*256*40*26 ''' arr = img.asarray() voxsz = img.getVoxDims() if info==1: print('Filename: ',fname) print('Bytes: ',arr.nbytes) print('Data type: ',arr.dtype.name) print('Dimensions: ',arr.shape) return arr,voxsz elif volumeloading=='vi': img = vi.load(fname) arr = np.array(img.get_data()) voxsz = img.get_metadata().get_zooms() return arr,voxsz else: return [],[]