psychopy.org | Reference | Downloads | Github

ReferenceError: os is not defined

URL of experiment:
https://run.pavlovia.org/jonas.tvedt/gazeproject/?__pilotToken=c20ad4d76fe97759aa27a0c99bff6710&__oauthToken=a72848ae19ac2a14c3ddc63d93b5490500697d9647b91ac1df508a41d649c62a

Description of the problem:

Any ideas how to resolve this? I’m guessing the problem stems from when I define a path in a code component tab, like this:

path = (“images/”)
stimlist = [os.path.join(path, file)
for file in os.listdir(path)
if file.endswith(‘C.png’)]

So I figure the error arrises because os is not imported, but when I add ‘import os’ to a code component tab (before experiment), the experiment gets stuck on ‘initializing experiment…’

You will need to create a spreadsheet containing the names of the files you want.

I see. Thank you. Can the spreadsheet variable be treated similarly as my variable in code? For example:

stimlist = []
for file in spreadsheet:
if file.endswith(‘C.png’)]
stimlist.append(file)

…or something like that?

A quick Google suggests that you might need to change endswith to endsWith

(as well as .append to .push if you aren’t using a code_JS from my crib sheet)

Thanks! But how do I reference the spreadsheet and a specific column so that it works online? Do I have to provide the xlsx file in the conditions file field of a loop or can I read it in from a code component?

Have a look at Trial Handlers in my coding snippets for Python and JS code for importing spreadsheets.

Great! That worked.

There seems to be a problem with the Trial handler method when it is used online. I get this error:

ReferenceError: data is not defined

I guess it comes from this line?:
myData = data.TrialHandler()

That’s a Python line. Did you forget to change the component to Both and edit the JS code?

I did not know that that was necessary. I have the component set to Auto - JS. So if I change it to Both, how should I edit the JS code to get it working?

Have a look at Trial Handlers in my coding snippets for Python and JS code for importing spreadsheets.

ah yes, editing the JS code according to your coding snippet got me past the error. Thanks again.

To anyone having a similar issue: you also have to specify the spreadsheet in the Additional Resources field of the Online tab in the Experiments Settings.

1 Like

Thanks for the reminder. I’ll add this note to the coding snippet page.

1 Like

I have one more problem regarding the trialHandler method that maybe you could help me with? How do I get the values, i.e., file names, that I guess are stored in _trialList in dictionaries? In python I could use list comprehension and refer to the column header / key like this:

stim_dict = data.TrialHandler(...)

allstim = [i['faces'] for i in stim_dict]
print('all stimuli',allstim)

I cant seem to find a method that works online with javascript, but I guess there must be some simple way of doing this?

According to my code snippets you should use aValue = myData.trialList[Idx][‘variableName’]

allstim = []
for Idx in range(len(myData.trialList)):
      allstim.append(myData.trialList[Idx]['faces']

Remember to define append as push in code_JS

However, personally I use Builder loops to push straight to lists as per my interleaved lists code.

Thanks! But unfortunately it gets stuck on ‘initializing experiment’ when using this code. It auto translates like this:

allstim = [];
for (var Idx = 0, _pj_a = stim_dict.trialList.length; (Idx < _pj_a); Idx += 1) {
    allstim.push(stim_dict.trialList[Idx]["faces"]);
}

Got past it! I used this python code:

allstim = [i['faces'] for i in stim_dict]
print('all stimuli',allstim)

…which was auto generated to this JS code:

allstim = function () {
    var _pj_a = [], _pj_b = stim_dict;
    for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
        var i = _pj_b[_pj_c];
        _pj_a.push(i["faces"]);
    }
    return _pj_a;
}

…then I added .trialList to the myData object manually (thanks for the clue):

allstim = function () {
    var _pj_a = [], _pj_b = stim_dict.trialList;
    for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
        var i = _pj_b[_pj_c];
        _pj_a.push(i["faces"]);
    }
    return _pj_a;
}
1 Like