Trying to add feedback after a Block in builder, getting Key Error

OS (e.g. Win10): Mac OS
PsychoPy version (e.g. 1.84.x): 3.1.5
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?:

Hello there, I’ve been following the instructions here Builder - providing feedback — PsychoPy v2021.2 to try to give participants feedback on how they did during the previous block.

I created a loop, with a text component called “text_13” and a keyboard component called “PlzWork”. Both of these are set to every repeat. I also checked “store correct” and references the correct answer in my excel sheet. The loop runs fine on its own.

However, I am running into issues with feedback. During the following feedback block, I used the exact syntax recommended on the website
nCorr = trials.data['PlzWork.corr'].sum() #.std(), .mean() also available meanRt = trials.data['PlzWork.rt'].mean() msg = "You got %i trials correct (rt=%.2f)" %(nCorr,meanRt)

I made sure to put the code component above the text component in the feedback block, and the text component is this: $msg + "/n Press space to continue"
It is also set to every repeat.

I keep getting the following error:
nCorr = trials.data['PlzWork.corr'].sum() #.std(), .mean() also available KeyError: 'PlzWork.corr'

I’m confused as to what’s happening. I checked to make sure that it was logging the data, and it appears to be doing so. There is indeed, in the output excel sheet, a column called “PlzWork.corr”.

The only thing I can think that may be a problem is that because it is the 3rd or 4th loop in the experiment, there are some empty values in the column?(picture below). Would this be an issue? If so, how does one solve this?

I think the problem here is the order in which functions are executed - the key 'PlzWork.corr' is created when the data from PlzWork is added to your trial handler at the end of the Routine. Your code however is called at the beginning, so in the first trial it hits that error because trials.data['PlzWork.corr'] doesn’t exist yet.

You could solve this by putting that line in an if statement:

if 'PlzWord.corr' in trials.data:
    nCorr = trials.data['PlzWork.corr'].sum()
else:
    nCorr = PlzWork.corr

That way, if the data doesn’t exist in the trial handler yet, it just takes what will be the first value (i.e. the current value of PlzWork.corr)

Hi @TParsons, thanks for your reply! Just to clarify- I’m creating PlzWork.corr in one routine (theoretically) where the participant is responding to stimuli, but my feedback routine is a separate routine entirely, and it follows the first routine. So theoretically, shouldn’t PlzWork.corr exist by the end of routine 1, meaning code that references it at the beginning of routine 2 should work?

Either way- your solution seems to help- but one issue. When the else condition is satisfied, (nCorr = PlzWork.corr), it seems to be giving me the last value in the vector, and not the entire vector. Would you happen to know how I can tell it in the else condition that I’d like the sum of the vector or the mean of the vector?

I would expect the else condition to only be met in the first trial, in which case the sum should be the same as the last value, could you share an example data file so I can see which trials the else condition is being met?

Sure! Attached is an example data file. When feedback is presented, it’s presenting the last row- so in this case it would be 1 trial correct and an RT of around .508 5678_FlankerFixedRSI_SONA_2020_Sep_22_1414.csv (28.6 KB)

So PlzWork.corr looks to be saving correctly, but where in this file is the value of nCorr saved? If it isn’t, could you add something like trials.addData('nCorr', nCorr) so that it is included?

Hi @TParsons, that didn’t seem to work either sadly. nCorr is added, and I can see that both it and PlzWork.corr is being saved correctly, but for some reason unbeknownst to me, PsychoPy will NOT recognize these variables in the following routine. It will only recognize the final trial that occurred, not the entire vector of 0’s and 1’s or the entire vector of RTs.

Could you send a csv output with nCorr saved? If it’s in the final output but Psychopy can’t see it in the next routine, it’s probably that it’s being added after you’re trying to access it, but seeing what nCorr is saved as in the data file will help

4567_FlankerMixedRSI_SONA_2020_Sep_28_1238.csv (1.4 KB)