diff --git a/pywafo/src/wafo/__init__.py b/pywafo/src/wafo/__init__.py index bb654e1..9a0efe9 100644 --- a/pywafo/src/wafo/__init__.py +++ b/pywafo/src/wafo/__init__.py @@ -10,7 +10,9 @@ import transform import definitions import polynomial import stats -import interpolate +import interpolate +import fig +import dctpack try: from wafo.version import version as __version__ diff --git a/pywafo/src/wafo/fig.py b/pywafo/src/wafo/fig.py new file mode 100644 index 0000000..77bcbb7 --- /dev/null +++ b/pywafo/src/wafo/fig.py @@ -0,0 +1,813 @@ +''' +Module FIG +------------ +Module for manipulating windows/figures created using +pylab or enthought.mayavi.mlab on the windows platform. + +Figure manipulation involves +maximization, minimization, hiding, closing, stacking or tiling. + +This module assumes that the figures are uniquely numbered in the following way: +Figure 1 +Figure 2 +.... +or +TVTK scene 1 +TVTK scene 2 +TVTK scene 3 +... + +Example +------- +>>> import pylab as p +>>> import wafo.fig as fig +>>> for ix in range(6): +... f = p.figure(ix) +>>> fig.stack('all') +>>> fig.stack(1,2) +>>> fig.hide(1) +>>> fig.restore(1) +>>> fig.tile() +>>> fig.pile() +>>> fig.maximize(4) +>>> fig.close('all') +''' + +#/usr/bin/env python +from __future__ import division +#import win32api +import win32gui +import win32con +import msvcrt +import numpy + +__all__ = ['close','cycle','hide','keep','maximize','minimize','pile','restore','stack','tile'] + +# Figure format strings to recognize in window title +_FIG_FORMATS = ('Figure','TVTK Scene','Chaco Plot Window: Figure') +_SCREENSIZE = None + +def _getScreenSize(wnds): + ''' Return screen size X,Y,W,H + + Returns + -------- + X Specifies the new position of the left side of the screen. + Y Specifies the new position of the top of the screen. + W Specifies the new width of the screen. + H Specifies the new height of the screen. + + Parameters + ---------- + wnds: list of handles to open figures + (Note: only needed the first time) + + ''' + + global _SCREENSIZE + if _SCREENSIZE == None: + MoveWindow = win32gui.MoveWindow + GetWindowRect = win32gui.GetWindowRect + SW_MAXIMIZE = win32con.SW_SHOWMAXIMIZED + hwnd = [wnds[0]] + pos = list(GetWindowRect(hwnd[0])) + pos[3] -= pos[1] + pos[2] -= pos[0] + _show_windows(hwnd, SW_MAXIMIZE) + _SCREENSIZE = list(GetWindowRect(hwnd[0])) # Screen size + _SCREENSIZE[3] -= _SCREENSIZE[1] + _SCREENSIZE[2] -= _SCREENSIZE[0] + _SCREENSIZE = tuple(_SCREENSIZE) + + MoveWindow(hwnd[0], pos[0], pos[1], pos[2], pos[3], 1) + + return list(_SCREENSIZE) + +def _windowEnumerationHandler(hwnd, resultList): + '''Pass to win32gui.EnumWindows() to generate list of window handle, window text tuples.''' + + resultList.append((hwnd, win32gui.GetWindowText(hwnd))) + +def _findTopWindows(wantedTitle=None): + ''' Return list of window handle and window title tuples + + @param wantedTitle: + ''' + topWindows = [] + win32gui.EnumWindows(_windowEnumerationHandler, topWindows) + if wantedTitle == None: + return topWindows + else: + return [(hwnd,windowTxt) for hwnd, windowTxt in topWindows if windowTxt.startswith(wantedTitle)] + +def findallfigs(): + ''' + Return list of all figure numbers + ''' + figs = [] + global _FIG_FORMATS + for wantedTitle in _FIG_FORMATS: + windowList = _findTopWindows(wantedTitle) + for unused_hwnd, wndTitle in windowList: + try: + fig = int(wndTitle.split()[-1]) + figs.append(fig) + except: + pass + figs.sort() + return figs + +def _figparse(*args): + figs = [] + for arg in args: + if isinstance(arg,(list,tuple,set)): + for val in arg: + figs.append(int(val)) + elif isinstance(arg,int): + figs.append(arg) + elif arg == 'all': + figs = 'all' + break + else: + raise TypeError('Only integers arguments accepted!') + #raise TypeError('Unrecognized argument type (%s)!'%type(arg)) + + if len(figs)==0 or figs =='all': + figs = findallfigs() + return figs + +def _fig2wnd(figs): + ''' Find figure handle from figure number + ''' + FindWindow = win32gui.FindWindow + wnd_handles = [] + global _FIG_FORMATS + for fig in figs: + for format_ in _FIG_FORMATS: + winTitle = format_ + ' %d' % fig + hwnd = FindWindow(None,winTitle) + if not hwnd == 0: + wnd_handles.append(hwnd) + return wnd_handles + +def _show_figure(figs,cmdshow): + ''' sets the specified figure's show state. + + @param figs: vector for figure numbers + @param cmdshow: one of following commands: + SW_FORCEMINIMIZE: Minimizes a window, even if the thread that owns the window is not + responding. This flag should only be used when minimizing windows + from a different thread. + SW_HIDE: Hides the window and activates another window. + SW_MAXIMIZE: Maximizes the specified window. + SW_MINIMIZE: Minimizes the specified window and activates the next top-level window in the Z order. + SW_RESTORE: Activates and displays the window. If the window is minimized or maximized, + the system restores it to its original size and position. An application should + specify this flag when restoring a minimized window. + SW_SHOW: Activates the window and displays it in its current size and position. + SW_SHOWDEFAULT: Sets the show state based on the SW_ value specified in the STARTUPINFO + structure passed to the CreateProcess function by the program that started the application. + SW_SHOWMAXIMIZED: Activates the window and displays it as a maximized window. + SW_SHOWMINIMIZED: Activates the window and displays it as a minimized window. + SW_SHOWMINNOACTIVE: Displays the window as a minimized window. This value is similar to + SW_SHOWMINIMIZED, except the window is not activated. + SW_SHOWNA: Displays the window in its current size and position. This value is similar to + SW_SHOW, except the window is not activated. + SW_SHOWNOACTIVATE: Displays a window in its most recent size and position. + This value is similar to SW_SHOWNORMAL, except the window is not actived. + SW_SHOWNORMAL: Activates and displays a window. If the window is minimized or maximized, + the system restores it to its original size and position. An application should + specify this flag when displaying the window for the first time. + ''' + BringWindowToTop = win32gui.BringWindowToTop + FindWindow = win32gui.FindWindow + ShowWindow = win32gui.ShowWindow + global _FIG_FORMATS + for fig in figs: + for format_ in _FIG_FORMATS: + winTitle = format_ + ' %d' % fig + hwnd = FindWindow(None,winTitle) + if not hwnd == 0: + #ShowWindow(hwnd,cmdshow) + BringWindowToTop(hwnd) + ShowWindow(hwnd,cmdshow) + +def _show_windows(wnds,cmdshow): + ''' sets the specified window's show state. + + @param wnds: list of window handles numbers + @param cmdshow: one of following commands: + SW_FORCEMINIMIZE: Minimizes a window, even if the thread that owns the window is not + responding. This flag should only be used when minimizing windows + from a different thread. + SW_HIDE: Hides the window and activates another window. + SW_MAXIMIZE: Maximizes the specified window. + SW_MINIMIZE: Minimizes the specified window and activates the next top-level window in the Z order. + SW_RESTORE: Activates and displays the window. If the window is minimized or maximized, + the system restores it to its original size and position. An application should + specify this flag when restoring a minimized window. + SW_SHOW: Activates the window and displays it in its current size and position. + SW_SHOWDEFAULT: Sets the show state based on the SW_ value specified in the STARTUPINFO + structure passed to the CreateProcess function by the program that started the application. + SW_SHOWMAXIMIZED: Activates the window and displays it as a maximized window. + SW_SHOWMINIMIZED: Activates the window and displays it as a minimized window. + SW_SHOWMINNOACTIVE: Displays the window as a minimized window. This value is similar to + SW_SHOWMINIMIZED, except the window is not activated. + SW_SHOWNA: Displays the window in its current size and position. This value is similar to + SW_SHOW, except the window is not activated. + SW_SHOWNOACTIVATE: Displays a window in its most recent size and position. + This value is similar to SW_SHOWNORMAL, except the window is not actived. + SW_SHOWNORMAL: Activates and displays a window. If the window is minimized or maximized, + the system restores it to its original size and position. An application should + specify this flag when displaying the window for the first time. + ''' + + ShowWindow = win32gui.ShowWindow + BringWindowToTop = win32gui.BringWindowToTop + for hwnd in wnds: + if not hwnd == 0: + #ShowWindow(hwnd,cmdshow) + BringWindowToTop(hwnd) + ShowWindow(hwnd,cmdshow) + +def keep(*figs): + ''' Keeps figure windows of your choice and closes the rest. + + Parameters + ---------- + figs : list of integers specifying which figures to keep. + + Example: + -------- + # keep only figures 1,2,3,5 and 7 + >>> import pylab as p + >>> import pyfig as fig + >>> for ix in range(10): f = p.figure(ix) + >>> fig.keep( range(1,4), 5, 7) + + or + fig.keep([range(1,4), 5, 7]) + >>> fig.close() + + See also + -------- + pyfig.close + ''' + figs2keep = [] + for fig in figs: + if isinstance(fig,(list,tuple,set)): + for val in fig: + figs2keep.append(int(val)) + elif isinstance(fig,int): + figs2keep.append(fig) + else: + raise TypeError('Only integers arguments accepted!') + + if len(figs2keep)>0: + allfigs = set(findallfigs()) + +# Remove figure handles in the "keep" list + figs2delete = allfigs.difference(figs2keep) + close(figs2delete) + #for fig in figs2delete: + # close(fig) + +def close(*figs): + """ Close figure window(s) + + Parameters + ---------- + figs : list of integers or string + specifying which figures to close (default 'all'). + + Examples + -------- + >>> import pylab as p + >>> import pyfig as fig + >>> for ix in range(5): f = p.figure(ix) + >>> fig.close(3,4) # close figure 3 and 4 + >>> fig.close('all') # close all remaining figures + + or even simpler + fig.close() # close all remaining figures + + See also + -------- + pyfig.keep + """ + figlist = _figparse(*figs) + wnds = _fig2wnd(figlist) + for wnd in wnds: + win32gui.SendMessage(wnd, win32con.WM_CLOSE, 0, 0) + + +def restore(*figs): + '''Restore figures window size and position to its default value. + + Parameters + ---------- + figs : list of integers or string + specifying which figures to restor (default 'all'). + + Description + ----------- + RESTORE Activates and displays the window. If the window is minimized + or maximized, the system restores it to its original size and position. + + Examples + --------- + >>> import pylab as p + >>> import pyfig as fig + >>> for ix in range(5): f = p.figure(ix) + >>> fig.restore('all') #Restores all figures + >>> fig.restore() #same as restore('all') + >>> fig.restore(p.gcf().number) #Restores the current figure + >>> fig.restore(3) #Restores figure 3 + >>> fig.restore([2, 4]) #Restores figures 2 and 4 + + or alternatively + fig.restore(2, 4) + >>> fig.close() + + See also + -------- + fig.close, + fig.keep + ''' + + figlist = _figparse(*figs) + + SW_RESTORE = win32con.SW_SHOWNORMAL #SW_RESTORE + _show_figure(figlist, SW_RESTORE) + +def hide(*figs): + '''hide figure(s) window size + + Parameters + ---------- + figs : list of integers or string + specifying which figures to hide (default 'all'). + + Examples: + -------- + >>> import pyfig as fig + >>> import pylab as p + >>> for ix in range(5): f = p.figure(ix) + >>> fig.hide('all') #hides all unhidden figures + >>> fig.hide() #same as hide('all') + >>> fig.hide(p.gcf().number) #hides the current figure + >>> fig.hide(3) #hides figure 3 + >>> fig.hide([2, 4]) #hides figures 2 and 4 + + or alternatively + fig.hide(2, 4) + >>> fig.close() + + See also + -------- + pyfig.cycle, + pyfig.keep, + pyfig.restore + ''' + + figlist = _figparse(*figs) + SW_HIDE = win32con.SW_HIDE + #SW_hide = win32con.SW_hide + _show_figure(figlist, SW_HIDE) + +def minimize(*figs): + '''Minimize figure(s) window size + + Parameters + ---------- + figs : list of integers or string + specifying which figures to minimize (default 'all'). + + Examples: + --------- + >>> import pyfig as fig + >>> import pylab as p + >>> for ix in range(5): f = p.figure(ix) + >>> fig.minimize('all') #Minimizes all unhidden figures + >>> fig.minimize() #same as minimize('all') + >>> fig.minimize(p.gcf().number) #Minimizes the current figure + >>> fig.minimize(3) #Minimizes figure 3 + >>> fig.minimize([2, 4]) #Minimizes figures 2 and 4 + + or alternatively + fig.minimize(2, 4) + >>> fig.close() + + See also + -------- + pyfig.cycle, + pyfig.keep, + pyfig.restore + ''' + + figlist = _figparse(*figs) + SW_MINIMIZE = win32con.SW_SHOWMINIMIZED + _show_figure(figlist, SW_MINIMIZE) + +def maximize(*figs): + '''Maximize figure(s) window size + + Parameters + ---------- + figs : list of integers or string + specifying which figures to maximize (default 'all'). + + Examples: + --------- + >>> import pylab as p + >>> import pyfig as fig + >>> for ix in range(5): f = p.figure(ix) + >>> fig.maximize('all') #Maximizes all unhidden figures + >>> fig.maximize() #same as maximize('all') + >>> fig.maximize(p.gcf().number) #Maximizes the current figure + >>> fig.maximize(3) #Maximizes figure 3 + >>> fig.maximize([2, 4]) #Maximizes figures 2 and 4 + + or alternatively + fig.maximize(2, 4) + >>> fig.close() + + See also + -------- + pyfig.cycle, + pyfig.keep, + pyfig.restore + ''' + + figlist = _figparse(*figs) + SW_MAXIMIZE = win32con.SW_SHOWMAXIMIZED + #SW_MAXIMIZE = win32con.SW_MAXIMIZE + _show_figure(figlist, SW_MAXIMIZE) + +def pile(*figs): + ''' Pile figure windows + + Parameters + ---------- + figs : list of integers or string + specifying which figures to pile (default 'all'). + + Description + ------------- + PILE piles all open figure windows on top of eachother + with complete overlap. PILE(FIGS) can be used to specify which + figures that should be piled. Figures are not sorted when specified. + + Example: + -------- + >>> import pylab as p + >>> import pyfig as fig + >>> for ix in range(5): f = p.figure(ix) + >>> fig.pile() # pile all open figures + >>> fig.pile(range(1,4), 5, 7) # pile figure 1,2,3,5 and 7 + >>> fig.close() + + See also + -------- + pyfig.cycle, pyfig.keep, pyfig.maximize, pyfig.restore, + pyfig.stack, pyfig.tile + ''' + + figlist = _figparse(*figs) + wnds = _fig2wnd(figlist) + numfigs = len(wnds) + if numfigs>0: + pos = _getScreenSize(wnds) + pos[3] = int(pos[3]/2) + pos[2] = int(pos[2]/2.5) + pos[1] = int(pos[3]/2) + pos[0] = int(pos[2]/2) + BringWindowToTop = win32gui.BringWindowToTop + MoveWindow = win32gui.MoveWindow + for wnd in wnds: + MoveWindow(wnd, pos[0], pos[1], pos[2], pos[3],1) + BringWindowToTop(wnd) + + +def stack(*figs): + ''' Stack figure windows + + Parameters + ---------- + figs : list of integers or string + specifying which figures to stack (default 'all'). + + Description + ----------- + STACK stacks all open figure windows on top of eachother + with maximum overlap. STACK(FIGS) can be used to specify which + figures that should be stacked. Figures are not sorted when specified. + + Example: + -------- + >>> import pylab as p + >>> import pyfig as fig + >>> for ix in range(5): f = p.figure(ix) + >>> fig.stack() # stack all open figures + >>> fig.stack(range(1,4), 5, 7) # stack figure 1,2,3,5 and 7 + >>> fig.close() + + See also + -------- + pyfig.cycle, pyfig.keep, pyfig.maximize, pyfig.restore, + pyfig.pile, pyfig.tile + ''' + + figlist = _figparse(*figs) + wnds = _fig2wnd(figlist) + numfigs = len(wnds) + if numfigs>0: + screenpos = _getScreenSize(wnds) + + maxfigs = numpy.fix(screenpos[3]/20) + + if (numfigs>maxfigs): # figure limit check + print(' More than %d requested '% maxfigs) + return + BringWindowToTop = win32gui.BringWindowToTop + MoveWindow = win32gui.MoveWindow + GetWindowRect = win32gui.GetWindowRect +# +# +#% tile figures by postiion +#% Location (1,1) is at bottom left corner +# + #print('Screensz = ',screenpos) + for iy in xrange(numfigs): + pos = list(GetWindowRect(wnds[iy])) + pos[3] -= pos[1] + pos[2] -= pos[0] + #print('[x, y, w, h] = ', pos) + ypos = screenpos[1]+iy*20 #int(screenpos[3] - iy*20 -pos[3] -70) # figure location (row) + xpos = int(iy*5 + 15+screenpos[0]) # figure location (column) + MoveWindow(wnds[iy],xpos,ypos, pos[2],pos[3],1) + BringWindowToTop(wnds[iy]) + + +def tile(*figs,**kwds): + ''' Tile figure windows. + + Parameters + ---------- + figs : list of integers or string + specifying which figures to tile (default 'all'). + kwds : dict with key pairs + specifying how many pairs of figures that are tiled at a time + + Description + ----------- + TILE places all open figure windows around on the screen with no + overlap. TILE(FIGS) can be used to specify which figures that + should be tiled. Figures are not sorted when specified. + + Example: + -------- + >>> import pylab as p + >>> import pyfig as fig + >>> for ix in range(5): f = p.figure(ix) + >>> fig.tile() # tile all open figures + >>> fig.tile( range(1,4), 5, 7) # tile figure 1,2,3,5 and 7 + >>> fig.tile(range(1,11), pairs=2) # tile figure 1 to 10 two at a time + >>> fig.tile(range(1,11), pairs=3) # tile figure 1 to 10 three at a time + >>> fig.close() + + See also + -------- + pyfig.cycle, pyfig.keep, pyfig.maximize, pyfig.minimize + pyfig.restore, pyfig.pile, pyfig.stack + + ''' + figlist = _figparse(*figs) + wnds = _fig2wnd(figlist) + + nfigs = len(wnds); # Number of windows. + + if nfigs>0: + nfigspertile = kwds.get('pairs',nfigs) + + ceil = numpy.ceil + sqrt = numpy.sqrt + maximum = numpy.maximum + + nlayers = int(ceil(nfigs/nfigspertile)) + + nh = int(ceil(sqrt(nfigspertile))) # Number of figures horisontally. + nv = int(ceil(nfigspertile/nh)); # Number of figures vertically. + + + nh = maximum(nh, 2); + nv = maximum(nv, 2); + +## Get the screen size. +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + BringWindowToTop = win32gui.BringWindowToTop + MoveWindow = win32gui.MoveWindow + screenpos = _getScreenSize(wnds) + #scrdim = win32gui.GetWindowPlacement(h)[4] + + scrwid = screenpos[2] # Screen width. + scrhgt = screenpos[3] # Screen height. + +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +#% The elements in the vector specifying the position. +#% 1 - Window left position +#% 2 - Window top position +#% 3 - Window horizontal size +#% 4 - Window vertical size +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + hspc = 10 # Horisontal space. + topspc = 20; # Space above top figure. + medspc = 10; # Space between figures. + botspc = 20; # Space below bottom figure. + + #print('scrwid = %d' % scrwid) + figwid = ( scrwid - (nh+1)*hspc )/nh + #print('figwid = %d' % figwid) + fighgt = ( scrhgt - (topspc+botspc) - (nv-1)*medspc )/nv; + + figwid = int(numpy.round(figwid)) + fighgt = int(numpy.round(fighgt)) + +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +#% Put the figures where they belong. +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + idx = 0 + for unused_ix in xrange(nlayers): + for row in xrange(nv): + for col in xrange(nh): + if (row)*nh + col < nfigspertile : + if idx>> import pylab as p + >>> import pyfig as fig + >>> for ix in range(4): f = p.figure(ix) + + fig.cycle(range(3)) #Cycle trough figure 0 to 2 + fig.cycle(range(3) maximize=True) #Cycle trough figure 1 to 3 with figs maximized + fig.cycle() #Cycle through all figures one at a time + fig.tile(pairs=2) + fig.cycle(pairs=2) #Cycle through all figures two at a time + >>> fig.close() + + See also + -------- + pyfig.keep, pyfig.maximize, pyfig.restore, pyfig.pile, + pyfig.stack, pyfig.tile + ''' + # TODO : display is not updated for each cycle => function is useless + figlist = _figparse(*figs) + wnds = _fig2wnd(figlist) + + numfigs = len(wnds) + if numfigs>0: + maximize = kwds.get('maximize',False) + pairs = kwds.get('pairs',1) + + if maximize or pairs == None: + nfigspercycle = 1 + else: + nfigspercycle = pairs + + #n = length(figs); + #nlayers = ceil(n/nfigspercycle); + + # Bring one figure up at a time. + i = 0; + escape_key = chr(27) + backspace_key = chr(8) + while 0<=i and i>> import wafo.graphutil as wg - >>> from wafo.demos import peaks + >>> import wafo.demos as wd >>> import pylab as plt - >>> x,y,z = peaks(); + >>> x,y,z = wd.peaks(); >>> h = plt.contour(x,y,z) >>> h = wg.cltext(h.levels) >>> plt.show() - - data = rndray(1,2000,2); f = kdebin(data,{'kernel','epan','L2',.5,'inc',128}); - contour(f.x{:},f.f,f.cl),cltext(f.pl,1) - - See also - pdfplot ''' # TODO : Make it work like legend does (but without the box): include position options etc... clevels = np.atleast_1d(levels) - _CLTEXT_TAG = 'CLTEXT' + cax = plotbackend.gca() axpos = cax.get_position() xint = axpos.intervalx @@ -68,25 +99,8 @@ def cltext(levels, percent=False, n=4, xs=0.036, ys=0.94, zs=0): xss = xint[0] + xs * (xint[1] - xint[0]) yss = yint[0] + ys * (yint[1] - yint[0]) - cf = plotbackend.gcf() # get current figure - #% delete cltext object if it exists - #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - def matchfun(x): - if hasattr(x, 'get_gid'): - return x.get_gid() == _CLTEXT_TAG - return False - h_cltxts = plotbackend.findobj(cf, matchfun); - if len(h_cltxts): - for i in h_cltxts: - try: - cax.texts.remove(i) - except: - warnings.warn('Tried to delete a non-existing CL-text') - - try: - cf.texts.remove(i) - except: - warnings.warn('Tried to delete a non-existing CL-text') + # delete cltext object if it exists + delete_object(_CLTEXT_GID, ca=cax) charHeight = 1.0 / 33.0 delta_y = charHeight @@ -100,12 +114,12 @@ def cltext(levels, percent=False, n=4, xs=0.036, ys=0.94, zs=0): cltxt = ''.join([format_ % level for level in clevels.tolist()]) - titleProp = dict(gid=_CLTEXT_TAG, horizontalalignment='left', + titleProp = dict(gid=_CLTEXT_GID, horizontalalignment='left', verticalalignment='center', fontweight='bold', axes=cax) # ha1 = plotbackend.figtext(xss, yss, titletxt, **titleProp) yss -= delta_y; - txtProp = dict(gid=_CLTEXT_TAG, horizontalalignment='left', + txtProp = dict(gid=_CLTEXT_GID, horizontalalignment='left', verticalalignment='top', axes=cax) ha2 = plotbackend.figtext(xss, yss, cltxt, **txtProp) @@ -125,18 +139,24 @@ def tallibing(x, y, n, **kwds): color = color of text (optional) (default='white') h = column-vector of handles to TEXT objects - TALLIBING writes the numbers in a matrix as text at the positions + TALLIBING writes the numbers in a 2D array as text at the positions given by the x and y coordinate matrices. - When plotting binned results, the number of datapoints in each - bin can be written on the bins in the plot. + When plotting binned results, the number of datapoints in each + bin can be written on the bins in the plot. - EXAMPLE: - [x,y,z]= wafo.demos.peaks(); - epcolor(x,y,z); - tallibing(x,y,z); - % pcolor(x,y,z); shading interp; + Example + ------- + >>> import wafo.graphutil as wg + >>> import wafo.demos as wd + >>> [x,y,z] = wd.peaks(n=20) + >>> wg.epcolor(x,y,z) + >>> wg.tallibing(x,y,z) + + pcolor(x,y,z); shading interp; - See also TEXT TALLIBING3 + See also + -------- + text ''' x, y, n = np.atleast_1d(x, y, n) if mlab.isvector(x) or mlab.isvector(y): @@ -148,35 +168,80 @@ def tallibing(x, y, n, **kwds): y = y.ravel() n = n.ravel() n = np.round(n) - def matchfun(x): - if hasattr(x, 'get_gid'): - return x.get_gid() == _TALLIBING_TAG - return False - cf = plotbackend.gcf() # get current figure - h_cltxts = plotbackend.findobj(cf, matchfun); - if len(h_cltxts): - for i in h_cltxts: - try: - cax.texts.remove(i) - except: - warnings.warn('Tried to delete a non-existing TALLIBING-text') - - try: - cf.texts.remove(i) - except: - warnings.warn('Tried to delete a non-existing TALLIBING-text') - - txtProp = dict(gid=_TALLIBING_TAG, size=8, color='w', horizontalalignment='center', + # delete tallibing object if it exists + delete_object(_TALLIBING_GID, ca=cax) + + txtProp = dict(gid=_TALLIBING_GID, size=8, color='w', horizontalalignment='center', verticalalignment='center', fontweight='demi', axes=cax) txtProp.update(**kwds) h = [] - for xi,yi,ni in zip(x,y,n): + for xi,yi, ni in zip(x,y,n): if ni: h.append(plotbackend.text(xi, yi, str(ni), **txtProp)) return h +def epcolor(*args, **kwds): + ''' + Pseudocolor (checkerboard) plot with mid-bin positioning. + + h = epcolor(x,y,data) + + + [x,y]= the axes corresponding to the data-positions. Vectors or + matrices. If omitted, giving only data-matrix as inargument, the + matrix-indices are used as axes. + data = data-matrix + + EPCOLOR make a checkerboard plot where the data-point-positions are in + the middle of the bins instead of in the corners, and the last column + and row of data are used. + + + Example: + >>> import wafo.demos as wd + >>> import wafo.graphutil as wg + >>> x, y, z = wd.peaks(n=20) + >>> wg.epcolor(x,y,z) + + See also + -------- + pylab.pcolor + ''' + midbin = kwds.pop('midbin', True) + if not midbin: + return plotbackend.pcolor(*args,**kwds) + + nargin = len(args) + data = np.atleast_2d(args[-1]).copy() + M, N = data.shape + if nargin==1: + x = np.arange(N) + y = np.arange(M) + elif nargin==3: + x, y = np.atleast_1d(*args[:-1]) + if min(x.shape)!=1: + x = x[0] + if min(y.shape)!=1: + y = y[:,0] + else: + raise ValueError('pcolor takes 3 or 1 inarguments! (x,y,data) or (data)') + + xx = _findbins(x) + yy = _findbins(y) + return plotbackend.pcolor(xx, yy, data, **kwds) + +def _findbins(x): + ''' Return points half way between all values of X _and_ outside the + endpoints. The outer limits have same distance from X's endpoints as + the limits just inside. + ''' + dx = np.diff(x) * 0.5 + dx = np.hstack((dx, dx[-1])) + return np.hstack((x[0] - dx[0], x + dx)) + + def test_docstrings(): import doctest doctest.testmod() diff --git a/pywafo/src/wafo/stats/core.py b/pywafo/src/wafo/stats/core.py index 933e513..b9a1440 100644 --- a/pywafo/src/wafo/stats/core.py +++ b/pywafo/src/wafo/stats/core.py @@ -1151,7 +1151,7 @@ class RegLogit(object): np1 = pcov.shape[0] [U, S, V]= np.linalg.svd(pcov,0); - R = np.dot(U,np.dot(sqrt(S),V.T)); #%squareroot of pcov + R = np.dot(U,np.dot(np.diag(sqrt(S)),V)) #%squareroot of pcov ib = np.r_[0,nz:np1] #% Var(eta_i) = var(theta_i+Xnew*b) @@ -1159,7 +1159,7 @@ class RegLogit(object): u = np.hstack((one,Xnew)) for i in range(nz): ib[0] = i - vareta[:,i] = np.maximum(sum((np.dot(u,R[ib,ib]))**2,axis=1),eps) + vareta[:,i] = np.maximum(((np.dot(u,R[ib][:,ib]))**2).sum(axis=1),eps) #end else: vareta = np.diag(pcov) @@ -1170,14 +1170,14 @@ class RegLogit(object): ecrit = crit * sqrt(vareta); mulo = _logitinv(eta-ecrit); muup = _logitinv(eta+ecrit); - ylo1 = np.diff(np.hstack((zeros((n,1)), mulo , one)),1,2); - yup1 = np.diff(np.hstack((zeros((n,1)), muup , one)),1,2); + ylo1 = np.diff(np.hstack((zeros((n,1)), mulo , one)),n=1,axis=1) + yup1 = np.diff(np.hstack((zeros((n,1)), muup , one)),n=1,axis=1) - ylo = np.minimum(ylo1,yup1); - yup = np.maximum(ylo1,yup1); + ylo = np.minimum(ylo1,yup1) + yup = np.maximum(ylo1,yup1) for i in range(1, nz): #= 2:self.numk-1 - yup[:,i] = np.hstack((yup[:,i],muup[:,i]-mulo[:,i-1])).max(axis=1) + yup[:,i] = np.vstack((yup[:,i],muup[:,i]-mulo[:,i-1])).max(axis=0) #end return y,ylo,yup return y @@ -1225,26 +1225,6 @@ class RegLogit(object): #end %function -def dmult(A,B): - ''' Return the product of diag(A) and B - - USAGE: m = dmult(a,b) - where: a = a matrix - b = a matrix - ----------------------------------------------------- - RETURNS: m = diag(A) times B - ----------------------------------------------------- - NOTE: a Gauss compatability function - ----------------------------------------------------- - ''' - #% written by: - #% Gordon K Smyth, U of Queensland, Australia, gks@maths.uq.oz.au - #% Nov 19, 1990. Last revision Aug 29, 1995. - - return A[:,None]*B; - - - def _test_dispersion_idx(): import wafo.data xn = wafo.data.sea() @@ -1288,6 +1268,7 @@ def test_reglogit(): b.summary() [mu,plo,pup] = b.predict(fulloutput=True); + pass #plot(x,mu,'g',x,plo,'r:',x,pup,'r:') def test_doctstrings():