如何为嵌套循环创建变量?

时间:2016-04-15 01:50:46

标签: python variables psychopy

我有一些代码可以绘制一系列行(32X32网格)。绘制它的实际代码从#Nested loop to draw anti-aliased lines in a 32X32 grid开始。因为我在其中有一个具有不同方向线的补丁,所以代码只有几行。

目前,我在嵌套循环的末尾有一个.draw()来绘制我的数组。

这似乎不是一个好方法。

有没有办法为整个嵌套循环创建变量,所以我可以在需要时调用它?例如myStim.draw()

# Import what is needed
import numpy as np
from psychopy import visual, event, core, logging
from math import sin, cos
import random, math

# Create space variables and a window
lineSpaceX = 0.55
lineSpaceY = 0.55

patch_orientation = 45 # zero is vertical, going anti-clockwise
surround_orientation = 90

#Jitter values
g_posJitter = 0.05 #gaussian positional jitter
r_posJitter = 0.05 #random positional jitter

g_oriJitter = 5 #gaussian orientation jitter
r_oriJitter = 5 #random orientation jitter

#Region where the rectangular patch would appear
x_rand=random.randint(6,13) #random.randint(Return random integers from low (inclusive) to high (inclusive).
y_rand=random.randint(6,16)

#rectangular patch dimensions
width=15
height=12

message = visual.TextStim(win,pos=(0.0,-12.0),text='...Press SPACE to continue...')

# Initialize clock to record response time
rt_clock = core.Clock()


#Nested loop to draw anti-aliased lines in a 32X32 grid
for x in xrange(1,33): #32x32 grid.
    for y in xrange(1,33): 
        ##Define x & y value (Gaussian distribution-positional jitter)
        x_pos = (x-32/2-1/2 )*lineSpaceX + random.gauss(0,g_posJitter) #random.gauss(mean,s.d); -1/2 is to center even-numbered stimuli; 32x32 grid
        y_pos = (y-32/2-1/2 )*lineSpaceY + random.gauss(0,g_posJitter)

        if (x >= x_rand and x < x_rand+width) and (y >= y_rand and y < y_rand+height): # note only "=" on one side
            Line_Orientation = random.gauss(patch_orientation,g_oriJitter) #random.gauss(mean,s.d) - Gaussian func.
        else:
            Line_Orientation = random.gauss(surround_orientation,g_oriJitter) #random.gauss(mean,s.d) - Gaussian func.
            #stimOri = random.uniform(xOri - r_oriJitter, xOri + r_oriJitter) #random.uniform(A,B) - Uniform func.
        visual.Line(win, units = "deg", start=(0,0), end=(0.0,0.35), pos=(x_pos,y_pos), ori=Line_Orientation, autoLog=False).draw() #Gaussian func.

frameN = 0
for frameN in range(80): #for exactly 80 frames; 1 frame = 16.67ms on the 1920 x 1080 monitor
    if frameN == 0:
        rt_clock.reset() # set reaction time clock to 0
    message.draw()
    win.flip()#   display stimulus 

    frameN = frameN + 1
    keys = event.waitKeys(keyList=['space', 'escape','q'])    #create key list response

    # handle key responses 
    if len(keys)>0:
        rt = rt_clock.getTime()
        if keys == ['space']:
            event.clearEvents()
            break
        else:
            print 'Stopped Early'
            win.close()
            core.quit()

print x_rand, y_rand 
print keys, rt  #display response and reaction time on screen output window

1 个答案:

答案 0 :(得分:1)

  1. 这不是你想要的变量,而是一个函数。

  2. 您目前的工作方式(通过var box_top = $("#box").offset().top; $(window).scroll(function (event) { if ($(window).scrollTop() >= (box_top - 50)) { $("#box").css({position:"fixed",top:"50px"}); } else { $("#box").css({position:"relative"}); } }); )效率非常低。您正在迭代中创建一个新行,只是为了绘制一次,而不是存储对它的引用。一个更加节省时间的方案是创建一个单行对象实例,用变量名称引用,然后在每次迭代时,只需在绘制之前更新其属性(方向等)。

  3. 另一种方法是创建多个行对象实例,但将每个实例存储在列表中。然后根据需要再次绘制它们是一个简单的问题:

  4. visual.Line(...).draw()

    for line_instance in line_list: