Skip to content

Commit

Permalink
ENH: allow saving of data without needing a loop; see psychopy#71
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremygray committed Mar 2, 2014
1 parent 30e2a59 commit 5668548
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
10 changes: 8 additions & 2 deletions psychopy/app/builder/components/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from os import path

from psychopy.app.builder.experiment import CodeGenerationException, _valid_var_re
from psychopy.app.builder.experiment import TrialHandler

thisFolder = path.abspath(path.dirname(__file__))#the absolute path to the folder containing this path
iconFile = path.join(thisFolder,'keyboard.png')
Expand Down Expand Up @@ -177,10 +178,13 @@ def writeRoutineEndCode(self,buff):
store=self.params['store'].val
if len(self.exp.flow._loopList):
currLoop=self.exp.flow._loopList[-1]#last (outer-most) loop
else: currLoop=None
else:
# dummy TrialHandler, same name as the ExperimentHandler that will be created in the exp script:
currLoop = TrialHandler(self.exp, name='thisExp')
currLoop.type = 'ExperimentHandler' # displayed in a code comment

#write the actual code
if (store!='nothing') and currLoop:#need a loop to do the storing of data!
if store != 'nothing': # there's always a loop: user defined or thisExp (implicitly)
buff.writeIndented("# check responses\n" %self.params)
buff.writeIndented("if %(name)s.keys in ['', [], None]: # No response was made\n"%self.params)
buff.writeIndented(" %(name)s.keys=None\n" %(self.params))
Expand All @@ -205,3 +209,5 @@ def writeRoutineEndCode(self,buff):
buff.writeIndented("if %(name)s.keys != None: # we had a response\n" %(self.params))
buff.writeIndented(" %s.addData('%s.rt', %s.rt)\n" \
%(currLoop.params['name'], name, name))
if currLoop.params['name'].val == 'thisExp':
buff.writeIndented("thisExp.nextEntry()\n")
26 changes: 17 additions & 9 deletions psychopy/app/builder/components/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from _base import *
from os import path
from psychopy.app.builder.experiment import Param
from psychopy.app.builder.experiment import Param, TrialHandler

thisFolder = path.abspath(path.dirname(__file__))#the absolute path to the folder containing this path
iconFile = path.join(thisFolder,'mouse.png')
Expand Down Expand Up @@ -136,14 +136,20 @@ def writeRoutineEndCode(self,buff):
name = self.params['name']
store = self.params['saveMouseState'].val#do this because the param itself is not a string!
forceEnd = self.params['forceEndRoutineOnPress'].val
#check if we're in a loop (so saving is possible)
if len(self.exp.flow._loopList):
currLoop=self.exp.flow._loopList[-1]#last (outer-most) loop
else: currLoop=None
if store!='nothing' and currLoop and currLoop.type=='StairHandler':
buff.writeIndented("# NB PsychoPy doesn't handle a 'correct answer' for mouse events so doesn't know how to handle mouse with StairHandler")
if store == 'final' and currLoop!=None:
buff.writeIndented("# get info about the %(name)s\n" %(self.params))
else:
# dummy TrialHandler, same name as the ExperimentHandler that will be created in the exp script:
currLoop = TrialHandler(self.exp, name='thisExp')
currLoop.type = 'ExperimentHandler' # displayed in a code comment

if store != 'nothing':
if currLoop.type=='StairHandler':
buff.writeIndented("# NB PsychoPy doesn't handle a 'correct answer' for mouse events so doesn't know how to handle mouse with StairHandler")
else:
buff.writeIndented("# store data for %s (%s)\n" %(currLoop.params['name'], currLoop.type))
if store == 'final':
#buff.writeIndented("# get info about the %(name)s\n" %(self.params))
buff.writeIndented("x, y = %(name)s.getPos()\n" %(self.params))
buff.writeIndented("buttons = %(name)s.getPressed()\n" %(self.params))
if currLoop.type!='StairHandler':
Expand All @@ -152,11 +158,13 @@ def writeRoutineEndCode(self,buff):
buff.writeIndented("%s.addData('%s.leftButton', buttons[0])\n" %(currLoop.params['name'], name))
buff.writeIndented("%s.addData('%s.midButton', buttons[1])\n" %(currLoop.params['name'], name))
buff.writeIndented("%s.addData('%s.rightButton', buttons[2])\n" %(currLoop.params['name'], name))
elif store != 'never' and currLoop!=None:
buff.writeIndented("# save %(name)s data\n" %(self.params))
elif store != 'never':
#buff.writeIndented("# save %(name)s data\n" %(self.params))
for property in ['x','y','leftButton','midButton','rightButton','time']:
if store=='every frame' or not forceEnd:
buff.writeIndented("%s.addData('%s.%s', %s.%s)\n" %(currLoop.params['name'], name,property,name,property))
else:
#we only had one click so don't return a list
buff.writeIndented("%s.addData('%s.%s', %s.%s[0])\n" %(currLoop.params['name'], name,property,name,property))
if store != 'nothing' and currLoop.params['name'].val == 'thisExp':
buff.writeIndented("thisExp.nextEntry()\n")

0 comments on commit 5668548

Please sign in to comment.