Python Code Question

Hi All,
My experiment uses the following code in PsychoPy to essentially randomize rows and columns, but it will not work correctly on Pavlovia. My original post about the details is here: Randomizing Columns and Rows

Essentially the code helps to randomly choose a row from my conditions file, randomize the list of three items in the row, and play the audio for each of those randomized items. This code works great on PsyhoPy Builder, but when I moved it to Pavlovia, the items are not all being played. For example, sometimes the sequence plays as “Tone1, Tone1, Tone1”, when it should always be some variation of “Tone1, Tone2, Tone3” in whatever order. Sometimes all tones play, but usually one of the tones plays more than once and not all tones are played. I have checked to make sure that the variables are labelled correctly in the audio properties and that it is set every repeat. I’ve also made sure my loop is set to random. My actual experiment code can be found here: https://gitlab.pavlovia.org/jfeiler/m-tone

I have added the following, per the Crib sheet, to my “Begin Experiment” tab on the code sequence:

thisExp=psychoJS.experiment;
tones = [];
shuffle = util.shuffle;

Code that’s not working correctly:

# only on the first iteration per trial:
if trials.thisTrialN == 0:
    tones = [Tone1,Tone2,Tone3]
    shuffle(tones);

# record the order in the data file:
thisExp.addData('first_tone', tones[0])
thisExp.addData('second_tone', tones[1])
thisExp.addData('third_tone', tones[2])

# pull one out of the hat for this trial:
current_tone = tones.pop()

`

I’d really like to begin piloting my study, so any help would be GREATLY appreciated.

I know that .thisN doesn’t currently work, so I suspect that .thisN doesn’t work either.

Would you get your observed effect if this code never runs?

@wakecarter Oh really? Didn’t know that.

Do you mean if the randomization code didn’t run? If so, no I wouldn’t get the effect. It’s one of the most important parts that this code run because participants need to identify the tone of the word they hear, and if they’re always played in the same order, it loses its effect.

I use a dummy variable (Idx) which I increment at the end of each loop.

Any chance you have an example @wakecarter ? I’m not great with coding so I don’t entirely know how it work.

Begin Experiment

Idx = 0

In the loop

if Idx == 0:
    tones = [Tone1,Tone2,Tone3]
    shuffle(tones);
Idx +=1

Thanks @wakecarter! This works for the first sequence of tones (1, 2, and 3), but then a loud single tone plays continuously, and no other tone sounds play even though there are six more trials of three tones. Is there something I need to adjust with the audio? Here is my current code:

if ((Idx === 0)) {
    tones = [Tone1, Tone2, Tone3];
    shuffle(tones);
}
Idx += 1;
current_tone = tones.pop();
thisExp.addData("first_tone", tones[0]);
thisExp.addData("second_tone", tones[1]);
thisExp.addData("third_tone", tones[2]);

Do you just have three notes played in different sequences?

What I do for that is set up the notes as three sound components in an earlier routine and then play them using code.

For example (in Each Frame)

if t>startTime+noteLength:
    if nowPlaying == 1:
        note_1.stop()
    elif nowPlaying == 2:
        note_2.stop()
    elif nowPlaying == 3:
        note_3.stop()
    elif nowPlaying == 4:
        note_4.stop()
    nowPlaying = 0
    Idx +=1


if nowPlaying == 0:
    if Idx == len(Sequence):
        print('End of sequence')
        continueRoutine=False
    else:
        startTime=t
        if Sequence[Idx]=='1':
            nowPlaying=1
            note_1.play()
        elif Sequence[Idx]=='2':
            nowPlaying=2
            note_2.play()
        elif Sequence[Idx]=='3':
            nowPlaying=3
            note_3.play()
        elif Sequence[Idx]=='4':
            nowPlaying=4
            note_4.play()
            

@wakecarter No, it’s actually different Mandarin words that play each time. For example, trial 1 might be an audio recording of a Mandarin speaker saying ba1, ba2, ba4 (with the numbers representing the Mandarin lexical tone of the word), and trial 2 might be piao1, piao2, piao4. There are seven words each with three tones that need to play, and I need each word (ba, piao, etc.) to play fully through for each tone (1, 2, and 4), but I need the order in which 1, 2, and 4 play to be random and I need the order of each word (ba, piao, etc.) to be random as well.

That’s why I was originally using the shuffle(tones)to randomize the three tones of each word and then the current_tone = tones.pop() to pick one out randomly and play it by putting the current_tone variable in the sound component. Because of an inner loop set to n = 3, the sequence repeats three times and never picks the same tone twice for a word on PsychoPy builder. On Pavlovia, the same word# plays more than once sometimes. Here are some screenshots (spreadhseet, sequences, and loops) if that helps at all.

![1|690x183 (upload://wYS1MrvWP362c8bb0ymnmb69aCr.jpeg)