Calculating standard deviation in PsychoJS

Hi all,

I’m trying to calculate the standard deviation of an array of bars. I have my experiment working locally using the code component in the builder, however I can seem to get it working online. I’m currently using Psychopy version 2020.2.

Below is the python version of my code:

#initialize item list, avg, and holding variables
item_list = []
avg = 0
res =0
random.shuffle(set_avg)
end_num = set_avg[0]
for i in range(items):
    #create list of 8 items randomly sampled b/t start and end numbers
    res = random.randint(strt_num, end_num)
    item_list.append(res)
avg = sum(item_list)/items
avg_list.append(avg)
sd = np.std(item_list)
dist =2*sd + end_num
#create variable for each line orientation
or1= item_list[0]
or2 = item_list[1]
or3 = item_list[2]
or4 = item_list[3]
or5 = item_list[4]
or6 = item_list[5]
or7 = item_list[6]
or8 = item_list[7]

And I converted it to JS, but I keep getting the error that “std is not defined”.

I can’t figure out why the std.math() function isn’t working or what the correct replacement should be. Below is my JS code:

item_list = [];
avg = 0;
res = 0;
shuffle(set_avg);
end_num = set_avg[0];
for (var i = 0, _pj_a = items; (i < _pj_a); i += 1) {
    res = randint(strt_num, end_num);
    item_list.push(res);
}
a = sum(item_list);
avg = (a/items);
avg_list.push(avg);
sd = std.math(item_list);
dist =2*sd + end_num;

or1 = item_list[0];
or2 = item_list[1];
or3 = item_list[2];
or4 = item_list[3];
or5 = item_list[4];
or6 = item_list[5];
or7 = item_list[6];
or8 = item_list[7];

I think the issue I’m having is with the std.math() function, so any help on how to calculate the standard deviation of an array would be helpful!

Thanks in advance,
Kristina

Try math.std instead of std.math

Thank you! I just tried this, but now I’m getting the error that “math” is not defined. Do I need to import/ or define a new function in the begin experiment tab?

Try with a capital M ie Math.std


I did try that, but I still am getting an error.