psychopy.org | Reference | Downloads | Github

Presenting Text Stimuli from a list of numbers and letters (Coder)

I am trying to present up to 50 stimuli in a row from a list with Psychopy coder. I previously used python for data analyses, but I have almost no experience of visual programming.
I read that in psychopy one can present stimuli as an an x,y-pair (two numbers; see below), therefore, I would like to ask: is it possible to present more stimuli in a sequence? It is not a problem to do this with builder, but how can one accomplish this same task in coder?
My problem is that sometimes the stimuli are joined in groups up to 4 depending on the complexity of the task and one also has to account for the stimuli that both precede and follow the group of the given 4 stimuli at all times during the block. I solved this issue by writing .py scripts that have the correct stimuli sequences (and correct replies attached to them). Now my issue is just to run these sequences in a sequential order with psychopy, but coder either:

  1. takes the last values only from my xlsx. file
  2. does not allow me to print out any values from the list, if I create a list in my coder .py file.

Hi. I’m afraid you’re going to need to give a much more precise description of:

  • what you want to achieve
  • how you are currently try trying to implement that
  • how it goes wrong

At the moment, it isn’t really possible to give you useful suggestions with the current information.

Sorry for being unclear. I want to present 50 visual.text stimuli (letters and numbers) which will be presented in a specified sequence one after another. I wanted to do it in coder. However, if I try to use excel file (as in Builder), coder only takes the last stimulus (that is stimulus number 50) and repeats it in a loop. If I try to create a python list and put it in my script and run it with psychopy, psychopy refuses to do that :S I think choosing a list [] is a problem or something like that.
For example, with python I would simply create a for loop in such case and take all the items from my list sequentially. However, Psychopy does not do this and I do not understand why…
sorry for being unclear, I don’t know how to explain any better

You can explain by showing us your code. It isn’t “Coder” or “PsychoPy” that is refusing to do what you want, it’s your code :wink:

Strip it down to the essentials: showing us an entire experiment is likely too much to wade through.

I managed to make it take the loop and run it. However, it takes the whole list and presents all items simultaneously instead of sequentially. The loop and list are now commented. The question is: what line of the code I need that the psychopy would run the python loop: it should take each item from the given list and present items one by one until the list is completed? Please see the code below:

Letter.py (6.4 KB)

Hi!
I’m not sure, but I think there are still several issues in your code that might not work.
You’re creating a single Stimulus with your list of letters as text, that’s what is displayed correctly. Consider to create the Stimulus for each letter inside the loop, or to change its text accordingly.

Another thing I’ve noticed, is that you’re using a global variable inside your function Routine_Faces. You need to mark that as global with global LocalStart in order to make it work.

You might also want to review your if elif else statements in Routine_Faces, I’m not exactly sure what the algorithm is supposed to do, but the logic behind it can definitely be improved.

Dear Robin,

thanks a lot for trying to help. This was my question already in the beginning, how one simply creates a ‘for loop’ and presents the stimuli from that loop sequentially but in psychopy (because I know how one does it in python)?
I understand that your suggestion was to create something like: stimulus0, stimulus1, stimulus2 etc (please note that 5 letter list was an example, I will have 50 list items in each block and 12 blocks in total). Such solution would work if I only had 5 items in my loop…
Or did I misunderstood you and you meant something else?

Hi Monika,
no that was not what I meant. I was proposing something like

my_stimulus = visual.textStim(text='', ...)
my_letters = [A, B, C, ...]

for letter in my_letters:
    my_stimulus.setText(letter)
    my_stimulus.draw()
    win.flip()
    ...

however, I’m new to Psychopy myself, so I’m not sure if I’m missing an essential point here. I tried to understand the code you provided and I feel like you’re over-complicating things a bit. You mentioned before, that first attempts worked in builder, including the conditions file. Did you try the compile option, to view the parsed Python code of you builder project? That might be an easier point to start from.

Dear Robin,

Your suggestion worked partially, meaning that Psychopy loops just the first letter of the list, in your example that would be my_letters[0]. Hypothetically it just misses one line which tells the program to loop in such way [:]

No, Builder code creates some sort of the trial handler for this. It is more complicated than the code I have (at least for me).

Hi Monika,
well, if you’re not looping over your list of letters, the letter will not change.

I still don’t fully understand your code, but what you want to achieve can probably be done by changing the stimulus text like this:

        while(Cnt3<PauseStop):
            if Cnt3==0:
                print 'ok'
                letter_on.setText(stimuli[random.randint(0, len(stimuli)-1)])
                # or alternatively:
                letter_on.setText(stimuli[my_stimulus_counter])
                my_stimulus_counter += 1

Here I just pick a random character from your list, if you want to display them sequentially you would need to add another counter.

Hi again Robin and thanks a lot for all your help :wink:

Actually I worked out a sort of “dumbed out” version of the solution. Apparently if I have just a string, for example:

K 2 L R and I create a list in my psychopy script:
words = “K 2 R L”.split()
text1.setText(word)
text1.draw()
win.flip() {I am also specifying the letter presentation time and gap time in the for loops}

In that case psychopy takes my strings, makes a list on its own and present it sequentially :smiley:

I took the script from my colleague, that is why it was so complicated (she had a more complex task), but I “juggled” with her script for several days and decided to make my own simpler script from scratch.