psychopy.org | Reference | Downloads | Github

Variable Flip (mirror) property triggers error message


#1

Dear colleagues,

When we try to make the Flip (mirror) property in a Text Component variable via an embedded .xlsx file, we consistently get this error message:

File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\app\builder\components\text_init_.py”, line 116, in writeInitCode
raise ValueError(msg % self.params[‘name’].val)
ValueError: flip value should be ‘horiz’ or ‘vert’ (no quotes) in component ‘text’

Details: Windows OS, PsychoPy v1.85.2 (error replicated by multiple people on different computers)

For your convenience, a demo scripts with a .xlsx file and .psyexp illustrating this problem can be found in this google drive folder: http://bit.ly/PsychoFlipQ

Do any of you have an idea on what we might be getting wrong? We’d be very grateful for any advice!


#2

Can you just tell us how you are specifying the flip values, both in the component and in the conditions file?


#3

Hi Michael,

Thanks for the quick reply!

The flip property is set to $orient in the text component interface, which is set to “set every repeat”. In the Excel file the column is named orient and has two values in the two rows below: horiz and vert.

The Excel file is embedded in the loop that contains the Text component. It correctly detects the orient variable.

Is that the clarification you need?

Best,
Esther


#4

Oh wow, there seems to be a bug in Builder when it tries to create an initial definition for the text component. It should accept a variable in the Flip field, but instead the code shows that it will only actually accept the literal values horiz and vert.

Strange that no one has been tripped up by this before, @jon.

However, in the interim, you can work around it with a code component. Insert a code component in your routine. Probably best for it to be above your text component. although in this particular case, it might not matter.

In the Begin routine tab, insert some code something like this, which effectively does what putting the variable in the Flip field of the component would do, if it was working properly.

if orient == 'horiz':
    text.flipHoriz = True
    text.flipVert = False
else:
   text.flipHoriz = False 
   text.flipVert = True

#5

Hi Michael,

Awesome, this bug actually came up in one of the Builder exercises for our students (first year we are teaching a Python/PsychoPy course), so this is a great opportunity to show them the use of this forum. It’s also a perfect layup for the coming Coder lessons, I’ll come back to your solution there :wink:

Thanks for the quick clarification!

Cheers,
Esther


#6

Using small snippets of Python like this within the Builder environment (i.e. in a Builder Code Component) can also be a gentle introduction to programming, without going the full-on Coder route.


#7

Hello,

I’m inexperienced with PsychoPy and I’m actually trying to build an experiment involving letters. Some of them should be presented in a usual way and others should be flipped. Just as you did a few months ago, I tried to enter $Orientation to refer to my excel document in the builder view (text component interface) but there is no way even when it is set to “set every repeat”.
I then found your topic and tried to insert a code component as you did in the coder view with something like this :

if Orientation == 'horiz':
     StimS1.flipHoriz = True
else:
   StimS1.flipHoriz = False

I tried several places to insert the code component but it always triggers an error message
" if Orientation == ‘horiz’:
NameError: name ‘Orientation’ is not defined"

I tried to search for an answer throughout others topics about this type of error message, unsuccessfully.

Could you please help me with this ?


#8

Yes there is, this is a common and absolutely standard thing to do. You should explain exactly what goes wrong for you so we can suggest a fix. Then there would be no need to use any custom code at all.


#9

Thank you for answering Michael. I will try to be clearer today.

As I said earlier, my experiment involves letters and I have to dysplay some of them in a vertical mirror orientation but not all of them. Thus, I have done an excel file with my variables and I called one of them “Orientation” (I’ll try to insert the excel file here so you can see what I mean). Stim12-11.xlsx (11.6 KB)
In this “Orientation” column, I wrote “horiz” for the stimuli I want to be flipped and left a blank space for the others I want to be as usual. This criterion might randomly change among stimuli throughout trials (this explains the different lines with same stimuli).

I tried to insert this parameter in the builder view in the routine called “PrésentationStimS2M” as you can see below.

The problem is that then, the program is not running anymore and I got this error message
image

That is the reason why I wrote on this topic because it looks like Esther’s problem.

Here is my PsychoPy Experiment if it might help you (PsychoPy v1.90.1)
Exp. 12 - 11.psyexp (48.2 KB)

I would be very grateful for any advice !


#10

Hello Julia,

Apologies, I hadn’t read the thread above your post. When you posted the error message, it made it clear what was going on (that there is still a Builder bug when generating the experimental script, rather than a problem at run time).

So you are right, you need to use some custom code to work around it (I’ll try to fix the actual bug in Builder this time, but we still need to get your experiment going in the meantime). Your code could indeed simply be something like you have above:

if Orientation == 'horiz':
    StimS1.flipHoriz = True
else:
    StimS1.flipHoriz = False

This code should be in the “begin routine” tab and I think could be either above or below your text stimulus component. In the text component itself, leave the flip field empty and set to “constant”.

Of course you still have the NameError: name ‘Orientation’ is not defined error to solve. This is due to the Orientation variable not being available to the code at the time it is being executed. This variable will only be available to routines within a loop connected to a conditions file that contains a column with the header Orientation. So the code wouldn’t work in the “begin experiment” tab of code component, for example, as any loop doesn’t get created until later.

It should work in the “Begin routine” tab of any routine that is encompassed by the relevant loop, though, and you mentioned that you’d tried it in multiple tabs. So can you specify exactly where and when the code is being run, relative to the loop that contains that variable?

Do also check for spelling or capitalisation differences between Orientation and whatever appears in the conditions file itself.


#11

Thank you for your quick answer.

I tried to insert the code in “Prepare to start routine. PrésentationStimS2M”, below the test stimulus component. Is it what you call “begin routine” ?

Unexpectedly, I don’t have

NameError : name 'Orientation' is not defined
but instead
##### Running: C:\Users\Julia\Desktop\Memoire\PsychoPy\Exp. 12 - 11\Exp. 12 - 11.py ##### Traceback (most recent call last): File "C:\Users\Julia\Desktop\Memoire\PsychoPy\Exp. 12 - 11\Exp. 12 - 11.py", line 558, in <module> StimS1.flipHoriz = False AttributeError: 'str' object has no attribute 'flipHoriz'

I search for “str” in the code and find this


# Store info about the experiment session
expName = 'Exp. 12 - 11'  # from the Builder filename that created this script
expInfo = {'participant': '', 'session': '001'}
dlg = gui.DlgFromDict(dictionary=expInfo, title=expName)
if dlg.OK == False:
    core.quit()  # user pressed cancel
expInfo['date'] = data.getDateStr()  # add a simple timestamp
expInfo['expName'] = expName

I don’t understand because it didn’t matter earlier.

I also tried to insert the code below “Start routine PrésentationStimS2M” and I had the same error message with AttributeError.
I search in the excel file but there is no naming conflict.

For theNameError : name 'Orientation' is not defined,

I do have a loop connected to that routine and containing a column with the header ‘Orientation’ and within this column I have “horiz” in front of the stimuli I want to flip.
In this experiment I only have a welcome screen and then routines connected to a loop, which is itself connected to an Excel file (the one I posted above).
Should I code something about the column ‘Orientation’ (I check names but they are ok) ?

Sorry I’m stuck. I planned to insert images in this new post but I can’t as a newbie. I hope you will understand.


#12

Don’t attempt to edit the .py script that Builder generates. That path leads only to heartbreak and despair. Please read this post for how to insert custom code:


Please Help Random Grey Screen in Go No-Go
#13

Thank you very much Michael !
It took time to understand what you meant by “custom code” and “graphical code component” but I eventually got it and my experiment is now running without error messages !
I also realized that I was mistaken about the code I wrote above :

“StimS1” was a name in my excel file whereas I needed to insert the name of the routine in PsychoPy instead.

With all my thanks