Calling an object name from Conditions file

Hi,

I presume this is a n00b issue, but I’m lost nonetheless.

Within each iteration of a certain routine, I am trying to check whether a drag&droppable stimulus (DDstim) is intersecting another stimulus. However, the stimulus to be intersected (i.e., not DDstim) differs for each iteration, and the name of the correct stimulus object is contained within a variable (OverlapCorrect). Thus, my aim was to call the name of the object to be intersected and use it as a parameter in DDstim.overlaps() within a condition, like so: (let’s assume the actual name of the object to be intersected was “ObjectA” in this case)

if DDstim.overlaps(thisTrial.OverlapCorrect): …

however, I’m getting an error:

pyo version 0.8.0 (uses single precision)
Traceback (most recent call last):
File “…”, line 574, in
if DDstim.overlaps(thisTrial.OverlapCorrect):
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy-1.84.2-py2.7.egg\psychopy\visual\basevisual.py”, line 583, in overlaps
return polygonsOverlap(self, polygon)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy-1.84.2-py2.7.egg\psychopy\visual\helpers.py”, line 104, in polygonsOverlap
if any(mplPath(poly1).contains_points(poly2)):
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\matplotlib\path.py”, line 512, in contains_points
result = _path.points_in_path(points, radius, self, transform)
ValueError: could not convert string to float: ObjectA

…not sure why I’m getting this kind of error, since when I hardcoded this instead, like so:

if DDstim.overlaps(ObjectA): …

the code works as intended. I’m probably missing something regarding the connection between what is read as a string from the conditions file and the object the name of which is identical to this string.

Thanks for help!

Well it’s not clear to me what type of stim you are dealing with, but the docs indicate that at least an ImageStim has a “name” attribute. After creating the stimuli (and assigning each a “name” in their constructor), you could put them in a list, then later loop through the list and check the names. Have an extra column in the spreadsheet indicating which is the correct name. I’m assuming the type of object (ImageStim, DotStim) is the same throughout?

# pseudocode
im1 = ImageStim( <etc> ..., name = "im1", ...)
# ... repeat for other stimuli ...

# after creation put in a list
ims = [im1, im2, im3]

# assume there's an object they're supposed to 
# drop on, the "target"
for im in ims:
    if target.overlaps(im):
        # corrIm would be the column name containing
        # the name of the correct image
        if im.name == corrIm:
            print(u"Correct!")
        else:
            print(u"Incorrect!")

Actually even if they don’t have a “name” attribute, you could just add one after creating them:

im1.objName = "im1"

And that error you’re getting is because you’re giving that overlaps() function an argument of a type it’s not expecting.

Thanks, Daniel. Yep, all stims are images, forgot to mention that, sorry. And yes, the object type is the same throughout.

The method you propose would surely solve the issue, but I’m still not 100% why the code behaves the way it does. Like I said, when I hardcode a solution, like so:

if DDstim.overlaps(ObjectA): …

then the code works as intended. When I replace “ObjectA” with “thisTrial.OverlapCorrect”, however, it does not, even though calling “thisTrial.OverlapCorrect” should output “ObjectA” (without parentheses) if I’m not mistaken, since that’s what’s stored under variable “OverlapCorrect” in the “thisTrial” row. I’m guessing that calling “thisTrial.OverlapCorrect” outputs a string which is not directly usable to refer to an object - is there any simple way of referring to a particular object having only its name as a string at hand?

Anyway, thanks again. I’ll use your solution.

eval(OverlapCorrect)

Beware, this can easily get a bit mind-bending. Daniel’s approach (matching the string content of a variable to the name property of an object (which is also a string)) will often be more cognitively straight-forward.

Perfect. Thank you both.