randomizing blocks?

4,147 views
Skip to first unread message

Eric Flyn

unread,
Feb 7, 2011, 6:09:27 PM2/7/11
to psychopy-users
Hi,
Great software! Looks incredible! I'm wondering if there is a
straightforward way to create experiment blocks and have Psychopy
randomize the order for each subject. I'm a Psyscope user trying to
migrate over to PsychoPy (which seems much more powerful) and so I'm
trying to translate my experiment designs (randomizing blocks being a
common task in Psyscope). At the moment I'm mostly playing around
with the builder, since my Python coding isn't great - is there anyway
to do this with the builder?
Thanks - and great work!
Eric

Michael MacAskill

unread,
Feb 8, 2011, 2:30:20 AM2/8/11
to psychop...@googlegroups.com, Eric Flyn
Good question. One can do that within the Coder view of course, because you have full Python flexibility to do whatever you want. I don't use the Builder view much, but although it allows one to randomise the order of trials WITHIN a block, I can't find an obvious way of randomising the blocks themselves.

What you could do is use the Builder to set up the study as if there was just one block. Then switch to the coder view. It will have done all the mechanical aspects of creating the experiment for you (Jon has done a great job of this: the code is even nicely commented for you.) This is what we often do in our lab: use the Builder to do the 90% monkey work and then switch to the coder view to add the finer level of control that might be needed.

Find the line where the csv file is loaded which contains your trial data for the block. The only Python coding you'll need to do is replace that with a loop so that it runs multiple times, each time loading another (randomly ordered) file.

# i.e. create a list of four filenames containing your block information:
blockList = ['Block1.csv', 'Block2.csv', 'Block3.csv', 'Block4.csv']
random.shuffle(blockList) # reorder them each time

# then find the lines that looks something like these:
#set up handler to look after randomisation of trials etc
trials=data.TrialHandler(nReps=5.0, method=u'random', extraInfo=expInfo,
trialList=data.importTrialList(u'../../../../Users/michael/parameters.csv'))

and replace them with:

for fileName in blockList:
trials=data.TrialHandler(nReps=5.0, method=u'random', extraInfo=expInfo,
trialList=data.importTrialList(fileName))
# i.e. we are now loading a variable file name four times rather than a hard-coded one just once.
# Make sure that the subsequent code which runs each of the trials is also indented
# so that it also gets repeated for each loop.

Hope that helps (but bear in mind this is all just code typed into e-mail and I have no idea if it will actually work). I'm not a real Python programmer either but using the Builder view gives a very good insight into how to learn to write in it just well enough to get things done.

Cheers,

Michael

--
Michael R. MacAskill, PhD michael....@vanderveer.org.nz
Chief Scientist,
Van der Veer Institute
for Parkinson's & Brain Research

66 Stewart St http://www.vanderveer.org.nz
Christchurch 8011 Ph: +64 3 3786 072
NEW ZEALAND Fax: +64 3 3786 080


Jonathan Peirce

unread,
Feb 8, 2011, 4:04:17 AM2/8/11
to psychop...@googlegroups.com
It's a good question Eric and, yes, I think it should be possible
depending critically on what differs between the blocks. The key is that
you can create a loop (of blocks) around a loop (of trials or stimuli)
and your block loop simply controls one (or more) variable, while the
trial compares another.

If you really need completely different routines for your different
blocks then this may not work and it's going to get very ugly very
quickly. Here are a couple of examples.

1) Consider an FFA/PPA localiser in fMRI. You want randomly chosen faces
to appear for a block/epoch and then randomly chosen 'place' stimuli to
appear for another block (and maybe scrambled faces). Have folders for
your different stimuli with the same names inside:
/faces
/stim1.jpg
/stim2.jpg
...
/places
/stim1.jpg
/stim2.jpg
...

Set up a loop called stimulus with a set of Trial Types determining
sitmulus names (stim1.jpg...) and around that create a loop for blocks
that determines the folder to be used. Now, for the stimulus being
presented in your experiment set the image to update 'every repeat' with
a value of
$folderName+'/'+stimName
(folderName is a column in your blocks spreadsheet and stimName is
defined in your stimuli spreadsheet).

2) Consider an experiment to test contrast sensitivity for a range of
spatial freqs that you want to be presented in blocks (no idea why! ;-)
). Set the contrast to be determined in the trials loop, and surround
that by a blocks loop that sets the SF.

Actually, one slight caveat here is that I don't know how data output is
(or should be) handled for nested loops. That might need some tweaking...

Jon

--
Dr. Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk/

Eric Flyn

unread,
Feb 10, 2011, 4:13:44 PM2/10/11
to psychopy-users
Thanks for all the good suggestions everyone. I should be able to get
it working using the loops.
In some future version it might be cool to have the ability to
add multiple timelines at the bottom of the builder GUI (to represent
different blocks) and to be able to set the order in which the
timelines/blocks are executed (sequential or random). It might also
be cool to add a "cycles function", like in Psyscope, where the
experiment runs through it's pattern a specified number of times (I
suppose this is done with loops now, so it would be a bit redundant).
Not being a programmer, i have no idea if what I just suggested is
complicated or easy, but it might be nice.
Thanks,
Eric

Yaroslav Halchenko

unread,
Feb 10, 2011, 5:17:25 PM2/10/11
to psychop...@googlegroups.com
Hi Everyone,

On a side- but very relevant topic:

when the goal is to randomize trials, remember that not every random is
random. E.g. for two conditions A and B, fully legal, but not highly
probable sequence could be ABABABAB, which, I bet, you would like to
avoid (similarly AAAABBBB etc). Thus please assure that your
random sequence is good enough in terms of trials conditional
probabilities.

It would be great if in addition to 'random' psychopy provided some
additional methods to generate appropriate stimulation sequences
(e.g. m-sequence [1] etc).

[1] http://www.cfn.upenn.edu/aguirre/wiki/public:m_sequences

On Tue, 08 Feb 2011, Jonathan Peirce wrote:

> 1) Consider an FFA/PPA localiser in fMRI. You want randomly chosen
> faces to appear for a block/epoch and then randomly chosen 'place'
> stimuli to appear for another block (and maybe scrambled faces).
> Have folders for your different stimuli with the same names inside:
> /faces
> /stim1.jpg
> /stim2.jpg
> ...
> /places
> /stim1.jpg
> /stim2.jpg
> ...

--
.-.
=------------------------------ /v\ ----------------------------=
Keep in touch // \\ (yoh@|www.)onerussian.com
Yaroslav Halchenko /( )\ ICQ#: 60653192
Linux User ^^-^^ [175555]


Jeremy Gray

unread,
Feb 10, 2011, 11:29:44 PM2/10/11
to psychop...@googlegroups.com
> It would be great if in addition to 'random' psychopy provided some
> additional methods to generate appropriate stimulation sequences
> (e.g. m-sequence [1] etc).

yes, great idea!

optseq for fMRI designs would be cool. haven't used it myself but its
well regarded.

for behavioral studies I generally (but not always) like to: a) cycle
through all items in a list before repeating them, and repeat in a
different random order the next time through (we called this
"mini-blocking" in grad school, not sure how widely that term is
used), b) constrain the number of possible consecutive items in a row
(e.g., no more than 3 otherwise people start to wonder if the next
item will also be the same), and c) fix the first 3-5 items so that
all subjects in a study will experience the same thing initially,
avoiding potential differences in anchoring (especially if some items
are low frequency but by chance are over represented early in a random
sequence).

what others do people like?

--Jeremy

> --
> You received this message because you are subscribed to the Google Groups "psychopy-users" group.
> To post to this group, send email to psychop...@googlegroups.com.
> To unsubscribe from this group, send email to psychopy-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/psychopy-users?hl=en.
>
>

Gary Lupyan

unread,
Feb 10, 2011, 11:34:44 PM2/10/11
to psychop...@googlegroups.com
I always manually set the seed ( random.seed() ) before calling any
random functions so that I can replicate a given subject's trial order
if I need to. This has proven very handy.

Also very useful when you have a mixed design and want to make sure
that all the items were seen by all the subjects the same number of
times (albeit in crossed conditions), e.g.,

subject 1: ABBAABABBABA
subject 2: BAABBABAABAB

subject 3: BABABBABABB
subject 4: ABABAABABAA

just use the same seed in each pair and flip the condition values

-----------------------------------------------------
Gary Lupyan - lup...@wisc.edu
Assistant Professor of Psychology
University of Wisconsin, Madison
http://mywebspace.wisc.edu/lupyan/web/
-----------------------------------------------------

Jonathan Peirce

unread,
Feb 11, 2011, 5:08:13 AM2/11/11
to psychop...@googlegroups.com
very happy to take patches that implement m-sequences and/or optseq. You
can look in TrialHandler._createSequence() for how to implement a new
method. I agree it would be nice, but not a priority for me.

Gary's idea of using a fixed seed is already built into trial handler.
Give the seed attribute the same number twice and it will produce the
same sequence (using random.seed()) fixes the seed for all calls to the
numpy random number generator, whereas this only sets the seed for trial
randomisation.

Jeremy mentioned the method that he tentatively calls mini-bocking. That
is what PsychoPy calls random - ie the random setting is not a full
randomisation of all trials, just the trials within one repeat.

Jon

--

Dr. Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk/


This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment
may still contain software viruses which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.

Yaroslav Halchenko

unread,
May 27, 2011, 8:15:24 PM5/27/11
to psychop...@googlegroups.com
btw -- today Michael has uploaded to NeuroDebian a brand new tool
http://neuro.debian.net/pkgs/debruijn.html
which implements recently published

GK Aguirre, MG Mattar, L Magis-Weinberg (2011). de Bruijn cycles for
neural decoding. NeuroImage 56: 1293-1300. (DOI)

which is also described at
http://cfn.upenn.edu/aguirre/wiki/public:de_bruijn

might be a valuable addition to psychopy ;-) initial code is in C++ so
might be a fun evening project to get a Python equivalent ;-)

On Thu, 10 Feb 2011, Jeremy Gray wrote:

> > It would be great if in addition to 'random' psychopy provided some
> > additional methods to generate appropriate stimulation sequences
> > (e.g. m-sequence [1] etc).

> yes, great idea!

> optseq for fMRI designs would be cool. haven't used it myself but its
> well regarded.

--
=------------------------------------------------------------------=
Keep in touch www.onerussian.com
Yaroslav Halchenko www.ohloh.net/accounts/yarikoptic

Dehuijt

unread,
Aug 19, 2013, 10:20:35 AM8/19/13
to psychop...@googlegroups.com
Hi all,

I also want to randomize blocks in my task switch experiment, so I thought I might add my question here. 

The following picture may aid my explanation. 




Basically, I have two condition files, one for S3-S6 and the other one for D3-D6, each contains 192 stimuli. I have 4 trials each, because I don't want all 192 stimuli be presented, just, for example 3, 4, 5, or 6 stimuli from one condition in a row. So, every routine has a code component where the trials gets finished after the respective count. 

I would like to randomize all my 8 routines (S3, S4,..., D3, D4,...) using another loop. This loop is based on the condition file "Blocks" having the variable Blocks 



The variable names are the names according to the trials responsible for each routine. 

My condition files look something like this: 





According to Dr. Peirce's suggestion some posts above, I specified each image like this: 



I am not sure if I got that right, because it does not work... That's the error message I get: 

41.4714 ERROR Couldn't find image file 'Solid_Symbol_5/3_eckig_lines_solid_05.jpg'; check path?
Traceback (most recent call last):
  File "/Users/Clemens/Documents/Uni Oldenburg/PsychoPy/5. Task_switch/Task_Switch_Shape_Number_18082013_lastrun.py", line 1331, in <module>
    image_3.setImage(Blocks+'/'+Stimulus_Symbol_Solid)
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/visual.py", line 5550, in setImage
    maskParams=self.maskParams, forcePOW2=False)
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/visual.py", line 7373, in createTexture
    % (tex, os.path.abspath(tex))#ensure we quit
OSError: Couldn't find image file 'Solid_Symbol_5/3_eckig_lines_solid_05.jpg'; check path? (tried: /Users/Clemens/Documents/Uni Oldenburg/PsychoPy/5. Task_switch/Solid_Symbol_5/3_eckig_lines_solid_05.jpg)


There is probably something wrong with the variable description ($Blocks+'/'+Stimulus_Symbol_Solid). 
However, I have no clue what. I am thankful for any help. perhaps someone even has a suggestion how to make my randomization even simpler and more pretty. Obviously, I am using the builder view... But I am aiming at getting more familiar with the codes. 
I sense life will be easier then.

Kind regards and greetings from Germany,

Clemens 

Clemens Dickhut

unread,
Aug 19, 2013, 10:22:30 AM8/19/13
to psychop...@googlegroups.com
Oh no, inserting screenshots did not work…. 


Please bear with me a second. 


--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/e5b_Lddu4G0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.

To post to this group, send email to psychop...@googlegroups.com.

Clemens Dickhut

unread,
Aug 19, 2013, 10:27:12 AM8/19/13
to psychop...@googlegroups.com
I attach a word file with the pictures, as well as my experiment. 

Best,

clemens 
TaskSwitch_Questions.docx
Task_Switch_Shape_Number_18082013.psyexp

Angie Carrillo

unread,
Jul 17, 2014, 11:50:46 PM7/17/14
to psychop...@googlegroups.com
Hi all,

I have the same problem as Clemens, except that I am using audio stimuli instead of images. Does anyone have a thought on how to solve it? All help will be appreciated!

thanks.

Best,

Angie.

Michael MacAskill

unread,
Jul 18, 2014, 1:16:12 AM7/18/14
to psychop...@googlegroups.com
Dear Angie,

We would love to help but will need more detail. Clemens raised quite a few issues in that e-mail, so instead of saying you "have the same problem", please give a very precise description of:

- what you want to do.
- what you have done.
- what went wrong (including any error messages, if they occurred).

Regards,

Michael

Angie Carrillo

unread,
Jul 18, 2014, 1:43:16 AM7/18/14
to psychop...@googlegroups.com
For sure Michael, thank you for your reply.

I am using the builder mode. What I am trying to do is to randomly present 7 routines. Each of these routines have a loop (to randomize between 3 stimuli and present just one of them).
I have tried with an outer loop (embracing all the routines), setting "random" and "fullrandom" in the looptype and setting a condition file (.xlsx) which contains the names of the routines in the first column... The result is that all the routines are presented in the sequence I have in the flow, instead of a random order, so this hasn't worked.
Other thing I have tried is to get my audio stimuli selected by using:  $folderName+'/'+stimName (as Jon explains in the reply he wrote the 08/02/11...) So, I have modified my excel files and set it as he suggests. The result is the same, I have the routines presented in the sequence I have in the flow window.

What can I do?

Thanks :)

Jonathan Peirce

unread,
Jul 18, 2014, 5:05:53 AM7/18/14
to psychop...@googlegroups.com
Angie, I think your problem is that you're trying to create a separate routine for each condition. You want a single routine that changes on each repeat, not a separate routine. Note that the demos (including the stroop demo on youtube) mostly have a single routine called 'trial' but each time trial is repeated it has a different stimulus (controlled byu variables in the conditions file).

Jon
--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.

To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/6540c7db-aca5-4fa7-ae35-fc8720259f69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk

Angie Carrillo

unread,
Jul 18, 2014, 7:09:25 PM7/18/14
to psychop...@googlegroups.com
Yes Jon, that is another option I have tried but I find trouble to set it right.

I have 7 audio stimuli: 160.wav, 166.wav... etc. What I am doing is a bisection task. So, in the test block, I have to present these 7 stimuli in a random order, but also the duration of each stimuli has to be randomly chosen between three options: 3, 5, and 7 seconds. In total I need 7 trials. For example, my trials should present the stimuli like this:
trial 1: 166.wav with 5 secs of duration.
trial 2: 172.wav with 3 secs of duration.
trial 3: 160.wav with 5 secs of duration.
trial 4: 190.wav with 7 secs of duration.
trial 5: 178.wav with 7 secs of duration.
trial 6: 184.wav with 3 secs of duration.
trial 7: 196.wav with 5 secs of duration.

So, If the selected duration for the 1st trial has been 5 secs, I don´t need to present the same stimuli again with the other two durations. My problem is that I don't know how to skip the other two durations in the random loop. I have tried to solve this by setting the durations either as conditions (having 21 in total) or as parameters in the conditions file, but this hasn't worked.

How can I solve it?

thank you for your attention and suggestions :)


--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/e5b_Lddu4G0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.

To post to this group, send email to psychop...@googlegroups.com.

Danielle Taylor

unread,
Jul 6, 2016, 2:48:54 PM7/6/16
to psychopy-users
Hello Jon,

I have taken these instructions and modified them to an experiment I have been working on, which pulls from different excel files instead of folders, and I have had some success! For example, I have an outside loop that pulls from a file called BlockOrder.xlsx and an inside loop that pulls from Block. Block is a condition in my BlockOrder file that references 1 of 4 other excel files. The routine inside my inner loop references a condition in either Block1.xlsx, Block2.xlsx, etc. This experiment runs successfully!

HOWEVER, I have one issue that I cannot seem to resolve. I also have a different set of instructions for each block, that I only want to appear once at the start of the block, so I have placed this routine outside my inner loop (which I understand is problematic, but I only want it to appear once, not each loop). Is there a way I can make my instructions vary with my block type (by, say referencing a condition in my Block#.xlsx file), but not get repeated each trial?

Thank you very much for your time and help!
Danielle

Michael MacAskill

unread,
Jul 6, 2016, 4:49:58 PM7/6/16
to <psychopy-users@googlegroups.com>
Dear Danielle,

Your instruction routine should indeed be outside the inner loop but still inside the outer loop, so it only runs once per block.

Your outer loop is connected to a file called 'BlockOrder.xlsx'. The instructions belong in that block-level conditions file, not one of the trial-level conditions files (Block#.xlsx).

Add a second column to that 'BlockOrder.xlsx' called, say, 'instructionText'. Paste the instructions for each block into a cell in that column. Then in the text stimulus in your instruction routine, just put this:

$instructionText

Regards,

Michael



On 7/07/2016, at 06:15, Danielle Taylor <danielle...@gmail.com> wrote:

Hello Jon,

I have taken these instructions and modified them to an experiment I have been working on, which pulls from different excel files instead of folders, and I have had some success! For example, I have an outside loop that pulls from a file called BlockOrder.xlsx and an inside loop that pulls from Block. Block is a condition in my BlockOrder file that references 1 of 4 other excel files. The routine inside my inner loop references a condition in either Block1.xlsx, Block2.xlsx, etc. This experiment runs successfully!

HOWEVER, I have one issue that I cannot seem to resolve. I also have a different set of instructions for each block, that I only want to appear once at the start of the block, so I have placed this routine outside my inner loop (which I understand is problematic, but I only want it to appear once, not each loop). Is there a way I can make my instructions vary with my block type (by, say referencing a condition in my Block#.xlsx file), but not get repeated each trial?

Thank you very much for your time and help!
Danielle 

-- 
Michael R. MacAskill, PhD 66 Stewart St
Research Director, Christchurch 8011
New Zealand Brain Research Institute NEW ZEALAND

Senior Research Fellow, michael....@nzbri.org
Te Whare Wānanga o Otāgo, Otautahi Ph:   +64 3 3786 072
University of Otago, Christchurch http://www.nzbri.org/macaskill

Danielle Taylor

unread,
Jul 7, 2016, 5:27:07 PM7/7/16
to psychopy-users
Thank you Michael!

I don't know why I didn't think of this! This solution seems so obvious now.

Thank you very much for your help!
Reply all
Reply to author
Forward
0 new messages