Randomly Select the Beginning of a Sequence


How can I select a random starting point to run range of rows in a csv for each trial? For example, in a twenty line csv, how could I play a sequence of five sounds starting at a randomly selected point from 0 to 15 for that trial and then select a different starting point on the next trial?

What am I trying to achieve?:
I’m trying to create an audio serial response time task where participants respond every time they hear a syllable. The syllables occur at a fixed point in three-syllable nonwords. However, there are four three syllable words that need to be played in a pseudorandomized sequences (words cannot immediately repeat). Further, don’t want the sequences to always begin at a word boundary.

I’ve figured out how to run my training sequences, but for my testing sequences, I want 36 trials and in each trial I want to play a sequence of 48 syllables (ie, the four nonwords four times each in a pseudorandom order).

Because the timing that each syllable begins is important and because of the large number of potential sequences, what seems most logical to me is to create fairly long .csv with all allowable syllable sequences and then just select a range of lines/rows in - Loop - ‘Selected Rows’, with each row instructing Psychopy to play an individual syllable.

The basics of the experiment I’ve worked out and I have an elementary understanding of how to add code to the experiment (I found this useful - Help with randomization). The issue is how to get random fixed sequences to play for the trials.

Screenshots of my Minimal (non)Working Example follow:
The flow randomly selects the syllable to identify, then plays the syllable (this works), then plays the sequence in which to identify it (this doesn’t work).

See below for the rest of the Builder screenshots

I was getting an error that said:
TypeError: object of type ‘numpy.int64’ has no len() psychopy

But now the problem is:

Any help would be appreciated, or if someone could direct me to an example experiment that is closer to what I’m looking for.




I probably don’t need to record ‘seq_start’

Not sure why it says the conditionsFile is not found - it is - this isn’t a problem.

Hello Brian,

I think that you have two problems here.

  1. AFAIK you can’t refer to a loop, select_X_add_48, in its definition. This gives probably the error select_X_add_48 is not defined.

  2. seq_range[select_X_add_48.thisN] used elsewhere would return thisN-th element of the list seq_range. But in useRows you need a range, e.g. seq_range

  3. Do you plan to run the experiment online? If so, then
    from numpy import random
    won’t work. But that is not a problem because the builder imports the relevant (and available) online JS-libraries. So

seq_range = []
seq_start = randint(5)
seq_range.extend(range(seq_start, seq_start + 5))

should work. (I am not quite sure if range will work online but there is nothing mentioned in the crib sheet. At least it did not work in older PsychoPy-versions.).

Best wishes Jens

Hi Jens,

Thanks for the response. I’m (obviously) new to PsychoPy but your post helped me better understand how everything works. Then, I was able to rethink what I was doing and got it to work.

I don’t plan to run it online, but I also eliminated numpy just in case.

Thanks again.