Counterbalancing (Help with Python)

macOS 10.12.4
Psychopy 1.84.2

I am designing a mixed-language lexical decision experiment in PsychoPy, and what I would like to do is counterbalance presentation orders across participants in order to control for lexical variables and order effects.

I originally posted a more general question, but since then I have researched the question more thoroughly. I have a rough idea of what the code should look like, but unfortunately, my Python is elementary, and I need help with (1) the syntax, (2) how to refer to columns in the conditions file, and (3) where to put this code in the experiment.

What I would like to do is, have each real word (positive response, correctAns = rshift), appear equally in four conditions across participants. Each word will only appear one time per participant, but I would like it to appear in condition A for participant 1, B for 2, C 3, D 4, A 5, etc.

The four “conditions” (I’m using the term here in an experimental sense, not the PsychoPy sense) I have are defined by the language of the previous response (1, 2), and whether the previous response was a word or nonword (correctAns = rshift, lshift). This should be true for each real word in each language. So, the four (eight) “conditions” are as follows:

1+, 1+
1-, 1+
2+, 1+
2-, 1+
1+, 2+
1-, 2+
2+, 2+
2-, 2+

(I don’t care too much about how the nonwords (-) appear, so they are not included in this list).

I have made an attempt at what the code should look like, but I know very little Python. What I need, as I said above, is to (1) fix the syntax to make it actually Python, (2) properly reference the other conditions (language, correctAns) for a given item (these are other columns in the conditions file, in addition to word), and (3) know where to put this in the code.

For each participant, I will give them a number 1-4 and enter this upon starting the experiment. I hope to have ~20 total participants.

If you could help me with writing the code, I would GREATLY appreciate it. Thank you so much.

(As a note, in the code below, I put the range as 4-457 even though I have a total range of 0-467 because I wanted to guard against it being stuck if only a few items remained but the conditions could not be fulfilled.)


r = (trials.thisIndex + thisExp.participant) % 4

For i in range(4, 457):
	If correctAns == ‘rshift’:
		If r == 0 and thisTrial.language == thisTrial.language(i-1) and thisTrial.correctAns == thisTrial.correctAns(i-1):
			Return True
		If r == 1 and thisTrial.language != thisTrial.language(i-1) and thisTrial.correctAns == thisTrial.correctAns(i-1):
			Return True
		If r == 2 and thisTrial.language == thisTrial.language(i-1) and thisTrial.correctAns != thisTrial.correctAns(i-1):
			Return True
		If r == 3 and thisTrial.language != thisTrial.language(i-1) and thisTrial.correctAns != thisTrial.correctAns(i-1):
			Return True
	Else:
		random.shuffle
Repeat

Please let me know if you need a copy of my conditions file, psyexp file, etc. Thank you.

Hi there,

I don’t think it’s a good idea for someone to turn this into python code for you since you should understand python yourself if you want to use it for an experiment :slight_smile: It’s a very easy programming language to pick up!

I’m guessing the experiment is working well apart from this component? Did you make it in the builder? One way you could do this is to use the Select rows parameter:
http://www.psychopy.org/builder/flow.html#selecting-a-subset-of-conditions

So you could duplicate your conditions in the file, and then simply use your participant number to select rows.

For example, if I open the stroop demo in the builder, and in the “Select rows” of the trials loop enter:
[int(expInfo['participant'])], and participant is a number such as 2, then only the second condition happens.

You could do something similar here, right?

Hi Jan,

Thank you for the comment and ideas.

I have been learning Python, and I have been working on this specific problem for a couple weeks now, with general Python tutorials, reading in the forums, reading posts on stack exchange, and reading the Psychopy manual. I am really stuck here, and I’m turning here as a last resort. I understand that asking somebody to write my code for me is a no-no, but I’m not aware of any other resources that address my specific difficulties. What I’m having the most trouble with is (1) how to refer to the trial and experiment variables (especially the specific conditions in the xlsx file) and (2) the more difficult structure of checking for the four (eight) conditions and then shuffling items until all are met.

I understand your suggestion, but what I’m trying to do is a little bit different. Every participant will see every word, not a subset of rows. Then, the words will be randomized according to one of four sets of conditions, defined as the previous correctAns as same or different and previous language as same or different.

I’ve updated the code I have above to something that might be closer to what I need.

Best,
Jason

Ah OK, I see what you mean and why my suggestion wasn’t that helpful! I think it would probably help a lot to see your psyexp file to really visualise what’s going on, so if you could post that and the conditions file that would be great.

Thank you, Jan. I’ve attached all my files. The routine practice, loop trials_2, and conditions file practice list.xlsx do not need the same sort of randomization, but I included them since they’re part of the whole experiment. I need help with the routine trial, loop trials, and conditions file list.xlsx. Thank you!

[deleted]