Continuous Performance Task (AKA AX-task) - coding help

613 views
Skip to first unread message

JRoss

unread,
Mar 24, 2013, 8:51:36 PM3/24/13
to psychopy-users
Hi all. I'm trying to create a task where the participant is supposed
to press the space bar when an "X" appears, but only if the "X"
follows and "A". The text is all single letters.

For context, I have figured out how to do an X-task, where the
participant presses the spacebar when an "X" appears, regardless of
what came before it or after it. This was fairly simple. I attached
the conditions file of that task.

The AX task is more complicated because the corrAns is to push the
spacebar when you see "X", but only if the previous stimulus was an
"A". I have no idea how this could be done. I'd prefer to stay in
builder, but I can attempt directly coding it if you guys can give me
good instructions.

I hope this makes sense - I can hardly keep track of it all. Thanks
for the help!
Message has been deleted

JRoss

unread,
Mar 24, 2013, 9:35:05 PM3/24/13
to psychop...@googlegroups.com
Hopefully the attachment will go through now that it's a zip file.
X task.zip

Jared Roberts

unread,
Mar 24, 2013, 10:03:27 PM3/24/13
to psychop...@googlegroups.com
Hi there,

I just got something working in the builder by adding a code component.  Here's what I did:

1.  Remove the corrAns column from the conditions file, we're going to set it in the code component instead.
2.  Insert a code component into the trial routine(be sure to keep all of the indentation, that's how python knows where if statments begin and end):

In "Begin Experiment" type:
prevA = False

in "Begin Routine" type:
if prevA and text == 'X':
    corrAns = 'space'
else:
    corrAns ='none'

in "End Routine" type:
prevA = False
if text == 'A':
    prevA = True

Basically this creates a variable that keeps track of whether the last trial used an A.  Before it gathers the key response, it checks if the last trial was an A and if the current trial is an X, of both conditions are true, it sets corrAns to 'space', otherwise it sets it to 'none'.

Good Luck!
 
-Jared
--
Jared Roberts
Graduate Student
The Yassa Learning and Memory Laboratory
Johns Hopkins University
"A lost cause can be as spiritually satisfying as a victory." - Robert Heinlein

JRoss

unread,
Mar 24, 2013, 10:07:11 PM3/24/13
to psychop...@googlegroups.com
So would I add that in the python window, or can I add it through builder? Or are you referring to the custom code box?

Jared Roberts

unread,
Mar 24, 2013, 10:13:26 PM3/24/13
to psychop...@googlegroups.com
The custom code box in the builder, AKA a code component.

-Jared

--
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/msg/psychopy-users/-/rSIOX_cfoPMJ.

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

JRoss

unread,
Mar 24, 2013, 10:14:33 PM3/24/13
to psychop...@googlegroups.com
SWEET!!!!!!! THANK YOU SO MUCH!

JRoss

unread,
Mar 24, 2013, 10:54:44 PM3/24/13
to psychop...@googlegroups.com
Will this code affect the entire experiment, or just the trial it's in? I want it to affect the AX trial, but not the X trial.


On Sunday, March 24, 2013 10:03:27 PM UTC-4, Jared Roberts wrote:

Jared Roberts

unread,
Mar 24, 2013, 11:06:12 PM3/24/13
to psychop...@googlegroups.com
In that case, you'll probably want to keep the 'corrAns' column in the condition file, then use some other variable (maybe 'corrAnsAX') in the AX trials.  Make sure that you change the variable in the keyboard input component of the AX trial as well.  Just make sure that you add the code component to the AX trial and not the X trials.

-Jared

--
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/msg/psychopy-users/-/x0OTlPY__psJ.

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

JRoss

unread,
Mar 24, 2013, 11:13:24 PM3/24/13
to psychop...@googlegroups.com
So I need to use the same condition file for both X and AX? I keep getting errors...

Jared Roberts

unread,
Mar 24, 2013, 11:16:25 PM3/24/13
to psychop...@googlegroups.com
I'm not super familiar with the operations of the builder myself...  If you can use more than one conditions file, then I would do that.   What are the errors that you're getting?  I can give better advice if I know what you're actually seeing.

To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/tV5tg19J2nMJ.

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

JRoss

unread,
Mar 24, 2013, 11:19:34 PM3/24/13
to psychop...@googlegroups.com
Here's the error:
text.setText(text)
AttributeError: 'unicode' object has no attribute 'setText'

Jared Roberts

unread,
Mar 24, 2013, 11:24:55 PM3/24/13
to psychop...@googlegroups.com
That's most likely a variable name conflict.  You've named the variable that you're using "text", and you've named one of your text components "text".  When it pulls in your variable from your conditions file, it overwrites the text component.  I'd just change the name of the text component to text_1 or something like that.  If you decide to change your created variable instead, make sure to update the name in the code component.

-Jared

To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/5t2-nXGWzt8J.

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

JRoss

unread,
Mar 24, 2013, 11:57:14 PM3/24/13
to psychop...@googlegroups.com
OK. So it works now, but it doesn't record what the correct answer should be. It records all the corrAnsAX as "none". However, it records whether the response was correct in 1s and zeros, which is weird. If we can make it fill corrAnsAX with "space when appropriate, that'd be awesome. It's not a huge deal though.

JRoss

unread,
Mar 25, 2013, 1:03:35 AM3/25/13
to psychop...@googlegroups.com
Thanks to Jared Roberts, I have a working program. I'm attaching it to this post as thanks for the support and in the hope that none will have to expend this much time reverse-engineering a 70 year old task...............

Thanks again to all!
Full CPT - v1.2c3.zip

JRoss

unread,
Mar 26, 2013, 9:15:13 PM3/26/13
to psychop...@googlegroups.com
OK. So things just got more complicated.

I also need to make it so that the A-X sequence accounts for 20% of the letters shown. Do you have any idea how to do that?


On Sunday, March 24, 2013 8:51:36 PM UTC-4, JRoss wrote:

Jeremy Gray

unread,
Mar 26, 2013, 9:48:19 PM3/26/13
to psychop...@googlegroups.com
Pre-specified "sequential order" items have a lot to recommend them,
especially when things start getting complex, like also needing to
specify BX, BY, and AY frequencies, and not having too many AX in a
row, ... Sequences that are randomly generated at run-time may have
suboptimal properties. To do this, you just figure out and specify
what all the items are for each trial, in order. Have at least 2
orders, ideally more.
> --
> 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/msg/psychopy-users/-/YUmr269xNh0J.

JRoss

unread,
Mar 26, 2013, 9:51:40 PM3/26/13
to psychop...@googlegroups.com
The problem is, my wacko of a professor wants the items to be "pseudo-random" - he wants everything to be random, but he also wants 20% of the stuff shown to be A-X. If I do sequential, I can't have randomness...

Dave Braze

unread,
Mar 26, 2013, 9:56:48 PM3/26/13
to psychop...@googlegroups.com
Of course you can. You just generate those sequences in advance, rather than doing it at run time. You can pre-generate unique random sequences for every subject you plan to run before recruiting a single subject.
-DB
To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/rV3Lhx_Np1UJ.

JRoss

unread,
Mar 26, 2013, 9:59:59 PM3/26/13
to psychop...@googlegroups.com, dave...@gmail.com
I hear you, and that makes total sense, but that's not how he wants it done. Doing it any way other than exactly how he specifies is unacceptable to him. If I could do that, I definitely would have a LONG time ago... >.< This guy is extremely horrible to work with...

Jeremy Gray

unread,
Mar 26, 2013, 10:21:13 PM3/26/13
to psychop...@googlegroups.com, dave...@gmail.com
the way I've seen this done is to treat a "trial" as being a pair of
stimuli, and randomize the pairs. have a list of pairs, and construct
it to consist of 20% AX pairs (and the rest being BX, BY, and AY).
then randomly order the list, and present the items.
> https://groups.google.com/d/msg/psychopy-users/-/ZaBglKExS4cJ.

Jonathan Ross

unread,
Mar 26, 2013, 10:24:43 PM3/26/13
to psychop...@googlegroups.com
I've done that too, but he didn't like it. He said it'd be too predictable or something.

Also, I'll be sure to keep it professional from now on.

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/s1bgNVqPNRY/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.



--
Jonathan Ross

Jared Roberts

unread,
Mar 26, 2013, 10:33:26 PM3/26/13
to psychop...@googlegroups.com
If you want a random sequence where 20% of the trials are of a certain type, then you're going to have to create sequence before you start running the trials. Creating the sequence at the outset of the program or creating it days before and assigning it a priori to a participant aren't really different.  You could randomly determine the trial type at the outset of the trial (grab a random number 1-5 and do an AX trial if it's 1), but doing that isn't going to get you an exact 20% distribution.

JRoss

unread,
Mar 26, 2013, 11:01:57 PM3/26/13
to psychop...@googlegroups.com
Is there any program that could do exactly what the professor wants? SuperLab? Anything?

Jared Roberts

unread,
Mar 26, 2013, 11:17:33 PM3/26/13
to psychop...@googlegroups.com
I'm not sure exactly what you want.  There are basically two ways of doing this.  One way is like shuffling a deck of cards and flipping them one by one (a random sequence containing exactly 20% of the AX trials), the other is like rolling a die at the beginning of each trial.  You could mix them a bit, like shuffle cards and if it's not an AX card, you roll the die a couple of times to select random letters, but those are really the only two ways of going about it. 

--
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/msg/psychopy-users/-/EfGvUoqFEwEJ.

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

Michael MacAskill

unread,
Mar 26, 2013, 11:30:15 PM3/26/13
to psychop...@googlegroups.com

On 27 Mar, 2013, at 16:01, JRoss <jr3...@student.american.edu> wrote:

> Is there any program that could do exactly what the professor wants? SuperLab? Anything?
PsychoPy runs on Python, a Turing-complete programming language. Whatever randomisation system you want, it can be implemented.

The problem is not what system you are using (PsychoPy, Superlab, e-Prime, etc). It is defining exactly what constraints need to be satisfied by your randomisation process. A number of suggestions have been made that seem to comply precisely with what you need to achieve. The objections to each of them don't seem very clear to me, so I'm not sure how much more help could be offered?

Regards,

Michael

Nate Vack

unread,
Mar 27, 2013, 10:21:06 AM3/27/13
to psychop...@googlegroups.com
On Tue, Mar 26, 2013 at 9:24 PM, Jonathan Ross
<jr3...@student.american.edu> wrote:
> I've done that too, but he didn't like it. He said it'd be too predictable
> or something.

One other approach you can take is:

1: At the beginning of the experiment, generate a trial order that is
likely, but not guaranteed, to have the properties you need.
2: Check the list to see if it's valid. If not, go to 1.
3: You have a randomized trial list!

Runtime depends on how likely it is that your generator builds
something valid -- but this approach can be a lot simpler than finding
a closed-form algorithm that does exactly what you want.

It's also possible that your professor doesn't actually know exactly
what he wants, but doesn't realize it. "I want this random, but not
too random, but still not predictable" feature requests can be pretty
tricky to exactly pin down.

In any case, pyschopy (or any package) can't solve it for you. This is
gonna be some combination of better communication with your prof
("it'd be too predictable or something" suggests you have more work to
do on this end), deciding on really concrete rules for you task
ordering, and writing code.

Good luck!
-n

David McFarlane

unread,
Mar 28, 2013, 1:38:10 PM3/28/13
to psychop...@googlegroups.com
At 3/27/2013 10:21 AM Wednesday, Nate Vack wrote:
>On Tue, Mar 26, 2013 at 9:24 PM, Jonathan Ross
><jr3...@student.american.edu> wrote:
> > I've done that too, but he didn't like it. He said it'd be too predictable
> > or something.
>
>One other approach you can take is:
>
>1: At the beginning of the experiment, generate a trial order that is
>likely, but not guaranteed, to have the properties you need.
>2: Check the list to see if it's valid. If not, go to 1.
>3: You have a randomized trial list!

Ah yes, a variant of the venerable bogosort (see
http://en.wikipedia.org/wiki/Bogosort ). For some cases of
"randomization with constraints" I have used a more systematic method
where I started with step 1 above, and then systematically walked
through the candidate trial order and swapped items around to make
them satisfy my constraints while still preserving the basic
randomness of the order (of course, I did that long ago using C, but
Python should also handle this well). And if you want an even better
algorithm, see Remillard & Clark (1999), Beh Res Meth Instr Comp 31:
235-243. I would really like to see that implemented in Python/PsychoPy!

-- David McFarlane, Professional Faultfinder

David McFarlane

unread,
Mar 28, 2013, 1:57:11 PM3/28/13
to psychop...@googlegroups.com
Gratuitous anecdote...

In the mid 2000s I programmed a Continuous Performance Task -
Identical Pairs (CPT-IP), using a popular and very capable
programming platform. My program generated the trial sequence on the
fly, but unfortunately, it had about a 4% chance of producing a
spurious identical-pair trial on any run. I just could not find a
way to fix this, so I wondered how the original researchers did
it. Well, the original study went back to the 1980s as I recall, and
the methods specified only that they used images of playing cards,
presented in blocks of 80. Why blocked trials, and why that block
length? In particular, 80 does not correspond to any obvious
arrangements of 52 playing cards. The paper did not explain. Then,
because I go back that far myself, I thought about the era. Many
experiments were not computerized yet, and a Kodak Carousel slide
projector held -- you guessed it -- 80 slides (the paper did not say
that they used a Kodak Carousel slide projector, because back then
that was tacitly understood). Clearly, the experimenters in this
case used a fixed pre-randomized trial sequence for their CPT-IP, and
they felt it just fine! (Now, just think how much is left unsaid in
your Procedures write-ups today, and how opaque they will be to
future generations.)

(In the end, we first just ran the program as-is and deleted the
spurious trials, and later just used a fixed pre-randomized order as
others have suggested here. And FWIW, many fMRI designs still
*require* fixed prerandomized sequences.)

-- David McFarlane, Professional Faultfinder


Nate Vack

unread,
Mar 28, 2013, 2:48:39 PM3/28/13
to psychop...@googlegroups.com
On Thu, Mar 28, 2013 at 12:57 PM, David McFarlane <mcfa...@msu.edu> wrote:

> Clearly, the
> experimenters in this case used a fixed pre-randomized trial sequence for
> their CPT-IP, and they felt it just fine!

Well... just because they *did* it doesn't mean they felt it was fine :)

Cool story, though :)

Also: great reference.

-n

Jeremy Gray

unread,
Mar 28, 2013, 3:04:09 PM3/28/13
to psychop...@googlegroups.com
I found the Remillard & Clark (1999) paper but not the Pascal program
they said was available on the web...
> --
> 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.

Andrew Poppe

unread,
Mar 28, 2013, 4:26:30 PM3/28/13
to psychop...@googlegroups.com
It seems like that program ( for DOS ) was replaced by this one ( associated paper ). There is also the attached spreadsheet for generating the probability matrices.

Andrew Poppe

Graduate Student, Clinical Science and Psychopathology Research Program
University of Minnesota
N427 Elliott Hall
75 E. River Rd.
Minneapolis, MN 55455

Popp...@UMN.edu
TransitionMatrix.xls

JRoss

unread,
Mar 28, 2013, 5:54:50 PM3/28/13
to psychop...@googlegroups.com
I'm not sure what that is, but it kinda looks right... How would I implement it in PsychoPy?

iamjen...@gmail.com

unread,
Jun 14, 2013, 6:07:08 AM6/14/13
to psychop...@googlegroups.com
Hi all,

I assume that you have probably solved this by now as these posts were a long time ago, but I thought I'd add anyway!
I also had to create a CPT-AX task, with 10% of trials AX, 10% A-not followed by-X, and 10% X-not preceded by-A. In the end the easiest (although definitely not the neatest) way to do it was to use a new routine for each block, and create a separate conditions file for each block, where I made my 'pseudo-randomised' sequences by hand and got PsychoPy to call them in a sequential order. This also allowed me to make sure that only AX trials were considered 'correct' too. It was practical for my study as I wanted to compare two structurally identical versions of the task, so giving all participants the same stimulus order was actually a benefit, but I can see how this might not be ideal for others.

Jenny

____

PhD Student, School of Psychology, University of Nottingham
Reply all
Reply to author
Forward
0 new messages