psychopy - 尝试通过条件文件设置非标量变量时出错

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

总结

在 PsychoPy v1.85.1 中,我正在配置 'polygon' 刺激对象。我正在尝试通过 csv 条件文件设置高度属性。我收到一条错误消息,提示“参数无效。不接受单个数字”

详情

Windows 上的 PsychoPy v1.85.1。

PsychoPy 给出错误信息:

  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

疑难解答 - 杂项

故障排除 - 运行 PsychoPy 之外的代码

我转到 arraytools.py 文件并找到相关的代码片段。我将它粘贴到 Python 笔记本中(尽管它是 python 3.3)并添加一些打印行以进行调试:

# 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))))

我通过输入一个值然后 运行 函数来测试它。

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

结果如下。似乎工作正常:

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

Out: array([ 1.5,  0. ])

在编码器视图中调试

谢谢迈克尔。似乎输入值变成了 numpy 数组函数无法转换的 unicode 字符串

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)

知道我做错了什么吗?

错误与位置属性有关,而不是与高度有关。您是否将位置设置为标量?它必须是一个坐标。尝试将 csv 文件中的位置写为 (1.5, 0.0),然后在 Builder 中输入 $eval(position)(或任何您称为位置列的内容)。

问题已通过使用方括号输入属性值(在 GUI 弹出窗口中)得到解决:

[1.5, 0]