def plotstatsimg(cbf, ref_vol, plot_params=None, order=('z', 'x', 'y'), estimate_brightness=False, label=None, compress='auto'): """ plot zsts """ plot_params = {} if plot_params is None else plot_params image_nii = _3d_in_file(cbf) data = image_nii.get_fdata() from nilearn.image import threshold_img bbox_nii = threshold_img(nb.load(cbf), 1) cuts = cuts_from_bbox(bbox_nii, cuts=7) out_files = [] if estimate_brightness: plot_params = robust_set_limits(data, plot_params) # Plot each cut axis for i, mode in enumerate(list(order)): plot_params['display_mode'] = mode plot_params['cut_coords'] = cuts[mode] plot_params['draw_cross'] = False plot_params['symmetric_cbar'] = False plot_params['threshold'] = 0.05 plot_params['vmax'] = 90 plot_params['colorbar'] = False plot_params['cmap'] = 'gray' if i == 0: plot_params['title'] = label plot_params['colorbar'] = False else: plot_params['title'] = None # Generate nilearn figure from nilearn.plotting import plot_stat_map display = plot_stat_map(stat_map_img=cbf, bg_img=ref_vol, **plot_params) svg = extract_svg(display, compress=compress) display.close() from lxml import etree from .. import NIWORKFLOWS_LOG # Find and replace the figure_1 id. try: xml_data = etree.fromstring(svg) except etree.XMLSyntaxError as e: NIWORKFLOWS_LOG.info(e) return svg_fig = SVGFigure() svg_fig.root = xml_data out_files.append(svg_fig) return out_files
def plot_segs(image_nii, seg_niis, out_file, bbox_nii=None, masked=False, colors=None, compress='auto', **plot_params): """ plot segmentation as contours over the image (e.g. anatomical). seg_niis should be a list of files. mask_nii helps determine the cut coordinates. plot_params will be passed on to nilearn plot_* functions. If seg_niis is a list of size one, it behaves as if it was plotting the mask. """ plot_params = {} if plot_params is None else plot_params image_nii = _3d_in_file(image_nii) data = image_nii.get_data() plot_params = robust_set_limits(data, plot_params) bbox_nii = nb.load(image_nii if bbox_nii is None else bbox_nii) if masked: bbox_nii = nlimage.threshold_img(bbox_nii, 1e-3) cuts = cuts_from_bbox(bbox_nii, cuts=7) plot_params['colors'] = colors or plot_params.get('colors', None) out_files = [] for d in plot_params.pop('dimensions', ('z', 'x', 'y')): plot_params['display_mode'] = d plot_params['cut_coords'] = cuts[d] svg = _plot_anat_with_contours(image_nii, segs=seg_niis, compress=compress, **plot_params) # Find and replace the figure_1 id. try: xml_data = etree.fromstring(svg) except etree.XMLSyntaxError as e: NIWORKFLOWS_LOG.info(e) return find_text = etree.ETXPath("//{%s}g[@id='figure_1']" % SVGNS) find_text(xml_data)[0].set('id', 'segmentation-%s-%s' % (d, uuid4())) svg_fig = SVGFigure() svg_fig.root = xml_data out_files.append(svg_fig) return out_files
def plot_acpc(acpc_registered_img, div_id, plot_params=None, order=('z', 'x', 'y'), cuts=None, estimate_brightness=False, label=None, compress='auto'): """ Plot the results of an AC-PC transformation. """ plot_params = plot_params or {} # Do the low-b image first out_files = [] if estimate_brightness: plot_params = robust_set_limits( acpc_registered_img.get_fdata(dtype='float32').reshape(-1), plot_params) # Plot each cut axis for low-b for i, mode in enumerate(list(order)): plot_params['display_mode'] = mode plot_params['cut_coords'] = [-20.0, 0.0, 20.0] if i == 0: plot_params['title'] = label else: plot_params['title'] = None # Generate nilearn figure display = plot_anat(acpc_registered_img, **plot_params) for coord, axis in display.axes.items(): axis.ax.axvline(0, lw=1) axis.ax.axhline(0, lw=1) svg = extract_svg(display, compress=compress) display.close() # Find and replace the figure_1 id. xml_data = etree.fromstring(svg) find_text = etree.ETXPath("//{%s}g[@id='figure_1']" % SVGNS) find_text(xml_data)[0].set('id', '%s-%s-%s' % (div_id, mode, uuid4())) svg_fig = SVGFigure() svg_fig.root = xml_data out_files.append(svg_fig) return out_files
def plot_denoise(lowb_nii, highb_nii, div_id, plot_params=None, highb_plot_params=None, order=('z', 'x', 'y'), cuts=None, estimate_brightness=False, label=None, lowb_contour=None, highb_contour=None, compress='auto', overlay=None, overlay_params=None): """ Plot the foreground and background views. Default order is: axial, coronal, sagittal Updated version from sdcflows """ plot_params = plot_params or {} highb_plot_params = highb_plot_params or {} # Use default MNI cuts if none defined if cuts is None: raise NotImplementedError # Do the low-b image first out_files = [] if estimate_brightness: plot_params = robust_set_limits( lowb_nii.get_fdata(dtype='float32').reshape(-1), plot_params) # Plot each cut axis for low-b for i, mode in enumerate(list(order)): plot_params['display_mode'] = mode plot_params['cut_coords'] = cuts[mode] if i == 0: plot_params['title'] = label + ": low-b" else: plot_params['title'] = None # Generate nilearn figure display = plot_anat(lowb_nii, **plot_params) if lowb_contour is not None: display.add_contours(lowb_contour, linewidths=1) svg = extract_svg(display, compress=compress) display.close() # Find and replace the figure_1 id. xml_data = etree.fromstring(svg) find_text = etree.ETXPath("//{%s}g[@id='figure_1']" % SVGNS) find_text(xml_data)[0].set('id', '%s-%s-%s' % (div_id, mode, uuid4())) svg_fig = SVGFigure() svg_fig.root = xml_data out_files.append(svg_fig) # Plot each cut axis for high-b if estimate_brightness: highb_plot_params = robust_set_limits( highb_nii.get_fdata(dtype='float32').reshape(-1), highb_plot_params) for i, mode in enumerate(list(order)): highb_plot_params['display_mode'] = mode highb_plot_params['cut_coords'] = cuts[mode] if i == 0: highb_plot_params['title'] = label + ': high-b' else: highb_plot_params['title'] = None # Generate nilearn figure display = plot_anat(highb_nii, **highb_plot_params) if highb_contour is not None: display.add_contours(highb_contour, linewidths=1) svg = extract_svg(display, compress=compress) display.close() # Find and replace the figure_1 id. xml_data = etree.fromstring(svg) find_text = etree.ETXPath("//{%s}g[@id='figure_1']" % SVGNS) find_text(xml_data)[0].set('id', '%s-%s-%s' % (div_id, mode, uuid4())) svg_fig = SVGFigure() svg_fig.root = xml_data out_files.append(svg_fig) return out_files
def plot_registration(anat_nii, div_id, plot_params=None, order=('z', 'x', 'y'), cuts=None, estimate_brightness=False, label=None, contour=None, compress='auto'): """ Plots the foreground and background views Default order is: axial, coronal, sagittal """ plot_params = {} if plot_params is None else plot_params # Use default MNI cuts if none defined if cuts is None: raise NotImplementedError # TODO out_files = [] if estimate_brightness: plot_params = robust_set_limits(anat_nii.get_data().reshape(-1), plot_params) # FreeSurfer ribbon.mgz ribbon = contour is not None and np.array_equal( np.unique(contour.get_data()), [0, 2, 3, 41, 42]) if ribbon: contour_data = contour.get_data() % 39 white = nlimage.new_img_like(contour, contour_data == 2) pial = nlimage.new_img_like(contour, contour_data >= 2) # dual mask dual_mask = contour is not None and np.array_equal( np.unique(contour.get_data().astype(np.uint8)), [0, 1, 2]) if dual_mask: contour_data = contour.get_data() outer_mask = nlimage.new_img_like(contour, contour_data == 1) inner_mask = nlimage.new_img_like(contour, contour_data == 2) all_mask = nlimage.new_img_like(contour, contour_data > 0) # Plot each cut axis for i, mode in enumerate(list(order)): plot_params['display_mode'] = mode plot_params['cut_coords'] = cuts[mode] if i == 0: plot_params['title'] = label else: plot_params['title'] = None # Generate nilearn figure display = plot_anat(anat_nii, **plot_params) if ribbon: kwargs = {'levels': [0.5], 'linewidths': 0.5} display.add_contours(white, colors='b', **kwargs) display.add_contours(pial, colors='r', **kwargs) elif dual_mask: kwargs = {'levels': [0.5], 'linewidths': 0.75} display.add_contours(inner_mask, colors='b', **kwargs) display.add_contours(outer_mask, colors='r', **kwargs) display.add_contours(all_mask, colors='c', **kwargs) elif contour is not None: display.add_contours(contour, colors='b', levels=[0.5], linewidths=0.5) svg = extract_svg(display, compress=compress) display.close() # Find and replace the figure_1 id. try: xml_data = etree.fromstring(svg) except etree.XMLSyntaxError as e: NIWORKFLOWS_LOG.info(e) return find_text = etree.ETXPath("//{%s}g[@id='figure_1']" % SVGNS) find_text(xml_data)[0].set('id', '%s-%s-%s' % (div_id, mode, uuid4())) svg_fig = SVGFigure() svg_fig.root = xml_data out_files.append(svg_fig) return out_files
def plot_registration(anat_nii, div_id, plot_params=None, order=('z', 'x', 'y'), cuts=None, estimate_brightness=False, label=None, contour=None, compress='auto'): """ Plot the foreground and background views. Default order is: axial, coronal, sagittal """ from uuid import uuid4 from lxml import etree from nilearn.plotting import plot_anat from svgutils.transform import SVGFigure from niworkflows.viz.utils import robust_set_limits, extract_svg, SVGNS plot_params = plot_params or {} # Use default MNI cuts if none defined if cuts is None: raise NotImplementedError # TODO out_files = [] if estimate_brightness: plot_params = robust_set_limits(anat_nii.get_data().reshape(-1), plot_params) # Plot each cut axis for i, mode in enumerate(list(order)): plot_params['display_mode'] = mode plot_params['cut_coords'] = cuts[mode] if i == 0: plot_params['title'] = label else: plot_params['title'] = None # Generate nilearn figure display = plot_anat(anat_nii, **plot_params) if contour is not None: display.add_contours(contour, colors='g', levels=[0.5], linewidths=0.5) svg = extract_svg(display, compress=compress) display.close() # Find and replace the figure_1 id. xml_data = etree.fromstring(svg) find_text = etree.ETXPath("//{%s}g[@id='figure_1']" % SVGNS) find_text(xml_data)[0].set('id', '%s-%s-%s' % (div_id, mode, uuid4())) svg_fig = SVGFigure() svg_fig.root = xml_data out_files.append(svg_fig) return out_files
def fromfile(fname): fig = SVGFigure() svg_file = etree.parse(fname) fig.root = svg_file.getroot() return fig
def plot_registration( anat_nii, div_id, plot_params=None, order=("z", "x", "y"), cuts=None, estimate_brightness=False, label=None, contour=None, compress="auto", overlay=None, overlay_params=None, ): """ Plot the foreground and background views. Default order is: axial, coronal, sagittal """ from uuid import uuid4 from lxml import etree import matplotlib.pyplot as plt from nilearn.plotting import plot_anat from svgutils.transform import SVGFigure from niworkflows.viz.utils import robust_set_limits, extract_svg, SVGNS plot_params = plot_params or {} # Use default MNI cuts if none defined if cuts is None: raise NotImplementedError # TODO out_files = [] if estimate_brightness: plot_params = robust_set_limits( anat_nii.get_fdata(dtype="float32").reshape(-1), plot_params) # Plot each cut axis for i, mode in enumerate(list(order)): plot_params["display_mode"] = mode plot_params["cut_coords"] = cuts[mode] if i == 0: plot_params["title"] = label else: plot_params["title"] = None # Generate nilearn figure display = plot_anat(anat_nii, **plot_params) if overlay is not None: _overlay_params = { "vmin": overlay.get_fdata(dtype="float32").min(), "vmax": overlay.get_fdata(dtype="float32").max(), "cmap": plt.cm.gray, "interpolation": "nearest", } _overlay_params.update(overlay_params) display.add_overlay(overlay, **_overlay_params) if contour is not None: display.add_contours(contour, colors="g", levels=[0.5], linewidths=0.5) svg = extract_svg(display, compress=compress) display.close() # Find and replace the figure_1 id. xml_data = etree.fromstring(svg) find_text = etree.ETXPath("//{%s}g[@id='figure_1']" % SVGNS) find_text(xml_data)[0].set("id", "%s-%s-%s" % (div_id, mode, uuid4())) svg_fig = SVGFigure() svg_fig.root = xml_data out_files.append(svg_fig) return out_files
def plot_registration(anat_nii, div_id, plot_params=None, order=('z', 'x', 'y'), cuts=None, estimate_brightness=False, label=None, contour=None, compress='auto'): """ Plots the foreground and background views Default order is: axial, coronal, sagittal """ plot_params = {} if plot_params is None else plot_params # Use default MNI cuts if none defined if cuts is None: raise NotImplementedError # TODO out_files = [] if estimate_brightness: plot_params = robust_set_limits(anat_nii.get_data().reshape(-1), plot_params) # FreeSurfer ribbon.mgz ribbon = contour is not None and np.array_equal( np.unique(contour.get_data()), [0, 2, 3, 41, 42]) if ribbon: contour_data = contour.get_data() % 39 white = nlimage.new_img_like(contour, contour_data == 2) pial = nlimage.new_img_like(contour, contour_data >= 2) # Plot each cut axis for i, mode in enumerate(list(order)): plot_params['display_mode'] = mode plot_params['cut_coords'] = cuts[mode] if i == 0: plot_params['title'] = label else: plot_params['title'] = None # Generate nilearn figure display = plot_anat(anat_nii, **plot_params) if ribbon: kwargs = {'levels': [0.5], 'linewidths': 0.5} display.add_contours(white, colors='b', **kwargs) display.add_contours(pial, colors='r', **kwargs) elif contour is not None: display.add_contours(contour, colors='b', levels=[0.5], linewidths=0.5) svg = extract_svg(display, compress=compress) display.close() # Find and replace the figure_1 id. try: xml_data = etree.fromstring(svg) except etree.XMLSyntaxError as e: NIWORKFLOWS_LOG.info(e) return find_text = etree.ETXPath("//{%s}g[@id='figure_1']" % SVGNS) find_text(xml_data)[0].set('id', '%s-%s-%s' % (div_id, mode, uuid4())) svg_fig = SVGFigure() svg_fig.root = xml_data out_files.append(svg_fig) return out_files