random fixation duration

684 views
Skip to first unread message

Amy

unread,
Mar 16, 2013, 6:08:14 AM3/16/13
to psychop...@googlegroups.com
Hi All,

I am designing a task which has a variable fixation duration and variable cue duration so that it is difficult for participants to preempt the onset of the target stimulus.

Could anyone tell me how to generate this random duration using code within the builder. e.g. between 1 and 2 seconds.

Thank you


Michael MacAskill

unread,
Mar 16, 2013, 10:27:01 PM3/16/13
to psychop...@googlegroups.com
Dear Amy,

Insert a Code Component above your fixation and cue stimuli and put this code in its Begin Routine field:

fixDuration = numpy.random.random() + 1.0 #random number 0 to 1 plus 1.0 gives a random interval from 1.0 to 2.0
cueDuration = numpy.random.random() + 1.0

Then in the fixation component, in the start field enter 0.0 and in the Stop field, put:
$fixDuration

In the cue component, in the start field put:
$fixDuration
And in the Stop field, put
$fixDuration + cueDuration

Back in the Code component, also put these lines in the End Routine field:
thisExp.addData("Fixation duration", fixDuration)
thisExp.addData("Cue duration", cueDuration)

This ensures that the relevant durations are stored in your data for that trial.

NB the actual duration of stimuli is constrained to multiples of your screen refresh rate. So if your screen is running at 100 Hz, and you have a random cue duration of, say, 1.236 seconds, it will likely be shown on screen for 1.240 seconds. With a bit more sophistication, you could create random durations with a granularity that matches your screen refresh rate, but it may likely be a level of error that you are prepared to tolerate.

Regards,

Michael

Amy Bland

unread,
Mar 17, 2013, 5:14:01 PM3/17/13
to psychop...@googlegroups.com
That worked great, thank you!

I'm just wondering about variations of this. For instance how would I
generate randoms numbers:

a) 1.5 - 2 seconds
b) -1 to 1

Thank you again.
> --
> 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/PqbP5cNu7Vc/unsubscribe?hl=en.
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Michael MacAskill

unread,
Mar 17, 2013, 7:06:50 PM3/17/13
to psychop...@googlegroups.com

On 18 Mar, 2013, at 10:14, Amy Bland <amy.r...@gmail.com> wrote:

> That worked great, thank you!
>
> I'm just wondering about variations of this. For instance how would I
> generate randoms numbers:
>
> a) 1.5 - 2 seconds
> b) -1 to 1
>
> Thank you again.

The numpy.random.random() function returns a floating point number between 0 and 1, so just use simple arithmetic to scale it to the range you need. Standard rules of precedence apply, i.e. multiplications & divisions are done before addition & subtraction, unless you use brackets to group operations in different orders.

> a) 1.5 - 2 seconds
numpy.random.random()/2.0 + 1.5
i.e. dividing by 2 reduces the random range to 0–0.5 s, and then we add 1.5s to get the desired 1.5-2.0s interval.

> b) -1 to 1
numpy.random.random()*2.0 - 1.0

(But negative numbers don't make much sense in defining stimulus onsets and durations.)

Mike

Felipe Corchs

unread,
Feb 11, 2015, 1:45:42 PM2/11/15
to psychop...@googlegroups.com
Dear Amy and Michel, greetings!

I designing something similar here, would you help me?

I need a Inter Trial Interval with random duration, between 10 and 12 seconds. 

I created a routine named ITI and, as I need a fixation mark in the center of the screen during the ITIs, I used the text component with a "+" in the center. 

In this same routine I  inserted a code component with "itiDuration = numpy.random.random()+10.0 + 1.0" in its begin routine tab.

In the text component, start = 0.0 and end = $ItiDuration

Back in the code component, I inserted "thisExp.addData("intertrial Duration", itiDuration)" in its end routine tab.

The ITI routine in the flow window keeps red and psychopy won't run the experiment.

What am I doing wrong here?

Thanks in advance.

Best,

Felipe

Michael MacAskill

unread,
Feb 11, 2015, 4:30:45 PM2/11/15
to psychop...@googlegroups.com

> On 12/02/2015, at 07:45, Felipe Corchs <felipe...@gmail.com> wrote:
> I need a Inter Trial Interval with random duration, between 10 and 12 seconds.
>
> I created a routine named ITI and, as I need a fixation mark in the center of the screen during the ITIs, I used the text component with a "+" in the center.
>
> In this same routine I inserted a code component with "itiDuration = numpy.random.random()+10.0 + 1.0" in its begin routine tab.

I don't think the numpy.random prefix is necessary any more: after those messages were posted, that random function became explicitly imported by name.
But arithmetically, you actually want:

itiDuration = random() * 2.0 + 10.0

in order to produce a random value between 10 and 12 (your version would yield a value between 11 and 12).

> In the text component, start = 0.0 and end = $ItiDuration
Variables are case sensitive, so you should specify $itiDuration

> Back in the code component, I inserted "thisExp.addData("intertrial Duration", itiDuration)" in its end routine tab.
Good idea.

> The ITI routine in the flow window keeps red
That's fine, it's just a visual indicator that PsychoPy doesn't know in advance exactly how long this trial will run (because you are specifying a duration at run-time).

> and psychopy won't run the experiment.
That doesn't give us any useful details. Please post any actual error messages.

Regards,

Michael


FELIPE CORCHS

unread,
Feb 11, 2015, 5:05:08 PM2/11/15
to psychop...@googlegroups.com
Thanks a lot, Michael.
I made the corrections you suggested, but its still not running.
Actually, no error message at all, it simply won’t run… When I set ITI duration for 10.0 and exclude the code component ir runs perfectly…
If you have any suggestion it will be very appreciated.
Best,
Felipe
> --
> 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/PqbP5cNu7Vc/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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/CF0D7C34-4FAA-4887-B87C-EF133758B07E%40otago.ac.nz.
> For more options, visit https://groups.google.com/d/optout.

Amy Bland

unread,
Feb 12, 2015, 8:15:50 AM2/12/15
to psychop...@googlegroups.com
I'm sure you already have, but just to check - have you used "import numpy" at the start of your experiment?

from looking at your code you are using itiDuration but in you duration condition you are using a capital I, i.e. ItiDuration. have you  also ensured this variable is set to condition and not time?



FELIPE CORCHS

unread,
Feb 12, 2015, 9:22:08 AM2/12/15
to psychop...@googlegroups.com
Thanks, Amy!
Yes, I’ve tried all these things…. I press “run” and nothing happens…


Jonathan Peirce

unread,
Feb 12, 2015, 9:24:22 AM2/12/15
to psychop...@googlegroups.com
Felipe, could you open the Coder view in the background, then go back to your Builder experiment and press run. Possibly an error message is being sent to the Ouput panel in the Coder view?
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.

For more options, visit https://groups.google.com/d/optout.



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.

FELIPE CORCHS

unread,
Feb 12, 2015, 9:26:43 AM2/12/15
to psychop...@googlegroups.com
Yes, it is: UnicodeDecodeError: 'ascii' codec can't decode byte 0xfa in position 165: ordinal not in range(128)

Sorry, this is my fist week with programing….



Jeremy Gray

unread,
Feb 12, 2015, 1:34:37 PM2/12/15
to psychop...@googlegroups.com
This is progress but still not much to go on. Can you look for the whole error message and post that, starting from Traceback: ... This give the context of the error, which is a huge help for tracking it down.

--Jeremy

FELIPE CORCHS

unread,
Feb 12, 2015, 6:39:06 PM2/12/15
to psychop...@googlegroups.com
Dear all,

I’ve just realized what the problem was — One of the folder’s name where the file was saved in had an “accent” (Araújo). I changed it to Araujo and it’s running perfectly. Thanks a lot for your support.

I have additional questions about I/O and interface with Biopac. Can any of you help me with that issue too?

Thanks again,

Best,

Felipe

 

k Cheng

unread,
Jun 16, 2015, 6:52:04 AM6/16/15
to psychop...@googlegroups.com
Dear Michael

could i ask you for the exact code for your comment here? 
because i don't know where the 'start field' to enter 0.0 and in the Stop field to put $fixDuration 

this is my fixation code 

    fixation = visual.ShapeStim(win, vertices=((0,-0.5), (0,0.5), (0,0), (-0.5,0), (0.5,0)), lineWidth=5, closeShape=False, lineColor='white')
    fixation.draw()
    win.flip()
    core.wait(sec)

Thank you 

2013년 3월 17일 일요일 오전 11시 27분 1초 UTC+9, Michael MacAskill 님의 말:

Michael MacAskill

unread,
Jun 16, 2015, 5:42:04 PM6/16/15
to psychop...@googlegroups.com

> On 16/06/2015, at 19:47, k Cheng <sweet...@gmail.com> wrote:
>
> could i ask you for the exact code for your comment here?
> because i don't know where the 'start field' to enter 0.0 and in the Stop field to put $fixDuration
>
> this is my fixation code
>
> fixation = visual.ShapeStim(win, vertices=((0,-0.5), (0,0.5), (0,0), (-0.5,0), (0.5,0)), lineWidth=5, closeShape=False, lineColor='white')
> fixation.draw()
> win.flip()
> core.wait(sec)

Hello,

Those suggestions were for someone using the graphical Builder interface, where there are simple fields to specify the start and duration of stimuli.

You seem to be using Coder to create and draw your stimuli. This means that you have to control the onset and duration of your stimuli manually (but get a lot more flexibility and control in return).

In your code above, the stimulus starts being displayed at the win.flip() line and its duration will be covered (approximately) by the core.wait(sec) line (the 'sec' variable here is your equivalent of the 'fixDuration' variable). Putting another win.flip() immediately after that will mean that the fixation stimulus will disappear.

You should check the Demos menu for many simple examples of how to display stimuli. Be aware that core.wait() isn't as accurate as some other methods, such as counting frames (screen refreshes).

Regards,

Michael


Reply all
Reply to author
Forward
0 new messages