4

I am creating a probabilistic learning task. It has a learning phase which is what I am preparing at the moment using the builder interface with custom code in PsychoPy. The learning phase has at least 60 trials in a loop called practice.

Apart from the correct answer which is used to give feedback to participants, there are three conditions by which it can be decided whether the learning phase can be finished. Once the criteria are reached, the training phase will be terminated.

I need to append three variables and keep count of the scores. I have created 3 variables (resembling the resp.corr variable) calculated specifically for the three conditions. The code is placed in the ‘end routine’ section because in the earlier sections resp was not yet defined.

End routine

if (resp.keys == letterA):
    resp1 = 1
else:
    resp1 = 0

if (resp.keys == letterC):
    resp2 = 1
else:
    resp2 = 0

if (resp.keys == letterE):
    resp3 = 1
else:
    resp3 = 0

This is working fine. I added variables resp1, resp2 and resp3 to the excel output. I checked and they are all calculated correctly.

So i know that I need to append these variables in a list and I used the following code:

End Routine

resplist1.append(resp1)
resplist2.append(resp2)
resplist3.append(resp3)

I also saved these lists in the excel output to check if they are calculated correctly. I used the following code:

End Routine

practice.addData('resplist1', resplist1)

practice.addData('resplist2', resplist2)

practice.addData('resplist3', resplist3)

Unfortunately, replist1, replist2 and replist3 fail to append the list. Instead the values of resp1, resp2 and resp3 are printed in []. Picture at the bottom:

imageimage.png1635×655 34.1 KB

I also checked whether it was possible to calculate the sum of replist1, replist2 and replist3 and as you can guess it did not work. The calculated values were exactly the same as resp1, resp2 and resp3.

I am not sure why the list is not being appended correctly and I will appreciate all help! I have been stuck on this task for way too long now and I am desperate to have it finished.

https://i.stack.imgur.com/aXW60.png

  • The problem comes after `.. resplist3.append(resp3)` etc. when you come to do the writing to the file, but you haven't shown that – roganjosh Mar 14 '19 at 19:50
  • Thanks! I added it there now – Marta Topor Mar 14 '19 at 19:55
  • `resplist1` will be a list with 1 element in it, if you just try to print it as is it'll print the list (with the surrounding [] ) When you print you should access the 0th element as `repslist1[0]` to get just the number without the `[]` – Hoog Mar 14 '19 at 19:56
  • Maybe I wasn't clear with the fact that there are at least 60 trials in the training phase, so I would expect `resplist1` to have 60 elements so that I can then calculate the sum of the scores in that condition after each trial. How can I achieve that? – Marta Topor Mar 14 '19 at 20:12

1 Answers1

1

You don't show how you initialise your lists resplist1 etc. My guess is that you are doing so in the "Begin routine" tab. That would continually reset them so that they never contain more than one value.

If so, shift their initialisation to the "Begin experiment" tab so you don't lose the newly appended values on every trial.

When you have a list that actually contains multiple elements, the easiest way to do calculations upon it is to convert it to a numpy (imported as np) array, which allows vectorised operations, e.g:

sum_1 = np.array(resplist1).sum()
Michael MacAskill
  • 2,411
  • 1
  • 16
  • 28
  • Thank you so much Michael! This makes so much sense and the issue is now resolved. I have overlooked this part of the code - can't believe it was that simple! – Marta Topor Mar 15 '19 at 13:44