Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- This experiment was created using PsychoPy3 Experiment Builder (v3.1.5),
- on Tue Sep 24 13:58:42 2019
- If you publish work using this script please cite the PsychoPy publications:
- Peirce, JW (2007) PsychoPy - Psychophysics software in Python.
- Journal of Neuroscience Methods, 162(1-2), 8-13.
- Peirce, JW (2009) Generating stimuli for neuroscience using PsychoPy.
- Frontiers in Neuroinformatics, 2:10. doi: 10.3389/neuro.11.010.2008
- """
- from __future__ import absolute_import, division
- from psychopy import locale_setup, 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.1.5'
- expName = 'LOLTEMPREM' # 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/lowe/Documents/Programmering/Python/PsychoPy projects/My own practice projects/LOLTEMPREM.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
- # Start Code - component code to be run before the window creation
- # Setup the Window
- win = visual.Window(
- size=[1280, 800], 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()
- polygon = visual.ShapeStim(
- win=win, name='polygon', units='deg',
- vertices=[[-(3, 3)[0] / 2.0, -(3, 3)[1] / 2.0], [+(3, 3)[0] / 2.0, -(3, 3)[1] / 2.0], [0, (3, 3)[1] / 2.0]],
- ori=0, pos=(0, 0),
- lineWidth=1, lineColor=[1, 1, 1], lineColorSpace='rgb',
- fillColor=[1, 1, 1], fillColorSpace='rgb',
- opacity=1, depth=0.0, interpolate=True)
- mouse = event.Mouse(win=win)
- x, y = [None, None]
- mouse.mouseClock = core.Clock()
- # 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
- # ------Prepare to start Routine "trial"-------
- t = 0
- trialClock.reset() # clock
- frameN = -1
- continueRoutine = True
- # update component parameters for each repeat
- # setup some python lists for storing info about the mouse
- mouse.x = []
- mouse.y = []
- mouse.leftButton = []
- mouse.midButton = []
- mouse.rightButton = []
- mouse.time = []
- mouse.clicked_name = []
- gotValidClick = False # until a click is received
- # keep track of which components have finished
- trialComponents = [polygon, mouse]
- for thisComponent in trialComponents:
- thisComponent.tStart = None
- thisComponent.tStop = None
- thisComponent.tStartRefresh = None
- thisComponent.tStopRefresh = None
- if hasattr(thisComponent, 'status'):
- thisComponent.status = NOT_STARTED
- # -------Start Routine "trial"-------
- while continueRoutine:
- # get current time
- t = trialClock.getTime()
- frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
- # update/draw components on each frame
- # *polygon* updates
- if t >= 0.0 and polygon.status == NOT_STARTED:
- # keep track of start time/frame for later
- polygon.tStart = t # not accounting for scr refresh
- polygon.frameNStart = frameN # exact frame index
- win.timeOnFlip(polygon, 'tStartRefresh') # time at next scr refresh
- polygon.setAutoDraw(True)
- # *mouse* updates
- if t >= 0.0 and mouse.status == NOT_STARTED:
- # keep track of start time/frame for later
- mouse.tStart = t # not accounting for scr refresh
- mouse.frameNStart = frameN # exact frame index
- win.timeOnFlip(mouse, 'tStartRefresh') # time at next scr refresh
- mouse.status = STARTED
- mouse.mouseClock.reset()
- prevButtonState = mouse.getPressed() # if button is down already this ISN'T a new click
- if mouse.status == STARTED: # only update if started and not finished!
- buttons = mouse.getPressed()
- if buttons != prevButtonState: # button state changed?
- prevButtonState = buttons
- if sum(buttons) > 0: # state changed to a new click
- # check if the mouse was inside our 'clickable' objects
- gotValidClick = False
- for obj in [polygon]:
- if obj.contains(mouse):
- gotValidClick = True
- mouse.clicked_name.append(obj.name)
- x, y = mouse.getPos()
- mouse.x.append(x)
- mouse.y.append(y)
- buttons = mouse.getPressed()
- mouse.leftButton.append(buttons[0])
- mouse.midButton.append(buttons[1])
- mouse.rightButton.append(buttons[2])
- mouse.time.append(mouse.mouseClock.getTime())
- if gotValidClick: # abort routine on response
- 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 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)
- thisExp.addData('polygon.started', polygon.tStartRefresh)
- thisExp.addData('polygon.stopped', polygon.tStopRefresh)
- # store data for thisExp (ExperimentHandler)
- if len(mouse.x): thisExp.addData('mouse.x', mouse.x[0])
- if len(mouse.y): thisExp.addData('mouse.y', mouse.y[0])
- if len(mouse.leftButton): thisExp.addData('mouse.leftButton', mouse.leftButton[0])
- if len(mouse.midButton): thisExp.addData('mouse.midButton', mouse.midButton[0])
- if len(mouse.rightButton): thisExp.addData('mouse.rightButton', mouse.rightButton[0])
- if len(mouse.time): thisExp.addData('mouse.time', mouse.time[0])
- if len(mouse.clicked_name): thisExp.addData('mouse.clicked_name', mouse.clicked_name[0])
- thisExp.addData('mouse.started', mouse.tStart)
- thisExp.addData('mouse.stopped', mouse.tStop)
- thisExp.nextEntry()
- # the Routine "trial" was not non-slip safe, so reset the non-slip timer
- routineTimer.reset()
- # 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement