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

2024-09-30 20:27:06 发布

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

我有一段代码可以画一组线(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

Tags: andtoinposrandomgriddrawrt
1条回答
网友
1楼 · 发布于 2024-09-30 20:27:06
  1. 你想要的不是一个变量,而是一个函数。

  2. 你现在做事情的方式(通过visual.Line(...).draw())非常低效。您在每次迭代中创建一条新线,只是为了绘制一次,而不是存储对它的引用。一个更省时的方案是只创建一个用变量名引用的单行对象实例,然后在每次迭代中,只需在绘制它之前更新它的属性(方向等)。

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

for line_instance in line_list:

    line_instance.draw()

相关问题 更多 >