Example #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
Example #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
Example #3
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))

        # 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["qi1"] = art_qi1(airdata, artdata)
        qi2, bg_plot = art_qi2(imdata, airdata, ncoils=self.inputs.ncoils, erodemask=not self.inputs.testing)
        self._results["qi2"] = qi2
        self._results["out_noisefit"] = bg_plot

        # 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))

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

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

        # Summary stats
        mean, stdv, p95, p05 = summary_stats(imdata, pvmdata)
        self._results["summary"] = {"mean": mean, "stdv": stdv, "p95": p95, "p05": p05}

        # 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.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
Example #4
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)

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

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

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

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

        # Artifacts
        self._results['qi1'] = art_qi1(airdata, artdata)
        qi2, bg_plot = art_qi2(imdata, airdata, ncoils=self.inputs.ncoils)
        self._results['qi2'] = qi2
        self._results['out_noisefit'] = bg_plot

        # 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))

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

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

        # Summary stats
        mean, stdv, p95, p05 = summary_stats(imdata, pvmdata)
        self._results['summary'] = {'mean': mean, 'stdv': stdv,
                                    'p95': p95, 'p05': p05}

        # 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


        # Flatten the dictionary
        self._results['out_qc'] = _flatten_dict(self._results)
        return runtime
Example #5
0
File: qc.py Project: suyashdb/mriqc
    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)

        # 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))

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

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

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

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

        # Artifacts
        self._results['qi1'] = art_qi1(airdata, artdata)
        qi2, bg_plot = art_qi2(imdata,
                               airdata,
                               ncoils=self.inputs.ncoils,
                               erodemask=not self.inputs.testing)
        self._results['qi2'] = qi2
        self._results['out_noisefit'] = bg_plot

        # 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))

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

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

        # Summary stats
        mean, stdv, p95, p05 = summary_stats(imdata, pvmdata)
        self._results['summary'] = {
            'mean': mean,
            'stdv': stdv,
            'p95': p95,
            'p05': p05
        }

        # 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

        # Flatten the dictionary
        self._results['out_qc'] = _flatten_dict(self._results)
        return runtime