psychopy.org | Reference | Downloads | Github

How to store image file paths on Pavlovia

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

OS: Win10
PsychoPy version: 3.2.3
What are you trying to achieve?:
Export my code to HTML. I am specifically getting tripped up by the following code (shortened to 3 items for simplicity):

// Import stim file manually as JSON
imageStims = ['images/FRI_01.jpeg', 'images/FRI_02.jpeg', 'images/FRI_03.jpeg']

What did you try to make it work?:
My original Python code does this using xlrd. The image file names are stored in an excel document,
but I could not figure out how to do this with JavaScript (I am very new to JS). So I created an array with each image path name and stored it like you see above. However, this generates the following error message in the terminal when I click “Export to HTML”:

ERROR           path is on mount "\\\\ Import stim file manually as JSON\nimageStims = ['images\\FRI_01.jpeg']\n\n", start on mount 'C:'

I have tried reformatting the JSON with double/single quotes and with/without a semicolon at the end, as I saw JSON’s formatted both ways on the internet. No avail.

What should I do? Thank you in advance!

Hi @jwhenry28, if you are using Builder to create your experiment, you can use a loop to handle your image files for each trial. You would use 1 row for every repeat of the routine, and each row would contain the image filenames that you need - you can use multiple columns for multiple images on each trial. You can refer to each image using the column name. In addition, PsychoPy recognises your images in your conditions files as a resource, and moves them to the resources folder, which gets uploaded to Pavlovia when you sync your task. See the PsychoPy Stroop tutorial for the basics of building a task using Builder.

Thanks for the info. But this does not help my problem of failing to export to HTML. My python code does just as you described. I cannot figure out how to transcribe it to JavaScript. Here is my python code:

# access the stim file
inbook = xlrd.open_workbook(infile)
insheet = inbook.sheet_by_index(0)

# four categories of stimuli
cate1 = [] 
cate2 = [] 
cate3 = [] 
cate4 = [] 

# images are displayed on left or right of screen
leftImages = []
rightImages = []

# read the stimuli from excel sheet
threshhold = int(numItems / numCates)

for rowx in range(numItems):
    
    # read in the row
    row = insheet.row_values(rowx)
    
    # save image filepath to appropriate list
    # images must be grouped in a single column
    # in category order to work
    if (rowx < threshhold):
        cate1.append(row[0])
    elif (rowx < threshhold * 2):
        cate2.append(row[0])
    elif (rowx < threshhold * 3):
        cate3.append(row[0])
    else:
        cate4.append(row[0])

How can this be written in JavaScript? I cannot seem to find good documentation for working with Excel documents in JS.

There are two methods for working with Excel documents in the attached link:

You can use the first code I posted in that link, which is a custom function for reading xlsx/csv. Alternatively, you can use the TrialHandler code for reading xlsx/csv files. The rest of the code would look something like:

// four categories of stimuli
cate1 = [];
cate2 = [];
cate3 = []; 
cate4 = []; 

// images are displayed on left or right of screen
leftImages = [];
rightImages = [];

// read the stimuli from excel sheet
threshhold = Math.round(numItems / numCates);

for (let rowx of [...Array(numItems).keys()]) {
    
    // read in the row
    row = NEW_JS_OBJECT_CONTAINING_DATA(rowx)  // This will be your conditions file
    
    // save image filepath to appropriate list
    // images must be grouped in a single column
    // in category order to work

    if (rowx < threshhold) {
        cate1.push(row[0])
    } else if (rowx < threshhold * 2) {
        cate2.push(row[0])
    } else if (rowx < threshhold * 3) {
        cate3.push(row[0])
    } else {
        cate4.push(row[0])
    }
}

I am getting a similar error from the first solution:

168.5712     ERROR         path is on mount "\\\\ Import stim file manually as JSON\nresult = [];\nfunction successFunction(data) {\n    var allRows = data.split('\\n'); ", start on mount 'C:'

For the second solution, would I just replace NEW_JS_OBJECT_CONTAINING_DATA with imageStims from the code I posted in my original query?

Weirdly, deleting the comment at the top of the code solved the error. However, it appears that the resources are nowhere present:


What happened to my Excel file?

Would mind sharing the link to your task? Not sure why you were getting the mount error, but can have a look if you share the link.

Here is the link to my repository.

I actually fixed the error, but my images don’t display. Additionally I was given the following code (in python) to add variables and values to the output csv file that prints at the end of an experiment:

thisExp.addData("foo", bar)

Which adds value bar to column foo. How can this be done with Javascript? I need to be able to keep track of the images that were displayed as they are random every time.

Ok I will have a look.

To save data with PsychoJS, you would use:

psychoJS.experiment.addData("foo", bar);

Seems to upload ok for me, although there is the “thisExp” issue which is fixed with the code in this post.

Thanks. Was able to upload. However, I still am having all kinds of issues with the JS variables. Certain variables keep popping up as undefined even when I’ve clearly defined them in a code block (without using let or var). Is there any official documentation for this process that I’ve missed? All I’ve been able to find is this which doesn’t mention any of the advice you have given me so far.

How/where do I need to declare my variables so that they are accessible by all code chunks? Currently, it looks like anything that I declare that is not in a “Begin Experiment” tab is unable to be used by other chunks. However, I need to initialize several variables to values that are manipulated during the routine. My git code is here.

Feel free to send me to documentation instead if there is any.