Recording data in CSV incorrect

Hi everyone,

In my experiment I have a list of locations in which an image stimulus is presented. Participants are required to press the spacebar if they see the image presented in the same location consecutively. I have coded a current location and last location, and I know this works correctly as it records that data perfectly.
I have created it so that in my csv it should tell me true or false if currentlocation = lastlocation. This works perfectly locally, but on Pavlovia it does not record correctly.

this is how I have created the list of locations:

this is how I have coded current and last location

Now this is the section in which it should record if currentloc === lastloc, and for some reason online it doesn’t work. I’ve tried replacing currentloc with locs[idx] and last loc with locs[idx]-1 and that did not work either. It is coming up false when it should be true.

I’ve also added a console log in to see when it records currentloc, lastloc, and the currentloc === lastloc. The locations are recorded correctly as you can see, but even when the 2 locations are the same it still comes up as false

Here is an example of how it comes up in my csv, and I’ve highlighted where a true response should be

Does anyone know why this is? Or a work around, it would be much appreciated!!

Hi @emily01, I think js is just not able to compare two arrays in their entirety. You could work around that by condensing both arrays to a string first, and then comparing both strings. So,

currentloc.join() === lastloc.join()

Hope that helps!

hi @ajus , thanks for the help. I’ve tried doing as you suggested and put in my endroutine tab
psychoJS.experiment.addData(“SMALLADAPT_PRESSYES”, (currentLoc.join() === lastLoc.join()));
it returns the error * TypeError: Cannot read properties of undefined (reading ‘join’)

any idea on how to fix this?
Thanks!!

To debug this, you could console.log() currentLoc.join() and lastLoc.join() before trying to save anything. If this does not lead to any insight there is also an alternative to check if both arrays are identical:

((currentLoc[0] === lastLoc[0]) && (currentLoc[1] === lastLoc[1]))

@ajus I tried implementing this:

and it now returns the error

I’m not too familiar with python or java as this is my first time doing something like this, but is there a problem with my currentLoc and lastLoc definition? In the console log it seems to be defined well and pulling the relevant data but then when it comes to these error messages it’s always related to the definition. What does that mean? Do you know how I can fix this?
Thanks for the help :slight_smile:

It’s not obvious to me, what’s going wrong. Could you make the experiment available to download, so I can have a look?

@ajus here is the github link, I’ve made it public. Emily Deschacht / randalloc · GitLab
here is the pavlovia link also:
randalloc [PsychoPy]

thank you!

Hi @emily01, the problem was that for the very first trial/presentation (idx = 0) there is no lastLoc (i.e., lastLoc is undefined. You can replace your code

lastLoc = locs[(idx - 1)];

with

 if(idx == 0){
    lastLoc = ["NA","NA"]
    }else{
    lastLoc = locs[(idx - 1)];
    }

This made it work for me.

Hi @ajus , thank you so much for your help. Your solution also worked for me! I do have another question that I hope you can help with. When I put the exact same code and format into a larger experiment, it does now record the true/false correctly, however for the first record of lastLoc where it should be [“NA”,“NA”] (as it is in the smaller experiment), still come up as whatever currentLoc is. Everything is the exact same word for word but it is just slotted in as part of a much bigger experiment. Do you know why that might be or how I could fix it?

I’m not sure if this is related or if I should make a new forum post for this. But another problem I have when I insert this small experiment into my full one, I occasionally receive this error message after a couple of image stimulus presentations of the different locations. It does not happen every time I run the experiment which is what confuses me, and the number of iterations it occurs after is also different each time.

Thank you!!

I’ve since figured out the [“NA”,“NA”] problem by console logging, it was to do with the value of the first idx starting at 30 instead of 0 for some reason, I renamed idx to index and it seems to work now, possibly a clash with another variable named idx elsewhere in the code. For the last few iterations I’ve also not had the error message, but it’s still hard for me to know if it will come back/ the root cause. I was thinking that maybe as the idx value went above the possible number of locations, but still confusing as to why it happened after different numbers of presentations each time. I can’t be sure of the starting idx for the others unfortunately.

Hi @emily01, good to hear!

I don’t quite understand what you mean by that. What can’t you be sure of and who are the “others”?

hi @ajus, by others I mean the starting idx value for the previous pilots that I ran in which the error occurred.

Ah ok, understood. I guess if it consistently works now this might have been the problem. To give yourself more peace of mind, you might want to go through your js script and search for idx / index and see if at any point something happens to or with this variable that you are not aware of or that could result in undefined values.

amazing, thank you so much for your help!