# Dynamic ImageStim paths and Pavlovia

**URL:** <https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148>\
**Category:** Builder\
**Tags:** images, pavlovia\
**Created:** [June 17, 2019, 9:47pm UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148 "2019-06-17T21:47:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgfitch](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dgfitch/32/4597_2.png) [@dgfitch](https://discourse.psychopy.org/u/dgfitch)\
**Post date:** [June 17, 2019, 9:47pm UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148/1 "2019-06-17T21:47:03Z")

</div>

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: [https://run.pavlovia.org/chm/mst/html/](https://run.pavlovia.org/chm/mst/html/)  
Code here: [https://gitlab.pavlovia.org/chm/mst](https://gitlab.pavlovia.org/chm/mst)

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

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [June 18, 2019, 8:33am UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148/2 "2019-06-18T08:33:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dgfitch](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dgfitch/32/4597_2.png) [@dgfitch](https://discourse.psychopy.org/u/dgfitch)\
**Post date:** [June 18, 2019, 3:38pm UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148/3 "2019-06-18T15:38:50Z")

</div>

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: [https://run.pavlovia.org/chm/mst/html/](https://run.pavlovia.org/chm/mst/html/)

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [June 18, 2019, 3:52pm UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148/4 "2019-06-18T15:52:48Z")

</div>

@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.,

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

```

---

<div class="post-metadata">

**Author:** ![dgfitch](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dgfitch/32/4597_2.png) [@dgfitch](https://discourse.psychopy.org/u/dgfitch)\
**Post date:** [June 18, 2019, 9:34pm UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148/5 "2019-06-18T21:34:38Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![njvack](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/njvack/32/3438_2.png) [@njvack](https://discourse.psychopy.org/u/njvack)\
**Post date:** [June 19, 2019, 2:08pm UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148/6 "2019-06-19T14:08:50Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [June 19, 2019, 2:16pm UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148/7 "2019-06-19T14:16:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![njvack](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/njvack/32/3438_2.png) [@njvack](https://discourse.psychopy.org/u/njvack)\
**Post date:** [June 19, 2019, 4:34pm UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148/8 "2019-06-19T16:34:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [June 19, 2019, 4:59pm UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148/9 "2019-06-19T16:59:28Z")

</div>

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'`.

---

<div class="post-metadata">

**Author:** ![njvack](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/njvack/32/3438_2.png) [@njvack](https://discourse.psychopy.org/u/njvack)\
**Post date:** [June 19, 2019, 5:49pm UTC](https://discourse.psychopy.org/t/dynamic-imagestim-paths-and-pavlovia/8148/10 "2019-06-19T17:49:43Z")

</div>

I’d advocate for either:

```auto
001 == '001'
0.1 == 0.1
01a == '01a'

```

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

```auto
001 == '001'
0.1 == '0.1'
01a == '01a'

```

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

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

```
