Hello all,
I’ve been working off of this tutorial to create an n-back with randomized stimuli in the PsychoPy builder with custom code; but this example doesn’t include all of the script needed.
Essentially, it is supposed to select a random letter from a list to present, but select the same letters as n items back whenever it is supposed to be a ‘target’ trial (based on a spreadsheet used in a loop). The tutorial code uses np.random.choice() which doesn’t work for javascript, so they say to use shuffle(). But this isn’t shown in the example, nor is the code for displaying the randomly selected letter.
After much trial and error and finding something approximating the missing code in this experiment, the code runs but it doesn’t actually display the randomly selected letter.
While I could just make everything based off of a fixed spreadsheet, I would rather participants did not always see the same stimuli in the same order.
I also cannot make this fully random because each participant needs to see a certain number of ‘target’ stimuli in order for scores to be comparable.
So far the python code looks like this (for a 1-back condition):
(in ‘begin experiment’)
letters = [‘B’,‘F’,‘K’,‘M’,‘Q’,‘R’,‘X’]
presentedLetters1back =
(in ‘begin routine’)
n = 1 # specify the n condition
letterSelected = False # a boolean to state a letter has not yet been selected
thisLetter = “0” # predefine the letter to zero based. Note that changing this does not make the stimuli appear.if not target: # if this is not a target trial
while not letterSelected:
shuffle(letters);
thisLetter = letters.slice((-1))[0];
if len(presentedLetters1back) < n or thisLetter != presentedLetters1back[-n]:
# if n letters have not been presented or this is not the same as the nth trial back
letterSelected = TruepresentedLetters1back.append(thisLetter); # add to list of presented stimuli
stimulustext_1back.setText(thisLetter); # add randomly selected letter to text component
Things I’ve tried and other settings:
- defining thisLetter with other randomization methods including Math.random().
- shuffling letters to a new variable and then selecting thisLetter as the first item from that variable
- the code component is the first component for the routine in builder view.
- the stimulustext_1back is set to “set every repeat”.
- I am not getting any errors when psychopy automatically translates this to javascript
- setting the text to thisLetter.toString().
- I cannot find the accompanying files to this demo.
I also don’t know how to check if thisLetter is even being defined properly. I can’t run it in the shell of the coder window because I don’t know how to get psychopy to add in until. or math. for javascript.