Saving the location of stimuli

What do I have?
An image and a word are presented concurrently. Sometimes the word is on the left, sometimes the image. The position of images is determined from the code below:

wordimage_pos = [[-.5,0],[.5,0]] #begin experiment
shuffle(wordimage_pos) #begin routine

In the component settings,
position[x,y] for the image is: $(wordimage_pos[0][0], wordimage_pos[0][1])component
position[x,y] for the text component is: $(wordimage_pos[1][0], wordimage_pos[1][1])

What do I want?
I want to save a variable when the image is on the left or right. For instance:

if position[x,y] == [-.5,0]:
    pos_image = 'left'
else:
    pos_image = 'right'
thisExp.addData('pos_image ', pos_image)

I guess due to the value I defined for the position[x,y], I am receiving a syntax error. Can someone help me with the correct way of defining the position?

Thank you in advance

if (wordimage_pos[0][0], wordimage_pos[0][1]) == (-0.5, 0):
    pos_image = 'left'
elif (wordimage_pos[0][0], wordimage_pos[0][1]) == (0.5, 0):
    pos_image = 'right'
thisExp.addData('pos_image ', pos_image)

On thing to wary of here is that this code might randomly and inexplicably fail in some trials but not others. This is because computers can’t be counted on to store floating point numbers with exact precision. So while you can always and reliably compare two integers for equality using ==, you should never do that with floating point numbers like 0.5. Often it will work, but sometimes it will not. In your case, if the internal representation of the position is actually stored as -0.49999999, then both comparisons would evaluate to False, meaning that pos_image would just retain its last value, and not get updated at all. These sorts of errors can be very difficult to chase down. In your case, no error would be generated, and even if the comparison failed, you wouldn’t notice, because pos_image would still have a value, and half of the time it would even be correct (randomly).

So in your situation, the code would be much more reliable if you didn’t test for equality but instead used < or >, so that the code would be robust to minor precision issues. e.g. simply this:

if wordimage_pos[0][0] < 0.0:
    pos_image = 'left'
else:
    pos_image = 'right'

thisExp.addData('pos_image ', pos_image)

would work reliably, because the difference in values (±0.5) is orders of magnitude larger than the possible precision error.

2 Likes

Hi Michael,

After I added my solution, I realized that the relevant columns were empty after running the study online. I did not know how to solve it. I am really appreciated your answer.

Thank you so much! :hibiscus: