psychopy.org | Reference | Downloads | Github

Specifying trial/loop number within a code component

I’m building an experiment in which participants (verbally) name pictures presented onscreen, and their responses are detected by a nearby microphone. I have a code component which saves each response as a separate audio file:

vpvk = vk.OnsetVoiceKey(
sec=10
file_out = english_word + ‘.wav’)

**note the pictures are pulled from an excel file, and ‘english_word’ is just the label given to each picture.

My problem is that I have multiple repetitions of each picture within my loop, meaning that when a picture is repeated, the previous audio file for that picture is overridden. Is there a way to specify the repetition/trial number for the output file, so that a separate file is generated for each iteration of each picture? Any help would be appreciated!

Check the API here for the TrialHandler class, which represents the loop in Builder:
http://www.psychopy.org/api/data.html

They have attributes for the current trial number. So do something like this:

file_out = english_word + str(name_of_your_loop.thisN) + '.wav')

Hi Michael,

Thanks for the help! Unfortunately I have multiple loops (with different names) which all use the same routine (and hence, the same lines of code). Is it possible to do what you suggest in a way that generalises to all of my loops?

Regards
Lyam

Hi Lyam,

Look into this post and where it links to:

Hi Michael,

Thanks for this. I’ve altered my code to include:

if practiceTrials.finished:
thisLoop = trials
else:
thisLoop = practiceTrials

However when I try to run it, Psychopy complains that “practiceTrials is not defined”. Any suggestions on how to correct this?

And the error message suggests, you need to make sure that the routine is within a loop called practiceTrials.

Your previous messages suggest that you’re wanting a way to refer to a generic loop name instead of a specific one, though. It’s hard to give advice without you telling us exactly what you are doing, and ideally giving a screenshot of your flow panel.

This sort of question suggests you would benefit from the workshop!

http://www.psychopy.org/resources/workshops.html#bep

Hi all,

Thanks again for the input. Fortunately I found a workaround to my problem. Technically I didn’t really need to specify thisN, I just needed a way to distinguish between each individual trial. I ended up adding str(core.getTime()) to my output file path, so that now each trial generates a unique filename. It’s not very elegant but it works :slight_smile: