Ejemplo n.º 1
0
    def run(self, image):
        "Correct for Nyquist ghosting due to field inhomogeneity."
        if not verify_scanner_image(self, image): return
	if not self.fmap_file:
	    self.log("No field map file provided, quitting")
	    return
        if not image.isepi:
            self.log("Can't apply inhomogeneity correction"\
                     " to non-epi sequences, quitting")
            return
	fMap = readImage(self.fmap_file)
        # grab the fieldmap, ignore the mask
        fMap = fMap.subImage(0)
	if fMap.shape[-3:] != image.shape[-3:]:
	    self.log("This field map's shape does not match"\
                     " the image shape, quitting")
	    return
	
	shift = (image.idim * image.T_pe/2/N.pi)
        
        #watch the sign
        pixel_pos = -shift*fMap[:] + N.outer(N.arange(fMap.jdim),
                                             N.ones(fMap.jdim))
        
        image[:].real = resample_phase_axis(abs(image[:]), pixel_pos)
        image[:].imag = 0.
Ejemplo n.º 2
0
    def test_orientation(self):
        rad_xform = np.array([[-1., 0., 0.],
                              [ 0., 1., 0.],
                              [ 0., 0., 1.],])

        neur_xform = np.array([[ 1., 0., 0.],
                               [ 0., 1., 0.],
                               [ 0., 0., 1.],])

        sag_xform = np.array([[ 0., 0.,-1.],
                              [ 1., 0., 0.],
                              [ 0., 1., 0.],])
        
        nif = nifti.readImage(os.path.join(self.pwd, 'avg152T1_LR_nifti'))
        assert_array_equal(nif.orientation_xform.tomatrix(), rad_xform)
        nif = nifti.readImage(os.path.join(self.pwd, 'avg152T1_RL_nifti'))
        assert_array_equal(nif.orientation_xform.tomatrix(), neur_xform)
        nif = nifti.readImage(os.path.join(self.pwd, 'gems2'))
        assert_array_equal(nif.orientation_xform.tomatrix(), sag_xform)
Ejemplo n.º 3
0
    def run(self, image):
        if not verify_scanner_image(self, image):
            return
        fmap_file = clean_name(self.fmap_file)[0]
        ##         if hasattr(image, 'n_chan'):
        ##             fmap_file += '.c%02d'%image.chan
        try:
            fmapIm = readImage(fmap_file)
        except:
            self.log("fieldmap not found: " + fmap_file)
            return -1
        (nslice, npe, nfe) = image.shape[-3:]
        # make sure that the length of the q1 columns of the fmap
        # are AT LEAST equal to that of the image
        regrid_fac = max(npe, fmapIm.shape[-2])
        # fmap and chi-mask are swapped to be of shape (Q1,Q2)
        fmap = np.swapaxes(
            regrid_bilinear(fmapIm[0], regrid_fac, axis=-2).astype(np.float64),
            -1, -2)
        chi = np.swapaxes(regrid_bilinear(fmapIm[1], regrid_fac, axis=-2), -1,
                          -2)
        Q1, Q2 = fmap.shape[-2:]

        # compute T_n2 vector
        Tl = image.T_pe
        delT = image.delT

        a, b, n2, _ = image.epi_trajectory()

        K = get_kernel(Q2, Tl, b, n2, fmap, chi)

        for s in range(nslice):
            # dchunk is shaped (nvol, npe, nfe)
            # inverse transform along nfe (can't do in-place)
            dchunk = ifft1(image[:, s, :, :])
            # now shape is (nfe, npe, nvol)
            dchunk = np.swapaxes(dchunk, 0, 2)
            for fe in range(nfe):
                # want to solve Kx = y for x
                # K is (npe,npe), and y is (npe,nvol)
                #
                # There seems to be a trade-off here as nvol changes...
                # Doing this in two steps is faster for large nvol; I think
                # it takes advantage of the faster BLAS matrix-product in dot
                # as opposed to LAPACK's linear solver. For smaller values
                # of nvol, the overhead seems to outweigh the benefit.
                iK = regularized_inverse(K[s, fe], self.lmbda)
                dchunk[fe] = np.dot(iK, dchunk[fe])
            dchunk = np.swapaxes(dchunk, 0, 2)
            # fft x back to kx, can do inplace here
            fft1(dchunk, inplace=True)
            image[:, s, :, :] = dchunk
Ejemplo n.º 4
0
    def run(self, image):
        if not verify_scanner_image(self, image):
            return
        fmap_file = clean_name(self.fmap_file)[0]
        ##         if hasattr(image, 'n_chan'):
        ##             fmap_file += '.c%02d'%image.chan
        try:
            fmapIm = readImage(fmap_file)
        except:
            self.log("fieldmap not found: " + fmap_file)
            return -1
        (nslice, npe, nfe) = image.shape[-3:]
        # make sure that the length of the q1 columns of the fmap
        # are AT LEAST equal to that of the image
        regrid_fac = max(npe, fmapIm.shape[-2])
        # fmap and chi-mask are swapped to be of shape (Q1,Q2)
        fmap = np.swapaxes(regrid_bilinear(fmapIm[0], regrid_fac, axis=-2).astype(np.float64), -1, -2)
        chi = np.swapaxes(regrid_bilinear(fmapIm[1], regrid_fac, axis=-2), -1, -2)
        Q1, Q2 = fmap.shape[-2:]

        # compute T_n2 vector
        Tl = image.T_pe
        delT = image.delT

        a, b, n2, _ = image.epi_trajectory()

        K = get_kernel(Q2, Tl, b, n2, fmap, chi)

        for s in range(nslice):
            # dchunk is shaped (nvol, npe, nfe)
            # inverse transform along nfe (can't do in-place)
            dchunk = ifft1(image[:, s, :, :])
            # now shape is (nfe, npe, nvol)
            dchunk = np.swapaxes(dchunk, 0, 2)
            for fe in range(nfe):
                # want to solve Kx = y for x
                # K is (npe,npe), and y is (npe,nvol)
                #
                # There seems to be a trade-off here as nvol changes...
                # Doing this in two steps is faster for large nvol; I think
                # it takes advantage of the faster BLAS matrix-product in dot
                # as opposed to LAPACK's linear solver. For smaller values
                # of nvol, the overhead seems to outweigh the benefit.
                iK = regularized_inverse(K[s, fe], self.lmbda)
                dchunk[fe] = np.dot(iK, dchunk[fe])
            dchunk = np.swapaxes(dchunk, 0, 2)
            # fft x back to kx, can do inplace here
            fft1(dchunk, inplace=True)
            image[:, s, :, :] = dchunk
Ejemplo n.º 5
0
 def setUp(self):
     self.pwd = os.path.split(__file__)[0]
     fname = os.path.join(self.pwd, 'filtered_func_data')
     self.image = nifti.readImage(fname, vrange=(0,1))