-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuroEconomicAnalysis.py
executable file
·355 lines (279 loc) · 11.4 KB
/
neuroEconomicAnalysis.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
# neuroEconomicAnalysis.py
# DDMCubeTeraGrid
#
# Created by Nicholas Cain on 6/3/10.
# Copyright 2010 __MyCompanyName__. All rights reserved.
# Need the data files, the settings file from the save dir, and
# the original DDMCube_Settings.py file.
################################################################################
# Preamble
################################################################################
# Import necessary packages:
import pbsTools as pt
import analysisTools as at
import numpy as np
import numpy, copy, os, pickle, math, sys
from math import sqrt as sqrt
import pylab as pl
import sys
# Main Settings:
DDMCubeDir = '~/Desktop/currentProjects/DDMCubeTeragrid'
saveResultDir = 'NeuroEconomicOptimizeBig'
outputDir = os.path.abspath(os.path.join(os.curdir,saveResultDir))
quickName = 'NeuroeconomicOptimizeBig-Steele'
################################################################################
# Settings:
################################################################################
# Plot Settings:
yBegin = 0
chopHatVals = np.array([0,1.5,2])
betaStd = 0.05
#runDists = {
# 'beta':[('Normal',betaStd)],
# 'C':[('Delta',4)]}
#runDists = {
# 'beta':[('Delta',6),('Delta',7),('Delta',8),('Delta',9),('Delta',10)],
# 'C':[('Delta',4)]}
#runDists = {
# 'beta':[('Normal',betaStd)],
# 'C':map(lambda x: ('Delta',x), range(11))}
runDists = {
'beta':map(lambda x: ('Delta',x), range(17)),
'C':[('Delta',4)]}
# Figure Saving Settings:
saveFileNamePrefix = 'BetaDelta'
figOutputSubDir = 'fixedC_4E00'
saveFig = 1
#-------------------------------------------------------------------------------
# General Settings:
smoothBeta = 5
# Figure Saving Settings:
figOutputDir = '/Users/Nick/Desktop/currentProjects/DDMCubeTeragrid/NeuroEcoFigs'
saveType = 'eps' # Default is parent directory.
transparentBackground = True
# Font settings:
fig_width = 3 # width in inches
fontSize = 12
labelSize = 10
################################################################################
# Set up:
################################################################################
# Set up:
currentDir = os.path.abspath(os.getcwd())
DDMCubeDir = os.path.abspath(os.path.expanduser(DDMCubeDir))
if figOutputDir == 'Parent':
figOutputDir = os.path.abspath(os.pardir)
os.chdir(DDMCubeDir)
sys.path.append(os.getcwd())
golden_mean = (sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_height = fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
params = {'backend': 'ps',
'axes.labelsize': fontSize,
'text.fontsize': fontSize,
'legend.fontsize': fontSize,
'xtick.labelsize': labelSize,
'ytick.labelsize': labelSize,
'text.usetex': True,
'figure.figsize': fig_size}
pl.rcParams.update(params)
################################################################################
# Define functions:
################################################################################
# Define the functions for the gaussian integral:
def intErfAB(a, b, mu=0, sigma=1):
# Ill need erf(x)
def erf(x):
# save the sign of x
sign = 1
if x < 0:
sign = -1
x = abs(x)
# constants
a1 = 0.254829592
a2 = -0.284496736
a3 = 1.421413741
a4 = -1.453152027
a5 = 1.061405429
p = 0.3275911
# A&S formula 7.1.26
t = 1.0/(1.0 + p*x)
y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x)
return sign*y
a=a*1.
b=b*1.
mu=mu*1.
sigma=sigma*1.
C1 = (b-mu)/(math.sqrt(2)*sigma)
C2 = (a-mu)/(math.sqrt(2)*sigma)
P1 = erf(C1)
P2 = erf(C2)
return .5*(P1-P2)
# Define the smoothing function:
def smooth(y,smoothBeta=smoothBeta):
m = len(y)
p = pl.diff(pl.eye(m),3).transpose()
A = pl.eye(m)+smoothBeta*pl.dot(p.transpose(),p)
smoothY = pl.solve(A, y)
return smoothY
# Define Main function:
def makePlot(jobDist):
############################################################################
# Do the marginalization:
############################################################################
# Setup:
saveName = saveFileNamePrefix
# Import Data:
crossTimeData, resultData, dims, settings, FD, numberOfJobs, gitVersion = at.getDataAndSettings(quickName, saveResultDir)
# Reduce Constant Dimensions:
constDims = ['A', 'B', 'K', 'dt', 'noiseSigma', 'tMax', 'xStd', 'xTau', 'yTau']
for collapseDim in constDims:
crossTimeData, resultData, dims = at.reduce1D(crossTimeData, resultData, dims, collapseDim, settings[collapseDim], settings[collapseDim])
crossTimeData = np.squeeze(crossTimeData)
resultData = np.squeeze(resultData)
# Pick Distribution, C:
if jobDist['C'][0] == 'Uniform':
numOfValues = len(settings['C'])
probDist = np.ones(numOfValues)*(1./numOfValues)
saveName += '_C_Uniform'
elif jobDist['C'][0] == 'Delta':
numOfValues = len(settings['C'])
probDist = np.zeros(numOfValues)
probDist[jobDist['C'][1]] = 1
saveName += '_C_Delta_' + str(jobDist['C'][1])
else:
print 'Not Supported: ' + jobDist['C'][0]
sys.exit()
# Marginalize across C:
marginalVal = settings['C'][0]
crossTimeCubeMarginal, resultCubeMarginal, newDimsMarginal = at.reduce1D(crossTimeData, resultData, copy.copy(dims), 'C', settings['C'], marginalVal)
crossTimeCubeMarginal *= probDist[0]
resultCubeMarginal *= probDist[0]
for i in range(1,numOfValues):
marginalVal = settings['C'][i]
crossTimeCubeTemp, resultCubeTemp, newDimsMarginalTemp = at.reduce1D(crossTimeData, resultData, copy.copy(dims), 'C', settings['C'], marginalVal)
crossTimeCubeMarginal += probDist[i]*crossTimeCubeTemp
resultCubeMarginal += probDist[i]*resultCubeTemp
# Pick Distribution, beta:
if jobDist['beta'][0] == 'Normal':
numOfValues = len(settings['beta'])
betaVals = np.array(settings['beta'])
delta = betaVals[1]-betaVals[0]
betaVals = betaVals - delta*1./2
L = np.append(-10, betaVals[1:])
R = np.append(betaVals[1:], 10)
probDists = [0]*len(L)
for j in range(len(L)):
probDists[j] = intErfAB(L[j], R[j], sigma=jobDist['beta'][1])
saveName += '_beta_Normal_' + str(jobDist['beta'][1])
elif jobDist['beta'][0] == 'Delta':
numOfValues = len(settings['beta'])
probDists = np.zeros(numOfValues)
probDists[jobDist['beta'][1]] = 1
saveName += '_beta_Delta_' + str(jobDist['beta'][1])
else:
print 'Not Supported: ' + jobDist['beta'][0]
sys.exit()
# Marginalize across beta:
marginalVal = settings['beta'][0]
finalMarginalCrossTimeCube, finalMarginalResultCube, finalDimsMarginal = at.reduce1D(crossTimeCubeMarginal, resultCubeMarginal, copy.copy(newDimsMarginal), 'beta', settings['beta'], marginalVal)
finalMarginalCrossTimeCube *= probDists[0]
finalMarginalResultCube *= probDists[0]
for j in range(1,numOfValues):
marginalVal = settings['beta'][j]
crossTimeCubeTemp, resultCubeTemp, newDimsMarginalTemp = at.reduce1D(crossTimeCubeMarginal, resultCubeMarginal, copy.copy(newDimsMarginal), 'beta', settings['beta'], marginalVal)
finalMarginalCrossTimeCube += probDists[j]*crossTimeCubeTemp
finalMarginalResultCube += probDists[j]*resultCubeTemp
############################################################################
# Now make Speed/Accuracy Tradeoff Plot:
############################################################################
# Initializations:
sliceDict={'yBegin':yBegin}
FC = [0]*len(chopHatVals)
RT = [0]*len(chopHatVals)
# Get data slices:
for i in range(len(chopHatVals)):
sliceDict['chopHat'] = chopHatVals[i]
crossTimeDataThisSlice = finalMarginalCrossTimeCube
resultDataThisSlice = finalMarginalResultCube
crossDimsTemp = copy.copy(finalDimsMarginal)
for collapseDim in iter(sliceDict):
crossTimeDataThisSlice, resultDataThisSlice, crossDimsTemp = at.reduce1D(crossTimeDataThisSlice, resultDataThisSlice, crossDimsTemp, collapseDim, settings[collapseDim], sliceDict[collapseDim])
RT[i] = np.squeeze(crossTimeDataThisSlice)
FC[i] = np.squeeze(resultDataThisSlice)
# Set up plot window:
pl.figure(1)
pl.clf()
LCushion = 0.19
RCushion = .06
BCushion = .21
TCushion = .05
pl.axes([LCushion, BCushion, 1-RCushion-LCushion, 1-TCushion-BCushion])
# Plot Data: Comment out one of the two, for smoothing or not...
for i in range(len(chopHatVals)):
if i == 0:
pl.plot((RT[i]), (FC[i]),'--')
else:
pl.plot((RT[i]), (FC[i]))
# Set Axes:
pl.xlabel('RT')
pl.ylabel('FC')
pl.ylim([.5,1])
# Save figure:
if saveFig == 1:
(pl.savefig(os.path.join(figOutputDir, figOutputSubDir, saveName + '.' + saveType),
format = saveType, transparent = transparentBackground))
return (RT,FC)
################################################################################
# Generate the plots:
################################################################################
# Create jobDist from runDist
# Call the Main function:
for betaSetting in runDists['beta']:
for CSetting in runDists['C']:
makePlot({'beta':betaSetting,'C':CSetting})
################################################################################
# Clean Up
################################################################################
# Change back directory:
os.chdir(currentDir)
################################################################
# Gather data:
################################################################
#settingsFile = 'NeuroeconomicOptimizeBig-Steele_a6f9c67c-fc7e-4bcc-b687-3ebb002d0104.settings'
# Split off uuid fomr settingFile:
#jobQuickNameIn, currentSuffix = settingsFile.split('_')
#myUUID, trash = currentSuffix.split('.')
#quickNamePrefix, quickNameSuffix = jobQuickNameIn.split('-')
#
# Import settings:
#execfile(os.path.join(outputDir,'DDMCube_Settings.py'))
#quickName = quickNamePrefix + '-' + quickNameSuffix
#numberOfJobs = [simsPerRep, simsPerRep*repsPerProc*procsPerNode*nodes]
#
# Grab the name of the settings for the run:
#settings, FD, numberOfJobs, gitVersion = pt.unpickle(os.path.join(os.getcwd(),saveResultDir,settingsFile))
#
# Generate crossTimesArray and resultsArray:
#resultList = pt.getFromPickleJar(loadDir = outputDir, fileNameSubString = 'simResults.dat')
#arrayLength = len(resultList[0][0])
#resultsArray = np.zeros(arrayLength, dtype=float)
#crossTimesArray = np.zeros(arrayLength, dtype=float)
#for i in range(len(resultList)):
# resultsArray = resultsArray + resultList[i][0]
# crossTimesArray = crossTimesArray + resultList[i][1]
#crossTimesArray = crossTimesArray/numberOfJobs[1]
#resultsArray = resultsArray/numberOfJobs[1]
#
# Reshape results:
#params = settings.keys()
#params.sort()
#newDims = [len(settings[parameter]) for parameter in params]
#crossTimesArray = np.reshape(crossTimesArray,newDims)
#resultsArray = np.reshape(resultsArray,newDims)
#
# Export data in familiar format, to outputDir:
#fOut = open(os.path.join(outputDir,quickName + '_' + str(myUUID) + '.dat'),'w')
#pickle.dump((crossTimesArray, resultsArray, params),fOut)
#fOut.close()