Reputation: 274
I am trying to run a counter balancing program. I cannot understand what Python is doing to get the output I receive. I have 8 variants of 16 maps. Variants are defined by timingConditions
* distractionConditions
. There are 4 key parts of the initial variable definitions:
inst
is a list containing all 8 variants * 16 experimental maps
ordered by variant (the maps are literally like a video game map). This leaves len(inst) = 128
.inst
is subdivided into 8 sublists in conds
. Each of these
sublists represents maps 1-16 of a specific variant. Each index
within a sublist represts one map/variant combination. Calling the index of each sublist in conds
will show you this clearly.The 16 maps are broken into 8 lists containing two maps each, defined in variable MapGroups
. These 8 lists are for use in the matrix below.
counterBalanceMatrix
represents eight unique balanced orderings of maps to condition assignment. Each subject in range(1,9)
is assigned to one of these rows. The numbers in the row represent map groups. Columns (i.e. index ordering) represent assignment of variant to map group. For example, counterBalanceMatrix[0][0]
returns 1
, the first index corresponds to assignment of first variant column SSTrue
; and second index corresponds to MapGroups[0]
(which would return '0','15'). Therefore, the desired output would be maps 0 and 15 (or 1 and 16 without zero based ordering) are assigned SS-True. You can picture this as follows:
####+--------+--------+--------+--------+---------+---------+---------+---------+
####| SSTrue | SLTrue | LSTrue | LLTrue | SSFalse | SLFalse | LSFalse | LLFalse |
####+--------+--------+--------+--------+---------+---------+---------+---------+
####| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
####| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 |
####| 3 | 4 | 5 | 6 | 7 | 8 | 1 | 2 |
####| 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 |
####| 5 | 6 | 7 | 8 | 1 | 2 | 3 | 4 |
####| 6 | 7 | 8 | 1 | 2 | 3 | 4 | 5 |
####| 7 | 8 | 1 | 2 | 3 | 4 | 5 | 6 |
####| 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
####+--------+--------+--------+--------+---------+---------+---------+----- ----+
The expected output of the code below that for subject in range(1,9):
, there will be one instance of each MapGroup
, and two observations of each variant (e.g. SS-TRUE, LL-False etc). Across all subjects, there will be equal observations of all MapGroups
and variants. This part of the code works as expected.
import re
timingConditions = ["SS","SL","LS","LL"]
distractionConditions = ["True","False"]
maps = [i for i in range(1,17)]
#This loop sets up the 128 (16*8) variants. Inst for instances of scenario.
inst = []
for d in distractionConditions:
for t in timingConditions:
for map in maps:
inst.append(str(str(map)+"-"+t+"-"+d))
conds = [inst[0:16],inst[16:32],inst[32:48],inst[48:64],inst[64:80],inst[80:96],inst[96:112],inst[112:128]]
MapGroups= [[0,8],
[1,9],
[2,10],
[3,11],
[4,12],
[5,13],
[6,14],
[7,15]]
counterBalanceMatrix= [[1,2,3,4,5,6,7,8],
[2,3,4,5,6,7,8,1],
[3,4,5,6,7,8,1,2],
[4,5,6,7,8,1,2,3],
[5,6,7,8,1,2,3,4],
[6,7,8,1,2,3,4,5],
[7,8,1,2,3,4,5,6],
[8,1,2,3,4,5,6,7]]
for subject in range(1,9):
cRow = counterBalanceMatrix[(subject-1)%8]#Use the modulus operator to find out which row to use in counterbalancing for this subject.
scenArray = []
for group in range(0,8):#This loops across map groups and look up their assigned interruption condition
scenArray.extend([conds[cRow[group]-1][i] for i in MapGroups[group]])
print "****","Participant",subject,"Correct Day 1****"
print scenArray
print "\n\n"
This is where the problem is:
I want to repeat this procedure, but mirrored. That is, whatever variant each list in MapGroups
was assigned initially, I want it to be inversed (e.g. If you received MapGroups[0]
as True, then I want them to be False. MapGroups[0]
was assigned SS, it now must be LL.
My initial solution was to reverse the counterBalanceMatrix
and apply the same Loop. However this did not work:
counterBalanceMatrixReverse= []
for list in counterBalanceMatrix:
counterBalanceMatrixReverse.append(list[::-1])
###And then run the exact same loop over.
for subject in range(1,9):
cRow = counterBalanceMatrixReverse[(subject-1)%8]
#
scenArray = []
for group in range(0,8):
scenArray.extend([conds[cRow[group]-1][i] for i in MapGroups[group]])
print "****","Participant",subject,"Broken Reversed 2****"
print scenArray
print "\n\n"
The output is incorrect, for instance:
>Participant 4
>'2-SL-True', '10-SL-True'
Participant 4 Reversed
>'2-SS-False', '10-SS-False'
Exptected Reversed Ouput:
>'2-LS-False', '10-LS-False'
However, the simply reversing the cond array does solve my problem
condsreverse = []
condsreverse.extend(conds[::-1])
for subject in range(1,9):
cRow = counterBalanceMatrix[(subject-1)%8]#Note I use same matrix
scenArray = []
for group in range(0,8):
scenArray.extend([condsreverse[cRow[group]-1][i] for i in MapGroups[group]])
print "****","Subject",subject,"Correct Reverse****"
print scenArray
print "\n\n"
I have sat on this now for three days, and I can't work out why the initial reversing solution does not produce the desired output.
Upvotes: 1
Views: 66
Reputation: 274
I believe solution is as follows, if only for the purpose of closing question.
My mistake was that I assumed the [conds[cRow[group]-1]
was assigning the variant based on the index of the cRow[i]
and the cRow[i]
was simply being used to call the specific maps. However, what is actually doing is assigning the variant based on the value of cRow[i]
. Consequently, the "columns" were not being assigned to MapGroups
based on indexing. This means that simply reversing the counterBalanceMatrix
is basically just creating another version of the counterBalanceMatrix
.
The only way I can get the conditions to mirror each other is by reversing the order of conds
because this the list which is actually being used in the assignment; unless of course I wanted to change scenArray.extend([condsreverse[cRow[group]-1][i] for i in MapGroups[group]])
to call index of each item (for both the original and reversed), and then based on that index call the map group, as follows:
for index in range(0,8):
scenArray.extend([conds[index][i] for i in MapGroups[cRow[index]-1]])
PS: I guess writing it out as a question helped me realize my fault. Sorry for this.
Upvotes: 1