Repeat a random loop?

1,178 views
Skip to first unread message

James Abraham

unread,
Mar 7, 2012, 6:14:52 PM3/7/12
to psychopy-users
Hi all, is it possible to have PsychoPy create a randomized loop and
then repeat that same loop? Here's what I'm trying to run for my
study:

I have six mugshots that I need to present sequentially in a random
order. I'll then have a routine instructing participants to indicate
whether each person is or is not the culprit they saw previously in a
video. I want to show the routine again in the same order that they
were presented initially, but this time I'll be including a yes/no
response and confidence scale.

I think it will have to be programmed because I have another piece of
code that I'll use to make sure that the actual culprit doesn't appear
in the first position. Is there some way to have PsychoPy remember the
order of the .jpg images and re-use that order for each participant?
Thanks in advance!

James

Michael MacAskill

unread,
Mar 7, 2012, 6:49:15 PM3/7/12
to psychop...@googlegroups.com
Hi James,

The TrialHandler has an optional seed parameter:
<http://www.psychopy.org/api/data.html#psychopy.data.TrialHandler>

By specifying a constant seed value, the random order chosen will be the same from one run to the next. So start by selecting a new random seed for each run of the experiment (so each subject gets a different random order):

fixedSeed = random.randint(0, 1000000) # a random integer between 0 and a million

Then for the first run of the pictures, do:
firstTrials = data.TrialHandler(nReps=1, method=u'random',
extraInfo=expInfo, originPath=None,
trialList=data.importConditions(u'Mugshot names file.csv'),
seed=fixedSeed)

And then use exactly the same for the second run:

secondTrials = data.TrialHandler(nReps=1, method=u'random',
extraInfo=expInfo, originPath=None,
trialList=data.importConditions(u'Mugshot names file.csv'),
seed=fixedSeed)

The order should then be the same from one repetition to the next within a session, but vary across subjects (but test this…)

Regards,

Michael

--
Michael R. MacAskill, PhD michael....@nzbri.org
Research Director,
New Zealand Brain Research Institute

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


James Abraham

unread,
May 18, 2012, 5:16:05 PM5/18/12
to psychop...@googlegroups.com
Hi Michael,

Thank you for the response and sorry I haven't replied. I just tried the code you posted above, but I get the following error when I try to run the program:

    fixedSeed = random.randint(0, 1000000)
AttributeError: 'builtin_function_or_method' object has no attribute 'randint'

Do I need to add something?

Thank you,

James

--
Michael R. MacAskill, PhD                michael.maca...@nzbri.org

Research Director,  
New Zealand Brain Research Institute

Michael MacAskill

unread,
May 18, 2012, 10:27:14 PM5/18/12
to psychop...@googlegroups.com

On 19 May, 2012, at 09:16, James Abraham wrote:

> Hi Michael,
>
> Thank you for the response and sorry I haven't replied. I just tried the code you posted above, but I get the following error when I try to run the program:
>
> fixedSeed = random.randint(0, 1000000)
> AttributeError: 'builtin_function_or_method' object has no attribute 'randint'
>
> Do I need to add something?
Dear James,
Try adding:

import random

prior to the random.randint() line so that you get access to the functions in the 'random' library.

Regards,

Michael



--
Michael R. MacAskill, PhD michael....@nzbri.org

Michael MacAskill

unread,
May 18, 2012, 10:41:06 PM5/18/12
to psychop...@googlegroups.com


> On 19 May, 2012, at 09:16, James Abraham wrote:
> I get the following error when I try to run the program:
>>
>> fixedSeed = random.randint(0, 1000000)
>> AttributeError: 'builtin_function_or_method' object has no attribute 'randint'
>>
>> Do I need to add something?
> Dear James,
> Try adding:
>
> import random
>
> prior to the random.randint() line so that you get access to the functions in the 'random' library.

On further reflection, what may likely be causing this error (assuming you originally created the experiment in Builder) is that you probably have an import line like this early on:

from numpy.random import random, randint, normal, shuffle

So importing the "random" library might cause a naming conflict. Instead you can use the numpy.random's randint function directly, i.e:

fixedSeed = randint(0, 1000000)

Regards,

Michael

James Abraham

unread,
May 19, 2012, 5:43:06 PM5/19/12
to psychop...@googlegroups.com
Success! I have another question, and hopefully it's my last for this project. I'll be displaying a mugshot and I want the subjects to answer whether the person was or was not the culprit from a video, and I also want them to indicate their confidence in that decision. My issue is that I'm trying to make the first prompt disappear after an answer is given by using the disappear=True command, but I keep getting this error:

TypeError: __init__() got an unexpected keyword argument 'disappear'

The other issue is that I want the confidence scale to appear only after the Yes/No prompt has been answered, but I'm not sure how to make that the start condition. Sorry for all these questions - I've been tinkering with this for the past two hours and haven't had any luck.

Thank you,

James

Jeremy Gray

unread,
May 19, 2012, 6:06:00 PM5/19/12
to psychop...@googlegroups.com

Success!

cool
 
I have another question, and hopefully it's my last for this project. I'll be displaying a mugshot and I want the subjects to answer whether the person was or was not the culprit from a video, and I also want them to indicate their confidence in that decision. My issue is that I'm trying to make the first prompt disappear after an answer is given by using the disappear=True command, but I keep getting this error:

TypeError: __init__() got an unexpected keyword argument 'disappear'

'disappear' is a new addition to RatingScale, and will be available in version 1.74.00. (its currently availale on github, for the curious.) but you could also make it effectively disappear by having it end the trial, and getting confidence in a subsequent routine, which I think would also solve your other issue.
 
The other issue is that I want the confidence scale to appear only after the Yes/No prompt has been answered, but I'm not sure how to make that the start condition.

the easiest way would be to put the confidence scale in another routine, after the yes/no culprit question. it could still be within the same loop.

I don't think there's a way to specify the start time of a routine as being linked to the end of another one through the builder, except through different routines (or code components).

--Jeremy

James Abraham

unread,
May 21, 2012, 2:33:03 PM5/21/12
to psychop...@googlegroups.com
I hadn't thought of that, but it did the trick. Thank you!
Reply all
Reply to author
Forward
0 new messages