Example #1
0
    def _generate_report(self):
        """ Generates the visual report """
        from niworkflows.viz.utils import compose_view, plot_registration
        NIWORKFLOWS_LOG.info('Generating visual report')

        anat = load_img(self._anat_file)
        contour_nii = load_img(
            self._contour) if self._contour is not None else None

        if self._mask_file:
            anat = unmask(apply_mask(anat, self._mask_file), self._mask_file)
            mask_nii = load_img(self._mask_file)
        else:
            mask_nii = threshold_img(anat, 1e-3)

        n_cuts = 7
        if not self._mask_file and contour_nii:
            cuts = cuts_from_bbox(contour_nii, cuts=n_cuts)
        else:
            cuts = cuts_from_bbox(mask_nii, cuts=n_cuts)

        # Call composer
        compose_view(plot_registration(anat,
                                       'fixed-image',
                                       estimate_brightness=True,
                                       cuts=cuts,
                                       contour=contour_nii,
                                       compress=self.inputs.compress_report),
                     [],
                     out_file=self._out_report)
Example #2
0
    def _run_interface(self, runtime):
        from niworkflows.viz.utils import (
            plot_registration,
            cuts_from_bbox,
            compose_view,
        )
        from nibabel import load

        rootdir = Path(self.inputs.subjects_dir) / self.inputs.subject_id
        _anat_file = str(rootdir / "mri" / "brain.mgz")
        _contour_file = str(rootdir / "mri" / "ribbon.mgz")

        anat = load(_anat_file)
        contour_nii = load(_contour_file)

        n_cuts = 7
        cuts = cuts_from_bbox(contour_nii, cuts=n_cuts)

        self._results["out_report"] = str(Path(runtime.cwd) / self.inputs.out_report)

        # Call composer
        compose_view(
            plot_registration(
                anat,
                "fixed-image",
                estimate_brightness=True,
                cuts=cuts,
                contour=contour_nii,
                compress=self.inputs.compress_report,
            ),
            [],
            out_file=self._results["out_report"],
        )
        return runtime
Example #3
0
File: plot.py Project: jerdra/niviz
def plot_orthogonal_views(data,
                          bbox_nii=None,
                          auto_brightness=False,
                          display_modes=['x', 'y', 'z'],
                          n_cuts=10,
                          plot_func=nplot.plot_anat,
                          figure_title=""):

    if bbox_nii is None:
        bbox_nii = nimg.threshold_img(data, 1e-3)

    cuts = cuts_from_bbox(data, cuts=n_cuts)
    if auto_brightness:
        robust_params = robust_set_limits(bbox_nii.get_fdata().reshape(-1), {})
    else:
        robust_params = {}

    svgs = []
    for d in display_modes:
        plot_params = {
            "display_mode": d,
            "cut_coords": cuts[d],
            **robust_params
        }
        display = plot_func(data, **plot_params)
        svg = extract_svg(display)
        svg = svg.replace("figure_1", f"{figure_title}-{d}")
        svgs.append(fromstring(svg))
        display.close()
    return svgs
Example #4
0
    def _generate_report(self):
        """Generate a reportlet."""
        NIWORKFLOWS_LOG.info('Generating visual report')

        refnii = load_img(self.inputs.reference)
        fmapnii = load_img(self.inputs.fieldmap)
        contour_nii = load_img(self.inputs.mask) if isdefined(
            self.inputs.mask) else None
        mask_nii = threshold_img(refnii, 1e-3)
        cuts = cuts_from_bbox(contour_nii or mask_nii, cuts=self._n_cuts)
        fmapdata = fmapnii.get_fdata()
        vmax = max(fmapdata.max(), abs(fmapdata.min()))

        # Call composer
        compose_view(plot_registration(refnii,
                                       'fixed-image',
                                       estimate_brightness=True,
                                       cuts=cuts,
                                       label='reference',
                                       contour=contour_nii,
                                       compress=False),
                     plot_registration(fmapnii,
                                       'moving-image',
                                       estimate_brightness=True,
                                       cuts=cuts,
                                       label='fieldmap (Hz)',
                                       contour=contour_nii,
                                       compress=False,
                                       plot_params={
                                           'cmap': coolwarm_transparent(),
                                           'vmax': vmax,
                                           'vmin': -vmax
                                       }),
                     out_file=self._out_report)
Example #5
0
    def _run_interface(self, runtime):
        from niworkflows.viz.utils import plot_registration, cuts_from_bbox, compose_view
        from nilearn.image import load_img

        rootdir = Path(self.inputs.subjects_dir) / self.inputs.subject_id
        _anat_file = str(rootdir / 'mri' / 'brain.mgz')
        _contour_file = str(rootdir / 'mri' / 'ribbon.mgz')

        anat = load_img(_anat_file)
        contour_nii = load_img(_contour_file)

        n_cuts = 7
        cuts = cuts_from_bbox(contour_nii, cuts=n_cuts)

        self._results['out_report'] = str(
            Path(runtime.cwd) / self.inputs.out_report)

        # Call composer
        compose_view(plot_registration(anat,
                                       'fixed-image',
                                       estimate_brightness=True,
                                       cuts=cuts,
                                       contour=contour_nii,
                                       compress=self.inputs.compress_report),
                     [],
                     out_file=self._results['out_report'])
        return runtime
Example #6
0
    def _generate_report(self):
        """ Generates the visual report """
        from niworkflows.viz.utils import compose_view, plot_registration
        NIWORKFLOWS_LOG.info('Generating visual report')

        fixed_image_nii = load_img(self._fixed_image)
        moving_image_nii = load_img(self._moving_image)
        contour_nii = load_img(
            self._contour) if self._contour is not None else None

        if self._fixed_image_mask:
            fixed_image_nii = unmask(
                apply_mask(fixed_image_nii, self._fixed_image_mask),
                self._fixed_image_mask)
            # since the moving image is already in the fixed image space we
            # should apply the same mask
            moving_image_nii = unmask(
                apply_mask(moving_image_nii, self._fixed_image_mask),
                self._fixed_image_mask)
            mask_nii = load_img(self._fixed_image_mask)
        else:
            mask_nii = threshold_img(fixed_image_nii, 1e-3)

        n_cuts = 7
        if not self._fixed_image_mask and contour_nii:
            cuts = cuts_from_bbox(contour_nii, cuts=n_cuts)
        else:
            cuts = cuts_from_bbox(mask_nii, cuts=n_cuts)

        # Call composer
        compose_view(plot_registration(fixed_image_nii,
                                       'fixed-image',
                                       estimate_brightness=True,
                                       cuts=cuts,
                                       label=self._fixed_image_label,
                                       contour=contour_nii,
                                       compress=self.inputs.compress_report),
                     plot_registration(moving_image_nii,
                                       'moving-image',
                                       estimate_brightness=True,
                                       cuts=cuts,
                                       label=self._moving_image_label,
                                       contour=contour_nii,
                                       compress=self.inputs.compress_report),
                     out_file=self._out_report)
Example #7
0
    def _generate_report(self):
        anat_img = nib.load(self.inputs.in_file)
        mask_img = nib.load(self.inputs.mask_file)
        assert nvol(anat_img) == 1
        assert nvol(mask_img) == 1

        plot_params = robust_set_limits_in_mask(anat_img, mask_img)

        template = self.inputs.template
        parc_file = getresource(
            f"tpl-{template}_RegistrationCheckOverlay.nii.gz")
        assert parc_file is not None
        parc_img = nib.load(parc_file)

        levels = np.unique(np.asanyarray(parc_img.dataobj).astype(np.int32))
        levels = (levels[levels > 0] - 0.5).tolist()
        colors = color_palette("husl", len(levels))

        label = None
        if isdefined(self.inputs.label):
            label = self.inputs.label

        compress = self.inputs.compress_report

        n_cuts = 7
        cuts = cuts_from_bbox(mask_img, cuts=n_cuts)

        outfiles = []
        for dimension in ["z", "y", "x"]:
            display = plot_anat(
                anat_img,
                draw_cross=False,
                display_mode=dimension,
                cut_coords=cuts[dimension],
                title=label,
                **plot_params,
            )

            display.add_contours(parc_img,
                                 levels=levels,
                                 colors=colors,
                                 linewidths=0.25)
            display.add_contours(mask_img,
                                 levels=[0.5],
                                 colors="r",
                                 linewidths=0.5)

            label = None  # only on first

            svg = extract_svg(display, compress=compress)
            svg = svg.replace("figure_1", str(uuid4()), 1)

            outfiles.append(fromstring(svg))

        self._out_report = op.abspath(self.inputs.out_report)
        compose_view(bg_svgs=outfiles, fg_svgs=None, out_file=self._out_report)
Example #8
0
    def _generate_report(self):
        """Generate a reportlet."""
        NIWORKFLOWS_LOG.info('Generating visual report')

        movnii = refnii = load_img(self.inputs.reference)
        fmapnii = load_img(self.inputs.fieldmap)

        if isdefined(self.inputs.moving):
            movnii = load_img(self.inputs.moving)

        contour_nii = mask_nii = None
        if isdefined(self.inputs.mask):
            contour_nii = load_img(self.inputs.mask)
            maskdata = contour_nii.get_fdata() > 0
        else:
            mask_nii = threshold_img(refnii, 1e-3)
            maskdata = mask_nii.get_fdata() > 0
        cuts = cuts_from_bbox(contour_nii or mask_nii, cuts=self._n_cuts)
        fmapdata = fmapnii.get_fdata()
        vmax = max(abs(np.percentile(fmapdata[maskdata], 99.8)),
                   abs(np.percentile(fmapdata[maskdata], 0.2)))

        fmap_overlay = [{
            'overlay': fmapnii,
            'overlay_params': {
                'cmap': coolwarm_transparent(max_alpha=self.inputs.max_alpha),
                'vmax': vmax,
                'vmin': -vmax,
            }
        }] * 2

        if self.inputs.show != 'both':
            fmap_overlay[not self.inputs.show] = {}

        # Call composer
        compose_view(plot_registration(movnii,
                                       'moving-image',
                                       estimate_brightness=True,
                                       cuts=cuts,
                                       label=self.inputs.moving_label,
                                       contour=contour_nii,
                                       compress=False,
                                       **fmap_overlay[1]),
                     plot_registration(refnii,
                                       'fixed-image',
                                       estimate_brightness=True,
                                       cuts=cuts,
                                       label=self.inputs.reference_label,
                                       contour=contour_nii,
                                       compress=False,
                                       **fmap_overlay[0]),
                     out_file=self._out_report)
Example #9
0
    def _generate_report(self):
        '''Make a composite for co-registration of surface and volume images'''

        import trimesh

        l_surf = nib.load(self._surf_l)
        r_surf = nib.load(self._surf_r)
        vol_img = nib.load(self._bg_nii)

        if vol_img.ndim == 4:
            vol_img = vol_img.slicer[:, :, :, 0]

        verts, trigs, offset = niviz.surface.gifti_get_full_brain_mesh(
            l_surf, r_surf)

        mesh = trimesh.Trimesh(vertices=verts, faces=trigs)
        mask_nii = nimg.threshold_img(vol_img, 1e-3)
        cuts = cuts_from_bbox(mask_nii, cuts=self._ncuts)

        sections = mesh.section_multiplane(plane_normal=[0, 0, 1],
                                           plane_origin=[0, 0, 0],
                                           heights=cuts['z'])

        zh = nplot.plot_anat(vol_img, display_mode='z', cut_coords=cuts['z'])

        for z, s in zip(cuts['z'], sections):
            ax = zh.axes[z].ax
            if s:
                for segs in s.discrete:
                    ax.plot(*segs.T, color='r', linewidth=0.5)

        if self._fg_nii:
            fg_img = nib.load(self._fg_nii).slicer[:, :, :, 0]
            fg_img = nimg.resample_to_img(fg_img,
                                          vol_img,
                                          interpolation="linear")
            # Custom colormap with transparencies
            ncolors = 256
            basecmap = 'viridis_r'
            color_array = plt.get_cmap(basecmap)(range(ncolors))
            color_array[:, -1] = np.linspace(1.0, 0.0, ncolors)

            # Set background intensity=0 to transparent
            color_array[0, :] = 0
            cmapviridis = mcolors.LinearSegmentedColormap.from_list(
                basecmap, colors=color_array)

            zh.add_overlay(fg_img, cmap=cmapviridis)

        zh.savefig(self._out_report)
Example #10
0
    def _generate_report(self):
        in_img = nib.load(self.inputs.in_file)
        assert nvol(in_img) == 1

        mask_img = nib.load(self.inputs.mask_file)
        assert nvol(mask_img) == 1

        label = None
        if isdefined(self.inputs.label):
            label = self.inputs.label

        compress = self.inputs.compress_report

        n_cuts = 7
        cuts = cuts_from_bbox(mask_img, cuts=n_cuts)

        img_vals = in_img.get_fdata()[np.asanyarray(mask_img.dataobj).astype(
            np.bool)]
        vmin = img_vals.min()
        vmax = img_vals.max()

        outfiles = []
        for dimension in ["z", "y", "x"]:
            display = plot_epi(
                in_img,
                draw_cross=False,
                display_mode=dimension,
                cut_coords=cuts[dimension],
                title=label,
                vmin=vmin,
                vmax=vmax,
                colorbar=(dimension == "z"),
                cmap=plt.cm.gray,
            )
            display.add_contours(mask_img, levels=[0.5], colors="r")
            label = None  # only on first
            svg = extract_svg(display, compress=compress)
            svg = svg.replace("figure_1", str(uuid4()), 1)
            outfiles.append(fromstring(svg))

        self._out_report = op.abspath(self.inputs.out_report)
        compose_view(bg_svgs=outfiles, fg_svgs=None, out_file=self._out_report)
Example #11
0
File: plot.py Project: jerdra/niviz
def plot_montage(data,
                 orientation,
                 bbox_nii=None,
                 n_cuts=15,
                 n_cols=5,
                 auto_brightness=False,
                 plot_func=nplot.plot_anat,
                 figure_title="figure"):
    '''
    Plot a montage of cuts for a given orientation
    for an image
    '''

    if bbox_nii is None:
        bbox_nii = nimg.threshold_img(data, 1e-3)

    cuts = cuts_from_bbox(bbox_nii, cuts=n_cuts)
    if auto_brightness:
        robust_params = robust_set_limits(bbox_nii.get_fdata().reshape(-1), {})
    else:
        robust_params = {}

    svgs = []
    for i in range(n_cuts // n_cols):

        start = i * n_cols
        end = min(i * n_cols + n_cols, n_cuts)
        row_cuts = cuts[orientation][start:end]

        plot_params = {
            "display_mode": orientation,
            "cut_coords": row_cuts,
            **robust_params
        }

        display = plot_func(data, **plot_params)
        svg = extract_svg(display)
        svg = svg.replace("figure_1", f"{figure_title}:{start}-{end}")
        svgs.append(fromstring(svg))

    return svgs
Example #12
0
    def _generate_report(self):
        epi_img = nib.load(self.inputs.in_file)
        mask_img = nib.load(self.inputs.mask_file)
        assert nvol(epi_img) == 1
        assert nvol(mask_img) == 1

        label = None
        if isdefined(self.inputs.label):
            label = self.inputs.label

        compress = self.inputs.compress_report

        n_cuts = 7
        cuts = cuts_from_bbox(mask_img, cuts=n_cuts)

        plot_params = robust_set_limits_in_mask(epi_img, mask_img)

        outfiles = []
        for dimension in ["z", "y", "x"]:
            display = plot_epi(
                epi_img,
                draw_cross=False,
                display_mode=dimension,
                cut_coords=cuts[dimension],
                title=label,
                colorbar=(dimension == "z"),
                cmap=plt.get_cmap("gray"),
                **plot_params,
            )

            display.add_contours(mask_img, levels=[0.5], colors="r")

            label = None  # only on first

            svg = extract_svg(display, compress=compress)
            svg = svg.replace("figure_1", str(uuid4()), 1)

            outfiles.append(fromstring(svg))

        self._out_report = op.abspath(self.inputs.out_report)
        compose_view(bg_svgs=outfiles, fg_svgs=None, out_file=self._out_report)
Example #13
0
    def _run_interface(self, runtime):
        fixed_img = load_img(self.inputs.fixed_img)
        moving_img = load_img(self.inputs.moving_img)
        mask = load_img(self.inputs.mask_img)
        out_dir = os.path.realpath(self.inputs.out_dir)
        cuts = cuts_from_bbox(mask, self.inputs.cuts)

        _, fname, _ = split_filename(self.inputs.moving_img)

        compose_view(plot_registration(fixed_img,
                                       'fixed-image',
                                       estimate_brightness=True,
                                       cuts=cuts,
                                       label='fixed'),
                     plot_registration(moving_img,
                                       'moving-image',
                                       estimate_brightness=True,
                                       cuts=cuts,
                                       label='moving'),
                     out_file=os.path.join(out_dir, (fname + '_vizQC.svg')))

        return runtime
Example #14
0
def make_registration(*, moving, fixed, mask, out_file):
    """
    Creates `out_file`.svg of registration between `moving` and `fixed`

    Parameters
    ----------
    moving : str
        Path to file that was registered to `fixed`
    fixed : str
        Path to file that `moving` was registered to
    mask : str
        Path to brain mask file
    out_file : str
        Path to where svg will be saved

    Returns
    -------
    out_file : str
        Where svg was saved
    """

    if not out_file.endswith('.svg'): out_file += '.svg'
    cuts = cuts_from_bbox(nib.load(mask), cuts=7)

    compose_view(
        plot_registration(nib.load(fixed),
                          'fixed-image',
                          estimate_brightness=True,
                          cuts=cuts,
                          label='fixed'),
        plot_registration(nib.load(moving),
                          'moving-image',
                          estimate_brightness=True,
                          cuts=cuts,
                          label='moving'),
        out_file=out_file
    )

    return out_file
Example #15
0
    def _generate_report(self):
        """Generate a reportlet."""
        NIWORKFLOWS_LOG.info("Generating visual report")

        movnii = load_img(self.inputs.reference)
        canonical_r = rotation2canonical(movnii)
        movnii = refnii = rotate_affine(movnii, rot=canonical_r)

        fmapnii = nb.squeeze_image(
            rotate_affine(load_img(self.inputs.fieldmap), rot=canonical_r))

        if fmapnii.dataobj.ndim == 4:
            for i, tstep in enumerate(nb.four_to_three(fmapnii)):
                if np.any(np.asanyarray(tstep.dataobj) != 0):
                    fmapnii = tstep
                    break

        if isdefined(self.inputs.moving):
            movnii = rotate_affine(load_img(self.inputs.moving),
                                   rot=canonical_r)

        contour_nii = mask_nii = None
        if isdefined(self.inputs.mask):
            contour_nii = rotate_affine(load_img(self.inputs.mask),
                                        rot=canonical_r)
            maskdata = contour_nii.get_fdata() > 0
        else:
            mask_nii = threshold_img(refnii, 1e-3)
            maskdata = mask_nii.get_fdata() > 0
        cuts = cuts_from_bbox(contour_nii or mask_nii, cuts=self._n_cuts)
        fmapdata = fmapnii.get_fdata()
        vmax = max(
            abs(np.percentile(fmapdata[maskdata], 99.8)),
            abs(np.percentile(fmapdata[maskdata], 0.2)),
        )
        if self.inputs.apply_mask:
            fmapdata[~maskdata] = 0
            fmapnii = fmapnii.__class__(fmapdata, fmapnii.affine,
                                        fmapnii.header)

        fmap_overlay = [{
            "overlay": fmapnii,
            "overlay_params": {
                "cmap": coolwarm_transparent(max_alpha=self.inputs.max_alpha),
                "vmax": vmax,
                "vmin": -vmax,
            },
        }] * 2

        if self.inputs.show != "both":
            fmap_overlay[not self.inputs.show] = {}

        # Call composer
        compose_view(
            plot_registration(movnii,
                              "moving-image",
                              estimate_brightness=True,
                              cuts=cuts,
                              label=self.inputs.moving_label,
                              contour=contour_nii,
                              compress=False,
                              **fmap_overlay[1]),
            plot_registration(refnii,
                              "fixed-image",
                              estimate_brightness=True,
                              cuts=cuts,
                              label=self.inputs.reference_label,
                              contour=contour_nii,
                              compress=False,
                              **fmap_overlay[0]),
            out_file=self._out_report,
        )