-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstim_analysis_two.py
127 lines (106 loc) · 3.43 KB
/
stim_analysis_two.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
124
125
126
127
import csv
import pandas as pd
from pandas import DataFrame
#returns median number both for even or odd list
def median(time_list):
time_list = [int(i) for i in time_list]
sorts = sorted(time_list) #convert a list of str to floats or ints
length = len(sorts)
if not length%2:
return (sorts[length/2] + sorts[length/2-1]) / float(2)
return sorts[length/2]
def confusion_matrix(df_list, fan, age):
# applied confusion matrix to given termins
# ------------------------
# | TP -> hits | FN |
# ------------------------
# | FP -> Fas | TN -> CRs |
# ------------------------
# structure of 2d list
#[[TP, FN],[FP, TN]]
tp_time = []
fp_time = []
tn_time = []
matrix = [[0,0],[0,0]]
for i in df_list:
if i[0] == "j":
if i[3] == fan and i[4] == age:
if i[2] == i[5]:
#TP
matrix[0][0] += 1
tp_time.append(i[1])
else:
#FP
matrix[1][0] += 1
fp_time.append(i[1])
if i[0] == "k":
if[3] == fan and i[4] == age:
if i[2] == i[5]:
#TN
matrix[1][1] += 1
tn_time.append(i[1])
else:
#FN
matrix[0][1] += 1
if fan == 'lf':
if age == 'young':
final = [
float(matrix[0][0])/12,
float(matrix[1][0])/12,
median(tp_time),
0#median(tn_time)
]
else:
final = [
float(matrix[0][0])/12,
float(matrix[1][0])/12,
median(tp_time),
0#median(tn_time)
]
if fan == 'hf':
if age == 'older':
final = [
float(matrix[0][0])/12,
float(matrix[1][0])/12,
median(tp_time),
0#median(tn_time)
]
else:
final = [
float(matrix[0][0])/12,
float(matrix[1][0])/12,
median(tp_time),
0#median(tn_time)
]
#final, TP, FP
return final, matrix[0][0], matrix[1][0]
def main():
#subject's data
subject_data = list(csv.reader(open('/path/to/<file>.txt', 'rb'), delimiter='\t'))
#master data
master_test = DataFrame(pd.read_csv('/path/to/<file>.csv'))
df_data = DataFrame(subject_data[584:775:2])
df_data = df_data.drop([x for x in range(13) if x not in (7,11)], axis=1)
cols = ['resp', 'time']
df_data.columns = cols
#j -> yes, k -> no
df_data['resp_cmp'] = [x.replace('j', 'old') for x in df_data['resp'].values.tolist()]
df_data['resp_cmp'] = [x.replace('k', 'new') for x in df_data['resp_cmp'].values.tolist()]
df_data['fan'] = master_test['fan']
df_data['status'] = master_test['age']
df_data['correct_test'] = master_test['test']
young_hf_data, young_hf_tp_total, young_hf_fp_total = confusion_matrix(df_list=df_data.values.tolist(), fan='hf', age='young')
young_lf_data, young_lf_tp_total, young_lf_fp_total = confusion_matrix(df_list=df_data.values.tolist(), fan='lf', age='young')
young_hf_data, young_hf_tp_total, young_hf_fp_total = confusion_matrix(df_list=df_data.values.tolist(), fan='hf', age='young')
older_lf_data, older_lf_tp_total, older_lf_fp_total = confusion_matrix(df_list=df_data.values.tolist(), fan='lf', age='older')
older_hf_data, older_hf_tp_total, older_hf_fp_total = confusion_matrix(df_list=df_data.values.tolist(), fan='hf', age='older')
out = csv.writer(open('/path/to/output/<file>.csv', 'w'), delimiter=',')
out.writerow(
sum(
[young_lf_data, young_hf_data, older_lf_data, older_hf_data,
[(young_lf_tp_total + young_hf_tp_total + older_lf_tp_total + older_hf_tp_total)/float(48)],
[(young_lf_fp_total + young_hf_fp_total + older_lf_fp_total + older_hf_fp_total)/float(48)]
], [])
)
if __name__ == '__main__':
main()