Coding multiple lists and Loops - python

For my experiment, I am creating a simple reaction time test. It will have 4 blocks, with 6 trials in each. I have managed to present the first trial of a block on screen, albeit for a practice round (the stimuli does appear however). However, I am not sure whether I should be creating a number of lists (i.e. to hold the different stimuli for the different trials) or if I should be altering the characteristics of my one existing loop. My code is below:
from psychopy import visual, core, event #import some libraries from PsychoPy
import psychopy.event
#Create the code for saving the data at some point
#create a window
mywin = visual.Window([1920,1080], monitor="testMonitor", units="deg")
mywin.update()
# create the six stimuli:
Target = visual.TextStim(mywin, text = "text default")
text_stim_list = [] # a list to hold them
Stim_text = ["Berlin", "Paris", "London", "Nice", "Vienna", "Charming"] #
their content
#the positions of the stimuli
positions = [
(-10, 10),
(10, 10),
(-10, -10),
(10, -10),
(-1, -10),
(1, 10),
]
#Initial message to participants about the study
message1 = visual.TextStim(mywin, text = "You have been captured by the
plotters. As a test to see if you have information regarding the event, an
on screen test will be adminstered. Press Spacebar when ready")
message1.draw()
mywin.update()
#this will wait for a button press confirmation from p's to continue
response = event.waitKeys(keyList = ['space',], timeStamped = True)
#Initial message to participants about the study
message1 = visual.TextStim(mywin, text = "On the next screen, you will be
presented with a practice round of stimuli. Press Q if any of the cities are
familiar, or press P if you do not recognise the cities")
message1.draw()
mywin.update()
#this will wait for a button press confirmation from p's to continue
response = event.waitKeys(keyList = ['space',], timeStamped = True)
#Loop for drawing the stimulus
#code for the keyboard response
keys = psychopy.event.waitKeys(keyList=["q", "p"])
#Practice round-should it be stopped by a button press?
for frameN in range(7*60):
# draw each stim *on each frame*
for i in range(len(Stim_text)):
Target.setPos(positions[i])
Target.setText(Stim_text[i])
Target.draw()
# now flip the window (after all stimuli drawn)
mywin.flip()
#Initial message to participants about the study
message1 = visual.TextStim(mywin, text = "On the next screen, the cities
which
our intelligence tells us are at risk will be presented. Press Q if any are
familiar, press P if not blah blah blah.. press space when ready")
message1.draw()
mywin.update()
response = event.waitKeys(keyList = ['space',], timeStamped = True)#wait for
subjects to state they are ready
#Cities block
#First trial
print Stim_text
del Stim_text[1,2]
print Stim_text
I am fairly sure it will involve changing the attributes of the loop- as otherwise it would not make sense to have so many lists.
Nathan

In general, I would do something like this:
# Run the code inside this loop four times
for block in range(4):
for i, position in enumerate(positions):
# Prepare stimulus
Target.pos = position
Target.text = Stim_text[i]
# Show it
Target.draw()
win.flip()
# Collect response and score it
key = event.waitKeys(keyList=['q', 'p'])[0] # Just get the first response key
# Save response here somehow.
As Mike pointed out in a comment, look into psychopy.data.TrialHandler which can do much of this for you, including saving data. Also, there's no need to generate a new visual.TextStim for each message. Just update the text of an existing one with the desired text.

Related

python-vlc switching playback media between launches

I'm trying to set up a system where my start-screen video loops until 1 of 2 buttons is pressed (GPIO buttons).
Then, the playback changes to either a video with subtitles or no-subtitles.
Once that has finished its play-through, it reverts back to the splash screen video.
I have additional tickers in here just to count the number of play-throughs per day for analytics. My Test device also only has 1 button hooked up which is why GPIO 18 is never used. Implementation will be identical to GPIO 17's, so once one is working the other won't be hard to match up.
Problem
When I launch the script, the media played is not always splash. The script also closes the window at the end of playback, and opens a new one to play the media. I believe this may be due to not establishing an xwindow (for raspberry pi).
Any advice?
#Vars
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN)
GPIO.setup(18,GPIO.IN)
update = True #Update to false to exit
def Main():
# Setup logs
print(date.today())
# Media Paths
path = "/home/pi/Videos/"
nosubs = path+"Content-NoSubs.mp4"
subs = path+"Content-Subtitles.mp4"
splash = path+"StartScreen.mp4"
Instance = vlc.Instance("-f")
playlist = set([splash,subs,nosubs])
url = [str(splash),str(subs),str(nosubs)] #Yes, this looks pretty redundant. Hopefully it's not.
#Setup the player
player = Instance.media_list_player_new()
Media = Instance.media_new(url[1])
Media_list = Instance.media_list_new(playlist)
Media.get_mrl()
player.set_media_list(Media_list)
playerState = {'State.NothingSpecial',
'State.Opening',
'State.Buffering',
'State.Playing',
'State.Paused',
'State.Stopped',
'State.Ended',
'State.Error'}
subsPlayed = 0
nosubsPlayed = 0
active = 0
playingMedia = 0
while update:
input = GPIO.input(17)
state = str(player.get_state())
if(state == playerState[0]):
player.play_item_at_index(0)
player.set_playback_mode(2)
if(state == playerState[7]):
player.play_item_at_index(0)
playingMedia = 0
if input == 1 and playingMedia == 0:
playingMedia = 1
player.play_item_at_index(1)
active +=1
nosubsPlayed +=1
print(playingMedia)
with open(str(date.today()))+'.txt','w' as file:
file.write("Active Views: " + active)
file.write("SubsPlayed: " + subsPlayed)
file.write("No Subs Played: " + nosubsPlayed)
Main()
So I figured out the solution, but not the problem's origin.
# Make my media paths into vlc.Media Objects
nosubs = vlc.Media(path+"Content-NoSubs.mp4")
subs = vlc.Media(path+"Content-Subtitles.mp4")
splash = vlc.Media(path+"SplashScreen.mp4")
#Setup the player
player = Instance.media_list_player_new()
Media_list = Instance.media_list_new()
Media_list.add_media(splash)
Media_list.add_media(subs)
Media_list.add_media(nosubs)
player.set_media_list(Media_list)
Media_list.lock()
Setting up each of the media by name in my list helps by switching the play function from play_item_at_index(int) to play_item(media)
Still not sure why it was kind of randomizing. My guess was that it changed the position of media in the list based on play through.
My next step will be to adjust this to work off of media_player and embedding playback into a tkinter window.

nonetype object not subscriptable pysimplegui

I'm trying to learn python and pysimplegui at the same time. Also I am old which doesn't help!
I am writing a practice program with my 10 year old son(blind leading the blind) and am running into a problem which i cant fix.
Basically the program lets you enter how many numbers to pick from and how many numbers to pick, then calculates the odds of winning. Hit generate to randomly pick the numbers for you and Print the results to a txt file for a record of your picks.
It all works fine but when i close the window i get a nonetype error which I can't work out.
Can any of ye genius's help?
This is the offending line
n=int(values['--tn--'])
from os import close
import random
from tkinter import Scrollbar
import PySimpleGUI as sg
import datetime
import math
from time import sleep, time
from PySimpleGUI.PySimpleGUI import Open, WIN_CLOSED, main
import sys
sg.theme('Reddit')
layout = [
[sg.In(size=(5,1),k="--tn--" ) ]+[sg.Text('Enter total amount of
numbers',size=(35,1))],
[sg.In(size=(5,1),k="--pn--")]+[sg.Text('Enter how many numbers
you are picking',size=(35,1))],
[sg.Text('Win odds')]+[sg.ML(background_color='light
coral',text_color='white',key='--oddout--',size=(50,2))],
[sg.ML(size=(20,30), key='--main--')],
[sg.Submit('Odds',key='--odds--')]+[sg.Submit('Generate',key='--
gen--')]+ [sg.Cancel('Cancel')]+[sg.Save(key='--save--')]+
[sg.CloseButton('Close',pad=(100,0))]
]
window = sg.Window('Lotto number generator',layout)
while True:
event, values = window.read()
n=int(values['--tn--'])
rr=int(values['--pn--'])
nf = math.factorial(n)
rf = math.factorial(rr)
winodds = (nf/(rf*math.factorial(n-rr)))
winodds = int(winodds)
now = datetime.datetime.now()
if event == WIN_CLOSED:
window['--tn--'].update('1')
break
if event == '--gen--':
r = random.sample(range(1,n),rr)
for i in r:
window['--main--'].print(i)
if event == '--odds--':
window['--oddout--'].print("Your chances of winning are
",f'{winodds:,d}', " to 1, Good Luck")
if event == 'Cancel':
window['--oddout--'].update('')
window['--tn--'].update('')
window['--pn--'].update('')
if event == '--save--':
sys.stdout = open("lotto.txt", "w")
print(values['--main--'])
sys.stdout=close(fd=0)
window.close()
event, values = window.read() is returning None. None['--tn--'] does not exist as it doesn't make sense for None to have a property, hence the error message. You have used the test to avoid this but moved it below an attempt to use the missing property. Hence the error.
It's also worth using a linting tool prompt you to make adjustments to syntax that will break your code and good practice warnings. I use pylint and flake8. The following addresses your specific error message with some tidying for the linter messages. There are still some warnings - good learning exercise :).
"""Learning program."""
from os import close
import random
import PySimpleGUI as sg
import datetime
import math
from PySimpleGUI.PySimpleGUI import Open, WIN_CLOSED, main
import sys
sg.theme('Reddit')
layout = [
[sg.In(size=(5, 1), k="--tn--")] +
[sg.Text('Enter total amount of numbers', size=(35, 1))],
[sg.In(size=(5, 1), k="--pn--")] +
[sg.Text('Enter how many numbers you are picking', size=(35, 1))],
[sg.Text('Win odds')] +
[sg.ML(
background_color='light coral', text_color='white', key='--oddout--', size=(50, 2)
)],
[sg.ML(size=(20, 30), key='--main--')],
[sg.Submit('Odds', key='--odds--')] +
[sg.Submit('Generate', key='--gen--')] +
[sg.Cancel('Cancel')] +
[sg.Save(key='--save--')] +
[sg.CloseButton('Close', pad=(100, 0))]
]
window = sg.Window('Lotto number generator', layout)
while True:
event, values = window.read()
# Moved the next three lines up and commented update which also errors
if event == WIN_CLOSED:
# window['--tn--'].update('1')
break
n = int(values['--tn--'])
rr = int(values['--pn--'])
nf = math.factorial(n)
rf = math.factorial(rr)
winodds = (nf/(rf*math.factorial(n-rr)))
winodds = int(winodds)
now = datetime.datetime.now()
if event == '--gen--':
r = random.sample(range(1, n), rr)
for i in r:
window['--main--'].print(i)
if event == '--odds--':
window['--oddout--'].print(
"Your chances of winning are", f'{winodds:,d}', " to 1, Good Luck"
)
if event == 'Cancel':
window['--oddout--'].update('')
window['--tn--'].update('')
window['--pn--'].update('')
if event == '--save--':
sys.stdout = open("lotto.txt", "w")
print(values['--main--'])
sys.stdout = close(fd=0)
window.close()
Flake8 in particular will prompt you to follow practices that don't have an obvious practical purpose. Later as you use more of the language the benefit of flake8 prompts are good habits that eventually pay large benefits.
There're something not good,
You should check the window close event first, not to processing event, values for other cases first, like following code. You may get event, values as None, None if not, then values['--tn--'] will be same as None['--tn--']. That's why you got TypeError: 'NoneType' object is not subscriptable.
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Close'):
break
# process other events from here
window.close()
In your input fields, values['--tn--'] or values['--pn--'] maybe not with correct format for integer number, so following code may get failure ValueError: invalid literal for int() with base 10
n=int(values['--tn--'])
rr=int(values['--pn--'])
Here's my way to avoid issue,
def integer(string):
try:
value = int(string)
except:
value = None
return value
for string in ("10.5", "", "10"):
value = integer(string)
if value is None:
print(f"{repr(string)} is not a legal integer string !")
else:
print(f"{repr(string)} converted to {value} !")
'10.5' is not a legal integer string !
'' is not a legal integer string !
'10' converted to 10 !
Basically, window destroied after you click close button X of window, so you should not update anything on it.
if event == WIN_CLOSED:
# window['--tn--'].update('1')
break
When you close a window, event and values are not set, see my example below.
While debugging, it's a good practice to print out the current values of event and values to be able to check whether you get what you thought you'd get, like this:
def test():
layout = [[sg.In(size=(5, 1), k="--tn--"), sg.Text('Enter total amount of numbers', size=(35, 1))],
[sg.In(size=(5, 1), k="--pn--"), sg.Text('Enter how many numbers you are picking', size=(35, 1))],
[sg.Text('Win odds'),
sg.ML(background_color='light coral', text_color='white', key='--oddout--', size=(50, 2))],
[sg.ML(size=(20, 30), key='--main--')],
[sg.Submit('Odds', key='--odds--'), sg.Submit('Generate', key='--gen--'),
sg.Cancel('Cancel'), sg.Save(key=' - -save - -'), sg.CloseButton('Close', pad=(100, 0))]
]
window = sg.Window('Lotto number generator', layout)
while True:
event, values = window.read()
print(f'event = {event}, values = {values}')
if event == WIN_CLOSED:
break
window.close()
When you close the window, you get
event = None, values = {'--tn--': None, '--pn--': None, '--oddout--': None, '--main--': None}
so, it is important to start your main loop with if event == WIN_CLOSED: (and break the loop in that case). Only after that, you can go on to process various events and values.

How can I make PySimpleGui read my input and update my window?

I am not sure if this has been answered before, sorry for a duplicate if it was, but I couldn't find it anywhere clearly.
I am making a GUI for my simple AIML chatbot (entertainment purposes mostly)
and I found PySimpleGui. I read the whole documents of it and been trying to use their code, implementing it into my own small code I got from a tutorial.
Originally:
kernel = aiml.Kernel()
kernel.learn("std-startup.xml")
kernel.respond("load aiml b")
while True:
input_text = input("You: ")
response = kernel.respond(input_text)
print("Csigusz Foxoup (bot): "+response)
I got this code working, all good (Thanks Misbah)
And I got my bot to say some words in the cmd accurately.
Next I wanted to add a simple gui.
I'd much rather it look more chatty but all I could come up with with my lacking coding experince is a simple window with 2 buttons and 2 texts.
The cood looks like this:
import aiml
import PySimpleGUI as sg
kernel = aiml.Kernel()
kernel.learn("std-startup.xml")
kernel.respond("load aiml b")
sg.theme('LightBlue 1')
layout = [[sg.Text('You: '), sg.Text(size=(12,1), key='-mytext-')],
[sg.Text('Csigusz Foxoup (bot): '), sg.Text(size=(12,1), key='-CSI-')],
[sg.Input(key='-myinput-')],
[sg.Button('Send message'), sg.Button('Bye!')]]
window = sg.Window('Csigusz Foxoup, your friend in a box (bot)', layout, [200,400])
while True:
event = window.read()
values = window.read()
if event == sg.WIN_CLOSED or event == 'Bye!':
break
if event == 'Send message':
# change the "output" element to be the value of "input" element
input_text = (values)
response = kernel.respond(input_text)
window['-mytext-'].update(values['-myinput-'])
print("Csigusz Foxoup(bot): "+response)
window.close()
And it produces a nice little window for me. looks like this
My problem is that when I type something, and click the buttons, nothing happens. When I press close window (X) I get an error message saying: "You have tried 100 times to read a closed window, you need to add a check for event == WIN_CLOSED, ERROR"
Now since i have a check, also a button, I have no idea why it doesnt work. Also don't know how I could get the button to send my bot the user text then retrieve the bot output.
What Am I doing wrong? Thank you for all replies in advance. All help greatly appreciated! 🙏
All your problem is that you use .read() in wrong way.
You have to use only one .read() which returns both values as tuple (event, values)
event, values = window.read()
print('event:', event)
print('values:', values)
Minimal working code (without aiml)
import PySimpleGUI as sg
sg.theme('LightBlue 1')
layout = [[sg.Text('You: '), sg.Text(size=(50,1), key='-mytext-')],
[sg.Text('Csigusz Foxoup (bot): '), sg.Text(size=(50,1), key='-CSI-')],
[sg.Input(key='-myinput-')],
[sg.Button('Send message'), sg.Button('Bye!')]]
window = sg.Window('Csigusz Foxoup, your friend in a box (bot)', layout, [200,400])
while True:
event, values = window.read()
print('event:', event)
print('values:', values)
if event == sg.WIN_CLOSED or event == 'Bye!':
break
if event == 'Send message':
input_text = values['-myinput-']
response = "some response for " + input_text
#response = kernel.respond(input_text)
window['-mytext-'].update(input_text)
window['-CSI-'].update(response)
window.close()

How to update a GUI window in Python?

My goal is to have a window with the latest quote of a stock updating during the day. I chose alpha_vantage as a quote source, pysimplegui to create the window and twisted to run a loop to update the window every minute. The code works as written, prints the correct quote and change, creates the window as desired, but the window does not update.
Why doesn't the window update?
from alpha_vantage.timeseries import TimeSeries
from twisted.internet import task, reactor
import PySimpleGUI as sg
def paintQuote():
quote, quote_meta = av.get_intraday(symbol='spy', interval = '1min')
last = quote.iloc[-1][3]
print('{0:6.2f}'.format(last))
change = (last / yesterday - 1) * 100
print('{0:4.2f}%'.format(change))
event, values = window.read()
window['quote'].update(last)
# window color
sg.theme('BluePurple')
# window layout
layout = [[sg.Text('last price', size=(20, 2), justification='center')],
[sg.Text(''), sg.Text(size=(24,1), key='quote')]]
# create window
window = sg.Window('MikeQuote', layout)
wait = 60.0
av = TimeSeries(key ='your_key', output_format = 'pandas')
yest, yest_meta = av.get_daily(symbol='spy')
yesterday = yest.iloc[-2][3]
loop = task.LoopingCall(paintQuote)
loop.start(wait)
reactor.run()
window.close()
Answer:
Your script is not calling paintQuote more than once. Add print lines in there and you'll see it never calls it more than once.
Suggested solutions:
I don't know much about that reactor or loopingCall thing or how it works. A simpler solution is just to use a while loop with a sleep in it. Here is my solution that seemed to work well:
import PySimpleGUI as sg
from alpha_vantage.timeseries import TimeSeries
import time
sg.theme('BluePurple')
layout = [[sg.Text('Last Price', size=(20, 2), justification='center')],
[sg.Text('', size=(10, 2), font=('Helvetica', 20),
justification='center', key='quote')]]
window = sg.Window('MikeQuote', layout)
av = TimeSeries(key = 'key')
spy, _ = av.get_quote_endpoint(symbol='SPY')
last = spy['05. price']
yest = spy['08. previous close']
wait = 1 # Wait is in seconds
while True:
event, values = window.read(timeout=10)
if event in (None, 'Quit'):
break
spy, _ = av.get_quote_endpoint(symbol='SPY')
last = spy['05. price']
window['quote'].update(last)
time.sleep(wait)
I added a few tweaks including:
Calling just the "GLOBAL_QUOTE" endpoint (so you're not returning the entire massive intraday dataset)
Remove twisted package for a simple while loop with a time.sleep function.
Added a 'Quit' event so it actually stop when you close the window.
Removed the paintQuote() function. I think clean code ideally would have this function not removed, but you can add it back in however you like.
Removed the pandas integration. You're not dealing with massive data manipulation so it's easier and faster to just use the JSON format.

Play a random sequence of 4 sounds while a video is played in Psychopy?

I'm trying to create an experiment using Psychopy.
In the specific I'm trying to create a routine ("trial") where a video ("movie1") is presented and at the same time I would like to play a sequence of 4 sounds (one per second) randomly chosen from a list of 10 in an excel file (sounds.routine.xlsx).
Here's what I have done so far:
from __future__ import absolute_import, division
from psychopy import locale_setup
from psychopy import prefs
from psychopy import sound, gui, visual, core, data, event, logging, clock
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
STOPPED, FINISHED, PRESSED, RELEASED, FOREVER)
import numpy as np # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle
import os # handy system and path functions
import sys # to get file system encoding
from psychopy.hardware import keyboard
# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(_thisDir)
# Store info about the experiment session
psychopyVersion = '3.2.4'
expName = 'dsffdsfads' # from the Builder filename that created this script
expInfo = {'participant': '', 'session': '001'}
dlg = gui.DlgFromDict(dictionary=expInfo, sortKeys=False, title=expName)
if dlg.OK == False:
core.quit() # user pressed cancel
expInfo['date'] = data.getDateStr() # add a simple timestamp
expInfo['expName'] = expName
expInfo['psychopyVersion'] = psychopyVersion
# Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
filename = _thisDir + os.sep + u'data/%s_%s_%s' % (expInfo['participant'], expName, expInfo['date'])
# An ExperimentHandler isn't essential but helps with data saving
thisExp = data.ExperimentHandler(name=expName, version='',
extraInfo=expInfo, runtimeInfo=None,
originPath='/Users/Documents/dsffdsfads.py',
savePickle=True, saveWideText=True,
dataFileName=filename)
# save a log file for detail verbose info
logFile = logging.LogFile(filename+'.log', level=logging.EXP)
logging.console.setLevel(logging.WARNING) # this outputs to the screen, not a file
endExpNow = False # flag for 'escape' or other condition => quit the exp
frameTolerance = 0.001 # how close to onset before 'same' frame
# Start Code - component code to be run before the window creation
# Setup the Window
win = visual.Window(
size=(1024, 768), fullscr=True, screen=0,
winType='pyglet', allowGUI=False, allowStencil=False,
monitor='testMonitor', color=[0,0,0], colorSpace='rgb',
blendMode='avg', useFBO=True,
units='height')
# store frame rate of monitor if we can measure it
expInfo['frameRate'] = win.getActualFrameRate()
if expInfo['frameRate'] != None:
frameDur = 1.0 / round(expInfo['frameRate'])
else:
frameDur = 1.0 / 60.0 # could not measure, so guess
# create a default keyboard (e.g. to check for escape)
defaultKeyboard = keyboard.Keyboard()
# Initialize components for Routine "trial"
trialClock = core.Clock()
sound1 = sound.Sound(Sounds, secs=-1, stereo=True, hamming=True,
name='sound1')
sound1.setVolume(1)
movie1 = visual.MovieStim3(
win=win, name='movie1',
noAudio = True,
filename='Movies/Random_4.mp4',
ori=0, pos=(0, 0), opacity=1,
loop=False,
depth=-1.0,
)
from np.random import choice
# Create some handy timers
globalClock = core.Clock() # to track the time since experiment started
routineTimer = core.CountdownTimer() # to track time remaining of each (non-slip) routine
# set up handler to look after randomisation of conditions etc
trials = data.TrialHandler(nReps=1, method='random',
extraInfo=expInfo, originPath=-1,
trialList=data.importConditions('../Desktop/Countingpuppet/sounds_routine.xlsx', selection=choice(10, size = 4, replace = False)),
seed=None, name='trials')
thisExp.addLoop(trials) # add the loop to the experiment
thisTrial = trials.trialList[0] # so we can initialise stimuli with some values
# abbreviate parameter names if possible (e.g. rgb = thisTrial.rgb)
if thisTrial != None:
for paramName in thisTrial:
exec('{} = thisTrial[paramName]'.format(paramName))
for thisTrial in trials:
currentLoop = trials
# abbreviate parameter names if possible (e.g. rgb = thisTrial.rgb)
if thisTrial != None:
for paramName in thisTrial:
exec('{} = thisTrial[paramName]'.format(paramName))
# ------Prepare to start Routine "trial"-------
# update component parameters for each repeat
sound1.setSound(Sounds, hamming=True)
sound1.setVolume(1, log=False)
# keep track of which components have finished
trialComponents = [sound1, movie1]
for thisComponent in trialComponents:
thisComponent.tStart = None
thisComponent.tStop = None
thisComponent.tStartRefresh = None
thisComponent.tStopRefresh = None
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
trialClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
continueRoutine = True
# -------Run Routine "trial"-------
while continueRoutine:
# get current time
t = trialClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=trialClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# start/stop sound1
if sound1.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
sound1.frameNStart = frameN # exact frame index
sound1.tStart = t # local t and not account for scr refresh
sound1.tStartRefresh = tThisFlipGlobal # on global time
sound1.play(when=win) # sync with win flip
# *movie1* updates
if movie1.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
movie1.frameNStart = frameN # exact frame index
movie1.tStart = t # local t and not account for scr refresh
movie1.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(movie1, 'tStartRefresh') # time at next scr refresh
movie1.setAutoDraw(True)
# check for quit (typically the Esc key)
if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
core.quit()
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in trialComponents:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "trial"-------
for thisComponent in trialComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
sound1.stop() # ensure sound has stopped at end of routine
trials.addData('sound1.started', sound1.tStartRefresh)
trials.addData('sound1.stopped', sound1.tStopRefresh)
trials.addData('movie1.started', movie1.tStartRefresh)
trials.addData('movie1.stopped', movie1.tStopRefresh)
# the Routine "trial" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
thisExp.nextEntry()
# completed 1 repeats of 'trials'
# Flip one final time so any remaining win.callOnFlip()
# and win.timeOnFlip() tasks get executed before quitting
win.flip()
# these shouldn't be strictly necessary (should auto-save)
thisExp.saveAsWideText(filename+'.csv')
thisExp.saveAsPickle(filename)
logging.flush()
# make sure everything is closed down
thisExp.abort() # or data files will save again on exit
win.close()
core.quit()
The problem is that using np.choice only one number is reproduced and not the entire sequence of four randomly chosen numbers without repetitions. How can I do this?
Thanks in advance
Not tested, but something like this:
FPS = 60 # Frame rate of your monitor
from random import choice
from psychopy import visual, sound
win = visual.Window()
movie = visual.MovieStim(win, 'my_file.avi')
sounds = [sound.Sound('sound1.wav'), sound.Sound('sound2.wav'), sound.Sound('sound3.wav'), sound.Sound('sound4.wav')]
frame = 1
while movie.status != visual.FINISHED:
movie.draw() # Show the next frame of the movie
if frame % FPS == 0: # If a second has passed
choice(sounds).play() # Play a random sound

Categories