Optimizing drawing script
edited July 2016 in OpenSesame
I'm working on a script within opensesame where the user will draw something on the screen. Here is what my code looks like :
from openexp.canvas import canvas from openexp.mouse import mouse my_mouse = mouse(exp, buttonlist = (), visible = True) my_canvas = canvas(exp) my_canvas.show() start = clock.time() last_show = start my_mouse.set_pos(pos=(0, 0)) # Draw for 10s while clock.time()- start <= 10000: button, position, timestamp = my_mouse.get_click(timeout=100) x,y = position if button is not None else (None,None) if button is not None: (x0, y0) = (None,None) while my_mouse.get_pressed()==(1,0,0) and clock.time()- start <= 10000: (x, y), timestamp = my_mouse.get_pos() if (x0,y0) ==(None,None): # Draw a simple dot my_canvas.circle(x, y, 10, fill=True, color='black') elif (x0,y0) != (None, None) and (x0,y0)!=(x,y): # Draw a line between (x0,y0) and (x,y) my_canvas.circle(x0, y0, 10, fill=True, color='black') my_canvas.line(x0,y0,x,y, penwidth=23) my_canvas.circle(x, y, 10, fill=True, color='black') (x0,y0) = (x,y) #my_canvas.show() # optimization : show canvas every 20ms if(clock.time() - last_show>=20): my_canvas.show() last_show = clock.time() my_canvas.show()
Works well, but not fluid enough :
I did some testing, this script can capture a mouse position every 25ms in average. I managed to lower this rate to 17ms using the #optimizization trick (show canvas only every 20ms and not after each line). Do you have any idea to make this code even better ?
If we can't have a better rate than 1 position every 17ms, maybe i should start to think about "curve fitting" : instead of drawing lines between 3 points i could draw a curve.
Any ideas ?
I think this largely depend on which backend you are using. Some are more good at this than others. I think the legacy backend will be the fastest for what you are trying to do (and you are currently probably using the default setting: expyriment). Have you tried selecting this?
Either way, you will only be able to achieve a sample rate as short as your screen refresh rate. If you for instance have a screen at 60 Hz, your screen will be refreshed each 16 ms. The mouse will not be sampled within this period (because my_canvas.show() will wait for a screen refresh to finish and thereby 'block' the execution of the script). So I don't think you will have a higher precision than 17 ms indeed. You could indeed check out some curve fitting solutions if you need smoother lines.
I don't really have a choice, the script will run on a tablet, i have to use the android backend.
I guessed that this 17ms was linked to the refresh rate but i wasn't sure. I'm surprised that we can't have a better way to get the mouse position with a higher precision : 17ms is pretty high !
I'm currently working on curve fitting solutions, hope it will give me nice results.
Nope, you can only use that backend then. You may want to look into multi-threading:
A solution would be create a separate thread in which the position is continuously polled. This way, if the drawing operation blocks the main thread to wait for the screen refresh, it doesn't block the polling of input.
Programming threaded applications can be quite daunting, so beware. I also cannot really guarantee it will work, because under the hood pygame is used to draw both the frames to the screen and collect the responses. It may be that pygame pauses completely while waiting for the screen refresh, and thus also stops collecting input data for that time. In this case, the multithreaded solution won't work and will still show a delay of around 17ms.
I see you've moved on from this, but I'll answer for anyone else looking in to this.
The problem here is your hardware. 17 msec the standard refresh time for usb mice, and I'm surprised that android touchscreens even sample that quickly. I've learned this the hard way while designing mouse tracking experiments: if you sample the mouse position more often that this, it will apear that it hasn't moved in between samples.