Is there a way to add an ISI after a movie ends (the times vary)? Since ISI does not have the option for conditions I was not able to set the code to ex: $movie.status==FINISHED.
The easiest thing to do is just split your trial across routines. i.e. the first routine will have an indefinite duration and just end whenever the movie ends. Then the second routine will start immediately afterwards, with no code required. Just insert some sort of stimulus there (which might even be invisible) and give it a variable duration as required.
Thank you for your response Michael, I wanted to get some clarification. I’ll explain my project in greater detail first. I want to show a video clip and then a sound byte. My desired ISI in between the video clip and the audio file is .05ms. So the order of my entire experiment is:
- Fixation Text File
- Video Clip
- .05 ms ISI
- Sound Clip
- Response Text File
- Key Response
Then I want this process to repeat a hundred times with varying video clips and sound bytes that have been ordered an paired in an excel.
I have included screen shots and my code. I appreciate your time and help!
"""
This experiment was created using PsychoPy3 Experiment Builder (v3.2.4),
on Mon Feb 24 11:47:35 2020
If you publish work using this script the most relevant publication is:
Peirce J, Gray JR, Simpson S, MacAskill M, Höchenberger R, Sogo H, Kastman E, Lindeløv JK. (2019)
PsychoPy2: Experiments in behavior made easy Behav Res 51: 195.
https://doi.org/10.3758/s13428-018-01193-y
"""
from __future__ import absolute_import, division
from psychopy import locale_setup
from psychopy import prefs
prefs.hardware['audioLatencyMode'] = '4'
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 = 'untitled' # 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/rasal4/Desktop/Testing Primes and Targets/Primes_Target/Mp4 Files/untitled.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=[2560, 1440], fullscr=True, screen=0,
winType='pyglet', allowGUI=False, allowStencil=False,
monitor='testMonitor', color=[-1,-1,-1], 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()
text = visual.TextStim(win=win, name='text',
text='*',
font='Arial',
pos=(0, 0), height=0.1, wrapWidth=None, ori=0,
color='white', colorSpace='rgb', opacity=1,
languageStyle='LTR',
depth=0.0);
# Initialize components for Routine "trial_2"
trial_2Clock = core.Clock()
ISI_2 = clock.StaticPeriod(win=win, screenHz=expInfo['frameRate'], name='ISI_2')
text_3 = visual.TextStim(win=win, name='text_3',
text=None,
font='Arial',
pos=(0, 0), height=0.1, wrapWidth=None, ori=0,
color='black', colorSpace='rgb', opacity=1,
languageStyle='LTR',
depth=-1.0);
# Initialize components for Routine "trial_3"
trial_3Clock = core.Clock()
sound_1 = sound.Sound('A', secs=-1, stereo=True, hamming=True,
name='sound_1')
sound_1.setVolume(1)
text_2 = visual.TextStim(win=win, name='text_2',
text='Word Vs NonWord',
font='Arial',
pos=(0, 0), height=0.1, wrapWidth=None, ori=0,
color='white', colorSpace='rgb', opacity=1,
languageStyle='LTR',
depth=-1.0);
key_resp = keyboard.Keyboard()
# 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=5, method='sequential',
extraInfo=expInfo, originPath=-1,
trialList=[None],
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
movie = visual.MovieStim3(
win=win, name='movie',
noAudio = False,
filename='/Users/rasal4/Desktop/Testing Primes and Targets/Primes_Target/Mp4 Files/Dance.mov',
ori=0, pos=(0, 0), opacity=1,
loop=False,
size=[1800,1200],
depth=-1.0,
)
# keep track of which components have finished
trialComponents = [text, movie]
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
# *text* updates
if text.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
text.frameNStart = frameN # exact frame index
text.tStart = t # local t and not account for scr refresh
text.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(text, 'tStartRefresh') # time at next scr refresh
text.setAutoDraw(True)
if text.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > text.tStartRefresh + 1.0-frameTolerance:
# keep track of stop time/frame for later
text.tStop = t # not accounting for scr refresh
text.frameNStop = frameN # exact frame index
win.timeOnFlip(text, 'tStopRefresh') # time at next scr refresh
text.setAutoDraw(False)
# *movie* updates
if movie.status == NOT_STARTED and text.status==FINISHED:
# keep track of start time/frame for later
movie.frameNStart = frameN # exact frame index
movie.tStart = t # local t and not account for scr refresh
movie.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(movie, 'tStartRefresh') # time at next scr refresh
movie.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)
trials.addData('text.started', text.tStartRefresh)
trials.addData('text.stopped', text.tStopRefresh)
trials.addData('movie.started', movie.tStartRefresh)
trials.addData('movie.stopped', movie.tStopRefresh)
# the Routine "trial" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
# ------Prepare to start Routine "trial_2"-------
routineTimer.add(0.050000)
# update component parameters for each repeat
# keep track of which components have finished
trial_2Components = [ISI_2, text_3]
for thisComponent in trial_2Components:
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")
trial_2Clock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
continueRoutine = True
# -------Run Routine "trial_2"-------
while continueRoutine and routineTimer.getTime() > 0:
# get current time
t = trial_2Clock.getTime()
tThisFlip = win.getFutureFlipTime(clock=trial_2Clock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *text_3* updates
if text_3.status == NOT_STARTED and tThisFlip >= 0-frameTolerance:
# keep track of start time/frame for later
text_3.frameNStart = frameN # exact frame index
text_3.tStart = t # local t and not account for scr refresh
text_3.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(text_3, 'tStartRefresh') # time at next scr refresh
text_3.setAutoDraw(True)
if text_3.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > text_3.tStartRefresh + 0.05-frameTolerance:
# keep track of stop time/frame for later
text_3.tStop = t # not accounting for scr refresh
text_3.frameNStop = frameN # exact frame index
win.timeOnFlip(text_3, 'tStopRefresh') # time at next scr refresh
text_3.setAutoDraw(False)
# *ISI_2* period
if ISI_2.status == NOT_STARTED and t >= 0.0-frameTolerance:
# keep track of start time/frame for later
ISI_2.frameNStart = frameN # exact frame index
ISI_2.tStart = t # local t and not account for scr refresh
ISI_2.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(ISI_2, 'tStartRefresh') # time at next scr refresh
ISI_2.start(0.05)
elif ISI_2.status == STARTED: # one frame should pass before updating params and completing
ISI_2.complete() # finish the static period
# 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 trial_2Components:
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_2"-------
for thisComponent in trial_2Components:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
trials.addData('ISI_2.started', ISI_2.tStart)
trials.addData('ISI_2.stopped', ISI_2.tStop)
trials.addData('text_3.started', text_3.tStartRefresh)
trials.addData('text_3.stopped', text_3.tStopRefresh)
# ------Prepare to start Routine "trial_3"-------
# update component parameters for each repeat
sound_1.setSound('/Users/rasal4/Desktop/Testing Primes and Targets/Primes_Target/Mp4 Files/Croob.wav', hamming=True)
sound_1.setVolume(1, log=False)
key_resp.keys = []
key_resp.rt = []
# keep track of which components have finished
trial_3Components = [sound_1, text_2, key_resp]
for thisComponent in trial_3Components:
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")
trial_3Clock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
continueRoutine = True
# -------Run Routine "trial_3"-------
while continueRoutine:
# get current time
t = trial_3Clock.getTime()
tThisFlip = win.getFutureFlipTime(clock=trial_3Clock)
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 sound_1
if sound_1.status == NOT_STARTED and ISI_2.status==FINISHED:
# keep track of start time/frame for later
sound_1.frameNStart = frameN # exact frame index
sound_1.tStart = t # local t and not account for scr refresh
sound_1.tStartRefresh = tThisFlipGlobal # on global time
sound_1.play(when=win) # sync with win flip
# *text_2* updates
if text_2.status == NOT_STARTED and sound_1.status==FINISHED:
# keep track of start time/frame for later
text_2.frameNStart = frameN # exact frame index
text_2.tStart = t # local t and not account for scr refresh
text_2.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(text_2, 'tStartRefresh') # time at next scr refresh
text_2.setAutoDraw(True)
# *key_resp* updates
waitOnFlip = False
if key_resp.status == NOT_STARTED and sound_1.status==FINISHED:
# keep track of start time/frame for later
key_resp.frameNStart = frameN # exact frame index
key_resp.tStart = t # local t and not account for scr refresh
key_resp.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(key_resp, 'tStartRefresh') # time at next scr refresh
key_resp.status = STARTED
# keyboard checking is just starting
waitOnFlip = True
win.callOnFlip(key_resp.clock.reset) # t=0 on next screen flip
win.callOnFlip(key_resp.clearEvents, eventType='keyboard') # clear events on next screen flip
if key_resp.status == STARTED and not waitOnFlip:
theseKeys = key_resp.getKeys(keyList=['y', 'n'], waitRelease=False)
if len(theseKeys):
theseKeys = theseKeys[0] # at least one key was pressed
# check for quit:
if "escape" == theseKeys:
endExpNow = True
key_resp.keys = theseKeys.name # just the last key pressed
key_resp.rt = theseKeys.rt
# a response ends the routine
continueRoutine = False
# 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 trial_3Components:
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_3"-------
for thisComponent in trial_3Components:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
sound_1.stop() # ensure sound has stopped at end of routine
trials.addData('sound_1.started', sound_1.tStartRefresh)
trials.addData('sound_1.stopped', sound_1.tStopRefresh)
trials.addData('text_2.started', text_2.tStartRefresh)
trials.addData('text_2.stopped', text_2.tStopRefresh)
# check responses
if key_resp.keys in ['', [], None]: # No response was made
key_resp.keys = None
trials.addData('key_resp.keys',key_resp.keys)
if key_resp.keys != None: # we had a response
trials.addData('key_resp.rt', key_resp.rt)
trials.addData('key_resp.started', key_resp.tStartRefresh)
trials.addData('key_resp.stopped', key_resp.tStopRefresh)
# the Routine "trial_3" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
thisExp.nextEntry()
# completed 5 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()
Thanks, you have provided a lot of detail, but haven’t said what the actual problem is?
Can you verify that is the actual value?
We ran it the way it is currently constructed and we recorded the audio of the trial independently as it ran, and tried to verify if we were actually getting an ISI of .05 seconds. It actually measured out to be double that. It was a full second of silence in between our video and sound clip. We are surmising that there is extra silence being added in between the routines. You said earlier they should start immediately after each other? But that would not explain why we have an extra half a second added to our ISI. It should be .05 seconds in between each stimuli, but we are ending up with a second plus.
@jon any ideas?