-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalcStayProb.py
273 lines (262 loc) · 11 KB
/
calcStayProb.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from __future__ import division
from __future__ import print_function # Only needed for Python 2
# A probably overly complicated way to determine the probability of repeating the action of stage 1, trial n in stage 1, trial n+1
# Should produce numbers that look like Fig. 2 of Daw et al. 2011
# Input should be in the form of: firstStageChoice, secondStageState, secondStageChoice, reward
#### This is incomplete #####
# How did I get it working before? I may have just hardcoded the filename
class CalcStayProb():
def __init__(self, actions=["left", "right"], states = [1, 2]):
self.actions = actions
self.numActions = len(actions)
self.trr = 0 # total rare rewarded
self.tru = 0 # total rare unrewarded
self.tcr = 0 # total common rewarded
self.tcu = 0 # total common unrewarded
self.trr_s = 0 # total rare rewarded stays
self.tru_s = 0 # total rare unrewarded stays
self.tcr_s = 0 # total common rewarded stays
self.tcu_s = 0 # total common unrewarded stays
self.states = states
self.numStates = len(states)
def countFile(self, f):
first = f.readline()
second = f.readline()
while second != "":
firstChoice, nextState, secondChoice, reward = first.split(' ')
reward = int(reward)
nextFirst = second.split(' ')[0]
# okay, this is not general at all, but I'm going to program it this way first so that I know roughly what I'm doing
if firstChoice == "left":
#print "a"
if nextState == "2":
#print "b"
if reward == 1:
#print "c"
# rare rewarded case
self.trr += 1
if nextFirst == firstChoice:
#print "d"
self.trr_s += 1
elif reward == 0:
#print "e"
# rare unrewarded case
self.tru += 1
if nextFirst == firstChoice:
#print "f"
self.tru_s += 1
else:
print ("something probably wrong with your string parsing of last token")
return 1
elif nextState == "1":
#print "g"
if reward == 1:
#print "h"
# common rewarded case
self.tcr += 1
if nextFirst == firstChoice:
#print "i"
self.tcr_s += 1
elif reward == 0:
#print "j"
# common unrewarded case
self.tcu += 1
if nextFirst == firstChoice:
#print "k"
self.tcu_s += 1
else:
print ("something probably wrong with your string parsing of last token")
return 2
else:
print ("something may have gone wrong with parsing the second token")
return 3
elif firstChoice == "right":
#print "l"
if nextState == "1":
#print "m"
if reward == 1:
#print "n"
#rare rewarded case
self.trr += 1
if nextFirst == firstChoice:
#print "o"
self.trr_s += 1
elif reward == 0:
#print "p"
# rare unrewarded case
self.tru += 1
if nextFirst == firstChoice:
#print "q"
self.tru_s += 1
else:
print ("something probably wrong with your string parsing of last token")
return 4
elif nextState == "2":
#print "r"
if reward == 1:
#print "s"
# common rewarded case
self.tcr += 1
if nextFirst == firstChoice:
#print "t"
self.tcr_s += 1
elif reward == 0:
#print "u"
# common unrewarded case
self.tcu += 1
if nextFirst == firstChoice:
#print "v"
self.tcu_s += 1
else:
print ("something probably wrong with your string parsing of last token")
return 5
else:
print ("something may have gone wrong with parsing the second token")
return 6
else:
print ("something may have gone wrong with parsing the first token")
return 7
first = second
second = f.readline()
# everything probably worked!
return 0
def countStrings(self, f):
first = f[0]
second = f[1]
i = 1
while i < len(f):
firstChoice, nextState, secondChoice, reward = first.split(' ')
reward = int(reward)
nextFirst = second.split(' ')[0]
# okay, this is not general at all, but I'm going to program it this way first so that I know roughly what I'm doing
if firstChoice == "left":
#print "a"
if nextState == "2":
#print "b"
if reward == 1:
#print "c"
# rare rewarded case
self.trr += 1
if nextFirst == firstChoice:
#print "d"
self.trr_s += 1
elif reward == 0:
#print "e"
# rare unrewarded case
self.tru += 1
if nextFirst == firstChoice:
#print "f"
self.tru_s += 1
else:
print ("something probably wrong with your string parsing of last token")
return 1
elif nextState == "1":
#print "g"
if reward == 1:
#print "h"
# common rewarded case
self.tcr += 1
if nextFirst == firstChoice:
#print "i"
self.tcr_s += 1
elif reward == 0:
#print "j"
# common unrewarded case
self.tcu += 1
if nextFirst == firstChoice:
#print "k"
self.tcu_s += 1
else:
print ("something probably wrong with your string parsing of last token")
return 2
else:
print ("something may have gone wrong with parsing the second token")
return 3
elif firstChoice == "right":
#print "l"
if nextState == "1":
#print "m"
if reward == 1:
#print "n"
#rare rewarded case
self.trr += 1
if nextFirst == firstChoice:
#print "o"
self.trr_s += 1
elif reward == 0:
#print "p"
# rare unrewarded case
self.tru += 1
if nextFirst == firstChoice:
#print "q"
self.tru_s += 1
else:
print ("something probably wrong with your string parsing of last token")
return 4
elif nextState == "2":
#print "r"
if reward == 1:
#print "s"
# common rewarded case
self.tcr += 1
if nextFirst == firstChoice:
#print "t"
self.tcr_s += 1
elif reward == 0:
#print "u"
# common unrewarded case
self.tcu += 1
if nextFirst == firstChoice:
#print "v"
self.tcu_s += 1
else:
print ("something probably wrong with your string parsing of last token")
return 5
else:
print ("something may have gone wrong with parsing the second token")
return 6
else:
print ("something may have gone wrong with parsing the first token")
return 7
i += 1
if i == len(f):
break
first = second
second = f[i]
# everything probably worked!
return 0
def calcStayProb(self, outfile=None, return_value=False):
stay_tcr = self.tcr_s/self.tcr
stay_trr = self.trr_s/self.trr
stay_tcu = self.tcu_s/self.tcu
stay_tru = self.tru_s/self.tru
if outfile is not None:
print('{0} {1} {2} {3}'.format(stay_tcr, stay_trr, stay_tcu, stay_tru), file=outfile)
elif return_value:
return (stay_tcr, stay_trr, stay_tcu, stay_tru)
else:
print(stay_tcr, stay_trr, stay_tcu, stay_tru)
def doItAll(self, fileName, outfile=None):
f = open(fileName)
# maybe some other stuff here to catch exceptions
success = self.countFile(f)
if success == 0:
# good
self.calcStayProb(outfile=outfile)
else:
print(success)
def doItAllString(self, strings, outfile=None, return_value=False):
# maybe some other stuff here to catch exceptions
success = self.countStrings(strings)
if success == 0:
# good
if return_value:
return self.calcStayProb(outfile=outfile, return_value=True)
else:
self.calcStayProb(outfile=outfile)
else:
print(success)
if __name__ == "__main__":
# something here to parse the input string for the correct file name
calculator = CalcStayProb()
calculator.doItAll("temp_file.txt")