[open] Timeout for a Python inline code -Screen display
Hi, its me again! I think there's a fairly simple solution to this problem, but I can't seem to find it.
I have a paradigm where I'm presenting two screens where people have to rate on a scale of 1 - 5. Now, when they click on the enter key, it accepts the data from the first screen, and moves to the second.
However, I'm running an fMRI on this paradigm and I need the timing to be exact. As such, i wanted to initiate a 2.5-3 second timeout for the screen, whether the participant has made his entry or not. I tried to use self.time() and condition it to close the canvas if it crosses 3000ms, but it closed the canvas 3000ms AFTER the enter key was finally pressed.
Is it because of the getkey function and its position in the loop? Here is the code.
cnv = canvas()
kb = keyboard(keylist=['7', '8', '9'])
min_score = 1
max_score = 5
var.scoreP1_1 = 3
question = 'Plaintive Rating'
while True:
score_str = var.scoreP1_1*'*' + (max_score-var.scoreP1_1) * '_'
cnv.clear()
cnv.text('%s<br /><br />%s' % (question, score_str))
cnv.show()
response, timestamp = kb.get_key()
if response == '8':
break
if response == '7':
var.scoreP1_1 = max(min_score, var.scoreP1_1-1)
elif response == '9':
var.scoreP1_1 = min(max_score, var.scoreP1_1+1)
I thought of possibly using a null value in the keylist, such that it accepts 'no response' as a key and thus begins counting even when no key has been pressed. But None, null and 0 don't seem to work for that option either.
Comments
Hi Jacob,
Normally,
keyboard.get_key()
returns only when a key is pressed. If you poll responses in a loop, as you're doing, a common strategy is to set the timeout value of the keyboard to 0, and to check whether a timeout occurred in the loop around it. Do you see what I mean? So you usekeyboard.get_key()
only to poll the keyboard in a non-blocking fashion, and implement the timeout logic in the loop around it.Below you see a slight modification of my previous script which shows the general idea.
Note that the scale is now always shown for 3000 ms or less. If you want to make sure that the scale is always shown for exactly 3000 ms, even if the participant pressed the spacebar before, you have to add some padding time to the end of the script. If needed, it might be a good exercise to try to figure that out yourself!
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi there,
I'm having a similar problem, but I couldn't workout how the above script would apply to my experiment. In my visual search experiment, I want my canvas to always remain for 2500ms, regardless of a key press. However, I would like to collect the response and response time of the keypress. The code below maintains the mask for 2500ms and collects the response, but it doesn't get the response_time (it currently returns a minimum of 2992, i.e. timeout, or as much as 5500ms). Is there a way to do this?
Many thanks,
Joff
Hi Joff,
Does this make sense?
Eduard
Hi Eduard,
Thanks for the quick response! I understand what is happening up until "if not var.response:", then I am a bit lost, as I would have thought that could be an else statement?
I tested the code and I get an error "t1 is not defined", I am guessing this is because the elif statement refers to t1?
Thanks,
Joff
Hi Joff,
Sorry, I should have used comments. Here again:
Is this clearer?
Eduard
Hi Eduard,
Thanks for the comments, that does make sense now. However, after presenting the mask OpenSesame (not the experiment) crashes, without an error report, as if it is overloaded or something.
Any ideas?
Thanks,
Joff
Yes, stupid me....
Swap the if and the first elif statement. You were caught in an infinite loop. See why?
here the updated code:
Hi Eduard,
I tested the code but it's the same problem I'm afraid! I think I see what you mean about the infinite loop: clock.time() - t0 is always greater than 495, after 495ms has passed (although I realise the if statement should interrupt this). Could this be the source of the continuing error?
Thanks,
Joff
Hi Joff,
Not sure what is wrong here. Could you try this code? Instead of showing the canvas on every frame, it executes this part only once (even though I don't think this should have caused a memory error or anything like that)
Eduard
Hi Eduard,
Thanks again for the suggestion, unfortunately it is still not working. What happens now is that the mask is shown for the correct duration, however all responses are shown as incorrect regardless of the response - I'm guessing it is logging everything as a timeout.
If you know of a simple solution that would be great, but I wouldn't want you too spend any more time on this issue and is not crucial to the experiment (I'm more interested in accuracy), although it could be useful data to have.
Thanks again for all your help.
Joff
Hi Joff,
Here another attempt. I tested it and I get response times that make sense and also the proper key labels out if it.
I hope this solves it now for good.
Eduard
Sorry Eduard, the response times are still not being recorded properly. I copied your code in, save your mask and canvas, but an incorrect response time is set on the first trial and this doesn't change for subsequent trials. Very strange, but I don't want to waste anymore of your time so thank you for your help.
Joff