psychopy - 尝试通过条件文件

时间:2017-06-25 19:23:42

标签: python psychopy

摘要

在PsychoPy v1.85.1中,我正在配置'多边形'刺激对象。我试图通过csv条件文件设置height属性。我收到一条错误消息“无效参数。不接受单个号码”

详细

Windows上的PsychoPy v1.85.1。

  • 在多边形的弹出式UI'尺寸'框中输入: $ height

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

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

疑难解答 - 其他

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

疑难解答 - 在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 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:\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)

知道我做错了吗?

2 个答案:

答案 0 :(得分:1)

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

[1.5,0]

答案 1 :(得分:0)

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