# How to experiment with random changes in shape and color of target stimuli in Builer

OS : Windows 11
PsychoPy version : 2023.1.2
Standard Standalone? (y/n) If not then what?: y
Hello guys:
I am an undergraduate psychology student , and I am recently setting up an experimental design on attentional selection guidance modality - statistical learning, in which I am going to use the additional singleton paradigm. this paradigm is mainly about searching for a singleton shaped target that suppressing a singleton color distractor (in which the target and distractor colors are randomly changing). I used Builer and code components in my experimental design, and used the following code in the code component:
import random
for i in range(5).
a=random.randrange(0,2)
if a==0.
targetshape=99
else.
targetshape=4
And targetshape is used as a variable in the builderâ€™s settings for stimulus shape presentation. Then the whole experiment was randomly presented 5 times. But the shape of the graph is the same in each cycle, and it is not randomized. How to solve this problem. I am using version 2023.1.2
Thank you very much for your help!.

Hello,

there is no need to use

import random

because the Builder automatically imports numpy.random for you. See here

import numpy as np  # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
from numpy.random import random, randint, normal, shuffle, choice as randchoice

By using

import random

you overwrite the library the Builder imports for you

You need to give us more information how you try to achieve this. So, where and when do you create the random values? When and how do try to set the shape? It might be better to change the opacity of the shape instead of changing the shape itself.

BTW: a is a variable, not a list or tuple that holds multiple values. If you want to create a list with five random numbers, you need to do something like the following

a = [randint(0,2) for i in range(5)]

or

b = []
for i in range(5):
b.append(randint(0,2))

Best wishes Jens

Hi JensďĽŚ
Thank you very much for your answer. The problem I am trying to solve is how to achieve a random change of stimulus shape per trial by Builer. My idea is to use the above code to achieve this and use the targetshape variable for the definition of stimulus shape , and then make the whole experiment repeat 10 times. However, the operation did not result in a random change in the shape of the presented stimulus for each trial, but rather the shape of the presented stimulus was the same for each trial, e.g., the shape of a stimulus was round or diamond for the previous trial, and the shape of a figure became round or diamond for the latter. How can I implement my experimental design?
Thank you very much!
Hu

Hello,

Well, I think I understand what your problem is and what you want to achieve. But I do not see how you have tried to achieve it. You have shown us some code that does not work. You did not show us how you tried to assign the targetshape variable to a component parameter. You did not show us where you put your code (Begin experiment, Begin routine aso.).

You are probably using the polygon component. AFAIK, the polygon component does not allow you to dynamically change the number of its vertices.

https://www.psychopy.org/builder/components/polygon.html

You may want to try another approach.

Best wishes Jens

Hello,

I think I have found a solution. Create a code component that sits on top of your polygon component.

Initialize the variable targetshape in the Begin experiment tab.

#diamond
targetshape = [[1,0],[0,-1],[-1,0],[0,1]]

We use a shape that we will not use in the experiment. If a diamond appears, something has gone wrong.

In the Begin routine tab add:

a = randint(0,2)
if a:
targetshape = [[.707,-.707],[-.707,-.707],[-.707,.707],[.707,.707]]
else:
targetshape = [[.866,-.5],[-.866,-.5],[0,1]]

This gives you a square and a triangle. Adjust the vertices to suit your needs.

Assign targetshape to the Vertices-parameter of your code-component

Set it to set every repeat. You may want to add a line of code to store the value of targetshape.

Best wishes Jens

1 Like

Hi Jens,
Thank you very much for your help, I have solved the problem using your method. But I have another question, why I canâ€™t achieve the above result with the variables in the condition file, and the program also reports an error: variable not defined. Is this because the variables in the condition file cannot be used to define the stimulus shape.
Thank you once more for your kindness.
Yours truly hu

Hello,

better start a new topic once it has been marked to contain a solution. Post in the new topic your approach, screen shoots of the excel-file aso.

Best wishes Jens