Hi All,
I wrote a simple script for rating pictures, but the task freezes when I run it for about 20 minutes. I was wondering if anyone might have suggestions for fixing this issue:
# Last updated: Feb. 8, 2020
# Author: Arkadiy
#!/usr/bin/env python
# -*- coding: utf-8 -*-
########################################################################################
# Conditions: PS*TS*P*S*NS*TS*P*S*NS*NS*TS*P*S*NS*NS*NS*TS*P*S*
# 30 Trials
# 4-6 squares in between each stimulus
# Each Each stimulus: 1 second
# ISI: 175
########################################################################################
# Important all the important libraries, without which the experiment would not work.
from __future__ import absolute_import, division
import os
import pandas as pd
import decimal
from psychopy import locale_setup, prefs, gui, visual, core, data, event, logging, clock
import numpy as np
import sys
from psychopy.iohub import launchHubServer
from psychopy.core import getTime
from psychopy.hardware import keyboard
import time
from datetime import datetime
import random
from random import choices, shuffle
# Set the Path for stimuli
Stim_Path = '/Users/Ratings/'
# Store info about the experiment session
psychopyVersion = '3.2.4'
expName = 'testing' # from the Builder filename that created this script
expInfo = {'Study Pool ID': '', '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
endExpNow = False # flag for 'escape' or other condition => quit the exp
frameTolerance = 0.001 # how close to onset before 'same' frame
# 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')
# Make the cursor disappear
win.mouseVisible = False
# 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
# Initialize components for Routine "trial"
clock = core.Clock()
# Show Text
def Text(x):
c = None
while c==None: # loop until a key is pressed
message = visual.TextStim(win, text=str(x), pos=(0.0, 0.0), height=0.05)
message.draw()
win.flip()
core.wait(2)
c = event.waitKeys(keyList=['space', 'Esc'])
if 'Esc' in c:
core.quit()
# fixation cross
fixation = visual.ShapeStim(win,
vertices=((0, -0.05), (0, 0.05), (0,0), (-0.05,0), (0.05, 0)),
lineWidth=10,
closeShape=False,
lineColor="white")
d = {'ID': [], 'Date': [], 'Time': [], 'Session': [], 'Trial_Number': [],'Stimulus': [],'Response_Type': [],'Reaction_Time': [], 'choiceHistory':[]}
# Create a list from Pics
Pic_List = []
for root, dirs, files in os.walk(Stim_Path):
for file in files:
if file.endswith('.jpg') and not file.startswith('Pract') :
Pic_List.append(Stim_Path + file)
# Shuffle the list
np.random.shuffle(Pic_List)
# Include Some Info about the experiment
d['ID'].append(('ID'))
d['Date'].append((time.strftime("%m/%d/%Y")))
d['Time'].append((time.strftime("%H:%M")))
d['Session'].append(('Encoding'))
OldNewScale = visual.RatingScale(win, choices=['Old High', 'Old Medium', 'Old Low', 'New Low', 'New Medium', 'New High'], markerStart=2.5, acceptKeys=['space'], leftKeys='left', rightKeys='right', pos= (0.0, -0.5), disappear=True, noMouse=True, skipKeys=None, size = 1, stretch= 2.5, acceptSize=1)
# Present the Pics and store responses
def Present_and_Record():
trial_count = 0
for stim in Pic_List:
Response = []
Pic_to_Draw = visual.ImageStim(win, image=str(stim), pos=(0.0, 0.1))
clock.reset()
while OldNewScale.noResponse:
Pic_to_Draw.draw()
OldNewScale.draw()
line= visual.Line(win, start=(0.0, -0.32), end=(0.0, -0.15), lineWidth=6, ori=0.5)
line.draw()
message = visual.TextStim(win, text=str('Old'), pos=(-0.19, -0.18), height=0.05)
message.draw()
message = visual.TextStim(win, text=str('New'), pos=(0.19, -0.18), height=0.05)
message.draw()
win.flip()
Response = OldNewScale.getRating()
trial_count = trial_count + 1
decisionTime = OldNewScale.getRT()
d['Stimulus'].append((stim))
d['Trial_Number'].append((trial_count))
d['Response_Type'].append((Response))
d['Reaction_Time'].append((decisionTime))
OldNewScale.reset() # reset between repeated uses of the same rating scale
Response = []
Text('Thank you for your participation! You are all done. Please get the experimenter to let him know that you are finished.')
# Record Responses
return(d)
#a = Practice()
d= Present_and_Record()
# Save Dict as a Df
df= pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in d.items() ]))
# Export df to CSV
df.to_csv('/Users/Ratings.csv', index = None, header=True)
# make sure everything is closed down
win.close()
core.quit()