Excel pseudo-random list

Hi, I am a novice at coding and to psychopy. I am creating an experiment to be conducted on pavlovia. I need help with converting my working python script to js which can be synched to pavlovia to collect data.
At present, I have an undefined token “*” error for import functions in javascript.

OS : win 10
PsychoPy version 3
Standard Standalone? Y

What are you trying to achieve?
I have an excel with a list of 40 categories in sheet 1(catgeories.xlsx). Each of the category has 2 questions that need to presented in a condition.
Sheet 2 and Sheet 3 has all the stimulus required for condition 1 and 2.

The python script I have right now

  1. randomizes the 40 categories,
  2. matches the first 20 categories to sheet 1,
  3. fetches all the stimulus of the matched category from sheet 1,
  4. randomizes this list,
  5. writes/updates in an existing CSV.

This same process is done for the next 20 categories for sheet 2, this is now my new stimulus lists that I am using the builder to present in 2 blocks.

The code works perfectly on my console, I have attached the wokring python3 code, auto generated javascript, and the excel and CSV files.

What did you try to make it work?:
I changed the “import * random” to a function in javascript. I suppose I need something similar for import xlrd.

working python3 code


import random
import xlrd

#randomize the seed
random.seed()

#stimulus file
infile = 'categories.xlsx' #path where the xlsx file is having the list of Categories name

#num of study items
num_stimulus = 41

num_Category=20


#total number of items
num_items = 81


#access the xls stimulus file with 3 sheets
inbook = xlrd.open_workbook(infile)
category_sheet = inbook.sheet_by_index(0)
similarity_sheet = inbook.sheet_by_index(1)
hierarchy_sheet = inbook.sheet_by_index(2)

#Array for the category
category = []

#read the category from sheet_by_index
for rowx in range(1,num_stimulus):   #to skip the header
    #read all the coumn values in this row
    row = category_sheet.row_values(rowx)
    
    #Print the output value of column A for this row
    #print(row[0])

#saving to the Category array
    category.append(row[0])

#randomize the order of the category array
random.shuffle(category)

#array of category for the stimuli
randSimilarity_category = []
randHierarchy_category = []


#create the chosen random category list

randSimilarity_category = (category[0:20])
randHierarchy_category = (category[20:40])

#print(randSimilarity_category)
#print(randHierarchy_category)
#print(category)


#function create columns

def create_columns(n):
    list_of_columns = []
    for i in range(n):
        list_of_columns.append([])
    return list_of_columns
    
#Array for the Stimulus with required number of columns present in original data

similarity_Stimulus = create_columns(4)
hierarchy_Stimulus = create_columns(4)

#declaring  

randSim_Category = []
randHier_Category = []


#creating header for stimulus files

similarity_Stimulus[0].append('Category_S')
similarity_Stimulus[1].append('Sim')
similarity_Stimulus[2].append('Fact_S')
similarity_Stimulus[3].append('Concl_S')

hierarchy_Stimulus[0].append('Category_H')
hierarchy_Stimulus[1].append('Hier')
hierarchy_Stimulus[2].append('Fact_H')
hierarchy_Stimulus[3].append('Concl_H')


for i in range(0, num_Category):
    randSim_Category = randSimilarity_category[i]
    for j in range(1,num_items):
        row_similarityStim = similarity_sheet.row_values(j)
        #print('----------comparison-----------')
        #print(randSim_Category,row_similarityStim[0])
        
        if randSim_Category == row_similarityStim[0]:
            #similarity_Stimulus.append((row_similarityStim[0][0],row_similarityStim[0][1],row_similarityStim[0][2],row_similarityStim[0][3]))
            similarity_Stimulus[0].append(row_similarityStim[0])
            similarity_Stimulus[1].append(row_similarityStim[1])
            similarity_Stimulus[2].append(row_similarityStim[2])
            similarity_Stimulus[3].append(row_similarityStim[3])


#print('----------S final-----------')
#print( similarity_Stimulus)




for i in range(0, num_Category):
    randHier_Category = randHierarchy_category[i]
    for j in range(1,num_items):
        row_hierarchyStim = hierarchy_sheet.row_values(j)
        #print(randHier_Category,row_hierarchyStim[0])
        
        if randHier_Category == row_hierarchyStim[0]:
           hierarchy_Stimulus[0].append(row_hierarchyStim[0])
           hierarchy_Stimulus[1].append(row_hierarchyStim[1])
           hierarchy_Stimulus[2].append(row_hierarchyStim[2])
           hierarchy_Stimulus[3].append(row_hierarchyStim[3])
           
#print('----------H final-----------')
#print(hierarchy_Stimulus)




#converting rows into columns : transposing

m = similarity_Stimulus 
transpose_S = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))] 
similarity_Stimulus = transpose_S

n =  hierarchy_Stimulus
transpose_H = [[n[j][i] for j in range(len(n))] for i in range(len(n[0]))] 
hierarchy_Stimulus = transpose_H



#shuffling the created stimulus sets

Sim = similarity_Stimulus[1:]
random.shuffle(Sim)  
similarity_Stimulus[1:] = Sim

Hier = hierarchy_Stimulus[1:]
random.shuffle(Hier)
hierarchy_Stimulus[1:] = Hier





#saving as CSV, create an empty csv in the folder and save contents in it



f = open('Sim_Stim.csv', 'w', encoding = 'utf-8')
for item in similarity_Stimulus:
    f.write(','.join([str(x) for x in item]) + '\n')
f.close()


f = open('Hier_Stim.csv', 'w', encoding = 'utf-8')
for item in hierarchy_Stimulus:
    f.write(','.join([str(x) for x in item]) + '\n')
f.close()
f.sheet_name = 'sheet'


auto generated javascript code with the changed random function

import * as random from 'random';
import * as xlrd from 'xlrd';
var Hier, Sim, category, category_sheet, f, hierarchy_Stimulus, hierarchy_sheet, inbook, infile, m, n, num_Category, num_items, num_stimulus, randHier_Category, randHierarchy_category, randSim_Category, randSimilarity_category, row, row_hierarchyStim, row_similarityStim, similarity_Stimulus, similarity_sheet, transpose_H, transpose_S;
random.seed();
infile = "categories.xlsx";
num_stimulus = 41;
num_Category = 20;
num_items = 81;
inbook = xlrd.open_workbook(infile);
category_sheet = inbook.sheet_by_index(0);
similarity_sheet = inbook.sheet_by_index(1);
hierarchy_sheet = inbook.sheet_by_index(2);
category = [];
for (var rowx = 1, _pj_a = num_stimulus; (rowx < _pj_a); rowx += 1) {
    row = category_sheet.row_values(rowx);
    category.append(row[0]);
}
random.shuffle(category);
randSimilarity_category = [];
randHierarchy_category = [];
randSimilarity_category = category.slice(0, 20);
randHierarchy_category = category.slice(20, 40);
function create_columns(n) {
    var list_of_columns;
    list_of_columns = [];
    for (var i = 0, _pj_a = n; (i < _pj_a); i += 1) {
        list_of_columns.append([]);
    }
    return list_of_columns;
}
similarity_Stimulus = create_columns(4);
hierarchy_Stimulus = create_columns(4);
randSim_Category = [];
randHier_Category = [];
similarity_Stimulus[0].append("Category_S");
similarity_Stimulus[1].append("Sim");
similarity_Stimulus[2].append("Fact_S");
similarity_Stimulus[3].append("Concl_S");
hierarchy_Stimulus[0].append("Category_H");
hierarchy_Stimulus[1].append("Hier");
hierarchy_Stimulus[2].append("Fact_H");
hierarchy_Stimulus[3].append("Concl_H");
for (var i = 0, _pj_a = num_Category; (i < _pj_a); i += 1) {
    randSim_Category = randSimilarity_category[i];
    for (var j = 1, _pj_b = num_items; (j < _pj_b); j += 1) {
        row_similarityStim = similarity_sheet.row_values(j);
        if ((randSim_Category === row_similarityStim[0])) {
            similarity_Stimulus[0].append(row_similarityStim[0]);
            similarity_Stimulus[1].append(row_similarityStim[1]);
            similarity_Stimulus[2].append(row_similarityStim[2]);
            similarity_Stimulus[3].append(row_similarityStim[3]);
        }
    }
}
for (var i = 0, _pj_a = num_Category; (i < _pj_a); i += 1) {
    randHier_Category = randHierarchy_category[i];
    for (var j = 1, _pj_b = num_items; (j < _pj_b); j += 1) {
        row_hierarchyStim = hierarchy_sheet.row_values(j);
        if ((randHier_Category === row_hierarchyStim[0])) {
            hierarchy_Stimulus[0].append(row_hierarchyStim[0]);
            hierarchy_Stimulus[1].append(row_hierarchyStim[1]);
            hierarchy_Stimulus[2].append(row_hierarchyStim[2]);
            hierarchy_Stimulus[3].append(row_hierarchyStim[3]);
        }
    }
}
m = similarity_Stimulus;
transpose_S = function () {
    var _pj_a = [], _pj_b = range(m[0].length);
    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(function () {
    var _pj_e = [], _pj_f = range(m.length);
    for (var _pj_g = 0, _pj_h = _pj_f.length; (_pj_g < _pj_h); _pj_g += 1) {
        var j = _pj_f[_pj_g];
        _pj_e.push(m[j][i]);
    }
    return _pj_e;
}
.call(this));
    }
    return _pj_a;
}
.call(this);
similarity_Stimulus = transpose_S;
n = hierarchy_Stimulus;
transpose_H = function () {
    var _pj_a = [], _pj_b = range(n[0].length);
    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(function () {
    var _pj_e = [], _pj_f = range(n.length);
    for (var _pj_g = 0, _pj_h = _pj_f.length; (_pj_g < _pj_h); _pj_g += 1) {
        var j = _pj_f[_pj_g];
        _pj_e.push(n[j][i]);
    }
    return _pj_e;
}
.call(this));
    }
    return _pj_a;
}
.call(this);
hierarchy_Stimulus = transpose_H;
Sim = similarity_Stimulus.slice(1);
random.shuffle(Sim);
similarity_Stimulus.slice(1) = Sim;
Hier = hierarchy_Stimulus.slice(1);
random.shuffle(Hier);
hierarchy_Stimulus.slice(1) = Hier;
f = open("Sim_Stim.csv", "w", {"encoding": "utf-8"});
for (var item, _pj_c = 0, _pj_a = similarity_Stimulus, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
    item = _pj_a[_pj_c];
    f.write((",".join(function () {
    var _pj_d = [], _pj_e = item;
    for (var _pj_f = 0, _pj_g = _pj_e.length; (_pj_f < _pj_g); _pj_f += 1) {
        var x = _pj_e[_pj_f];
        _pj_d.push(x.toString());
    }
    return _pj_d;
}
.call(this)) + "\n"));
}
f.close();
f = open("Hier_Stim.csv", "w", {"encoding": "utf-8"});
for (var item, _pj_c = 0, _pj_a = hierarchy_Stimulus, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
    item = _pj_a[_pj_c];
    f.write((",".join(function () {
    var _pj_d = [], _pj_e = item;
    for (var _pj_f = 0, _pj_g = _pj_e.length; (_pj_f < _pj_g); _pj_f += 1) {
        var x = _pj_e[_pj_f];
        _pj_d.push(x.toString());
    }
    return _pj_d;
}
.call(this)) + "\n"));
}
f.close();
f.sheet_name = "sheet";

Much appreciate your time working to help me with the issue,
Janani

Hier_Stim.csv (4.9 KB) Sim_Stim.csv (4.8 KB)
categories.xlsx (17.7 KB)