Skip to content

Commit

Permalink
Merge pull request psychopy#6907 from TEParsons/dev-enh-save-audio
Browse files Browse the repository at this point in the history
NF: Allow Mic and Camera Components to be automatically saved when experiment data saves
  • Loading branch information
peircej authored Oct 11, 2024
2 parents 6922b9b + 352b4c4 commit d5a0239
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
28 changes: 28 additions & 0 deletions psychopy/data/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def __init__(self,
self.status = constants.NOT_STARTED
# dict of filenames to collision method to be used next time it's saved
self._nextSaveCollision = {}
# list of call profiles for connected save methods
self.connectedSaveMethods = []

if dataFileName in ['', None]:
logging.warning('ExperimentHandler created with no dataFileName'
Expand Down Expand Up @@ -633,6 +635,29 @@ def queueNextCollision(self, fileCollisionMethod, fileName=None):
# queue collision
self._nextSaveCollision[fileName] = fileCollisionMethod

def connectSaveMethod(self, fcn, *args, **kwargs):
"""
Tell this experiment handler to call the given function with the given arguments and
keyword arguments whenever it saves its own data.
Parameters
----------
fcn : function
Function to call
*args
Positional arguments to be given to the function when it's called
**kwargs
Keyword arguments to be given to the function when it's called
"""
# create a call profile for the given function
profile = {
'fcn': fcn,
'args': args,
'kwargs': kwargs
}
# connect it
self.connectedSaveMethods.append(profile)

def save(self):
"""
Work out from own settings how to save, then use the appropriate method (saveAsWideText,
Expand All @@ -651,6 +676,9 @@ def save(self):
logging.warn(
"ExperimentHandler.save was called on an ExperimentHandler with no dataFileName set."
)
# call connected save functions
for profile in self.connectedSaveMethods:
profile['fcn'](*profile['args'], **profile['kwargs'])

return savedName

Expand Down
2 changes: 2 additions & 0 deletions psychopy/experiment/components/camera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ def writeInitCode(self, buff):
code = (
"# get camera object\n"
"%(name)s = deviceManager.getDevice(%(deviceLabel)s)\n"
"# connect camera save method to experiment handler so it's called when data saves\n"
"thisExp.connectSaveMethod(%(name)s.save)\n"
)
buff.writeIndentedLines(code % inits)

Expand Down
2 changes: 2 additions & 0 deletions psychopy/experiment/components/microphone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ def writeInitCode(self, buff):
"# tell the experiment handler to save this Microphone's clips if the experiment is "
"force ended\n"
"runAtExit.append(%(name)s.saveClips)\n"
"# connect camera save method to experiment handler so it's called when data saves\n"
"thisExp.connectSaveMethod(%(name)s.saveClips)\n"
)
buff.writeIndentedLines(code % inits)

Expand Down

0 comments on commit d5a0239

Please sign in to comment.