From: John H. <jdh...@ac...> - 2003-12-29 16:24:08
|
>>>>> "Jeremy" == Jeremy O'Donoghue <je...@o-...> writes: Jeremy> I can say that in the original design, the intention was Jeremy> that show() would be the last line of any script. I know Jeremy> that John (author of virtually everything in Matplotlib Jeremy> except backend_wx) has recently made some changes to allow Jeremy> show() to be called more than once. Jeremy> Unfortunately, the code needed to do thisis quite specific Jeremy> to each GUI library, and I cannot simply port what has Jeremy> been done for GTK (I've just tried something very close to Jeremy> the GTK implementation, and it doesn't work). Jeremy> A few questions for John Hunter: I think I need to do Jeremy> something like the following: - show() must now Jeremy> instantiate any figures already defined and enter the the Jeremy> main event loop. ShowOn needs to keep track of this. - I Jeremy> need to keep track of the number of figures Jeremy> instantiated. I assume that Gcf.destroy() does this. - I Jeremy> need to ensure that I do not exit when the last figure is Jeremy> destroyed, and therefore need to manage that I may need to Jeremy> create a new figure manager if there is none. Hi Jeremy, I think we should consider redoing this whole segment of the code from the ground up to make for a cleaner / cross GUI implementation. It may be that Pearu Peteson <pe...@ce...> gui_thread code is the way to go for this since that is what is was designed to do (enable interactive control of WX plots (chaco) from the shell). He has indicated a willingness to port it to pygtk provided we're willing to help test his code with the WX and GTK matplotlib backends. However, this would create a scipy dependency... But let me give a little overview of why the code is currently the way it is. When the use calls 'show' it realizes all the figures and sets self.show = on This is a critical part, because the function draw_if_interactive (which is called by every matplotlib.matlab command) only draws if this variable is set def draw_if_interactive(): if ShowOn().get(): figManager = Gcf.get_active() if figManager is not None: fig = figManager.figure fig.draw() fig.queue_draw() The point I want to emphasize is that the primary reason there is a difference between interactive and batch mode is for efficiency. In interactive mode, the entire figure must be redrawn with each command (eg, setting an xlabel, changing a ticklabel property, etc...). This can get quite expensive when you want to set a lot of properties. Thus the default 'batch mode' defers all the drawing operations until the end for efficiency, and just makes one draw. I don't think the current architecture for show, draw_if_interactive, ShowOn etc, is very elegant or easily understandable, and would be happy to refactor it for the next release. Ideally we could handle these two cases across backends 1) defer all drawing until a call to show, which draws and realizes all pending figures. This should not hang the script, ie, further drawing commands should be possible. 2) do drawing with each matplotlib.matlab command for interactive mode (current implementation in backend_gtk with ShowOn.set(1) at start of script) For the most part, I think we have this with the GTK backend, but it may be necessary to refactor in order to get something that works with both. I'll think it over and take a look at the WX code to see if I get any ideas how to proceed. In the meantime, we should also see if we can get matplotlib to work with gui_thread -- I'll take a look at this too. JDH |
From: Jeremy O'D. <je...@o-...> - 2003-12-30 17:16:27
|
Hi John, Thanks for your reply - and the patch! I am about to check in an updated backend_wx which basically contains your changes, with one difference: I have refactored the code: drawDC=wxClientDC(self) drawDC.BeginDrawing() drawDC.Clear() drawDC.DrawBitmap(self.bitmap, 0, 0) drawDC.EndDrawing() into a new function in FigureWX: gui_repaint(). I have done this for several reasons, but mainly because the same code appears in several places (FigureWX._onSize() as well as draw_if_interactive(), and it turns out to be needed for the Wx variant of interactive_demo (which I am also about to check in)). I have also made a change so that if _DEBUG is set to a value less than 5 (i.e. you are interested in debugging), the system exception hook is replaced with one which performs a traceback and enters pdb. This seems to work correctly for me on Linux, and I'll verify Win32 when I get back to work next week. It's a bit of an ugly hack, and I may rethink it later, but it's very handy for now, and doesn't intrude when not needed. On Monday 29 December 2003 4:15 pm, John Hunter wrote: > >>>>> "Jeremy" == Jeremy O'Donoghue <je...@o-...> writes: > > Jeremy> A few questions for John Hunter: I think I need to do > Jeremy> something like the following: - show() must now > Jeremy> instantiate any figures already defined and enter the the > Jeremy> main event loop. ShowOn needs to keep track of this. - I > Jeremy> need to keep track of the number of figures > Jeremy> instantiated. I assume that Gcf.destroy() does this. - I > Jeremy> need to ensure that I do not exit when the last figure is > Jeremy> destroyed, and therefore need to manage that I may need to > Jeremy> create a new figure manager if there is none. > > Hi Jeremy, > > I think we should consider redoing this whole segment of the code from > the ground up to make for a cleaner / cross GUI implementation. It > may be that Pearu Peteson <pe...@ce...> gui_thread code is the > way to go for this since that is what is was designed to do (enable > interactive control of WX plots (chaco) from the shell). He has > indicated a willingness to port it to pygtk provided we're willing to > help test his code with the WX and GTK matplotlib backends. However, > this would create a scipy dependency... I followed the thread form Pearu a few weeks back, although have not gotten around to trying the gui_thread code. I think there is something similar in the Python Cookbook for PyGtk (chapter 9.12), which might assist in doing a Gtk port. I'm not keen on adding a dependency on scipy, but perhaps Pearu would consider making gui_thread its own module (or allowing us to do so). Checking back over the mail you sent to me, there are only three files involved. One of the things which drew me to working on Matplotlib was the small set of dependencies, and it would be a shame to loose this. I'm very willing to play with the code he has with the Wx backend - I doubt that it will require much work on my behalf. [snip] > I don't think the current architecture for show, draw_if_interactive, > ShowOn etc, is very elegant or easily understandable, and would be > happy to refactor it for the next release. Ideally we could handle > these two cases across backends > > 1) defer all drawing until a call to show, which draws and realizes > all pending figures. This should not hang the script, ie, > further drawing commands should be possible. You'll notice in the code that I checked in that I have been playing with this in the backend_wx code. While it is possible to exit the mainloop, it seems that it is not possible to re-enter. It may be that I can do something akin to the Python Cookbook recepie I mentioned above for wx - it doesn't look too hard. I have attached the code from the book example below, if you're interested. > 2) do drawing with each matplotlib.matlab command for interactive > mode (current implementation in backend_gtk with ShowOn.set(1) at > start of script) As you noteed, this now works with your fix. > For the most part, I think we have this with the GTK backend, but it > may be necessary to refactor in order to get something that works with > both. I'll think it over and take a look at the WX code to see if I > get any ideas how to proceed. > > In the meantime, we should also see if we can get matplotlib to work > with gui_thread -- I'll take a look at this too. > > JDH Thanks again for the fix Jeremy ====== interactive_gtk.py ===== import __builtin__, __main__ import codeop, keyword, gtk, os, re, readline, threading, traceback, signal, sys def walk_class(klass): list = [] for item in dir(klass): if item[0] != "_": list.append(item) for base in klass.__bases__: for item in walk_class(base): if item not in list: list.append(item) return list class Completer: def __init__(self, lokals): self.locals = lokals self.completions = keyword.kwlist + \ __builtins__.__dict__.keys() + \ __main__.__dict__.keys() def complete(self, text, state): if state == 0: if "." in text: self.matches = self.attr_matches(text) else: self.matches = self.global_matches(text) try: return self.matches[state] except IndexError: return None def update(self, locs): self.locals = locs for key in self.locals.keys(): if not key in self.completions: self.completions.append(key) def global_matches(self, text): matches = [] n = len(text) for word in self.completions: if word[:n] == text: matches.append(word) return matches def attr_matches(self, text): m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) if not m: return expr, attr = m.group(1, 3) obj = eval(expr, self.locals) if str(obj)[1:4] == "gtk": words = walk_class(obj.__class__) else: words = dir(eval(expr, self.locals)) matches = [] n = len(attr) for word in words: if word[:n] == attr: matches.append("%s.%s" % (expr, word)) return matches class GtkInterpreter(threading.Thread): """ Run a GTK mainloop() in a separate thread. Python commands can be passed to the thread, where they will be executed. This is implemented by periodically checking for passed code using a GTK timeout callback. """ TIMEOUT = 100 # interval in milliseconds between timeouts def __init__(self): threading.Thread.__init__ (self) self.ready = threading.Condition () self.globs = globals () self.locs = locals () self._kill = 0 self.cmd = '' # current code block self.new_cmd = None # waiting line of code, or None if none waiting self.completer = Completer(self.locs) readline.set_completer(self.completer.complete) readline.parse_and_bind('tab: complete') def run(self): gtk.timeout_add(self.TIMEOUT, self.code_exec) gtk.mainloop() def code_exec(self): """ Execute waiting code. Called every timeout period. """ self.ready.acquire() if self._kill: gtk.mainquit() if self.new_cmd != None: self.ready.notify() self.cmd = self.cmd + self.new_cmd self.new_cmd = None try: code = codeop.compile_command(self.cmd[:-1]) if code: self.cmd = '' exec code, self.globs, self.locs self.completer.update(self.locs) except: traceback.print_exc() self.cmd = '' self.ready.release() return 1 def feed(self, code): """ Feed a line of code to the thread. This function will block until the code is checked by the GTK thread. Returns true if the thread has executed the code. Returns false if deferring execution until complete block is available. """ if code[-1:]!='\n': code = code +'\n' # raw_input strips newline self.completer.update(self.locs) self.ready.acquire() self.new_cmd = code self.ready.wait() # Wait until processed in timeout interval self.ready.release() return not self.cmd def kill(self): """ Kill the thread, returning when it has been shut down. """ self.ready.acquire() self._kill=1 self.ready.release() self.join() # Read user input in a loop and send each line to the interpreter thread def signal_handler(*args): print "SIGNAL:", args sys.exit() if __name__=="__main__": signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGSEGV, signal_handler) prompt = '>>> ' interpreter = GtkInterpreter() interpreter.start() interpreter.feed("from gtk import *") interpreter.feed("sys.path.append('.')") if len (sys.argv) > 1: for file in open(sys.argv[1]).readlines(): interpreter.feed(file) print 'Interactive GTK Shell' try: while 1: command = raw_input(prompt) + '\n' # raw_input strips newlines prompt = interpreter.feed(command) and '>>> ' or '... ' except (EOFError, KeyboardInterrupt): pass interpreter.kill() print |
From: John H. <jdh...@ac...> - 2003-12-30 17:55:24
|
>>>>> "Jeremy" == Jeremy O'Donoghue <je...@o-...> writes: Jeremy> Hi John, Thanks for your reply - and the patch! I am about Jeremy> to check in an updated backend_wx which basically contains Jeremy> your changes, with one difference: I have refactored the Jeremy> code: Jeremy> drawDC=wxClientDC(self) drawDC.BeginDrawing() Jeremy> drawDC.Clear() drawDC.DrawBitmap(self.bitmap, 0, 0) Jeremy> drawDC.EndDrawing() Excellent, I was going to suggest the same. I have rewritten the interactive interface so that you can call import matplotlib matplotlib.use('WX') matplotlib.interactive(True) (and I've removed the ShowOn abomination) Perhaps you should just send me the latest version of you code in case the mirror lags behind as usual and I'll then apply my patch to the wx_backend for the new interface, and commit. JDH |