| Reference | Downloads | Github

Dynamic ImageStim paths and Pavlovia

What’s the correct way to do csv-based image stimuli on Pavlovia?

We are starting a MST implementation that needs to display images from pre-selected random orders, so we have the ImageStim pull from $image_filename every repeat, and a code element that runs at Begin Routine sets that path.

This works fine running in Python, but on the JS side we get "Unfortunately we encountered an error: when setting the image of ImageStim: image … when getting the value of resource: stimulus_sets/set_1/1.jpg: unknown resource

So the JS side is setting the correct path, but the resource is not getting preloaded because the preload pipeline doesn’t know about it.

How to fix this? Do we need to add code to iterate over the possible stimuli and preload them into the resources? I’m not sure I understand how the backend for that works, yet.

See the error here:
Code here:

(We’ll also need to make the CSV conditions file dynamic as well, eventually which will tie into how resources are loaded…)

Hi @dgfitch, the problem is that your stimuli are defined using a code component, so those stim are not actually recognised as a resource needed for the task and so are not copied to the html/resource folder for use in the task. To overcome this issue, copy the stim folders into the resource directory in your html folder, and sync with Pavlovia.

This sort of issue will not occur in the future, when we flatten out the directory structure and remove the need for a separate html/resources folder containing duplicate resources.

Hmm, okay… when I put the images in html/resources manually, they do preload.

However, the conditions on the trial seem to be loaded differently. In the python side, the CSV defines the stimuli as “001a” or the like. On the JS side, when I log it, the CSV value is set to “1”, so the eventual resource path ends up being “stimulus_sets/set_1/1.jpg” instead of “stimulus_sets/set_1/001a.jpg”, and I still get the “unknown resource” error.

Check it out here:

@dgfitch, yes the issue there is that PsychoJs will treat things that look like numbers as numbers. A way around this is to add a letter to the beginning of your file variable in your conditions file so it is recognised as a string, e.g., a001b. Then, slice it off just before you create your filename. E.g.,

// 'a001b' becomes '001b'
path = 'stim/' + stim_image_name.slice(1) + '.jpg'

1 Like

Solves it! Thank you… would not have guessed that, but it makes sense.

Now on to the next problem, getting keyboard responses with visual feedback working right on the JS side. :slight_smile:

1 Like

It seems like this might not be the desired PsychoJS behavior… this is a weird workaround. Is it worth filing an issue on that or is this by design?

@dgfitch The simpler workaround in this case would be to rename the stims to not start with numbers…

Yes @njvack, that is simpler, but then you have to rename all of your files. I have requested a fix that checks for characters in any number strings, and does not convert to number if any characters are found. This should solve the issue, and should be released in 3.1.3.

I’m guessing it also should not consider it a number if it’s an integer with leading zeros…?

With the current fix (which is not yet implemented by Alain, so could change), any number without characters is treated as a number, otherwise it is treated as a string. So, 001 == 1, 0.1 == 0.1, and 01a == '01a'.

I’d advocate for either:

001 == '001'
0.1 == 0.1
01a == '01a'

Or delaying casting to numeric types until a value is used as such, so

001 == '001'
0.1 == '0.1'
01a == '01a'

until you try to use it as a duration or something, at which point:

001 == 1.0
0.1 == 0.1
01a == 1.0 # Or raise an exception