Ejemplo n.º 1
0
    def _run_interface(self, runtime):  # pylint: disable=R0914
        imnii = nb.load(self.inputs.in_file)
        imdata = np.nan_to_num(imnii.get_data())
        erode = np.all(
            np.array(imnii.get_header().get_zooms()[:3], dtype=np.float32) <
            1.2)

        # Cast to float32
        imdata = imdata.astype(np.float32)

        # Remove negative values
        imdata[imdata < 0] = 0

        # Load image corrected for INU
        inudata = np.nan_to_num(nb.load(self.inputs.in_noinu).get_data())
        inudata[inudata < 0] = 0

        segnii = nb.load(self.inputs.in_segm)
        segdata = segnii.get_data().astype(np.uint8)

        airdata = nb.load(self.inputs.air_msk).get_data().astype(np.uint8)
        artdata = nb.load(self.inputs.artifact_msk).get_data().astype(np.uint8)
        headdata = nb.load(self.inputs.head_msk).get_data().astype(np.uint8)

        # SNR
        snrvals = []
        self._results['snr'] = {}
        for tlabel in ['csf', 'wm', 'gm']:
            snrvals.append(snr(inudata, segdata, fglabel=tlabel, erode=erode))
            self._results['snr'][tlabel] = snrvals[-1]
        self._results['snr']['total'] = float(np.mean(snrvals))

        snrvals = []
        self._results['snrd'] = {
            tlabel: snr_dietrich(inudata,
                                 segdata,
                                 airdata,
                                 fglabel=tlabel,
                                 erode=erode)
            for tlabel in ['csf', 'wm', 'gm']
        }
        self._results['snrd']['total'] = float(
            np.mean([val for _, val in list(self._results['snrd'].items())]))

        # CNR
        self._results['cnr'] = cnr(inudata, segdata)

        # FBER
        self._results['fber'] = fber(inudata, headdata)

        # EFC
        self._results['efc'] = efc(inudata)

        # M2WM
        self._results['wm2max'] = wm2max(imdata, segdata)

        # Artifacts
        self._results['qi_1'] = art_qi1(airdata, artdata)

        # CJV
        self._results['cjv'] = cjv(inudata, seg=segdata)

        pvmdata = []
        for fname in self.inputs.in_pvms:
            pvmdata.append(nb.load(fname).get_data().astype(np.float32))

        # FWHM
        fwhm = np.array(self.inputs.in_fwhm[:3]) / np.array(
            imnii.get_header().get_zooms()[:3])
        self._results['fwhm'] = {
            'x': float(fwhm[0]),
            'y': float(fwhm[1]),
            'z': float(fwhm[2]),
            'avg': float(np.average(fwhm))
        }

        # ICVs
        self._results['icvs'] = volume_fraction(pvmdata)

        # RPVE
        self._results['rpve'] = rpve(pvmdata, segdata)

        # Summary stats
        self._results['summary'] = summary_stats(imdata, pvmdata, airdata)

        # Image specs
        self._results['size'] = {
            'x': int(imdata.shape[0]),
            'y': int(imdata.shape[1]),
            'z': int(imdata.shape[2])
        }
        self._results['spacing'] = {
            i: float(v)
            for i, v in zip(['x', 'y', 'z'],
                            imnii.get_header().get_zooms()[:3])
        }

        try:
            self._results['size']['t'] = int(imdata.shape[3])
        except IndexError:
            pass

        try:
            self._results['spacing']['tr'] = float(
                imnii.get_header().get_zooms()[3])
        except IndexError:
            pass

        # Bias
        bias = nb.load(self.inputs.in_bias).get_data()[segdata > 0]
        self._results['inu'] = {
            'range':
            float(np.abs(np.percentile(bias, 95.) - np.percentile(bias, 5.))),
            'med':
            float(np.median(bias))
        }  #pylint: disable=E1101

        mni_tpms = [nb.load(tpm).get_data() for tpm in self.inputs.mni_tpms]
        in_tpms = [nb.load(tpm).get_data() for tpm in self.inputs.in_pvms]
        overlap = fuzzy_jaccard(in_tpms, mni_tpms)
        self._results['tpm_overlap'] = {
            'csf': overlap[0],
            'gm': overlap[1],
            'wm': overlap[2]
        }

        # Flatten the dictionary
        self._results['out_qc'] = _flatten_dict(self._results)
        return runtime
Ejemplo n.º 2
0
    def _run_interface(self, runtime):  # pylint: disable=R0914,E1101
        imnii = nb.load(self.inputs.in_noinu)
        erode = np.all(
            np.array(imnii.header.get_zooms()[:3], dtype=np.float32) < 1.9)

        # Load image corrected for INU
        inudata = np.nan_to_num(imnii.get_data())
        inudata[inudata < 0] = 0

        # Load binary segmentation from FSL FAST
        segnii = nb.load(self.inputs.in_segm)
        segdata = segnii.get_data().astype(np.uint8)

        # Load air, artifacts and head masks
        airdata = nb.load(self.inputs.air_msk).get_data().astype(np.uint8)
        artdata = nb.load(self.inputs.artifact_msk).get_data().astype(np.uint8)
        headdata = nb.load(self.inputs.head_msk).get_data().astype(np.uint8)
        rotdata = nb.load(self.inputs.rot_msk).get_data().astype(np.uint8)

        # Load Partial Volume Maps (pvms) from FSL FAST
        pvmdata = []
        for fname in self.inputs.in_pvms:
            pvmdata.append(nb.load(fname).get_data().astype(np.float32))

        # Summary stats
        stats = summary_stats(inudata, pvmdata, airdata, erode=erode)
        self._results["summary"] = stats

        # SNR
        snrvals = []
        self._results["snr"] = {}
        for tlabel in ["csf", "wm", "gm"]:
            snrvals.append(
                snr(
                    stats[tlabel]["median"],
                    stats[tlabel]["stdv"],
                    stats[tlabel]["n"],
                ))
            self._results["snr"][tlabel] = snrvals[-1]
        self._results["snr"]["total"] = float(np.mean(snrvals))

        snrvals = []
        self._results["snrd"] = {
            tlabel: snr_dietrich(stats[tlabel]["median"], stats["bg"]["mad"])
            for tlabel in ["csf", "wm", "gm"]
        }
        self._results["snrd"]["total"] = float(
            np.mean([val for _, val in list(self._results["snrd"].items())]))

        # CNR
        self._results["cnr"] = cnr(
            stats["wm"]["median"],
            stats["gm"]["median"],
            sqrt(sum(stats[k]["stdv"]**2 for k in ["bg", "gm", "wm"])),
        )

        # FBER
        self._results["fber"] = fber(inudata, headdata, rotdata)

        # EFC
        self._results["efc"] = efc(inudata, rotdata)

        # M2WM
        self._results["wm2max"] = wm2max(inudata, stats["wm"]["median"])

        # Artifacts
        self._results["qi_1"] = art_qi1(airdata, artdata)

        # CJV
        self._results["cjv"] = cjv(
            # mu_wm, mu_gm, sigma_wm, sigma_gm
            stats["wm"]["median"],
            stats["gm"]["median"],
            stats["wm"]["mad"],
            stats["gm"]["mad"],
        )

        # FWHM
        fwhm = np.array(self.inputs.in_fwhm[:3]) / np.array(
            imnii.header.get_zooms()[:3])
        self._results["fwhm"] = {
            "x": float(fwhm[0]),
            "y": float(fwhm[1]),
            "z": float(fwhm[2]),
            "avg": float(np.average(fwhm)),
        }

        # ICVs
        self._results["icvs"] = volume_fraction(pvmdata)

        # RPVE
        self._results["rpve"] = rpve(pvmdata, segdata)

        # Image specs
        self._results["size"] = {
            "x": int(inudata.shape[0]),
            "y": int(inudata.shape[1]),
            "z": int(inudata.shape[2]),
        }
        self._results["spacing"] = {
            i: float(v)
            for i, v in zip(["x", "y", "z"],
                            imnii.header.get_zooms()[:3])
        }

        try:
            self._results["size"]["t"] = int(inudata.shape[3])
        except IndexError:
            pass

        try:
            self._results["spacing"]["tr"] = float(imnii.header.get_zooms()[3])
        except IndexError:
            pass

        # Bias
        bias = nb.load(self.inputs.in_bias).get_data()[segdata > 0]
        self._results["inu"] = {
            "range":
            float(np.abs(np.percentile(bias, 95.0) -
                         np.percentile(bias, 5.0))),
            "med":
            float(np.median(bias)),
        }  # pylint: disable=E1101

        mni_tpms = [nb.load(tpm).get_data() for tpm in self.inputs.mni_tpms]
        in_tpms = [nb.load(tpm).get_data() for tpm in self.inputs.in_pvms]
        overlap = fuzzy_jaccard(in_tpms, mni_tpms)
        self._results["tpm_overlap"] = {
            "csf": overlap[0],
            "gm": overlap[1],
            "wm": overlap[2],
        }

        # Flatten the dictionary
        self._results["out_qc"] = _flatten_dict(self._results)
        return runtime
def make_subject_qc(population, workspace):
    print '========================================================================================'
    print ''
    print '                    Tourettome - 006. QUALITY CONTROL                                   '
    print ''
    print '========================================================================================'

    count = 0
    for subject in population:

        count +=1

        print '%s.Running Quality Control for subject %s' %(count, subject)

        site_id = subject[0:2]
        subdir  = os.path.join(workspace, subject)
        qcdir   = mkdir_path(os.path.join(workspace, subject, 'QUALITY_CONTROL'))
        os.chdir(qcdir)

        df = pd.DataFrame(index=['%s' % subject])

        # EXTRACT ANATOMICAL AND FUNCTIONAL IMAGE QUALITY METRICS

        if not os.path.isfile(os.path.join(qcdir, 'quality_paramters.csv')):

            ############################################################################################
            #  Anatomical measures

            # Load data
            anat       = nb.load(os.path.join(subdir, 'RAW',        'ANATOMICAL.nii.gz' )).get_data()
            anat_mask  = nb.load(os.path.join(subdir, 'ANATOMICAL', 'ANATOMICAL_BRAIN_MASK.nii.gz' )).get_data()
            anat_gm    = nb.load(os.path.join(subdir, 'ANATOMICAL', 'seg_spm/c1ANATOMICAL.nii' )).get_data()
            anat_wm    = nb.load(os.path.join(subdir, 'ANATOMICAL', 'seg_spm/c2ANATOMICAL.nii' )).get_data()
            anat_csf   = nb.load(os.path.join(subdir, 'ANATOMICAL', 'seg_spm/c3ANATOMICAL.nii' )).get_data()

            # Intermediate measures
            anat_fg_mu, anat_fg_sd, anat_fg_size    = summary_mask(anat, anat_mask)
            anat_gm_mu, anat_gm_sd, anat_gm_size    = summary_mask(anat, np.where(anat_gm > 0.5, 1, 0 ))
            anat_wm_mu, anat_wm_sd, anat_wm_size    = summary_mask(anat, np.where(anat_wm > 0.5, 1, 0 ))
            anat_csf_mu, anat_gm_sd, anat_csf_size  = summary_mask(anat, np.where(anat_csf > 0.5, 1, 0 ))
            anat_bg_data, anat_bg_mask              = get_background(anat, anat_mask)
            anat_bg_mu, anat_bg_sd, anat_bg_size    = summary_mask(anat, anat_bg_mask)

            # Calculate spatial anatomical summary measures
            df.loc[subject, 'qc_anat_cjv']  = mriqca.cjv(anat_wm_mu, anat_gm_mu, anat_wm_sd, anat_gm_sd)
            df.loc[subject, 'qc_anat_cnr']  = mriqca.cnr(anat_wm_mu, anat_gm_mu, anat_bg_sd)
            df.loc[subject, 'qc_anat_snr']  = mriqca.snr(anat_fg_mu, anat_fg_sd, anat_fg_size)
            df.loc[subject, 'qc_anat_snrd'] = mriqca.snr_dietrich(anat_fg_mu, anat_bg_sd)
            df.loc[subject, 'qc_anat_efc']  = mriqca.efc(anat)
            df.loc[subject, 'qc_anat_fber'] = mriqca.fber(anat, anat_mask)
            # df.loc[subject]['qc_anat_fwhm'] = fwhm(os.path.join(subdir, 'RAW','ANATOMICAL.nii.gz' ),
            #                                        os.path.join(subdir, 'ANATOMICAL', 'ANATOMICAL_BRAIN_MASK.nii.gz'),out_vox=False)

            ############################################################################################
            # Functional measures

            # Load data
            func      =  np.mean(nb.load(os.path.join(subdir, 'FUNCTIONAL', 'REST_EDIT.nii.gz' )).get_data(), axis =3)
            func_mask =  nb.load(os.path.join(subdir, 'FUNCTIONAL', 'REST_BRAIN_MASK.nii.gz' )).get_data()
            movpar    =  os.path.join(subdir, 'FUNCTIONAL', 'moco/REST_EDIT_moco2.par')

            # Calculate spatial functional summary measures
            func_fg_mu, func_fg_sd, func_fg_size = summary_mask(func, func_mask)
            df.loc[subject, 'qc_func_snr']  = mriqca.snr(func_fg_mu, func_fg_sd, func_fg_size)
            df.loc[subject, 'qc_func_efc']  = mriqca.efc(func)
            df.loc[subject, 'qc_func_fber'] = mriqca.fber(func, func_mask)
            # df.loc[subject]['qc_func_fwhm'] = fwhm(func, func_mask, out_vox=False)

            # Calculate temporal functional summary measures
            FD1D          = np.loadtxt(calculate_FD_Power(movpar))
            frames_in     = [frame for frame, val in enumerate(FD1D) if val < 0.2]
            quat          = int(len(FD1D) / 4)
            fd_in_percent = (float(len(frames_in)) / float(len(FD1D))) * 100.

            df.loc[subject, 'qc_func_fd']     = str(np.round(np.mean(FD1D), 3))
            df.loc[subject, 'qc_func_fd_in']  = str(np.round(fd_in_percent, 2))
            df.loc[subject, 'qc_func_fd']     = str(np.round(np.mean(FD1D), 3))
            df.loc[subject, 'qc_func_fd_max'] = str(np.round(np.max(FD1D), 3))
            df.loc[subject, 'qc_func_fd_q4 '] = str(np.round(np.mean(np.sort(FD1D)[::-1][:quat]), 3))
            df.loc[subject, 'qc_func_fd_rms'] = str(np.round(np.sqrt(np.mean(FD1D)), 3))

            # Calculate DVARS
            func_proc = os.path.join(subdir, 'REGISTRATION', 'REST_EDIT_UNI_BRAIN_MNI2mm.nii.gz')
            func_gm = os.path.join(subdir, 'REGISTRATION', 'ANATOMICAL_GM_MNI2mm.nii.gz')
            df.loc[subject, 'qc_func_dvars']    = np.mean(np.load(calculate_DVARS(func_proc, func_gm)))

            # Calculate TSNR map
            if not os.path.isfile(os.path.join(qcdir, 'tsnr.nii.gz')):
                 tsnr = TSNR()
                 tsnr.inputs.in_file = os.path.join(subdir, 'FUNCTIONAL', 'REST_EDIT.nii.gz')
                 tsnr.run()
                 # os.system('flirt -in tsnr -ref ../ANATOMICAL/ANATOMICAL -applxfm -init ../REGISTRATION/reg_anat/rest2anat_2.mat -out tsnr2anat')

            if not os.path.isfile('TSNR_data.npy'):
                 tsnr_data = nb.load('./tsnr.nii.gz').get_data()
                 nan_mask = np.logical_not(np.isnan(tsnr_data))
                 mask = func_mask > 0
                 data = tsnr_data[np.logical_and(nan_mask, mask)]
                 np.save(os.path.join(os.getcwd(), 'TSNR_data.npy'), data)


            df.loc[subject, 'qc_func_tsnr'] = str(np.round(np.median(np.load('TSNR_data.npy')), 3))

            df.to_csv('quality_paramters.csv')

        ############################################################################################
        #  Make Image Quality Plots

        if not os.path.isfile(os.path.join(qcdir, 'plot_func_tsnr.png')):

            # 1. anat brain mask
            plot_quality(os.path.join(subdir, 'RAW', 'ANATOMICAL.nii.gz'),
                         os.path.join(subdir, 'ANATOMICAL', 'ANATOMICAL_BRAIN_MASK.nii.gz'),
                         subject[0:2], '%s-anat_brain_mask' % subject, 'r', alpha=0.9, title='plot_anat_brain_mask.png')

            # 2. anat gm seg
            plot_quality(os.path.join(subdir, 'RAW', 'ANATOMICAL.nii.gz'),
                         os.path.join(subdir, 'ANATOMICAL', 'ANATOMICAL_GM.nii.gz'),
                         subject[0:2], '%s-anat_gm_seg' % subject, 'r', alpha=0.9, title='plot_anat_gm_seg.png')

            # 3. anat2mni
            plot_quality(mni_head_1mm, os.path.join(subdir, 'REGISTRATION', 'ANATOMICAL_GM_MNI1mm.nii.gz'),
                         'MNI', '%s-anat_gm_seg' % subject, 'r', alpha=0.9, title='plot_anat2mni.png',
                         tissue2=os.path.join(subdir, 'REGISTRATION', 'ANATOMICAL_CSF_MNI1mm.nii.gz'))

            # 4. func2mni
            plot_quality(os.path.join(subdir, 'REGISTRATION', 'REST_EDIT_MOCO_BRAIN_MEAN_BBR_ANAT1mm.nii.gz'),
                         os.path.join(subdir, 'ANATOMICAL', 'ANATOMICAL_GM.nii.gz'),
                         subject[0:2], '%s-func2mni' % subject, 'r', alpha=0.9, title='plot_func2anat.png')

            # 5. func_tsnr
            plot_quality(os.path.join(subdir, 'QUALITY_CONTROL', 'tsnr.nii.gz'), None,
                         'TSNR', '%s-func_tsnr' % subject, 'r', alpha=0.9, title='plot_func_tsnr.png')

        # 6. plot FD, DVARS, CARPET

        resid = nb.load(os.path.join(subdir, 'DENOISE/residuals_compcor/residual_bp_z.nii.gz')).get_data().astype(np.float32)
        gm = resid[nb.load(os.path.join(subdir, 'DENOISE/tissue_signals/gm_mask.nii.gz')).get_data().astype('bool')]
        wm = resid[nb.load(os.path.join(subdir, 'DENOISE/tissue_signals/wm_mask.nii.gz')).get_data().astype('bool')]
        cm = resid[nb.load(os.path.join(subdir, 'DENOISE/tissue_signals/csf_mask.nii.gz')).get_data().astype('bool')]
        fd = np.loadtxt(os.path.join(subdir, 'QUALITY_CONTROL/FD.1D'))
        dv = np.load(os.path.join(subdir, 'QUALITY_CONTROL/DVARS.npy'))

        if not os.path.isfile(os.path.join(qcdir,'xplot_func_motion.png')):
            plot_temporal(gm, wm, cm, fd, dv, os.path.join(qcdir,'plot_func_motion.png'))
Ejemplo n.º 4
0
def test_snr_dietrich(gtruth, sigma, noise):
    data, wmdata, bgdata = gtruth.get_data(sigma, noise)
    assert abs(snr_dietrich(data, wmdata, bgdata) - (1/sigma)) < 10
Ejemplo n.º 5
0
def test_snr_dietrich(gtruth, sigma, noise):
    data = gtruth.get_data(sigma, noise)
    error = abs(snr_dietrich(*data) - (1 / sigma)) * sigma
    assert  error < 6.0
Ejemplo n.º 6
0
def test_snr_dietrich(gtruth, sigma, noise):
    data = gtruth.get_data(sigma, noise)
    error = abs(snr_dietrich(*data) - (1 / sigma)) * sigma
    assert error < 6.0