def sub_sample_vol(image_file, dest_file, dsf, interpolation="continuous", verb_lvl=0): from nipy.labs.datasets.volumes.volume_img import VolumeImg # ,CompositionError pyhrf.verbose(verb_lvl, "Subsampling at dsf=%d, %s -> %s" % (dsf, image_file, dest_file)) interp = interpolation data_src, src_meta = read_volume(image_file) affine = src_meta[0] daffine = dsf * affine original_dtype = data_src.dtype if np.issubdtype(np.int, data_src.dtype): # to avoid error "array type 5 not supported" on int arrays ... # data_src = np.asarray(np.asfarray(data_src), data_src.dtype) data_src = np.asfarray(data_src) if data_src.ndim == 4: ref_vol = data_src[:, :, :, 0] else: ref_vol = data_src # print 'ref_vol:', ref_vol.shape img_src = VolumeImg(ref_vol, affine, "mine") img_dest = img_src.as_volume_img(daffine, interpolation=interpolation) # setup dest geometry: dest_header = src_meta[1].copy() # dest_header['sform'] = img_src.affine dpixdim = np.array(src_meta[1]["pixdim"]) dpixdim[1:4] *= dsf # print 'pixdim:', ','.join(map(str,src_meta[1]['pixdim'])) # print 'dpixdim:', ','.join(map(str,dpixdim)) dest_header["pixdim"] = list(dpixdim) sh = ref_vol[::dsf, ::dsf, ::dsf, ...].shape # print 'sh:', sh dest_meta = (daffine, dest_header) # do the resampling: if data_src.ndim == 3: vol = img_dest.get_data()[: sh[0], : sh[1], : sh[2]] if ref_vol.dtype == np.int32 or np.allclose(np.round(ref_vol), ref_vol): # if input is int vol = np.round(vol).astype(np.int32) write_volume(vol, dest_file, dest_meta) elif data_src.ndim == 4: imgs = [VolumeImg(i, affine, "mine") for i in np.rollaxis(data_src, 3, 0)] dvols = [i.as_volume_img(daffine, interpolation=interp).get_data() for i in imgs] # print 'dvols[0]:', dvols[0].shape dvols = np.array(dvols) # print 'dvols:', dvols.shape sub_vols = np.rollaxis(dvols[:, : sh[0], : sh[1], : sh[2]], 0, 4) # print 'sub_vols:', sub_vols.shape write_volume(sub_vols, dest_file, dest_meta) else: raise Exception("Nb of dims (%d) not handled. Only 3D or 4D" % data_src.ndim)
# wm = wm_img.get_data() # csf_img = as_volume_img(CSF_PATH) # csf = csf_img.get_data() # probTotal = gm + wm + csf # dim = np.shape(probTotal) # ind = probTotal > 0 # gm[ind] = gm[ind] / probTotal[ind] # wm[ind] = wm[ind] / probTotal[ind] # csf[ind] = csf[ind] / probTotal[ind] # tissue = np.array([gm.ravel(), wm.ravel(), csf.ravel()]) # tissue_mask = tissue.argmax(axis=0) + 1 # tissue_mask[probTotal.ravel() == 0] = 0 # brain = np.reshape(tissue_mask == 1, (dim[0], dim[1], dim[2])) brain = gm > 0.15 brain = binary_dilation(brain, structure=generate_binary_structure(3, 2)) brain_img = VolumeImg(brain, affine=gm_img.affine, world_space=None, interpolation="nearest") brain_img = brain_img.resampled_to_img(ref_img, interpolation="nearest") brain = brain_img.get_data() brain = binary_closing(brain, structure=generate_binary_structure(3, 2)) labels, n_labels = label(brain) # Remove isolated voxels for i in np.unique(labels)[2:]: brain[labels == i] = 0 # Spatial smoothing to encourage smooth parcels dim = np.shape(brain) tc = tc.reshape((dim[0], dim[1], dim[2], -1)) n_tpts = tc.shape[-1] for t in np.arange(n_tpts): tc[:, :, :, t] = gaussian_filter(tc[:, :, :, t], sigma=1) tc = tc.reshape((-1, n_tpts)) tc = tc[brain.ravel() == 1, :]
def sub_sample_vol(image_file, dest_file, dsf, interpolation='continuous', verb_lvl=0): # ,CompositionError from nipy.labs.datasets.volumes.volume_img import VolumeImg logger.log(verb_lvl, 'Subsampling at dsf=%d, %s -> %s', dsf, image_file, dest_file) interp = interpolation data_src, src_meta = read_volume(image_file) affine = src_meta[0] daffine = dsf * affine original_dtype = data_src.dtype if np.issubdtype(np.int, data_src.dtype): # to avoid error "array type 5 not supported" on int arrays ... #data_src = np.asarray(np.asfarray(data_src), data_src.dtype) data_src = np.asfarray(data_src) if data_src.ndim == 4: ref_vol = data_src[:, :, :, 0] else: ref_vol = data_src img_src = VolumeImg(ref_vol, affine, 'mine') img_dest = img_src.as_volume_img(daffine, interpolation=interpolation) # setup dest geometry: dest_header = src_meta[1].copy() dpixdim = np.array(src_meta[1]['pixdim']) dpixdim[1:4] *= dsf dest_header['pixdim'] = list(dpixdim) sh = ref_vol[::dsf, ::dsf, ::dsf, ...].shape dest_meta = (daffine, dest_header) # do the resampling: if data_src.ndim == 3: vol = img_dest.get_data()[:sh[0], :sh[1], :sh[2]] if ref_vol.dtype == np.int32 or \ np.allclose(np.round(ref_vol), ref_vol): # if input is int vol = np.round(vol).astype(np.int32) write_volume(vol, dest_file, dest_meta) elif data_src.ndim == 4: imgs = [VolumeImg(i, affine, 'mine') for i in np.rollaxis(data_src, 3, 0)] dvols = [i.as_volume_img(daffine, interpolation=interp).get_data() for i in imgs] # print 'dvols[0]:', dvols[0].shape dvols = np.array(dvols) # print 'dvols:', dvols.shape sub_vols = np.rollaxis(dvols[:, :sh[0], :sh[1], :sh[2]], 0, 4) # print 'sub_vols:', sub_vols.shape write_volume(sub_vols, dest_file, dest_meta) else: raise Exception('Nb of dims (%d) not handled. Only 3D or 4D' % data_src.ndim)
#wm = wm_img.get_data() #csf_img = as_volume_img(CSF_PATH) #csf = csf_img.get_data() #probTotal = gm + wm + csf #dim = np.shape(probTotal) #ind = probTotal > 0 #gm[ind] = gm[ind] / probTotal[ind] #wm[ind] = wm[ind] / probTotal[ind] #csf[ind] = csf[ind] / probTotal[ind] #tissue = np.array([gm.ravel(), wm.ravel(), csf.ravel()]) #tissue_mask = tissue.argmax(axis=0) + 1 #tissue_mask[probTotal.ravel() == 0] = 0 #brain = np.reshape(tissue_mask == 1, (dim[0], dim[1], dim[2])) brain = gm > 0.15 brain = binary_dilation(brain, structure=generate_binary_structure(3, 2)) brain_img = VolumeImg(brain, affine=gm_img.affine, world_space=None, interpolation='nearest') brain_img = brain_img.resampled_to_img(ref_img, interpolation='nearest') brain = brain_img.get_data() brain = binary_closing(brain, structure=generate_binary_structure(3, 2)) labels, n_labels = label(brain) # Remove isolated voxels for i in np.unique(labels)[2:]: brain[labels == i] = 0 # Spatial smoothing to encourage smooth parcels dim = np.shape(brain) tc = tc.reshape((dim[0], dim[1], dim[2], -1)) n_tpts = tc.shape[-1] for t in np.arange(n_tpts): tc[:, :, :, t] = gaussian_filter(tc[:, :, :, t], sigma=1) tc = tc.reshape((-1, n_tpts)) tc = tc[brain.ravel() == 1, :]