def img_filt(fname): # Load the thresholded CT img = nib.load(fname) # convert to nipy format for filtering nipy_img = nipy.io.nifti_ref.nifti2nipy(img) # build and apply gaussian filter smoother = LinearFilter(nipy_img.coordmap, nipy_img.shape, 1) smoothed_im = smoother.smooth(nipy_img) # convert smoothed im back to nifti nifti_img = nipy.io.nifti_ref.nipy2nifti(smoothed_im) # get data array for saving smoothed_CT = nifti_img.get_data() smoothed_CT = np.array(smoothed_CT) # save the smoothed thresholded ct hdr = img.get_header() affine = img.get_affine() N = nib.Nifti1Image(smoothed_CT, affine, hdr) new_fname = 'smoothed_' + fname N.to_filename(new_fname)
def test_kernel(): # Verify the the convolution with a delta function # gives the correct answer. tol = 0.9999 sdtol = 1.0e-8 for x in range(6): shape = randint(30,60,(3,)) ii, jj, kk = randint(11,17, (3,)) coordmap = Affine.from_start_step('ijk', 'xyz', randint(5,20,(3,))*0.25, randint(5,10,(3,))*0.5) signal = np.zeros(shape) signal[ii,jj,kk] = 1. signal = Image(signal, coordmap=coordmap) kernel = LinearFilter(coordmap, shape, fwhm=randint(50,100)/10.) ssignal = kernel.smooth(signal) ssignal = np.asarray(ssignal) ssignal[:] *= kernel.norms[kernel.normalization] I = np.indices(ssignal.shape) I.shape = (kernel.coordmap.ndim[0], np.product(shape)) i, j, k = I[:,np.argmax(ssignal[:].flat)] yield assert_equal, (i,j,k), (ii,jj,kk) Z = kernel.coordmap(I) - kernel.coordmap([i,j,k]) _k = kernel(Z) _k.shape = ssignal.shape yield assert_true, (np.corrcoef(_k[:].flat, ssignal[:].flat)[0,1] > tol) yield assert_true, ((_k[:] - ssignal[:]).std() < sdtol) def _indices(i,j,k,axis): I = np.zeros((3,20)) I[0] += i I[1] += j I[2] += k I[axis] += np.arange(-10,10) return I vx = ssignal[i,j,(k-10):(k+10)] vvx = coordmap(_indices(i,j,k,2)) - coordmap([[i],[j],[k]]) yield assert_true, (np.corrcoef(vx, kernel(vvx))[0,1] > tol) vy = ssignal[i,(j-10):(j+10),k] vvy = coordmap(_indices(i,j,k,1)) - coordmap([[i],[j],[k]]) yield assert_true, (np.corrcoef(vy, kernel(vvy))[0,1] > tol) vz = ssignal[(i-10):(i+10),j,k] vvz = coordmap(_indices(i,j,k,0)) - coordmap([[i],[j],[k]]) yield assert_true, (np.corrcoef(vz, kernel(vvz))[0,1] > tol)
def test_kernel(): # Verify that convolution with a delta function gives the correct # answer. tol = 0.9999 sdtol = 1.0e-8 for x in range(6): shape = randint(30,60,(3,)) # pos of delta ii, jj, kk = randint(11,17, (3,)) # random affine coordmap (diagonal and translations) coordmap = AffineTransform.from_start_step('ijk', 'xyz', randint(5,20,(3,))*0.25, randint(5,10,(3,))*0.5) # delta function in 3D array signal = np.zeros(shape) signal[ii,jj,kk] = 1. signal = Image(signal, coordmap=coordmap) # A filter with coordmap, shape matched to image kernel = LinearFilter(coordmap, shape, fwhm=randint(50,100)/10.) # smoothed normalized 3D array ssignal = kernel.smooth(signal).get_data() ssignal[:] *= kernel.norms[kernel.normalization] # 3 points * signal.size array I = np.indices(ssignal.shape) I.shape = (kernel.coordmap.ndims[0], np.product(shape)) # location of maximum in smoothed array i, j, k = I[:, np.argmax(ssignal[:].flat)] # same place as we put it before smoothing? assert_equal((i,j,k), (ii,jj,kk)) # get physical points position relative to position of delta Z = kernel.coordmap(I.T) - kernel.coordmap([i,j,k]) _k = kernel(Z) _k.shape = ssignal.shape assert_true((np.corrcoef(_k[:].flat, ssignal[:].flat)[0,1] > tol)) assert_true(((_k[:] - ssignal[:]).std() < sdtol)) def _indices(i,j,k,axis): I = np.zeros((3,20)) I[0] += i I[1] += j I[2] += k I[axis] += np.arange(-10,10) return I.T vx = ssignal[i,j,(k-10):(k+10)] xformed_ijk = coordmap([i, j, k]) vvx = coordmap(_indices(i,j,k,2)) - xformed_ijk assert_true((np.corrcoef(vx, kernel(vvx))[0,1] > tol)) vy = ssignal[i,(j-10):(j+10),k] vvy = coordmap(_indices(i,j,k,1)) - xformed_ijk assert_true((np.corrcoef(vy, kernel(vvy))[0,1] > tol)) vz = ssignal[(i-10):(i+10),j,k] vvz = coordmap(_indices(i,j,k,0)) - xformed_ijk assert_true((np.corrcoef(vz, kernel(vvz))[0,1] > tol))
def smooth_volume(nifti_file, smoothmm): """ @param nifti_file: string @param smoothmm: int @return: """ try: img = load_image(nifti_file) except Exception as exc: raise Exception('Error reading file {0}.'.format(nifti_file), exc_info=True) from exc if smoothmm <= 0: return img filter = LinearFilter(img.coordmap, img.shape) return filter.smooth(img)
def _smooth_img(nii_img, smooth_fwhm): """ Parameters ---------- nii_img: nipy.Image smooth_fwhm: float Returns ------- smoothed nipy.Image """ # delayed import because could not install nipy on Python 3 on OSX from nipy.algorithms.kernel_smooth import LinearFilter if smooth_fwhm <= 0: return nii_img filter = LinearFilter(nii_img.coordmap, nii_img.shape) return filter.smooth(nii_img)
def test_anat_smooth(): anat = load_image(anatfile) smoother = LinearFilter(anat.coordmap, anat.shape) sanat = smoother.smooth(anat)