How to import CSV data into an array using JS

Description of the problem:

Hi All, I have the following code in my python script using builder.

with open(‘coordinates.csv’) as file:
coords = [line.split() for line in file]

Can anyone point me to where I can translate this into JS? The variable coordinates should essentially be a list of lists.

I’ve also tried manually adding the array to Psychopy to dodge the file open issue (the list has ~2000 items) but if I do this then I get the following error when trying to sync to Pavlovia (the error goes away if I shorten the list to few items).

Traceback (most recent call last):
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\app\builder\builder.py”, line 1221, in onPavloviaSync
self.fileExport(htmlPath=htmlPath)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\app\builder\builder.py”, line 719, in fileExport
target=“PsychoJS”)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\psyexpCompile.py”, line 73, in generateScript
compileScript(infile=exp, version=None, outfile=filename)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\psyexpCompile.py”, line 242, in compileScript
_makeTarget(thisExp, outfile, targetOutput)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\psyexpCompile.py”, line 214, in _makeTarget
script = thisExp.writeScript(outfile, target=targetOutput, modular=True)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment_experiment.py”, line 223, in writeScript
self_copy.flow.writeFlowSchedulerJS(script)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment\flow.py”, line 289, in writeFlowSchedulerJS
resourceFiles = set([resource[‘rel’].replace("\", “/”) for resource in self.exp.getResourceFiles()])
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment_experiment.py”, line 867, in getResourceFiles
thisFile = getPaths(thisParam.val)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment_experiment.py”, line 793, in getPaths
if os.path.isfile(thisFile[‘abs’]):
File “C:\Program Files\PsychoPy3\lib\genericpath.py”, line 30, in isfile
st = os.stat(path)
ValueError: stat: path too long for Windows

Hello TheSav,

Windows has a file path limit of 260 characters. You might want to shorten your path. In Builder usually everything is in the root directory of the experiment (some prefer specific directories, e.g. stimuli, audio aso.in that root directory), so the path shouldn’t be too long. Any reason why you open an *.csv with coordinates instead to initialising those in a code-element or using an *.xlsx in a loop?

Best wishes Jens

HI Jens.

The issue is that I have created a motion path that allows a circle to smoothly move around the screen in a seemingly random fashion using bezier functions. At present this generates about 2000 pairs of coordinates. My program works perfectly fine locally but of course as soon as I attempt to upload the file to pavlovia, all of the numpy functions break it and the conversion seems too laborious to implement.

So instead, I have written a python script that saves them to csv. I now simply import this file into an array using the above two lines of code but I can’t work out the JS equivalent.

So, as a workaround, I tried to list all the coordinates manually in the begin routine but this throws the path length error even though in that instance I’m not importing the file. When I shorten the array (list) to only two pairs of coordinates it bizarrely works.

My assumption is Pavlovia is baulking at the length (or size) of the file rather than the file path length.

The data when manually coded looks like:

coords[0][0]=‘0.949560018,0.255685697’
coords[1][0]=‘0.94552775,0.25836755’
coords[2][0]=‘0.94149563,0.261049269’

coords[2248][0]=‘0.759359342,0.715210124’
coords[2249][0]=‘0.758475978,0.714957805’

or xList=[0.949560018,0.94552775,0.94149563,0.937463806,0.933432426,0.929401638,0.925371589,0.921342429,0.917314304,0.913287363,0.909261754,0.905237624,0.901215123,0.897194396,0.893175594,0.889158863,0.885144352,0.881132209,0.877122581,0.873115617,0.869111464,0.865110271… up to 2249 instances

But in both cases, when I truncate them to shorter sequences the error goes away.

PS. The coordinate pair are changed every frame update which I why I don’t just import the file as a condition file. The array gets used in the single trial which has ~2000 frame updates.

@TheSav , take a look at this post - you can use a trialHandler to handle the file import

Thanks David.

I’ve applied the code and now it’s pushing online. But I’ve hit a JS code error. Unfortunately my debugging skills at this stage are poor so I don’t know how to interrogate the code at this point. The closest I get is shown in the image below. What’s the easiest way to pinpoint the offending code? I know in some instances you can trace it back to the javascript code and paste it into a document reader with line numbers (e.g. notepad++) but in this instance I’m stuck.

@TheSav , you will also need to import the conditions file via experiment settings - online tab using “additional resources”, so that the PsychoPy compiler picks up the file and sends it to Pavlovia.

Not sure about that error, but try opening the “null” and “FATAL” tabs, and hopefully there will be a link to your JS file showing where the error is.

Thank you David, I managed to use that code to get it to work. The javascript error appears to be specific to my machine and more specifically only chrome. It runs on others’ machine in chrome but only on firefox on mine.