示例#1
0
    def compute(self):
        in_image = self.getData('in')

        zres = self.getVal('res[-3]')
        yres = self.getVal('res[-2]')
        xres = self.getVal('res[-1]')
        inAffine = np.diag((zres, yres, xres, 1))

        inWriter = partial(writeNifti, affine=inAffine)

        # Nifti1 can't handle complex data, so we will use the magnitude if the
        # inputs are complex
        if in_image.dtype in complextypes:
            in_image_ = IFilePath(inWriter, np.abs(in_image), suffix=".nii")
        else:
            in_image_ = IFilePath(inWriter, in_image, suffix=".nii")

        brain_= OFilePath(readNifti)
        seg_= OFilePath(readNifti)

        bet = 'bet2'
        Command(bet, in_image_, brain_)

        fast = 'fast'
        Command(fast, '-t {}'.format(self.getVal('contrast')+1),
                '-o', seg_, brain_)

        self.setData('brain', brain_.data(".nii.gz"))
        self.setData('seg', seg_.data("_seg.nii.gz"))

        [f.close() for f in (in_image_, brain_, seg_)]

        return 0
示例#2
0
    def compute(self):
        fixed = self.getData('fixed')
        moving = self.getData('moving')
        T = self.getData('init')

        zres = self.getVal('res[-3]')
        yres = self.getVal('res[-2]')
        xres = self.getVal('res[-1]')
        fixedAffine = movingAffine = np.diag((zres, yres, xres, 1))

        fixedWriter = partial(writeNifti, affine=fixedAffine)
        movingWriter = partial(writeNifti, affine=movingAffine)

        # FLIRT can't handle complex data, so we will use the magnitude if the
        # inputs are complex
        if fixed.dtype in complextypes:
            fixed_ = IFilePath(fixedWriter, np.abs(fixed), suffix=".nii")
        else:
            fixed_ = IFilePath(fixedWriter, fixed, suffix=".nii")

        if moving.dtype in complextypes:
            moving_ = IFilePath(movingWriter, np.abs(moving), suffix=".nii")
        else:
            moving_ = IFilePath(movingWriter, moving, suffix=".nii")

        # this is where the registration is performed, unless an initial
        # transformation matrix (T) is provided
        out_ = None
        if T is None:
            out_ = OFilePath(readNifti, suffix=".nii.gz")
            omat_ = OFilePath(np.loadtxt)

            flirt = 'flirt'
            if fixed.ndim == 2:
                flirt += ' -2D'

            Command(flirt,
                '-dof {}'.format(self.getVal('dof')),
                '-interp {}'.format(self.getVal('interp')),
                '-cost {}'.format(self.getVal('cost')),
                '-searchcost {}'.format(self.getVal('cost')),
                '-coarsesearch {}.'.format(self.getVal('coarse search angle')),
                '-finesearch {}'.format(self.getVal('fine search angle')),
                '-searchrx -20 20', '-searchry -20 20', '-searchrz -20 20',
                '-in', moving_, '-ref', fixed_, '-out', out_, '-omat', omat_)

            T = omat_.data()

        # magnitude images have been registered
        # if the input is complex, apply the transformation to the real and
        # imaginary channels separately (this requires running FLIRT two more
        # times)
        if moving.dtype in complextypes or out_ is None:
            out = np.zeros(moving.shape, dtype=moving.dtype)
            for ii in (np.real, np.imag):
                if moving.dtype in realtypes and ii == np.imag:
                    continue

                fixed_ = IFilePath(fixedWriter, ii(fixed), suffix=".nii")
                moving_ = IFilePath(movingWriter, ii(moving), suffix=".nii")
                T_ = IFilePath(np.savetxt, T)
                out_ = OFilePath(readNifti, suffix=".nii.gz")

                flirt = 'flirt -applyxfm'
                if moving.ndim == 2:
                    flirt += ' -2D'

                Command(flirt,
                    '-interp {}'.format(self.getVal('interp')),
                    '-init', T_, '-in', moving_, '-ref', fixed_, '-out', out_)

                if ii == np.real:
                    out += out_.data()
                else:
                    out += 1j * out_.data()

                [f.close() for f in (fixed_, moving_, out_, T_)]
        else:
            # if the input is real, just use the output image from FLIRT
            out = out_.data()
            [f.close() for f in (fixed_, moving_, out_, omat_)]

        self.setData('out', out)
        self.setData('T', T)

        return 0
示例#3
0
    def compute(self):
        fixed = self.getData('fixed')
        moving = self.getData('moving')
        T = self.getData('init')

        zres = self.getVal('res[-3]')
        yres = self.getVal('res[-2]')
        xres = self.getVal('res[-1]')
        fixedAffine = movingAffine = np.diag((zres, yres, xres, 1))

        fixedWriter = partial(writeNifti, affine=fixedAffine)
        movingWriter = partial(writeNifti, affine=movingAffine)

        # FLIRT can't handle complex data, so we will use the magnitude if the
        # inputs are complex
        if fixed.dtype in complextypes:
            fixed_ = IFilePath(fixedWriter, np.abs(fixed), suffix=".nii")
        else:
            fixed_ = IFilePath(fixedWriter, fixed, suffix=".nii")

        if moving.dtype in complextypes:
            moving_ = IFilePath(movingWriter, np.abs(moving), suffix=".nii")
        else:
            moving_ = IFilePath(movingWriter, moving, suffix=".nii")

        # this is where the registration is performed, unless an initial
        # transformation matrix (T) is provided
        out_ = None
        if T is None:
            out_ = OFilePath(readNifti, suffix=".nii.gz")
            omat_ = OFilePath(np.loadtxt)

            flirt = 'flirt'
            if fixed.ndim == 2:
                flirt += ' -2D'

            Command(
                flirt, '-dof {}'.format(self.getVal('dof')),
                '-interp {}'.format(self.getVal('interp')),
                '-cost {}'.format(self.getVal('cost')),
                '-searchcost {}'.format(self.getVal('cost')),
                '-coarsesearch {}.'.format(self.getVal('coarse search angle')),
                '-finesearch {}'.format(self.getVal('fine search angle')),
                '-searchrx -20 20', '-searchry -20 20', '-searchrz -20 20',
                '-in', moving_, '-ref', fixed_, '-out', out_, '-omat', omat_)

            T = omat_.data()

        # magnitude images have been registered
        # if the input is complex, apply the transformation to the real and
        # imaginary channels separately (this requires running FLIRT two more
        # times)
        if moving.dtype in complextypes or out_ is None:
            out = np.zeros(moving.shape, dtype=moving.dtype)
            for ii in (np.real, np.imag):
                if moving.dtype in realtypes and ii == np.imag:
                    continue

                fixed_ = IFilePath(fixedWriter, ii(fixed), suffix=".nii")
                moving_ = IFilePath(movingWriter, ii(moving), suffix=".nii")
                T_ = IFilePath(np.savetxt, T)
                out_ = OFilePath(readNifti, suffix=".nii.gz")

                flirt = 'flirt -applyxfm'
                if moving.ndim == 2:
                    flirt += ' -2D'

                Command(flirt, '-interp {}'.format(self.getVal('interp')),
                        '-init', T_, '-in', moving_, '-ref', fixed_, '-out',
                        out_)

                if ii == np.real:
                    out += out_.data()
                else:
                    out += 1j * out_.data()

                [f.close() for f in (fixed_, moving_, out_, T_)]
        else:
            # if the input is real, just use the output image from FLIRT
            out = out_.data()
            [f.close() for f in (fixed_, moving_, out_, omat_)]

        self.setData('out', out)
        self.setData('T', T)

        return 0