0

In my experiment I'm showing a random generated stimulus 'x', which I need to compare to a key that's been giving in by the user of the experiment. Basically, I have two lists:

  • one with the stimuli
  • and one with the correct answers (the keys they should give)

The order is the same, by which I mean that stimulus 1 should get the key that's at 'place 1' in the list with answers.

I've searched several topics on how to compare these two lists but so far it hasn't been working.

These are the options I've tried:

Answerruning = True
while Answerrunning:
    if event.getKeys(keyList):
      ReactionTime.getTime() 
      Keys = event.waitKeys()
      for givenKey in Keys:
          if givenKey == keyList:
              answer_stimulus = 2
              Answerrunning = False
              window.flip(clearBuffer = True)
    else:
      answer_stimulus = 0

And this option but I think the other one is better:

keyList = []
givenKey = event.getKeys(keyList)
Answerrunning = True
while Answerrunning:
    for x in stimulus:
        if givenKey in keyList:
            ReactionTime.getTime()
            answer_stimulus = 2
            Answerrunning = False
            window.flip(clearBuffer = True)
        else:
            answer_stimulus = 0

I hope one of you can give me a hint on the problem how to compare those two en from there on my window will clear en the experiment can go on.

R Nar
  • 5,465
  • 1
  • 16
  • 32
vj93
  • 1
  • 2

1 Answers1

3

You don't mention this, but you really need to be using a TrialHandler object http://www.psychopy.org/api/data.html which will handle the variables for you, stepping through your conditions file (.xlsx or .csv) a row at a time for each trial. i.e. don't put the stimulus and correct response values in lists: put them in an external file, and let PsychoPy do the housekeeping of managing them trial by trial.

If you have a column in that file called correctResponse, another called stimulusText, and a TrialHandler called trials, then some pseudo-code would look like this:

trialClock = core.Clock() # just create this once, & reset as needed

# trials is a TrialHandler object, constructed by linking to an 
# external file giving the details for each trial:
for trial in trials:
    # update the stimulus for this trial.
    # the stimulusText variable is automatically populated
    # from the corresponding column in your conditions file:
    yourTextStimulus.setText(stimulusText)

    # start the next trial:
    trialClock.reset()
    answerGiven = False

    while not answerGiven:

        # refresh the stimuli and flip the window
        stimulus_1.draw() # and whatever other stimuli you have
        win.flip() # code pauses here until the screen is drawn
        # i.e. meaning we are checking for a keypress at say, 60 Hz

        response = event.getKeys() # returns a list

        if len(response) > 0: # if so, there was a response
            reactionTime = trialClock.getTime()

            # was it correct?
            if correctResponse in response:
                answer = True
            else:
                answer = False

            # store some data
            trials.addData('Keypress', response)
            trials.addData('KeypressRT', reactionTime) 
            trials.addData('KeypressCorrect', answer)

            # can now move on to next trial
            answerGiven = True

PsychoPy code is generally constructed around a cycle of drawing to the screen on every refresh, so the code above shows how within each trial, the stimulus is updated once but redrawn to the screen on every refresh. In this cycle, the keyboard is also checked once every time the screen is redrawn.

In your code, you are mixing getKeys(), which checks the instantaneous state of the keyboard, and waitKeys(), which pauses until a response is given (and hence breaks the screen refresh cycle). So gerenally avoid the latter. Also, when you use getKeys(), you have to assign the result to a variable, as this function clears the buffer. Above, you use getKeys() and then follow up by checking the keyboard again. In that case, the initial response will have disappeared, as it wasn't stored.

Clear as mud?

Michael MacAskill
  • 2,411
  • 1
  • 16
  • 28