fromwarningsimportwarnimportjsonfromos.pathimportjoinaspjoinimportnumpyasnpimportvtkfromfury.dataimportDATA_DIR# Allow import, but disable doctests if we don't have matplotlibfromfury.optpkgimportoptional_packagecm,have_matplotlib,_=optional_package('matplotlib.cm')
[docs]defcolormap_lookup_table(scale_range=(0,1),hue_range=(0.8,0),saturation_range=(1,1),value_range=(0.8,0.8)):"""Lookup table for the colormap. Parameters ---------- scale_range : tuple It can be anything e.g. (0, 1) or (0, 255). Usually it is the mininum and maximum value of your data. Default is (0, 1). hue_range : tuple of floats HSV values (min 0 and max 1). Default is (0.8, 0). saturation_range : tuple of floats HSV values (min 0 and max 1). Default is (1, 1). value_range : tuple of floats HSV value (min 0 and max 1). Default is (0.8, 0.8). Returns ------- lookup_table : vtkLookupTable """lookup_table=vtk.vtkLookupTable()lookup_table.SetRange(scale_range)lookup_table.SetTableRange(scale_range)lookup_table.SetHueRange(hue_range)lookup_table.SetSaturationRange(saturation_range)lookup_table.SetValueRange(value_range)lookup_table.Build()returnlookup_table
[docs]defboys2rgb(v):""" boys 2 rgb cool colormap Maps a given field of undirected lines (line field) to rgb colors using Boy's Surface immersion of the real projective plane. Boy's Surface is one of the three possible surfaces obtained by gluing a Mobius strip to the edge of a disk. The other two are the crosscap and Roman surface, Steiner surfaces that are homeomorphic to the real projective plane (Pinkall 1986). The Boy's surface is the only 3D immersion of the projective plane without singularities. Visit http://www.cs.brown.edu/~cad/rp2coloring for further details. Cagatay Demiralp, 9/7/2008. Code was initially in matlab and was rewritten in Python for fury by the FURY Team. Thank you Cagatay for putting this online. Parameters ------------ v : array, shape (N, 3) of unit vectors (e.g., principal eigenvectors of tensor data) representing one of the two directions of the undirected lines in a line field. Returns --------- c : array, shape (N, 3) matrix of rgb colors corresponding to the vectors given in V. Examples ---------- >>> from fury import colormap >>> v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> c = colormap.boys2rgb(v) """ifv.ndim==1:x=v[0]y=v[1]z=v[2]ifv.ndim==2:x=v[:,0]y=v[:,1]z=v[:,2]x2=x**2y2=y**2z2=z**2x3=x*x2y3=y*y2z3=z*z2z4=z*z2xy=x*yxz=x*zyz=y*zhh1=.5*(3*z2-1)/1.58hh2=3*xz/2.745hh3=3*yz/2.745hh4=1.5*(x2-y2)/2.745hh5=6*xy/5.5hh6=(1/1.176)*.125*(35*z4-30*z2+3)hh7=2.5*x*(7*z3-3*z)/3.737hh8=2.5*y*(7*z3-3*z)/3.737hh9=((x2-y2)*7.5*(7*z2-1))/15.85hh10=((2*xy)*(7.5*(7*z2-1)))/15.85hh11=105*(4*x3*z-3*xz*(1-z2))/59.32hh12=105*(-4*y3*z+3*yz*(1-z2))/59.32s0=-23.0s1=227.9s2=251.0s3=125.0ss23=ss(2.71,s0)cc23=cc(2.71,s0)ss45=ss(2.12,s1)cc45=cc(2.12,s1)ss67=ss(.972,s2)cc67=cc(.972,s2)ss89=ss(.868,s3)cc89=cc(.868,s3)X=0.0X=X+hh2*cc23X=X+hh3*ss23X=X+hh5*cc45X=X+hh4*ss45X=X+hh7*cc67X=X+hh8*ss67X=X+hh10*cc89X=X+hh9*ss89Y=0.0Y=Y+hh2*-ss23Y=Y+hh3*cc23Y=Y+hh5*-ss45Y=Y+hh4*cc45Y=Y+hh7*-ss67Y=Y+hh8*cc67Y=Y+hh10*-ss89Y=Y+hh9*cc89Z=0.0Z=Z+hh1*-2.8Z=Z+hh6*-0.5Z=Z+hh11*0.3Z=Z+hh12*-2.5# scale and normalize to fit# in the rgb spacew_x=4.1925trl_x=-2.0425w_y=4.0217trl_y=-1.8541w_z=4.0694trl_z=-2.1899ifv.ndim==2:N=len(x)C=np.zeros((N,3))C[:,0]=0.9*np.abs(((X-trl_x)/w_x))+0.05C[:,1]=0.9*np.abs(((Y-trl_y)/w_y))+0.05C[:,2]=0.9*np.abs(((Z-trl_z)/w_z))+0.05ifv.ndim==1:C=np.zeros((3,))C[0]=0.9*np.abs(((X-trl_x)/w_x))+0.05C[1]=0.9*np.abs(((Y-trl_y)/w_y))+0.05C[2]=0.9*np.abs(((Z-trl_z)/w_z))+0.05returnC
[docs]deforient2rgb(v):"""Standard orientation 2 rgb colormap. v : array, shape (N, 3) of vectors not necessarily normalized Returns ------- c : array, shape (N, 3) matrix of rgb colors corresponding to the vectors given in V. Examples -------- >>> from fury import colormap >>> v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> c = colormap.orient2rgb(v) """ifv.ndim==1:orient=vorient=np.abs(orient/np.linalg.norm(orient))ifv.ndim==2:orientn=np.sqrt(v[:,0]**2+v[:,1]**2+v[:,2]**2)orientn.shape=orientn.shape+(1,)orient=np.abs(v/orientn)returnorient
[docs]defline_colors(streamlines,cmap='rgb_standard'):""" Create colors for streamlines to be used in actor.line Parameters ---------- streamlines : sequence of ndarrays cmap : ('rgb_standard', 'boys_standard') Returns ------- colors : ndarray """ifcmap=='rgb_standard':col_list=[orient2rgb(streamline[-1]-streamline[0])forstreamlineinstreamlines]ifcmap=='boys_standard':col_list=[boys2rgb(streamline[-1]-streamline[0])forstreamlineinstreamlines]returnnp.vstack(col_list)
[docs]defget_cmap(name):"""Makes a callable, similar to maptlotlib.pyplot.get_cmap."""ifname.lower()=="accent":warn("The `Accent` colormap is deprecated as of version"+" 0.2 of Fury and will be removed in a future "+"version. Please use another colormap",DeprecationWarning)globaldipy_cmapsifdipy_cmapsisNone:filename=pjoin(DATA_DIR,"dipy_colormaps.json")withopen(filename)asf:dipy_cmaps=json.load(f)desc=dipy_cmaps.get(name)ifdescisNone:returnNonedefsimple_cmap(v):"""Emulates matplotlib colormap callable"""rgba=np.ones((len(v),4))fori,colorinenumerate(('red','green','blue')):x,y0,y1=zip(*desc[color])# Matplotlib allows more complex colormaps, but for users who do# not have Matplotlib fury makes a few simple colormaps available.# These colormaps are simple because y0 == y1, and therefor we# ignore y1 here.rgba[:,i]=np.interp(v,x,y0)returnrgbareturnsimple_cmap
[docs]defcreate_colormap(v,name='plasma',auto=True):"""Create colors from a specific colormap and return it as an array of shape (N,3) where every row gives the corresponding r,g,b value. The colormaps we use are similar with those of matplotlib. Parameters ---------- v : (N,) array vector of values to be mapped in RGB colors according to colormap name : str. Name of the colormap. Currently implemented: 'jet', 'blues', 'accent', 'bone' and matplotlib colormaps if you have matplotlib installed. For example, we suggest using 'plasma', 'viridis' or 'inferno'. 'jet' is popular but can be often misleading and we will deprecate it the future. auto : bool, if auto is True then v is interpolated to [0, 10] from v.min() to v.max() Notes ----- FURY supports a few colormaps for those who do not use Matplotlib, for more colormaps consider downloading Matplotlib (see matplotlib.org). """ifnothave_matplotlib:msg="You do not have Matplotlib installed. Some colormaps"msg+=" might not work for you. Consider downloading Matplotlib."warn(msg)ifname.lower()=='jet':msg='Jet is a popular colormap but can often be misleading'msg+='Use instead plasma, viridis, hot or inferno.'warn(msg,DeprecationWarning)ifv.ndim>1:msg='This function works only with 1d arrays. Use ravel()'raiseValueError(msg)ifauto:v=np.interp(v,[v.min(),v.max()],[0,1])else:v=np.clip(v,0,1)# For backwards compatibility with lowercase namesnewname=lowercase_cm_name.get(name)ornameget_colormap=cm.get_cmapifhave_matplotlibelseget_cmapcolormap=get_colormap(newname)ifcolormapisNone:e_s="Colormap {} is not yet implemented ".format(name)raiseValueError(e_s)rgba=colormap(v)rgb=rgba[:,:3].copy()returnrgb