def figure(n=None,style=os.path.join(_user_path,"currstyle.gs"), color=-2, frame=0, labelsize=14, labelfont='helvetica',aspect=1.618,land=0): global _figures if (aspect < 0.1) or (aspect > 10): aspect = 1.618 if isinstance(color, types.StringType): color = _colornum[color] fid = open(style,'w') syst = write_style.getsys(color=color,frame=frame, labelsize=labelsize,font=labelfont) if land: cntr = (5.5*inches,4.25*inches) # horizontal, vertical else: cntr = (4.25*inches,5.5*inches) height = 4.25*inches width = aspect*height syst['viewport'] = [cntr[0]-width/2.0,cntr[0]+width/2.0,cntr[1]-height/2.0,cntr[1]+height/2.0] fid.write(write_style.style2string(syst,landscape=land)) fid.close() if n is None: winnum = gist.window(style=style,width=int(width*1.25/inches*_dpi),height=int(height*1.4/inches*_dpi)) if winnum < 0: gist.window(style=style,width=int(width*1.25/inches*_dpi),height=int(height*1.4/inches*_dpi)) else: gist.window(n,style=style,width=int(width*1.25/inches*_dpi),height=int(height*1.4/inches*_dpi)) _current_style = style return
def imagesc(z,cmin=None,cmax=None,xryr=None,_style='default', palette=None, color='black',colormap=None): """Plot an image on axes. z -- The data cmin -- Value to map to lowest color in palette (min(z) if None) cmax -- Value to map to highest color in palette (max(z) if None) xryr -- (xmin, ymin, xmax, ymax) coordinates to print (0, 0, z.shape[1], z.shape[0]) if None _style -- A 'style-sheet' to use if desired (a default one will be used if 'default'). If None, then no style will be imposed. palette -- A string for a palette previously saved in a file (see write_palette) or an array specifying the red-green-blue values (2-d array N x 3) or gray-scale values (2-d array N x 1 or 1-d array). color -- The color to use for the axes. """ if xryr is None: xryr = (0,0,z.shape[1],z.shape[0]) try: _style = None saveval = gist.plsys(2) gist.plsys(saveval) except: _style = 'default' if not _hold: gist.fma() gist.animate(0) if _style is not None: if _style == "default": _style=os.path.join(_user_path,'image.gs') system = write_style.getsys(hticpos='below',vticpos='left',frame=1, color=color) fid = open(_style,'w') fid.write(write_style.style2string(system)) fid.close() gist.window(style=_style) _current_style=_style if cmax is None: cmax = max(ravel(z)) if cmin is None: cmin = min(ravel(z)) cmax = float(cmax) cmin = float(cmin) byteimage = gist.bytscl(z,cmin=cmin,cmax=cmax) if (colormap is not None): palette=colormap change_palette(palette) gist.pli(byteimage,xryr[0],xryr[1],xryr[2],xryr[3]) return
def imagesc_cb(z,cmin=None,cmax=None,xryr=None,_style='default', zlabel=None,font='helvetica',fontsize=16,color='black', palette=None): """Plot an image on axes with a colorbar on the side. z -- The data cmin -- Value to map to lowest color in palette (min(z) if None) cmax -- Value to map to highest color in palette (max(z) if None) xryr -- (xmin, ymin, xmax, ymax) coordinates to print (0, 0, z.shape[1], z.shape[0]) if None _style -- A 'style-sheet' to use if desired (a default one will be used if 'default'). If None, then no style will be imposed. palette -- A string for a palette previously saved in a file (see write_palette) or an array specifying the red-green-blue values (2-d array N x 3) or gray-scale values (2-d array N x 1 or 1-d array). zlabel -- The label to attach to the colorbar (font, fontsize, and color match this). color -- The color to use for the ticks and frame. """ if xryr is None: xryr = (0,0,z.shape[1],z.shape[0]) if not _hold: gist.fma() gist.animate(0) if _style is not None: if _style == 'default': _style=os.path.join(_user_path,"colorbar.gs") system = write_style.getsys(hticpos='below',vticpos='left',frame=1,color=color) fid = open(_style,'w') fid.write(write_style.style2string(system)) fid.close() gist.window(style=_style) _current_style=_style if cmax is None: cmax = max(ravel(z)) if cmin is None: cmin = min(ravel(z)) cmax = float(cmax) cmin = float(cmin) change_palette(palette) byteimage = gist.bytscl(z,cmin=cmin,cmax=cmax) gist.pli(byteimage,xryr[0],xryr[1],xryr[2],xryr[3]) colorbar.color_bar(cmin,cmax,ncol=240,zlabel=zlabel,font=font,fontsize=fontsize,color=color)
def twoplane(DATA,slice1,slice2,dx=[1,1,1],cmin=None,cmax=None,xb=None,xe=None, xlab="",ylab="",zlab="",clab="",titl="", totalheight=0.5,space=0.02, medfilt=5, font='helvetica',fontsize=16,color='black',lcolor='white', fcolor='black', cb=1, line=1, palette=None): """ Visualize a 3d volume as a two connected slices. The slices are given in the 2-tuple slice1 and slice2. These give the dimension and corresponding slice numbers to plot. The unchosen slice is the common dimension in the images. twoplane(img3d,(0,12),(2,60)) plots two images with a common "x"-axis as the first dimension. The lower plot is img3d[12,:,:] with a line through row 60 corresponding to the slice transpose(img3d[:,:,60]) plotted above this first plot. """ if xb is None: xb = [0,0,0] if xe is None: xe = DATA.shape # get two image slices # make special style file so that pixels are square getdx = array([1,1,1]) imgsl1 = [slice(None,None),slice(None,None),slice(None,None)] imgsl1[slice1[0]] = slice1[1] img1 = DATA[imgsl1] getdx1 = getdx.__copy__() getdx1[slice1[0]] = 0 dx1 = compress(getdx1,dx,axis=-1) xb1 = compress(getdx1,xb,axis=-1) xe1 = compress(getdx1,xe,axis=-1) imgsl2 = [slice(None,None),slice(None,None),slice(None,None)] imgsl2[slice2[0]] = slice2[1] img2 = DATA[imgsl2] getdx2 = getdx.__copy__() getdx2[slice2[0]] = 0 dx2 = compress(getdx2,dx,axis=-1) xb2 = compress(getdx2,xb,axis=-1) xe2 = compress(getdx2,xe,axis=-1) if (slice1[0] == slice2[0]): raise ValueError, "Same slice dimension.." for k in range(3): if k not in [slice1[0],slice2[0]]: samedim = k break if samedim == 2: pass elif samedim == 1: if samedim > slice1[0]: img1 = transpose(img1) dx1 = dx1[::-1] xb1 = xb1[::-1] xe1 = xe1[::-1] if samedim > slice2[0]: img2 = transpose(img2) dx2 = dx2[::-1] xb2 = xb2[::-1] xe2 = xe2[::-1] else: img1 = transpose(img1) dx1 = dx1[::-1] xb1 = xb1[::-1] xe1 = xe1[::-1] img2 = transpose(img2) dx2 = dx2[::-1] xb2 = xb2[::-1] xe2 = xe2[::-1] assert(img1.shape[1] == img2.shape[1]) units = totalheight - space totaldist = img1.shape[0]*dx1[0] + img2.shape[0]*dx2[0] convfactor = units / float(totaldist) height1 = img1.shape[0]*dx1[0] * convfactor xwidth = img1.shape[1]*dx1[1]*convfactor if xwidth > 0.6: rescale = 0.6 / xwidth xwidth = rescale * xwidth height1 = rescale * height1 totalheight = totalheight * rescale print xwidth, height1 else: print xwidth ystart = 0.5 - totalheight / 2 ypos1 = [ystart, ystart+height1] ypos2 = [ystart+height1+space,ystart+totalheight] xpos = [0.395-xwidth/2.0, 0.395+xwidth/2.0] systems = [] system = write_style.getsys(hticpos='', vticpos='left') system['viewport'] = [xpos[0],xpos[1],ypos2[0],ypos2[1]] if fcolor not in ['black',None]: _add_color(system, _colornum[color]) systems.append(system) system = write_style.getsys(hticpos='below', vticpos='left') system['viewport'] = [xpos[0],xpos[1],ypos1[0],ypos1[1]] if fcolor not in ['black',None]: _add_color(system, _colornum[color]) systems.append(system) the_style = os.path.join(_user_path,"two-plane.gs") write_style.writestyle(the_style,systems) gist.window(style=the_style) _current_style = the_style change_palette(palette) gist.plsys(1) if medfilt > 1: img1 = signal.medfilt(img1,[medfilt,medfilt]) img2 = signal.medfilt(img2,[medfilt,medfilt]) if cmax is None: cmax = max(max(ravel(img1)),max(ravel(img2))) if cmin is None: cmin = min(min(ravel(img1)),min(ravel(img2))) cmax = float(cmax) cmin = float(cmin) byteimage = gist.bytscl(img2,cmin=cmin,cmax=cmax) gist.pli(byteimage,xb2[1],xb2[0],xe2[1],xe2[0]) ylabel(zlab,color=color) if titl != "": title(titl,color=color) if line: xstart = xb2[1] xstop = xe2[1] yval = slice1[1]*(xe2[0] - xb2[0])/(img2.shape[0]) + xb2[0] gist.pldj([xstart],[yval],[xstop],[yval],type='dash',width=2,color='white') gist.plsys(2) ylabel(ylab,color=color) xlabel(xlab,color=color) byteimage = gist.bytscl(img1,cmin=cmin,cmax=cmax) gist.pli(byteimage,xb1[1],xb1[0],xe1[1],xe1[0]) if line: xstart = xb1[1] xstop = xe1[1] yval = slice2[1]*(xe1[0] - xb1[0])/(img1.shape[0]) + xb1[0] gist.pldj([xstart],[yval],[xstop],[yval],type='dash',width=2,color='white') if cb: colorbar.color_bar(cmin,cmax,ncol=240,zlabel=clab,font=font,fontsize=fontsize,color=color,ymin=ystart,ymax=ystart+totalheight,xmin0=xpos[1]+0.02,xmax0=xpos[1]+0.04)
def twoplane(DATA,slice1,slice2,dx=[1,1,1],cmin=None,cmax=None,xb=None,xe=None, xlab="",ylab="",zlab="",clab="",titl="", totalheight=0.5,space=0.02, medfilt=5, font='helvetica',fontsize=16,color='black',lcolor='white', fcolor='black', cb=1, line=1, palette=None): """ Visualize a 3d volume as a two connected slices. The slices are given in the 2-tuple slice1 and slice2. These give the dimension and corresponding slice numbers to plot. The unchosen slice is the common dimension in the images. twoplane(img3d,(0,12),(2,60)) plots two images with a common "x"-axis as the first dimension. The lower plot is img3d[12,:,:] with a line through row 60 corresponding to the slice transpose(img3d[:,:,60]) plotted above this first plot. """ if xb is None: xb = [0,0,0] if xe is None: xe = DATA.shape # get two image slices # make special style file so that pixels are square getdx = array([1,1,1]) imgsl1 = [slice(None,None),slice(None,None),slice(None,None)] imgsl1[slice1[0]] = slice1[1] img1 = DATA[imgsl1] getdx1 = getdx.__copy__() getdx1[slice1[0]] = 0 dx1 = compress(getdx1,dx,axis=-1) xb1 = compress(getdx1,xb,axis=-1) xe1 = compress(getdx1,xe,axis=-1) imgsl2 = [slice(None,None),slice(None,None),slice(None,None)] imgsl2[slice2[0]] = slice2[1] img2 = DATA[imgsl2] getdx2 = getdx.__copy__() getdx2[slice2[0]] = 0 dx2 = compress(getdx2,dx,axis=-1) xb2 = compress(getdx2,xb,axis=-1) xe2 = compress(getdx2,xe,axis=-1) if (slice1[0] == slice2[0]): raise ValueError, "Same slice dimension.." for k in range(3): if k not in [slice1[0],slice2[0]]: samedim = k break if samedim == 2: pass elif samedim == 1: if samedim > slice1[0]: img1 = transpose(img1) dx1 = dx1[::-1] xb1 = xb1[::-1] xe1 = xe1[::-1] if samedim > slice2[0]: img2 = transpose(img2) dx2 = dx2[::-1] xb2 = xb2[::-1] xe2 = xe2[::-1] else: img1 = transpose(img1) dx1 = dx1[::-1] xb1 = xb1[::-1] xe1 = xe1[::-1] img2 = transpose(img2) dx2 = dx2[::-1] xb2 = xb2[::-1] xe2 = xe2[::-1] assert(img1.shape[1] == img2.shape[1]) units = totalheight - space totaldist = img1.shape[0]*dx1[0] + img2.shape[0]*dx2[0] convfactor = units / float(totaldist) height1 = img1.shape[0]*dx1[0] * convfactor xwidth = img1.shape[1]*dx1[1]*convfactor if xwidth > 0.6: rescale = 0.6 / xwidth xwidth = rescale * xwidth height1 = rescale * height1 totalheight = totalheight * rescale print xwidth, height1 else: print xwidth ystart = 0.5 - totalheight / 2 ypos1 = [ystart, ystart+height1] ypos2 = [ystart+height1+space,ystart+totalheight] xpos = [0.395-xwidth/2.0, 0.395+xwidth/2.0] systems = [] system = write_style.getsys(hticpos='', vticpos='left') system['viewport'] = [xpos[0],xpos[1],ypos2[0],ypos2[1]] if fcolor not in ['black',None]: _add_color(system, _colornum[color]) systems.append(system) system = write_style.getsys(hticpos='below', vticpos='left') system['viewport'] = [xpos[0],xpos[1],ypos1[0],ypos1[1]] if fcolor not in ['black',None]: _add_color(system, _colornum[color]) systems.append(system) the_style = os.path.join(_user_path,"two-plane.gs") write_style.writestyle(the_style,systems) gist.window(style=the_style) _current_style = the_style change_palette(palette) gist.plsys(1) if medfilt > 1: import scipy.signal img1 = scipy.signal.medfilt(img1,[medfilt,medfilt]) img2 = scipy.signal.medfilt(img2,[medfilt,medfilt]) if cmax is None: cmax = max(max(ravel(img1)),max(ravel(img2))) if cmin is None: cmin = min(min(ravel(img1)),min(ravel(img2))) cmax = float(cmax) cmin = float(cmin) byteimage = gist.bytscl(img2,cmin=cmin,cmax=cmax) gist.pli(byteimage,xb2[1],xb2[0],xe2[1],xe2[0]) ylabel(zlab,color=color) if titl != "": title(titl,color=color) if line: xstart = xb2[1] xstop = xe2[1] yval = slice1[1]*(xe2[0] - xb2[0])/(img2.shape[0]) + xb2[0] gist.pldj([xstart],[yval],[xstop],[yval],type='dash',width=2,color='white') gist.plsys(2) ylabel(ylab,color=color) xlabel(xlab,color=color) byteimage = gist.bytscl(img1,cmin=cmin,cmax=cmax) gist.pli(byteimage,xb1[1],xb1[0],xe1[1],xe1[0]) if line: xstart = xb1[1] xstop = xe1[1] yval = slice2[1]*(xe1[0] - xb1[0])/(img1.shape[0]) + xb1[0] gist.pldj([xstart],[yval],[xstop],[yval],type='dash',width=2,color='white') if cb: colorbar.color_bar(cmin,cmax,ncol=240,zlabel=clab,font=font,fontsize=fontsize,color=color,ymin=ystart,ymax=ystart+totalheight,xmin0=xpos[1]+0.02,xmax0=xpos[1]+0.04)