psychopy.org | Reference | Downloads | Github

Replacing importing functions in Python

Dear Discoursers,

I have managed to upload my experiment to Pavlovia, however I am stuck now that I need to change my Python code to JS so that Pavlovia can support it . Although I have read through many other threads and although there seem to be some answers in there, I think I do not fully understand how to change my code exactly.

These are my imports:

import random, xlrd
from random import sample
import itertools

I am using xldr to open excel files:

inbook = xlrd.open_workbook(infile)
insheet = inbook.sheet_by_index(0)

This works perfectly, but ofcourse not for Pavlovia. How can I change this code to a type of code that Pavlovia can support? It seems so simple, and perhaps it is my lack of expertise in the field of coding, but I find it very difficult to grasp how I can open the Excel files from examples I have seen in the discourse.

This is how I then read in my file:

#read the stimuli from our sheet
for rowx in range(1,num_items+1):
    
    #read in the values of all columns on this row
    row = insheet.row_values(rowx)
    #print out the value of column
    #print(row[0])
    #print(row[1])
    
    #saving the image to the picture array and instruction to instruction
    picture.append(row[0])
    instruction.append(row[1])

After this I use the shuffle function to shuffle my stimuli:

picture1 = random.sample(picture, len(picture))
picture2 = random.sample(picture, len(picture))
picture3 = random.sample(picture, 20)

I later use itertools for a semi-counterbalanced manipulation:

instruction_shuffled2 = list(itertools.chain(*instruction_shuffled2))

As I am understanding it, I change the Python code to meet the requirements of Pavlovia. I do this by going into the code element in the first routine that I have made in the Builder where I have stored this code and change it to a code that does not use imports. Does anybody have insight into how I can alter the code so I do not use the imports. I am very much struggling with opening the Excel file in particular.

I understand this may be a very basic question, though I really hope that somebody could help me with this as I am at my wits’ end.

Kindest regards,

Snoek

Is the key problem that you want to work with multiple sheets within the same Excel file and you couldn’t use multiple single sheet Excel or CSV files instead?

Hi wakecarter,

Thank you for your reply.

No, the key problem is that I need to call the excel files without using the xlrd import function, as I want to be able to run it on Pavlovia.

Best regards,

Snoek

I thought that you’d already seen my solution for this. I use a loop to add (append=push) rows into a two dimensional array.

Thank you for your reply.

I’m not sure what solution exactly you are referring to? I do have a link to your crib cheat. The problem is that I cannot import Python functions and need a different way to load in my excel file.

I am currently trying to implement this solution but I am having some trouble with it as I never work with JS:

You could have a look at how I do it in https://pavlovia.org/Wake/prospective-memory-ldt

Since this comes up a lot I’ll add it to my template https://pavlovia.org/Wake/brookes-template-2020 over the weekend

Maybe I am understanding it wrong but I do not see you load in from excel in this example. Perhaps I am looking in the wrong place? I am looking in ‘pm_ldt.psyexp’.

From my understanding I have to change my experiment within the Builder so I can refresh it in Pavlovia.

Perhaps my question before was too vague and with too much unnecessary information

I want to know how I can replace

import xlrd
inbook = xlrd.open_workbook(infile)
insheet = inbook.sheet_by_index(0)

By something that will work in Pavlovia

I don’t know what inbook.sheet_by_index(0) does.

Have a look at my load_targets and loadwords routines. They read items.csv (but items.xlsx should work too) into arrays for use in my trials loop

It is the way to load in excel sheets in a lot of the tutorials of PsychoPy builder. Using the package xlrd that is imported in the first line of code.

I think I will choose to manually insert all my stimuli.

There’s another option here that you might like to look at

1 Like

I’ve just updated https://pavlovia.org/Wake/brookes-template-2020 so that it loads trials from an Excel and repeats incorrect trials.

There’s a bug offline (probably due to conflict with the slider I’ve added to the same routine).

Please let me know the sytax you used if you got the post you marked as a solution to work, since the person who suggested it hasn’t managed to get it working yet.