| Reference | Downloads | Github

How to pull a random subset of conditions based on one parameter?

OS (e.g. Win10): Win 7 64bit
PsychoPy version (e.g. 1.84.x): v.1.83.04


In my study, I present two images per trial for participants to respond to. I have many different parameters that specify how the two images are presented (image itself, position, size, etc.) so I have 192 unique conditions in my conditions file.

I would like to create 4 blocks of trials, with 128 trials each. What is important is that within each block of trials, I need an equal distribution of trials based on one specific parameter in my conditions file. There are 4 different values under that parameter, so 128/4 = 32 trials per value per block.

I would like to know how to randomly select a subset of conditions, but while keeping an even distribution based on one specific parameter. the other parameters don’t really matter so it’s ok if they’re a bit unbalanced at the end. The random selections should be without replacement.

I’m also not familiar with python so I would prefer a builder-only answer, but if it has to require code, if you could please explain to me what each line does so that I know for reference (I’m familiar with some coding language, as I use R for my analyzing).

I found some help in the psychopy website under for subsetting conditions using this code: random(4)*10 (to subset 4 random conditions from 11 conditions), but it didn’t see to work for me. I get the error message ‘NoneType’ object is not iterable.

Thank you very much!

Hi Ronda, yes, this gets complicated. It will need some code, but that can be incorporated within a code component in Builder. The Builder loop interface can’t possibly cater for the infinite number of possible designs, and so a specific combination of balancing and partial sampling like this will require some custom code to be inserted here and there.

To proceed, could you list each of your variables by name, how many levels each variable has (ideally with the values of those levels if feasible), and indicate which is the one that needs the balanced distribution.

In essence, I think we’ll need to create a conditions list programatically before each run of the experiment, sample from it randomly and selectively while maintaining balancing, and then pass that list through to the trial loop, to be stepped through sequentially.


Parameter names:

targetShape - 24 levels (all are image file paths so no use writing it out)
distShape - 4 levels (image file paths)
corrAns - 2 levels (left, right)
distPos - 2 levels ([-500, 0], [500, 0])
compatibility - 4 levels (compatible, incompatible, neutral, nodist)

These are the important parameters. I have a few more but they are just reiterating what is in the targetShape and distShape files so its easier for me to analyze data later.

The variable I need to have balanced distribution is “compatibility”, the last one!

In a full factorial crossing, that would give 1536 combinations. Perhaps distShape and distPos I’m guessing are simple factors, independent of everything else, but I guess corrAns and compatibility have to be related to the images in some way, providing some constraints.

I think you are showing pairs of images rather than single ones, and so I guess there is something in that pairing which indicates which is correct and whether the two are compatible? If so, how is that known? Is it expressed in the filenames? Or is compatibility unrelated to the images and refers to some other aspect of the experiment? Can any image be shown with any other, or are there some constraints?

i.e. it would be very simple to generate all 1536 possible combinations of the factors above, but I guess when manually creating your (smaller) conditions file, you’ve applied some semantic knowledge to restrict the possible combinations. The code would need to know those constraints too.


Hello, yes you are right, I am showing pairs of image (targetShape and distShape)

Indeed, targetShape and distShape are independent factors, and compatibility parameter links the two images together, because certain pairings of targetShape and distShape are compatible, etc.

There is no constraint on which image of targetShape can be shown with distShape. The only constraint required is that I keep an even amount of each level of the “compatibility” condition, which depends on the pairings of targetShape and distShape.

I hope this is enough information. Please let me know if it’s not!

Sorry, I dropped the ball on this, perhaps because we still need more info. i.e. if the conditions can be programatically created, the code needs to know the semantic knowledge you have,for example about how “certain pairings of targetShape and distShape are compatible”.

If you are still looking for a solution, it might be best to actually post a section of your current list of conditions and ideally explain the relationships and constraints between the variables.