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



摘要

在PsychoPy v1.85.1中,我正在配置"多边形"刺激对象。我正在尝试通过 csv 条件文件设置高度属性。我收到一条错误消息,指出"参数无效。不接受单号">

Windows 上的 PsychoPy v1.85.1。

  • 在多边形的弹出式 UI "大小"框中,我输入: $height

  • 在csv文件中,我有一个"高度"列。每行都有如下值: (1.5, 0)

PsychoPy给出错误消息:

File "C:Program Files (x86)PsychoPy2libsite-packagespsychopy   visualbasevisual.py", line 1312, in pos
self.__dict__['pos'] = val2array(value, False, False)
File "C:Program Files (x86)PsychoPy2libsite-packagespsychopytoolsarraytools.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

故障排除 - 杂项

  • 标量变量在其他csv列中工作正常,因此PsychoPy与csv文件连接。
  • 已尝试 xlsx 格式。
  • 尝试输入不带括号和方括号

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

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

# Copied code snippet from 
# C:Program Files (x86)PsychoPy2libsite-packagespsychopytoolsarraytools.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 arrary 函数无法转换的 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:UsersniklaDocumentsPsychoPytest2.py", line 127, in <module>
print "numpy.array(position, float): ", np.array(position, float)
ValueError: could not convert string to float: (0, 0.5)

知道我做错了什么吗?

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

[1.5, 0]

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

相关内容

  • 没有找到相关文章

最新更新