索引错误:索引 201 超出轴 1 的大小 201

2024-05-14 23:30:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这是一个常见的错误,但我似乎无法从运行我的脚本的回溯中理解问题的根源。在

基本上,这个脚本将生成两个csv文件,其中包含一系列0.5s和1的打印输出,以形成由脚本本身生成的201x161打印输出。不需要过多的细节,但是脚本的作用是每一帧有201个位置。对于161个“帧”,列出的10个位置的值为1(在位置列表中)。这10个位置每帧都会更新,因此每个“帧”的位置都会增加1s的值。在

脚本的作用并不是很重要,但我希望有一个敏锐的眼睛,能帮助我识别产生错误的问题。在

from numpy import random, pi, sin
import numpy as np
from csv import DictWriter
import csv

#fixed stim size from George's AdelsonBergen.m
stim_x = 201 #(x axis)
stim_t = 161 #(y axis)

CONspeedRAW = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
SINspeedRAW = [0.0, 0.00077066656671800611, 0.0015365817177795569, 0.002293023331585725, 0.0030353276940450328, 0.0037589182519216719, 0.0044593338288080528, 0.0051322561297608583, 0.0057735363650253425, 0.0063792208287037815, 0.0069455752746670745, 0.0074691079394238937, 0.00794659107000374, 0.0083750808241273289, 0.0087519354199731905, 0.0090748314236410669, 0.0093417780738945227, 0.0095511295558659819, 0.0097015951480526573, 0.0097922471800438275, 0.0098225267519174118, 0.0097922471800438275, 0.0097015951480526573, 0.0095511295558659837, 0.0093417780738945244, 0.0090748314236410669, 0.0087519354199731923, 0.0083750808241273306, 0.00794659107000374, 0.0074691079394238963, 0.0069455752746670754, 0.0063792208287037824, 0.0057735363650253434, 0.0051322561297608591, 0.0044593338288080537, 0.0037589182519216727, 0.0030353276940450341, 0.0022930233315857263, 0.001536581717779558, 0.0007706665667180073]

NrOfDots = 10

counter = 0 #Counter for pos list
counter2 = 1 #Counter for speed list
counter201 = 0 #Counter for stim_matrix
rowcounter = 0 #Counter for writer
a = 0 #Counter for pixel to matrix speed scaling
printpos = True

SINspeed = []
CONspeed = []
floatPOS = []
frameList = []

#Set file name for output data file which includes participant ID and the experiment time and date   
createfile = open('SinMatrix.csv', 'wb')

matrixwriter = csv.writer(createfile,
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)

#pos = np.array([random.uniform(0.,201.) for r in range(NrOfDots)])
pos = np.array([random.randint(0,201) for r in range(NrOfDots)])
pos2 = pos

stim_matrix = 0.5 + np.zeros((stim_t,stim_x)) 

#Sine Speed Generator
for x in SINspeedRAW:
    a = x*330
    SINspeed.append(a)

for x in SINspeedRAW:
    a = (x*330)*-1
    SINspeed.append(a)

print 'pos1', pos

#Convert int to float if not you can't add in decimal speed
for x in pos:
    floatX = np.float(x)
    floatPOS.append(floatX)

for x in range(stim_t):
    #Update each stim_matrix's line with 1 for 10 dots placement
    for x in range(len(pos)):
        stim_matrix[counter201,pos[counter]] = 1
        counter += 1
    counter = 0

    #Add generated sin speed to position of the 10 dots 
    for x in range(len(pos)):
        if floatPOS[counter] + SINspeed[counter2] >= stim_x:
            floatPOS[counter] = 0.
        if floatPOS[counter] + SINspeed[counter2] <= 0:
            floatPOS[counter] = stim_x
        floatPOS[counter] = floatPOS[counter] + SINspeed[counter2]
        counter += 1
    counter = 0
    counter2 += 1

    for x in pos:
        pos[counter] = round(floatPOS[counter])
        counter += 1
    counter = 0
    counter201 += 1    

    #Print data for the current trial into a line of text
    matrixwriter.writerow(stim_matrix[rowcounter,:])
    rowcounter += 1

    if counter2 == 40 and printpos == True:
        print 'pos2', pos
        printpos = False
    if counter2 == 80:
        counter2 = 0

createfile.close()

counter = 0 #Counter for pos list
counter2 = 1 #Counter for speed list
counter201 = 0 #Counter for stim_matrix
rowcounter = 0 #Counter for writer
a = 0 #Counter for pixel to matrix speed scaling
printpos = True

print floatPOS

floatPOS = []
#---------------------------------Sin Matrix done. Now to generate Con Matrix-----------------------------------

#Set file name for output data file which includes participant ID and the experiment time and date   
createfile = open('ConMatrix.csv', 'wb')

matrixwriter = csv.writer(createfile,
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)

stim_matrix = 0.5 + np.zeros((stim_t,stim_x)) 

#Con Speed Generator
for x in CONspeedRAW:
    a = x*330 #33
    CONspeed.append(a)

for x in CONspeedRAW:
    a = (x*330)*-1 #33
    CONspeed.append(a)

#Convert int to float if not you can't add in decimal speed
for x in pos2:
    floatX = np.float(x)
    floatPOS.append(floatX)

print 'Cons pos1', pos2

for x in range(stim_t):
    #Update each stim_matrix's line with 1 for 10 dots placement
    for x in pos2:
        stim_matrix[counter201,x] = 1

    #Add generated cons speed to position of the 10 dots 
    for x in range(len(pos2)):
        if floatPOS[counter] + SINspeed[counter2] >= stim_x:
            floatPOS[counter] = 0.
        if floatPOS[counter] + SINspeed[counter2] <= 0:
            floatPOS[counter] = stim_x
        floatPOS[counter] = floatPOS[counter] + CONspeed[counter2]
        counter += 1
    counter = 0
    counter2 += 1

    for x in pos2:
        pos2[counter] = round(floatPOS[counter])
        counter += 1
    counter = 0
    counter201 += 1    

    #Print data for the current trial into a line of text
    matrixwriter.writerow(stim_matrix[rowcounter,:])
    rowcounter += 1

    if counter2 == 40 and printpos == True:
        print 'Cons pos2', pos2
        printpos = False
    if counter2 == 80:
        counter2 = 0

createfile.close()
print floatPOS  

*编辑:回溯消息:回溯(最近一次通话):

^{pr2}$

Tags: csvinposforifnpcountermatrix

热门问题