# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-# vi: set ft=python sts=4 ts=4 sw=4 et:## Copyright 2021 The NiPreps Developers <nipreps@gmail.com>## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.## We support and encourage derived works from this project, please read# about our expectations at## https://www.nipreps.org/community/licensing/#"""Visualization tooling."""
[docs]defplot_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 """fromuuidimportuuid4fromlxmlimportetreeimportmatplotlib.pyplotaspltfromnilearn.plottingimportplot_anatfromsvgutils.transformimportSVGFigurefromnireports.reportlets.utilsimportrobust_set_limits,extract_svg,SVGNSplot_params=plot_paramsor{}# Use default MNI cuts if none definedifcutsisNone:raiseNotImplementedError# TODOout_files=[]ifestimate_brightness:plot_params=robust_set_limits(anat_nii.get_fdata(dtype="float32").reshape(-1),plot_params)# Plot each cut axisfori,modeinenumerate(list(order)):plot_params["display_mode"]=modeplot_params["cut_coords"]=cuts[mode]ifi==0:plot_params["title"]=labelelse:plot_params["title"]=None# Generate nilearn figuredisplay=plot_anat(anat_nii,**plot_params)ifoverlayisnotNone:_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)ifcontourisnotNone: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_dataout_files.append(svg_fig)returnout_files
[docs]defcoolwarm_transparent(max_alpha=0.7,opaque_perc=30,transparent_perc=8):"""Modify the coolwarm color scale to have full transparency around the middle."""importnumpyasnpimportmatplotlib.pylabasplfrommatplotlib.colorsimportListedColormap# Choose colormapcmap=pl.cm.coolwarm# Get the colormap colorsmy_cmap=cmap(np.arange(cmap.N))_20perc=(cmap.N*opaque_perc)//100midpoint=cmap.N//2+1_10perc=(cmap.N*transparent_perc)//100# Set alphaalpha=np.zeros(cmap.N)alpha[:_20perc]=max_alphaalpha[-_20perc:]=max_alphaalpha[_20perc:midpoint-_10perc-1]=np.linspace(max_alpha,0,len(alpha[_20perc:midpoint-_10perc-1]))alpha[midpoint+_10perc:-_20perc]=np.linspace(0,max_alpha,len(alpha[midpoint+_10perc:-_20perc]))my_cmap[:,-1]=alphareturnListedColormap(my_cmap)