psychopy.org | Reference | Downloads | Github

Searching for a file in the working directory

Hey!
I’m creating an online experiment, where each participant would get their own unique conditions file. I’ve set it up using tips I found here in the forum, by dynamically naming the conditions file, based on the Participants’ ID.
I’d like to add a piece of code that would search for the .csv before reaching the loop, and in case it can’t find it it would load a default .csv file, that I will put there (just in case I mess up something, I like to have a contingency, so as to not crash the whole experiment).
I’m new to python and to javascript, but in matlab I’d do something like:

conditions_filename = [‘s’ participant_id ‘.csv’];
default_file = ‘default_stim.csv’;
try
load(conditions_filename)
catch
load(default_file)
end

or, alternatively,

if ~isempty(dir(conditions_filename ))
conditions_filename = default_file

Any idea how I can achieve that with psychopy and pavlovia (so without importing any libraries)?
Thank you!

Hey!

It’s possible, but it’s a bit tricky to accomplish; you would need to add quite a lot of JavaScript to have it work robustly. I’ve got a simpler suggestion that might work too though: put all the participant-specific conditions in a single conditions file and select the right rows depending on the participant. Add your fallback rows in there too.

Best, Thomas

That’s a great suggestion, I’ll give it a go. Thanks!

Hey Thomas,
Thanks again for your help!
If I understand correctly, this solution would require me to upload the csv ahead of the loop (because i will need to find the relevant row index, based on the participant ID, to then give to the loop as the “selected row”)?

so based on that assumption, and browsing through some posts on this forum for uploading a csv file without using panda, I wrote this code:

# load the csv file
cond_file = 'all_messages.csv' # name of csv file, where each row is aimed at a single particiapnt + last row a fallback option
sID = str(expInfo['participant'])
trial_data = data.TrialHandler(trialList= data.importConditions(cond_file), nReps = 10, method='sequential', extraInfo= expInfo) # import the csv file

# find the subject-specific row in the csv file
if sID in trial_data[:]['receiver_id']: # if the subject id matches one of the items in "recevier id" column of the csv
    row_in_file = trial_data[index(sID)]['receiver_id'] # the row to select for the loop is this index
else: # if the subject id can't be found
    row_in_file = len(trial_data[:]['receiver_id'] ) # pick last row as a default

this is very much not working, I’m not even getting an error message, so no idea how to go about debugging this. As I’ve mentioned, I’m a total beginner and don’t understand what TrialHandler is doing exactly, so am probably using it wrong. As a result, I am also unsure about how to reference a column in the conditions file (e.g., if I wanted to print out the 2nd row in the “receiver_id” column, how would I do that?)

Some more info:
-I learned that I need to change the JS code for the trial_data bit, which I have, probably unsuccessfully.
-Unfortunately, my subject ids are a combination of strings and digits, so I can’t just use them as indices.

help :slight_smile:

Help is on it’s way! :slight_smile:

This one is indeed a bit tricky, since JS doesn’t support the kind of vector operations that Python with numpy does. Tomorrow I’ll build a little prototype to illustrate a nice approach.