psychopy.org | Reference | Downloads | Github

Syntax error when working with Auto -> JS

URL of experiment: https://run.pavlovia.org/lcdlab/spatial-memory/html

Description of the problem: Hello! I am trying to run this study online. Initially I had problem running it because I did not change the code type from ‘py’ to ‘both’. I changed this in all my code components, however in the ‘begin experiment’ part of my ‘initcode’ component gives a syntax error when I use ‘Auto -> JS’. I read that JS doesn’t have a built-in ‘shuffle’ function so I thought that may be the problem, but I assume that there may be more to it because the syntax error message stays even when I comment out the shuffle lines. I’d appreciate any help with this!

Thank you!

Can you show us the actual code it’s failing to translate? Or link to the code repository and make it public? (You can make it public by going to view code, then settings -> general -> permissions.)

import csv, os, codecs

#load in image list
with codecs.open(‘https://gitlab.pavlovia.org/lcdlab/spatial-memory/blob/master/trialsblock1.csv’, ‘r’, encoding=‘utf-8-sig’) as f:
reader = csv.reader(f)
allPics = list(reader)

random.shuffle(allPics)
print(allPics)

with codecs.open(‘https://gitlab.pavlovia.org/lcdlab/spatial-memory/blob/master/trialtypes.csv’, ‘r’, encoding=‘utf-8-sig’) as f:
reader = csv.reader(f)
trialTypes = list(reader)

#how many pics do we actually want?
numItems = 105
numEncoding = 25
numRetrieval = 60

#arrays of stim names
pics = []

#read images from sheet
for rowx in range(numItems):

row = allPics[rowx]

#print(row[0])
# save each pic to pic array
pics.append(row[0])

#randomize order of pics
random.shuffle(pics)

#arrays for final stimuli
trialPics = []

#create final stimuli list
for i in range(0, numItems, 3):

# make triplet
trialPics.append(pics[i:i+3]) 

#these triplets will be shown during encoding
encodingPics = trialPics[0:numEncoding]

#this will be the new triplets added at retrieval
newRetrievalPics = trialPics[numEncoding:numEncoding+numRetrieval]

newPics = []
for triplet in newRetrievalPics:
for pic in triplet:
newPics.append(pic)

#for randomizing trial orders

trialOrders = []
for rowx in range(numEncoding):

row = trialTypes[rowx]
trialOrders.append(row[0])
random.shuffle(trialOrders)

samePics = []
diffPics = []

Import statements don’t work in JS, for starters. “With” statements almost certainly don’t either. The syntax error is probably from the “with” statements, if I had to guess, but even fixing those, loading csv files like this isn’t going to work.

@wakecarter has come up with a good cheat sheet about what will and won’t translate to JS from Python, which may be helpful to you:

1 Like