psychopy.org | Reference | Downloads | Github

Hit or Miss results in output file

Windows 10
Psychopy V 3.14
Yes, standalone

My experiment is 2IF. I have in my csv file a column name “correctAns”. For each trial there are two possible answers, either [‘s’, ‘x’] or [‘k’,‘m’]. In the condition where ‘s’ or ‘x’ is a correct answer, I have “[‘s’,‘x’]” in the relevant row/column. For conditions where ‘k’ or ‘m’ are correct, I inputted “[‘k’,‘m’]”. In my response portion of the trial, I have $correctAns in the box labeled “correct answer”. As of now, when I do the experiment, in the output file I can see what key was pressed for each trial. In order to easily obtain a percentage of correct responses, I want to have in the output (csv) file either 1 for hit or 0 for miss for each trial. My experiment uses a TrialHandler to manage the conditions file.

Hi @ndelasoul, the keyboard cannot check for a correct answer from a list of correct answers, but you can do this easily enough with a bit of code in the code component:

In the end routine tab:

if key_resp.keys in correctAns or key_resp.keys == correctAns:
    correct= 1
else:
    correct= 0
    
thisExp.addData("correct", correct)

Now you will have a new column called “correct” which contains your correct answer data.

dvbridges, thank you for the suggestion. I tried entering the code and changed it according to my experiment and it looks like this:

if resp_trial.keys in correctAns or resp_trial.keys == correctAns:
    correct= 1
else:
    correct= 0
  
thisExp.addData('result', correct)

unfortunately, it returned the value “None” for each trial. Shouldn’t this piece of code that’s already in the experiment do what I am looking for?

if resp_trial.status == STARTED:
            theseKeys = resp_trial.getKeys(keyList=['s', 'x', 'k', 'm'], waitRelease=False)
            if len(theseKeys):
                theseKeys = theseKeys[0]  # at least one key was pressed
                
                # check for quit:
                if "escape" == theseKeys:
                    endExpNow = True
                resp_trial.keys = theseKeys.name  # just the last key pressed
                resp_trial.rt = theseKeys.rt
                # was this 'correct'?
                if (resp_trial.keys == str(correctAns)) or (resp_trial.keys == correctAns):
                    resp_trial.corr = 1
                else:
                    resp_trial.corr = 0
                # a response ends the routine
                continueRoutine = False

This is also in the script already, but it just returns zeros for each trial in the output file.

trial_loop.addData('resp_trial.corr', resp_trial.corr)

So I tried editing my csv file and put just one value for each condition under the column “correctAns”. The code you suggested worked. The code that is already in the builder script worked as well. So, is it possible to have this work when there are two possible answers for each trial?

The Builder code will not work, because it will compare a single response to a list of responses.
The following code (now changed because you only have pairs/lists of correct answers) should work:

if key_resp.keys in correctAns:
    correct= 1
else:
    correct= 0
    
thisExp.addData("correct", correct)

If you remove the keyboard checks for the correct answer, but still end the trial on each keypress, whilst only storing the first or last response, this should work. You just need to make sure the correctAns column is formatted correctly. So, add the list as ['x', 's'] in the excel sheet without quotation marks around the brackets.

Please see this example attached…
corrAns.psyexp (8.7 KB)
cond.xlsx (7.8 KB)

It worked! As you put in your example, having the code under the End Routine tab was important for making it work. Thank you very much.