-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresCollate.py
executable file
·123 lines (111 loc) · 5.33 KB
/
resCollate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
__author__ = 'james'
import csv
import datetime
import sys
import operator
def genArr():
y = []
for x in range(5):
y = y + [x+1]*6
arr = [[1, 2, 3]*10, [1, 1, 1, 2, 2, 2]*5, y]
return arr
def readCSVFile(CSVFile):
f = open(CSVFile, 'rb') # opens the csv file
array = []
reader = csv.reader(f) # creates the reader object
## Read in from CSV to array
for row in reader: # iterates the rows of the file in orders
if row[3] not in ('postquestionnaire', 'INSTRUCTIONS'): # Strip meta rows from data
array.append(row) # Add row to array
return array
def manipulateArray(array):
## Create condition lookup table
y = []
for x in range(5):
y = y + [x+1]*6
arr = [[1, 2, 3]*10, [1, 1, 1, 2, 2, 2]*5, y]
sortedList = [[]]
j = 0
condIndex = 12
condensedList = []
headers = ['Seed', 'Dynamic', 'Foregone', 'Block', 'WorkerID', 'AssignmentID', 'Trial#', 'SubmitTime',
'Card Selection', 'Selected Value', 'Max value', 'TrialNo.', 'Condition', 'Selected Cumulative',
'Counter1', 'Counter2', 'Counter3', 'Counter4', 'Gender', 'Age', 'Reaction Time', 'Card1 R',
'Card1 Mu', 'Card2 R', 'Card2 Mu', 'Card3 R', 'Card3 Mu', 'Card4 R', 'Card4 Mu']
## Make more readable
for i in xrange(len(array)):
split = array[i][0].split(':') # Split first column into Worker ID and Assignment ID
array[i][0] = split[0] # Replace into array
array[i].insert(1, split[1]) # Insert into array
array[i][3] = datetime.datetime.fromtimestamp(int(array[i][3])/1000.0).isoformat().split('.')[0] # Convert from js timestamp to date
## Group by workerID
for i in xrange(len(array)):
if array[i][0] == array[abs(i-1)][0]: # If workerID in array == previous workerID --> add it to same sorted index
sortedList[j].append(array[i])
elif array[i][0] != array[abs(i-1)][0]: # Else start add to a new sorted index, update counter to start again
sortedList.append([]) # Instantiate the next level
sortedList[j+1].append(array[i])
j += 1 # Update the counter (to allow the next row to be evaluated against the previous)
## Check each workers number of trials
for i in xrange(len(sortedList)):
if len(sortedList[i]) == 200: # Check to see worker has 200 trials
condensedList.append(sortedList[i]) # If yes then copy to condensedList, headers = list[0][i]
## Condition read & write. The columns are in reverse order to the js, but otherwise identical
for j in xrange(len(condensedList)):
for i in xrange(len(condensedList[j])): # Go one level deeper to the trial level
condensedList[j][i] = [9, 9, 9, 9] + condensedList[j][i] #Place holder for condition assignment
## 'Right' condition: Forgone Payoff
if arr[0][int(condensedList[j][i][condIndex])] == 1:
condensedList[j][i][2] = 1
elif arr[0][int(condensedList[j][i][condIndex])] == 2:
condensedList[j][i][2] = 2
elif arr[0][int(condensedList[j][i][condIndex])] == 3:
condensedList[j][i][2] = 3
## 'Middle' condition: Dynamic
if arr[1][int(condensedList[j][i][condIndex])] == 1:
condensedList[j][i][1] = 1
elif arr[1][int(condensedList[j][i][condIndex])] == 2:
condensedList[j][i][1] = 2
## 'Left' condition: Seed
if arr[2][int(condensedList[j][i][condIndex])] == 1:
condensedList[j][i][0] = 1
elif arr[2][int(condensedList[j][i][condIndex])] == 2:
condensedList[j][i][0] = 2
elif arr[2][int(condensedList[j][i][condIndex])] == 3:
condensedList[j][i][0] = 3
elif arr[2][int(condensedList[j][i][condIndex])] == 4:
condensedList[j][i][0] = 4
elif arr[2][int(condensedList[j][i][condIndex])] == 5:
condensedList[j][i][0] = 5
## Assign block
for k in xrange(len(condensedList[j][i])):
if i < 50:
condensedList[j][i][3] = 1
elif i >= 50 and i < 100:
condensedList[j][i][3] = 2
elif i >= 100 and i < 150:
condensedList[j][i][3] = 3
elif i >= 150 and i < 200:
condensedList[j][i][3] = 4
#print(condensedList[4])
condensedList = sorted(condensedList, key=lambda x: (x[0:][0][0], x[0:][0][1], x[0:][0][2])) # Sort by Seed, then dynamic, then foregone
condensedList.insert(0, headers)
return condensedList
def writeResults(list):
if raw_input('Would you like a CSV of the results? y or n: ') == 'y':
now = datetime.datetime.now() # Used in csv writing
c = open('Sorted_{}.csv'.format(now.strftime("%Y-%m-%d_%H.%M")), 'wb')
myWriter = csv.writer(c)
for i in list:
if len(i) < 200: # Ugly hack to avoid looping too deep into headers
myWriter.writerow(i)
else:
for j in xrange(len(i)):
myWriter.writerow(i[j])
c.close()
arr = genArr()
array = readCSVFile(sys.argv[1])
#array = readCSVFile('38.csv')
condensedList = manipulateArray(array)
writeResults(condensedList)