expInfo['date'] value changing between script start and experiment start in v2025.2.4

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Win11
PsychoPy version (e.g. 2024.2.4 Py 3.8): 2025.2.4
Standard Standalone Installation? (y/n) If not then what?: y
Do you want it to also run online? (y/n) n
What are you trying to achieve?: use expInfo[‘date’] for output folder naming

What specifically went wrong when you tried that?:
I want to use expInfo[‘date’] to name the output folder for each study participant and also for additional output files created by my script. I noticed that Psychopy 2025.2.4 updates expInfo[‘date’] after its initial use for the output folder naming (see code snippet below), making the additional files naming that uses the later updated expInfo[‘date’] to have different naming schemes.

How can I generate a stable timestamp to name not only the output folder but also any files created later by the script?

code snippet:

# data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
    if dataDir is None:
        dataDir = _thisDir
    filename = u'data/%s_%s/%s_%s_%s_%s' % (expInfo['participant'], expInfo['Visit'], expInfo['participant'], expInfo['Visit'], expInfo['date'], expName)
    # make sure filename is relative to dataDir
    if os.path.isabs(filename):
        dataDir = os.path.commonprefix([dataDir, filename])
        filename = os.path.relpath(filename, dataDir)
# mark experiment as started
    thisExp.status = STARTED
    # update experiment info
    expInfo['date'] = data.getDateStr()
    expInfo['expName'] = expName
    expInfo['expVersion'] = expVersion
    expInfo['psychopyVersion'] = psychopyVersion

I’m a bit confused about what you are trying to do.

I’ve just made a simple edit to the data folder in a test experiment.

u'%s/%s_%s_%s' % (expInfo['date'], expInfo['participant'], expName, expInfo['date'])

I’ve tested it an a new data folder is created for each participant and I can save additional files into it.

e.g. I tried in Each Frame

if frameN ==10:
    win.getMovieFrame()
    win.saveMovieFrames(thisExp.dataFileName+'_' + str(trials.thisN) + '.png')

and I added a microphone component.

I just confirmed that u'data/%s/%s_%s_%s' % (expInfo['date'], expInfo['participant'], expName, expInfo['date']) also works. Is Visit a custom expInfo variable?

Yes, Visit a custom expInfo variable. To clarify what I am trying to do, my script creates the data folder name with this code in lines 147 to 154:

# data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
    if dataDir is None:
        dataDir = _thisDir
    filename = u'data/%s_%s/%s_%s_%s_%s' % (expInfo['participant'], expInfo['Visit'], expInfo['participant'], expInfo['Visit'], expInfo['date'], expName)
    # make sure filename is relative to dataDir
    if os.path.isabs(filename):
        dataDir = os.path.commonprefix([dataDir, filename])
        filename = os.path.relpath(filename, dataDir)

Then the script has this code between lines 368 to 374:

# mark experiment as started
    thisExp.status = STARTED
    # update experiment info
    expInfo['date'] = data.getDateStr()
    expInfo['expName'] = expName
    expInfo['expVersion'] = expVersion
    expInfo['psychopyVersion'] = psychopyVersion

Later my script creates additional files that I want to be in the same data folder created above. So in lines 471 to 476 I use this code to retrieve the value of expInfo[‘date’] into a variable named exp_date:

## Variables for file names
    exp_name = expName
    subjID = expInfo['participant']
    visit_number = expInfo['Visit']
    exp_date = expInfo['date']
    LoadCellSet = expInfo['LoadCellsSet']

So problem is that the variable expInfo[‘date’] has one value in lines 147 to 154, but it then gets updated in lines 368 to 374 thus resulting in a different value when used in lines 471 to 476 - which leads to creating a folder with a different timestamp since the variable expInfo[‘date’] has a different value than its initial state. Does that make sense?

Where are you adding code in Builder? I changed the data folder in Experiment Settings/data.

1 Like

Hello @Estephan_Moana

I think the “problem” is that the info-dialog sets the date of expInfo['date'] via 'date|hid': data.getDateStr(),This happens before line 147 - 154. expInfo['date']is reset later in lines 368 - 374 to record the start of the experiment. Given that the data.getDateStr()commands are excuted at different times, the value of expInfo['date']changes.

Perhaps a way around this, is to define a variable, e.g. my_date = data.getDateStr()and use that to create the directory and file names. Alternatively, you could save the value of expInfo['date']before it is reset in line 147 - 154.

Did you try the suggestion made by @wakecarter ?

Best wishes Jens

Hello @Estephan_Moana

I think I found a solution. Add a field to the info dialog, e.g. my_dateand enter

$data.getDateStr()

as default-value.

Then use my_date for creating your directories and file names.

I haven’t tested whether this works online.

Best wishes Jens

Hi, thank you both for the suggestions. To answer the question about where I inserted the code that uses the expInfo[‘date’] variable to get the date, it happens in Builder in a code component “Begin Experiment”.

Both solutions will do what I need, and I adapted my code to used the variable thisExp.dataFileName to add files to the same folder created in Experiment Settings. Adding the field in the info dialog would work as well. Thanks again.

I imagine that you need to set the data saving directory “Before Experiment”, because data is already being saved by the time you get to “Begin Experiment”. Please could you mark whichever solution worked for you as the solution for this thread?