首页 > 编程问答 >随机化两个光栅的位置

随机化两个光栅的位置

时间:2024-07-25 05:51:27浏览次数:7  
标签:python psychopy

我正在尝试创建两个同时显示的圆形光栅(一个水平的,一个垂直的)。在每次试验中,两个光栅的位置应该随机改变。

这是我到目前为止的代码,但我不知道如何添加“随机化”。

import psychopy.visual
import psychopy.event

win = psychopy.visual.Window(
    size=[1200, 600],
    units="pix",
    fullscr=False
)

grating = psychopy.visual.GratingStim(
    win=win,
    units="pix",
    size=[400,400]
)

grating.sf = 5.0 / 150.0

grating.mask = "circle"

orientations = [0.0, 90.0]
grating_hpos = [-300, 300]

for i_grating in range(2):
    grating.ori = orientations[i_grating]
    grating.pos = [grating_hpos[i_grating], 0]
    grating.draw()

win.flip()

psychopy.event.waitKeys()

win.close()

import psychopy.visual
import psychopy.event
import random

win = psychopy.visual.Window(
    size=[1200, 600],
    units="pix",
    fullscr=False
)

grating = psychopy.visual.GratingStim(
    win=win,
    units="pix",
    size=[400,400]
)

grating.sf = 5.0 / 150.0
grating.mask = "circle"

orientations = [0.0, 90.0]

# Define the range within which you want the gratings to appear
# For example, if you want them within the central 800 pixels:
x_range = [-400, 400]
y_range = [-300, 300]

for _ in range(10):  # This will show the gratings 10 times

    # Randomize the position of each grating
    grating_hpos = [random.uniform(*x_range), random.uniform(*y_range)]
    grating_vpos = [random.uniform(*x_range), random.uniform(*y_range)]

    for i_grating in range(2):
        if i_grating == 0:
            grating.ori = orientations[i_grating]
            grating.pos = grating_hpos
        else:
            grating.ori = orientations[i_grating]
            grating.pos = grating_vpos
        grating.draw()

    win.flip()

    psychopy.event.waitKeys()

win.close()

This code does the following:

  1. Imports random : This module is needed for generating random numbers.
  2. Defines x_range and y_range : These variables control the area where the gratings can appear. Adjust the values to change the area.
  3. Loop for multiple trials: The code now includes a loop that runs for 10 trials. In each trial:
  4. Randomizes grating_hpos and grating_vpos : For each grating (horizontal and vertical), it generates random x and y coordinates within the specified ranges using random.uniform() .
  5. Draws the gratings: It sets the orientation and position of each grating based on the randomized values and draws them.
  6. Waits for a key press: After each presentation, it waits for the user to press a key before proceeding to the next trial.

Now, each time you run the code, the two gratings will appear at different, random positions within the defined area.

标签:python,psychopy
From: 78789886

相关文章

  • 在 Azure 上部署代码时使用 Python 的多处理模块是否有意义?
    我们的团队在Azure机器学习(AML)上部署了一个Python脚本来处理存储在Azure存储帐户上的文件。我们的管道由一个ForEach活动组成,该活动调用每个或列出的文件的Python脚本。从Azure数据工厂(ADF)运行它会触发多个单独的管道同时运行......
  • 我已成功安装 pypdf2 但无法将其导入到我的 python 文件中
    我已经成功安装了pypdf2模块,但在导入它时,我发现该模块丢失了。我尝试使用fromPyPDF2importPdfReader导入,但它不起作用此问题的各种解决方案是什么?在尝试导入PyPDF2时遇到问题。以下是可能导致此问题的一些常见原因和解决方案:安......
  • Python3打开图片时请求ConnectionResetError(10054)
    我试图从'http://xxx.jpg'之类的网站下载图片。代码:headers={'user-agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/66.0.3359.139Safari/537.36'}url='http://xxx.jpg'resp......
  • Jupyter Notebook 环境中的 Python 版本不匹配
    我遇到Jupyter笔记本启动横幅中报告的Python版本与我在笔记本中查询python--version时显示的版本之间的差异。启动横幅指示Python3.11.9,但是当我运行!python--version时,它返回Python3.11.7。我所做的步骤:basecondahas3.11.7versio......
  • Python XML 解析:字符串中的“<”被阻塞
    我有一个使用ET.XMLParser来解析CppCheckXML报告文件的Python模块。当尝试解析字符串中包含“<”的XML元素中的属性之一时,它会令人窒息,它会将其解释为格式错误的XML,例如:<errormsg="Includefile<iostream>notfound.">(注意字符和“iostream”之间的空格必须放......
  • 任意几行代码要成为Python中的函数需要什么?
    我正在上一门计算机科学课,我的任务是创建一个程序来实现一个带有参数的函数。我的老师告诉我,下面的代码不是一个函数,这让我很困惑,对于将某些代码行归类为“函数”所需的条件,我感到很困惑。defgame(numbers,max_turns,pfl,tgl):turns=0flag=Falseprint("You......
  • 如何使用 Python 创建新的 Azure 订阅?
    我正在尝试使用PythonSDK以编程方式创建新的Azure订阅。我发现的对AzurePythonSDK的唯一引用是这个这是我最终得到的结果:importazure.mgmt.billingimportazure.mgmt.subscriptioncreds=AzureCliCredential()client_name='test'defcreat......
  • 用于打印脚本输出的 Python 实用程序
    我可以发誓有一个实用程序可以打印一个python脚本,其输出交织在一起。例如,给定一个脚本:a=2b=3print(a+b)print(a*b)该实用程序将输出a=2b=3print(a+b)#>5print(a*b)#>6有人知道该实用程序的名称吗?我最难找到它。谢谢你!描述的实用程序没有标......
  • a method to make some handy tools with python
    Inmyworkingofcomputer,therearealotofsimplejobsthatarefrequentlyrepeated.Itriedtofindawaytomakethesejobbeenprocessedeasily.Method1:Themethodiswritingascripttodothejob,andexecutingthescriptbyutoolsextensionuto......
  • Python网络爬虫详解:实战豆瓣电影信息采集
    文章目录前言一、爬虫是什么?二、常用库及其作用1.Requests2.BeautifulSoup3.lxml4.Scrapy5.Selenium6.PyQuery7.Pandas8.JSON9.Time三、实现步骤步骤一:环境准备步骤二:数据采集步骤三:数据处理步骤四:数据存储总结前言随着互联网的迅猛发展和数据分析需求的不......