Niklas
Niklas

Reputation: 11

psychopy - error when trying to set non-scalar variable via conditions file

Summary

In PsychoPy v1.85.1 I am configuring the 'polygon' stimuli object. I am trying to set the height attribute via a csv conditions file. I get an error message saying "Invalid parameter. Single numbers are not accepted"

Details

PsychoPy v1.85.1 on Windows.

PsychoPy gives the error message:

  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy   \visual\basevisual.py", line 1312, in pos
self.__dict__['pos'] = val2array(value, False, False)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\tools\arraytools.py", line 176, in val2array
raise ValueError(msg % str(length))
ValueError: Invalid parameter. Single numbers are not accepted. Should be tuple/list/array of length 2

Troubleshooting - Misc

Troubleshooting - Running the code outside of PsychoPy

I go to the arraytools.py file and find the relevant code snippet. I paste it into a Python notebook (although it is python 3.3) and add some print rows for debugging:

# Copied code snippet from 
# C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\tools\arraytools.py

import numpy

def val2array(value, withNone=True, withScalar=True, length=2):
"""Helper function: converts different input to a numpy array.

    Raises informative error messages if input is invalid.

    withNone: True/False. should 'None' be passed?
    withScalar: True/False. is a scalar an accepted input?
        Will be converted to array of this scalar
    length: False / 2 / 3. Number of elements input should have or be
        converted to. Might be False (do not accept arrays or convert to   such)
"""
    if value is None:
        if withNone:
            return None
        else:
            raise ValueError('Invalid parameter. None is not accepted as '
                         'value.')
    value = numpy.array(value, float)

    print ("value:", value)  #I ADDED
    print ("value.shape:", value.shape)  #I ADDED
    print ("numpy.product(value.shape):", numpy.product(value.shape))  #I ADDED

    if numpy.product(value.shape) == 1: #MY COMMENT: WHY DOES THIS EVALUTE TRUE?
        if withScalar:
            # e.g. 5 becomes array([5.0, 5.0, 5.0]) for length=3
            return numpy.repeat(value, length)
        else:
            msg = ('Invalid parameter. Single numbers are not accepted. '
               'Should be tuple/list/array of length %s')
            raise ValueError(msg % str(length))
    elif value.shape[-1] == length:
        return numpy.array(value, float)
    else:
        msg = 'Invalid parameter. Should be length %s but got length %s.'
        raise ValueError(msg % (str(length), str(len(value))))

I test it by entering a value and then run the function.

# Run the function
value = (1.5,0.0)
val2array(value, False, False, length =2)

Results below. Seems to work fine:

value: [ 1.5  0. ]
value.shape: (2,)
numpy.product(value.shape): 2

Out: array([ 1.5,  0. ])

Debugging in Coder View

Thank you Michael. It seems the input value becomes a unicode string which the numpy arrary function cannot convert

print "position: ", position
print "type(position): ", type(position)
print "numpy.array(position, float): ", np.array(position, float)

#Results:
position:  (0, 0.5)
type(position):  <type 'unicode'>
numpy.array(position, float): 
Traceback (most recent call last):
 File "C:\Users\nikla\Documents\PsychoPy\test2.py", line 127, in <module>
print "numpy.array(position, float): ", np.array(position, float)
ValueError: could not convert string to float: (0, 0.5)

Any idea what I am doing wrong?

Upvotes: 0

Views: 853

Answers (2)

Niklas
Niklas

Reputation: 11

The problem was fixed by entering the attribute values (in the GUI pop-up) using square brackets:

[1.5, 0]

Upvotes: 1

Jonas Lindel&#248;v
Jonas Lindel&#248;v

Reputation: 5683

The error has to do with the position attribute rather than the height. Do you set the position to be a scalar? It has to be a coordinate. Try writing the position in your csv file as (1.5, 0.0) and then enter $eval(position) in Builder (or whatever you called the position column).

Upvotes: 0

Related Questions