psychopy.org | Reference | Downloads | Github

Setup UUID4 as the participant name

I have the following code to generate a UUID for each participant:
import uuid
uuid.uuid4()
print (“The random id is : “,end=””)
print (uuid.uuid4())

I would like to attach this ID to all the questionnaires the participant completes. I think if I add it as the participant name, that would work fine. So how do I code the UUID to be input into the following code for the participant name?

expInfo = {‘participant’: ‘id’, ‘session’: ‘001’}

I need the ‘id’ to be the UUID4 but it will not accept it. In this way, the participant number is coded randomly and stored with that participant’s data.

You need to assign the result of the uuid4() function to a variable so you can use it again (perhaps explicitly converting it to a string to avoid type problems). e.g.

id = str(uuid.uuid4())
expInfo = {'participant': id, 'session': '001'}

or if you don’t need to have a persistent reference to it (because it’s now stored in expInfo anyway), just assign it there directly:

expInfo = {'participant': str(uuid.uuid4()), 'session': '001'}

Dear Michael:

Thanks, that’s perfect. I’ll try it out.

Dear Michael:

  I am able to get the uuid to work in the Code view but I don't

know how to get it into the Builder View. So, every time I run
from Builder View, it overwrites the code. Is there a way to add
code to the Experiment Setting pop-up window?

You shouldn’t edit the generated script: as you know, it gets regenerated after each change. The way to use custom code within Builder is by inserting a code component from the “custom” component panel section:

https://www.psychopy.org/builder/components/code.html

Code set to run at the beginning of the experiment will run after the initial dialog box, so you should change it to just alter a single entry of expInfo, not to over-write the whole dictionary completely. e.g. something like:

expInfo['participant'] = str(uuid.uuid4())

Dear Michael:

  I did try that but the Participant id comes up blank every time.

I’m not sure why.

(Attachment pEpkey.asc is missing)

(Attachment pEpkey.asc is missing)