Recover participant data from .psydat file

I am trying to help some trainees recover their data. This experiment had subjects come for visit one and visit two. The task in question was built using PsychoPy infrastructure and output data files with behaviour using .txt and .psydat formats. Sadly, the .txt file from the second visit might have overwritten the .txt file from the first visit, but the two .psydat files from the two visits are there, and we are hoping there might be some chance to recover the participant data from the .psydat file.

I loaded the .psydat file and looked at its content with dir(psydatfile). This gives a list of attributes for this object. I put these attributes in a list, and using a python loop, I then printed the content of each of these attributes, e.g. dir(psydatfile.attribute). I was able to recover a little bit of the data, but not things like confidence, or reaction times etc. which would have normally been stored in the .txt file.

I tried then to pierce more deeply into some of these attributes with dir(psydatfile.attribute.name_of_subattribute). I chose some attributes that the PsychoPy code says they should be storing participant behaviour from the experiment, e.g. addData, addOtherData, addResponse. There’s just another list of attributes there, i.e. no RT data… more like a matryoschka of subsubattributes of subattributes in attributes.

I looked on the PsychoPy forum to see if there’s a way to get csv, excel or txt files out of the psydat, but so far this is inconclusive. The notes on the PsychoPy website say that this .psydat file can be used to generate back the excel, and that it has much more information since it also has the code of the task (which is true, it does have the code). However, there’s no indication on the PsychoPy website how to generate back the excel.

Do you happen to have an idea how to get the csv, excel or txt from psydat?
Or do you reckon that the only way is to load the .psydat and extract the content of some of the attributes, and save that to csv?
How deep do I have to look into the psydat to recover the data, e.g. first level attributes, attributes of attributes, subattributes of attributes?
Is there a better way to recover data other than looking at each of these attributes and subattributes of the .psydat object?

Thanks in advance.

Have you tried the saveAsWideText() method?

# import PsychoPy function for loading Pickle/JSON data
from psychopy.misc import fromFile

# (replace with the file path to your .psydat file)
fpath = '/Users/my_user/myexperiments/myexperiment/participant_expname_date.psydat'

# load in the data
psydata = fromFile(fpath)

# (replace with the file path to where you want the resulting .csv
# to be saved)

save_path = '/Users/my_user/Desktop/test_out.csv'

# save the data as a .csv file, ie separating the values with a
# comma. 'CSV' simply means 'comma-separated values'
psydata.saveAsWideText(save_path, delim=',')

(from Data outputs — PsychoPy v2023.2.0dev1)

Thank you for chipping in. Yes, I’ve tried this code below with:
.txt and no delim
.csv and delim=‘,’
.tsv and delim=‘\t’
.xlsx and no delim

Note that my psydat object has .saveAsText and saveAsExcel attributes, and doesn’t have .saveAsWideText.

Each time I get a file with only two pieces of data and not the rich kind of participant data contained in the txt files generated at the end of the experiment. This bit of data I can already access by looking at the first level attributes with dir(psydatfile) after loading the .psydat file.

# EDIT THE NEXT LINE to be your .psydat file, with the correct path:

name = 'path_to_psydat_file'

file_psydat = os.path.abspath(name)
print("psydat: {0}".format(file_psydat))

# read in the experiment session from the psydat file:
exp = fromFile(file_psydat)

file = file_psydat
file += '.tsv'
exp.saveAsText(file, delim='\t')