Re: [psychopy-users] can't use variables in Allowed Keys in Builder keyboard component?

964 views
Skip to first unread message

Jeremy Gray

unread,
Nov 26, 2012, 8:34:45 PM11/26/12
to psychop...@googlegroups.com
Hi Toby,

Unfortunately its not possible at this time without a little extra effort.

If you download this file:
https://github.com/jeremygray/psychopy/blob/1e9577dd118f1c0cbbf25cbc866a6eda1dfc1e4b/psychopy/app/builder/components/keyboard.py

and replace the corresponding file in your version of psychopy, specifically this file:
    psychopy / app / builder / components / keyboard.py

then it should work, with any luck.

--Jeremy


On Mon, Nov 26, 2012 at 7:01 PM, Toby Mintz <tmi...@usc.edu> wrote:
Is it true that one cannot set Allowed Keys to a variable in the Builder?  I'd like to dynamically change the allowed keys based on a field in my loop condition file (as I can for "Correct answer"), but no matter what I try I get the error: "<keyboard-component-name>:  Allowed keys list is invalid."  I know I can do this pretty easily in the coder, but so far I've been able to keep everything I want to do in the builder, and I'd like to keep it that way.

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To post to this group, send email to psychop...@googlegroups.com.
To unsubscribe from this group, send email to psychopy-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/XT_cJjkb0l4J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Toby Mintz

unread,
Nov 27, 2012, 1:04:20 AM11/27/12
to psychop...@googlegroups.com
Jeremy,

Thanks for your speedy reply and supplying a patch so quickly!  I successfully copied the new code, but there is a problem, which I think involves line 115 of the new keyboard.py file.  The "keyList=list(%s)" statement leads to problematic results when the value of the variable is something like 'num_3' (which it is in my case)--it expands to ['n', 'u', 'm', '_', '3']--clearly not the desired result. I am not a python expert, and I also don't know the constraints on the format of "theseKeys", so I didn't try to fix it myself.

Thanks,
Toby

Jonathan Peirce

unread,
Nov 27, 2012, 4:41:35 AM11/27/12
to psychop...@googlegroups.com
Does it work if you set the value of the variable to be ['num_3'] instead?
Jon
To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/akc4tulcY7UJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

-- 
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk



Jeremy Gray

unread,
Nov 27, 2012, 9:02:22 AM11/27/12
to psychop...@googlegroups.com
Does it work if you set the value of the variable to be ['num_3'] instead?

this should work. for various reasons, including the case of 'num_3', I updated the code to add more checking at run-time. it now does the right thing for 'num_3' as well as "'num_3', 'space'", and list-like things (and regular non-variable usage).


--Jeremy

Jeremy Gray

unread,
Nov 27, 2012, 11:11:22 AM11/27/12
to psychop...@googlegroups.com
Hi Zoey,

I suspect that it will be quite a lot easier to get the new version working than to redo your experiment using the coder. 

--Jeremy


On Tue, Nov 27, 2012 at 12:02 AM, Zoey <zoe...@gmail.com> wrote:
Hi toby, 

Were you successful in making it work with the builder? I've tried the above^ method with no luck.

How would one accomplish this task with the coder (in case builder does not work)?


On Monday, November 26, 2012 4:01:38 PM UTC-8, Toby Mintz wrote:
Is it true that one cannot set Allowed Keys to a variable in the Builder?  I'd like to dynamically change the allowed keys based on a field in my loop condition file (as I can for "Correct answer"), but no matter what I try I get the error: "<keyboard-component-name>:  Allowed keys list is invalid."  I know I can do this pretty easily in the coder, but so far I've been able to keep everything I want to do in the builder, and I'd like to keep it that way.

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To post to this group, send email to psychop...@googlegroups.com.
To unsubscribe from this group, send email to psychopy-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/EEuHJp92nnIJ.

Toby Mintz

unread,
Nov 27, 2012, 5:01:03 PM11/27/12
to psychop...@googlegroups.com
Jeremy,

The new code works perfectly for my purposes. Many thanks!

Just curious: I note that one cannot use the "$" prefix with the variable in the Allowed keys field of the keyboard dialogue, but one does in the Correct answer field.  What is the reason for this?

Thanks again for adding this functionality.

Toby Mintz

Jeremy Gray

unread,
Nov 27, 2012, 5:09:50 PM11/27/12
to psychop...@googlegroups.com
thanks for the feedback, glad to hear its working.

some fields are automatically interpreted as being code. these always have a $ to the left of the input box (like Name and AllowedKeys), and so need to be legal python syntax. $ typically causes an error, since its not part of python syntax. 

some fields are strings, but are "smart" strings--they can figure out if they should be interpreted as being code. the way to signal this is to use a $ somewhere in the string. use \$ if you want an actual $ symbol, such as "$5.02". $ is used to indicate a variable in quite a few languages, from csh to perl, so we used it like that in PsychoPy too.

--Jeremy


--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To post to this group, send email to psychop...@googlegroups.com.
To unsubscribe from this group, send email to psychopy-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/nCRqMS6bI8IJ.
Message has been deleted

Jeremy Gray

unread,
Dec 4, 2012, 7:15:53 AM12/4/12
to psychop...@googlegroups.com
Hi Belel,

You definitely need to restart psychopy after replacing the file. If it works for some people but not for you, I think the likely explanation is that you have not replaced the correct file, or its not finding or using that file (e.g., because you need to restart).

Also, I don't think it will solve your problem but here is a slightly better version of the fixed file: https://github.com/jeremygray/psychopy/blob/ed27ac9b9d2032e3d34d0c55e2f1ca9c378e6b93/psychopy/app/builder/components/keyboard.py

--Jeremy


On Mon, Dec 3, 2012 at 5:30 PM, Belel <belel.ait...@gmail.com> wrote:
I tried this method too and for some reason it does not work. I get the "Allowed keys list is invalid" message just as i have prior to changing the keyboard.py file. Are there any possible explanations for this?


On Monday, November 26, 2012 4:01:38 PM UTC-8, Toby Mintz wrote:
Is it true that one cannot set Allowed Keys to a variable in the Builder?  I'd like to dynamically change the allowed keys based on a field in my loop condition file (as I can for "Correct answer"), but no matter what I try I get the error: "<keyboard-component-name>:  Allowed keys list is invalid."  I know I can do this pretty easily in the coder, but so far I've been able to keep everything I want to do in the builder, and I'd like to keep it that way.

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To post to this group, send email to psychop...@googlegroups.com.
To unsubscribe from this group, send email to psychopy-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/0gbWLFBB-aYJ.

Michael MacAskill

unread,
Jan 3, 2013, 10:25:57 PM1/3/13
to psychop...@googlegroups.com
Hi Raul,

I could be wrong, but it seems from looking at the code within the Builder keyboard component that it can't (currently) work with variable names in the AllowedKeys field.

Raul- ignore the following bit in brackets, it is mainly directed to Jon, or someone else who understands Builder component conventions:

[In the lines from approximately 112 in keyboard.py it says:

try:
keyList = eval(self.params['allowedKeys'].val)
except:
raise CodeGenerationException(self.params["name"], "Allowed keys list is invalid.")

So if a variable name was used in the allowedKeys field, Builder would be trying to evaluate it at compile time, although it won't actually be defined until runtime?]


Raul- just to check explicitly: do you want to have lists of allowed keys that vary from trial to trial, and hence you are trying to have them specified as a variable from a conditions .xlsx file?

Regards,

Michael


On 4 Jan, 2013, at 08:43, Raul Gonçalves <rag...@gmail.com> wrote:

> Hi Everyone,
>
> I'm having a puzzling issue here. I'm running the latest update 1.75.01 on windows on the builder, and I know nothing of python whatsoever. I am trying to use variables in the allowed keys too... I've tried a few combinations of configuration between the xlsx file and the allowed keys field, including inserting the key names between apostrophes in the xlsx, etc. I get the "Allowed keys list is invalid" for all configurations I tried, except one: in the xlsx file, I wrote the names of the keys that should be allowed, and in the builder field, i wrote the variable name between apostrophes ( like this: '$corrTecla' ). It compiles without error, but the keyboard becomes useless (none of the keys i tried, except for 'esc', work).
>
> What I want to do is keep record of all keys pressed by the subject during each trial, and only end the trial when he/she presses the correct one. If there is another way of doing it, I'd appreciate.
>
> By the way, let me take the opportunity to thank Jon, the other developers and all the Psychopy community for this amazing tool. I've been promoting it to all my colleagues here in Brazil - we just need more psychologists that can code down here :D !!!

Jeremy Gray

unread,
Jan 3, 2013, 10:35:38 PM1/3/13
to psychop...@googlegroups.com
Thanks Mike. I think this means that the fix for this has not made it into a patch update. Its fixed on github, and will eventually work the way it should. In the meantime, Raul will have to follow the instructions earlier in this thread to update his version of microphone.py (the component). 

--Jeremy


--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To post to this group, send email to psychop...@googlegroups.com.
To unsubscribe from this group, send email to psychopy-user...@googlegroups.com.

Jeremy Gray

unread,
Jan 3, 2013, 10:37:59 PM1/3/13
to psychop...@googlegroups.com
sorry, I meant keyboard.py (not microphone.py). specifically: https://github.com/psychopy/psychopy/blob/master/psychopy/app/builder/components/keyboard.py

Michael MacAskill

unread,
Jan 3, 2013, 10:42:52 PM1/3/13
to psychop...@googlegroups.com
Ahh, I looked at my local standalone copy, and should have been looking at the up-to-date source. I see there is now a check there for if the value is a variable.

Cheers,

Mike


Cheers

On 4 Jan, 2013, at 16:35, Jeremy Gray <jrg...@gmail.com> wrote:

> Thanks Mike. I think this means that the fix for this has not made it into a patch update. Its fixed on github, and will eventually work the way it should. In the meantime, Raul will have to follow the instructions earlier in this thread to update his version of microphone.py (the component).

--
Michael R. MacAskill, PhD michael....@nzbri.org
Research Director,
New Zealand Brain Research Institute

66 Stewart St http://www.nzbri.org/macaskill
Christchurch 8011 Ph: +64 3 3786 072
NEW ZEALAND Fax: +64 3 3786 080










Raul Gonçalves

unread,
Jan 4, 2013, 3:50:01 PM1/4/13
to psychop...@googlegroups.com
Thnaks for the help, Michael and Jeremy. I downloaded keyboard.py from the link, but I clearly don't know what to do with it. i used it to replace the "keyboard.py" file in 
"PsychoPy2\Lib\site-packages\PsychoPy-1.74.01-py2.6.egg\psychopy-1.74.01\app\builder\components", 
but then psychopy didn't even open. It was fixed when I put the original "keyboard.py" back.

As for Michael's question: "just to check explicitly: do you want to have lists of allowed keys that vary from trial to trial, and hence you are trying to have them specified as a variable from a conditions .xlsx file?"

Yes... its a workaround for my lack of programming skill, I suppose. I'm building a classic visual probe for attention bias. After seeing two side-by-side images, one of them is replaced by an arrow, and the subject must push one button if the arrow is pointing up, and another if the arrow is pointing down. So, the response is always limited to those two buttons. It is working perfectly, except that I have no way to make the trial last until the correct button is pressed - i.e., since both buttons are allowed, if the subject pushes the wrong button, the trial ends. I want to record the mistakes as well as the correct answers, but only end the trial after a correct response is given.

Since my lack of skill limits me to the builder, I thought I'd do this using two keyboard objects: one that allows both buttons and records all inputs, but does not have the "force end of routine" checked. And another keyboard object that doesn't record answers, but would only allow input from the correct response button and have the "force end of Routine" checked, so it moves to the next trial. Since the correct response changes from trial to trial, I listed the correct response key in the conditions.xlsx, and I'm trying to feed that as a variable to thar second keyboard.

If there's a better way (that i can implement), I'm all for it...

Thanks again, and have a good weekend.





Jeremy Gray

unread,
Jan 5, 2013, 7:49:28 AM1/5/13
to psychop...@googlegroups.com
Hi Raul,

I suggest that you try having one keyboard component (that accepts either key and does NOT end the routine), and one code component (that checks whether the most recent key press was correct, and if so, ends the routine).

Have the code component be below (= after) the keyboard component. In the "Each Frame" section, have something like this (where "correctKey" is a column in your conditions file)

  if correctKey in theseKeys:
      continueRoutine = False

--Jeremy

Raul Gonçalves

unread,
Jan 5, 2013, 6:09:30 PM1/5/13
to psychop...@googlegroups.com
Hi jeremy, thanks for the tip, it was very helpful. Since I got my feet wet into python, I experimented a few variations of code in the "Each frame" section. I gave up registering all keys, it would be useless without the RT for each button press and that seems a stretch way too far for me. With the tip you gave, plus setting the keyboard to register "last key" [rather than all keys], this bit of code worked:

"
if (key_resp.keys != str(corrAns)): continueRoutine = True
else: continueRoutine = False
"
This should be enough for my current purposes.

Once again, thanks!!! hope I can retribute the help some day.

shark_scott

unread,
Jan 10, 2014, 11:29:47 AM1/10/14
to psychop...@googlegroups.com
Hi,
    I was wondering if this is now working in the Builder view?  I'm trying to vary the allowed keys depending on condition (so set it to a variable) and I'm getting the same error.
    Thanks,
    Mark

Jeremy Gray

unread,
Jan 10, 2014, 11:44:05 AM1/10/14
to psychop...@googlegroups.com
> I was wondering if this is now working in the Builder view? I'm trying
> to vary the allowed keys depending on condition (so set it to a variable)
> and I'm getting the same error.

Yes, using a variable is now working for me in Builder, in 1.79.00

--Jeremy
> To unsubscribe from this group and stop receiving emails from it, send an
> email to psychopy-user...@googlegroups.com.
>
> To post to this group, send email to psychop...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/psychopy-users/a60ce025-ef1e-4132-a127-2c9443954060%40googlegroups.com.

ian hussey

unread,
Jan 12, 2014, 6:10:23 PM1/12/14
to psychop...@googlegroups.com
Hi Mark & Jeremy,

I too am getting a "AllowedKeys variable `keys` is not string- or list-like" error in 1.79.01 for OS X when I try to set the set allowed keys to a variable which contains several keys.

I have tried:
['d','k']
"'d','k'"
d,k
'd','k'
etc.

Best
Ian

Jeremy Gray

unread,
Jan 12, 2014, 8:24:42 PM1/12/14
to psychop...@googlegroups.com
For me, a variable named "key" works like this. In a code component,
Begin Experiment, put the next line of text:

key = ['y','n','left','right','space']

Then in a keyboard component, set Allowed keys to be "key" (no quotes).

So I define the variable, and then put its name in the Allowed keys
box. I presume that defining the variable in other ways would work
just the same, whether like this, or the name of a column in a .xlsx
file, and so on.

--Jeremy
> https://groups.google.com/d/msgid/psychopy-users/27c3ce1e-d57e-4e7c-a52a-8cf8602f7973%40googlegroups.com.

shark_scott

unread,
Jan 13, 2014, 4:08:06 AM1/13/14
to psychop...@googlegroups.com
Hi,
    Thanks for your responses - I was making a stupid mistake and putting a "$" variable marker in the field, I don't know if that applies to you Ian.
    Thanks,
    Mark
Reply all
Reply to author
Forward
0 new messages