-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuron_plot_perturb.py
419 lines (309 loc) · 12.1 KB
/
neuron_plot_perturb.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/python
_usageStr = \
"""usage: neuron_plot_perturb perturbFileName
visualize perturbation landscape from file of perturbed neuron
model"""
import sys, os, numpy
_useMatplotlib = True
_useMayavi = not _useMatplotlib
_useFakeData = False
###############################################################################
if _useMatplotlib:
# import necessary modules
import mpl_toolkits.mplot3d.axes3d as axes3d
import matplotlib
from matplotlib import pyplot
from matplotlib import cm
# update font properties for illustrator compatibility
matplotlib.rcParams['pdf.fonttype'] = 42
#matplotlib.rcParams['font.sans-serif'] = ['Arial', 'Helvetica']
#matplotlib.rc('font', **{'sans-serif' : 'Arial', 'family' : 'sans-serif'})
def plotTrace(trace, labelSize=30, tickSize=24, loglog=False):
"""plot a trace using matplotlib"""
mSize = 10.0
fig = pyplot.figure()
if loglog:
pyplot.loglog(trace['x'], trace['value'], 'bo', ms = mSize)
else:
pyplot.semilogx(trace['x'], trace['value'], 'bo', ms = mSize)
pyplot.title(trace['title'], fontsize=labelSize)
pyplot.xlabel(trace['xName'], fontsize=labelSize)
pyplot.ylabel('rms error (mV)', fontsize=labelSize)
pyplot.xticks(fontsize=tickSize)
pyplot.yticks(fontsize=tickSize)
def plotSurface(surface, labelSize=30, tickSize=24):
"""plot a surface using matplotlib"""
# create a figure
fig = pyplot.figure()
# create an axis
axes = axes3d.Axes3D(fig)
x = numpy.log10(surface['x'])
y = numpy.log10(surface['y'])
value = -numpy.log10(surface['value'])
# create a surface plot
# had cmap=cm.jet_r
# hsv is better
surfPlot = axes.plot_surface(x, y, value, \
rstride=1, cstride=1, cmap=cm.gist_ncar, \
linewidth=0, antialiased=False, edgecolors='none')
axes.set_title(surface['title'], fontsize=labelSize)
axes.set_xlabel(surface['xName'], fontsize=labelSize)
axes.set_ylabel(surface['yName'], fontsize=labelSize)
axes.set_zlabel('rms error (mV)', fontsize=labelSize)
axes.view_init(45, 255)
def _unLog(_t):
"""
Convert _t back to 10^t, keeping 2 digits of precision
"""
if t >= 1:
if t > 2:
if t >= 5:
return '%.2g' % 10**t
else:
return '%d' % (100 * round(10**(t-2)))
else:
return '%d' % round(10**t)
else:
if t < 0:
return '%.2f' % 10**t
else:
return '%.2f' % 10**t
axes.set_xticklabels([_unLog(t) for t in axes.get_xticks()], \
fontsize=tickSize)
axes.set_yticklabels([_unLog(t) for t in axes.get_yticks()], \
fontsize=tickSize)
axes.set_zticklabels([_unLog(-t) for t in reversed(axes.get_zticks())], \
fontsize=tickSize)
# add a colorbar
#fig.colorbar(surfPlot, shrink = 0.5, aspect = 5)
def showFigures():
"""after all figures are created, show them and wait"""
pyplot.show()
if _useMayavi:
# import necessary modules
from enthought.mayavi import mlab
def plotTrace(trace):
# make a blank figure
mlab.figure()
def plotSurface(surface):
"""plot a surface using mayavi"""
# create a figure
mlab.figure()
# create a surface plot
surfPlot = mlab.mesh(surface['x'], surface['y'], surface['value'])
def showFigures():
"""after all figures are created, show them and wait"""
mlab.show()
###############################################################################
def _makeFakeData():
"""make a couple surfaces to test plotting routines"""
# choose the x and y points that will be evaluated
x = numpy.arange(-numpy.pi, numpy.pi, 0.05 * numpy.pi)
y = numpy.arange(-numpy.pi, numpy.pi, 0.05 * numpy.pi)
# form x and y into a grid
(x, y) = numpy.meshgrid(x, y)
# compute the radius of each x,y pair
r = numpy.sqrt(x**2 + y**2)
# make the first surface
z1 = numpy.sin(r**2) / (1 + r**2)
# make the second surface
z2 = r
# put into a list
surfaces = [\
{'x' : x, 'y' : y, 'value' : z1, \
'title' : 'ripple', 'xName' : 'x', 'yName' : 'y'},
{'x' : x, 'y' : y, 'value' : z2, \
'title' : 'cone', 'xName' : 'x', 'yName' : 'y'}]
return surfaces
###############################################################################
def _getNextLine(fIn):
"""get the next line from the file, comments removed, split into words"""
splitLine = []
while not splitLine:
# get the next line from the file
line = fIn.next()
# remove comments and endline
line = line.split('#', 1)[0].strip('\n')
# split line into words separated by white space
splitLine = line.split(None)
return splitLine
###############################################################################
def loadPerturbInfo(perturbFile):
with open(perturbFile, 'r') as fIn:
# get the parameters that are perturbed
perturbParams = _getNextLine(fIn)
# make a list of indices to perturbed params, set to NaN for now
perturbInds = [float('NaN') for n in range(len(perturbParams))]
# get the number of total parameters
numParameters = int(_getNextLine(fIn)[1])
# get the value of the unperturbed parameters
splitLine = _getNextLine(fIn)
unperturbedValue = float(splitLine[1])
# read in the parameter descriptions (starting with the value)
parameterNames = []
parameterVals = []
for n in range(numParameters):
# get the description
splitLine = _getNextLine(fIn)
name = splitLine[0]
parameterNames.append(name)
parameterVals.append(float(splitLine[1]))
if name in perturbParams:
# this is one of the parameters that is perturbed
perturbInds[perturbParams.index(name)] = n
# get the number of perturbed parameter sets
numParamSets = int(_getNextLine(fIn)[1])
# get the perturbed parameter sets
values = []
perturbedParamSets = []
for n in range(numParamSets):
splitLine = _getNextLine(fIn)
values.append(float(splitLine[0]))
perturbedParamSets.append([float(x) for x in splitLine[1:]])
perturbInfo = { \
'numPerturbed' : len(perturbParams), \
'perturbParams' : perturbParams, \
'perturbInds' : perturbInds, \
'unperturbedValue' : unperturbedValue, \
'numParameters' : numParameters, \
'parameterNames' : parameterNames, \
'parameterVals' : parameterVals, \
'numParamSets' : numParamSets, \
'values' : values, \
'perturbedParamSets' : perturbedParamSets \
}
return perturbInfo
###############################################################################
def getPerturbTraces(perturbInfo):
"""load in the data from a perturb file and turn it into surfaces"""
perturbTraces = []
perturbInds = perturbInfo['perturbInds']
for n in range(perturbInfo['numPerturbed']):
# loop over each perturb param and get its trace
# get the parameter index of the parameter that's changing in this trace
ind_n = perturbInds[n]
# make a list of what parameters aren't changing in this trace
constInds = [ind for ind in perturbInds if ind != ind_n]
# create a dictionary obj to hold trace info
paramName = perturbInfo['perturbParams'][n]
trace = {'x' : [], 'value' : [], \
'title' : 'Perturbing ' + paramName, 'xName' : paramName}
# loop over population of perturbed parameter sets, adding appropriate
# parameter sets to trace
for m in range(perturbInfo['numParamSets']):
paramSet = perturbInfo['perturbedParamSets'][m]
if wantedSet(paramSet, constInds, perturbInfo):
trace['x'].append(paramSet[ind_n])
trace['value'].append(perturbInfo['values'][m])
perturbTraces.append(trace)
return perturbTraces
###############################################################################
def getPerturbSurfaces(perturbInfo):
"""load in the data from a perturb file and turn it into surfaces"""
# get all the pairs of perturbed parameters
pairs = []
for n in range(perturbInfo['numPerturbed']):
for m in range(n + 1, perturbInfo['numPerturbed']):
pairs.append((n, m))
# loop over all possible pairs
perturbInds = perturbInfo['perturbInds']
perturbSurfaces = []
for pair in pairs:
# create an error landscape for this pair of perturbed parameters
# get the parameter index of the parameters that are changing
ind_x = perturbInds[pair[0]]
ind_y = perturbInds[pair[1]]
# make a list of what parameters aren't changing in this trace
constInds = [ind for ind in perturbInds if ind not in [ind_x, ind_y]]
# create a dictionary obj to hold trace info
xName = perturbInfo['perturbParams'][pair[0]]
yName = perturbInfo['perturbParams'][pair[1]]
surface = {'x' : [], 'y' : [], 'value' : [], \
'title' : 'Perturbing ' + xName + ' vs ' + yName, \
'xName' : xName, 'yName' : yName}
# loop over population of perturbed parameter sets, adding appropriate
# parameter sets to trace
for m in range(perturbInfo['numParamSets']):
paramSet = perturbInfo['perturbedParamSets'][m]
if wantedSet(paramSet, constInds, perturbInfo):
surface['x'].append(paramSet[ind_x])
surface['y'].append(paramSet[ind_y])
surface['value'].append(perturbInfo['values'][m])
# reorder/reform the x/y/values arrays to conform to the format necessary
# for plotting (produced by numpy.meshgrid)
reorderSurface(surface)
perturbSurfaces.append(surface)
return perturbSurfaces
###############################################################################
def wantedSet(paramSet, constInds, perturbInfo):
"""return True if all the constInds are unchanged from their base values,
False otherwise"""
for n in range(len(constInds)):
ind = constInds[n]
if paramSet[ind] != perturbInfo['parameterVals'][ind]:
return False
return True
###############################################################################
def reorderSurface(surface):
"""reorder/reform the x/y/values arrays to conform to the format necessary
for plotting (produced by numpy.meshgrid)"""
oldX = surface['x'][:]
uniqueX = list(set(oldX))
uniqueX.sort()
numX = len(uniqueX)
oldY = surface['y'][:]
uniqueY = list(set(oldY))
uniqueY.sort()
numY = len(uniqueY)
oldZ = surface['value'][:]
shape = (numY, numX)
meshX = numpy.ndarray(shape)
meshY = numpy.ndarray(shape)
meshZ = numpy.ndarray(shape)
meshZ[:] = 1.0e10
for (x_n, y_n, z_n) in zip(oldX, oldY, oldZ):
xInd = uniqueX.index(x_n)
yInd = uniqueY.index(y_n)
if meshZ[yInd, xInd] != 1.0e10:
print('Duplicate of %g, %g' % (y_n, x_n))
print('indices: %d, %d' % (yInd, xInd))
meshX[yInd, xInd] = x_n
meshY[yInd, xInd] = y_n
meshZ[yInd, xInd] = z_n
for yInd in range(numY):
for xInd in range(numX):
if meshZ[yInd, xInd] == 1.0e10:
print('Missing %g, %g' % (uniqueY[yInd], uniqueX[xInd]))
print('Indices: %d, %d' % (yInd, xInd))
surface['x'] = meshX
surface['y'] = meshY
surface['value'] = meshZ
###############################################################################
def _parseArguments():
arguments = sys.argv
if len(arguments) != 2:
print(_usageStr)
if len(arguments) > 1:
raise TypeError('Incorrect number of arguments.')
sys.exit(0)
perturbFile = arguments[1]
return perturbFile
###############################################################################
if __name__ == "__main__":
# load in data (or make fake data if only testing
if _useFakeData:
surfaces = _makeFakeData()
else:
perturbFile = _parseArguments()
perturbInfo = loadPerturbInfo(perturbFile)
perturbTraces = getPerturbTraces(perturbInfo)
perturbSurfaces = getPerturbSurfaces(perturbInfo)
# plot each perturb trace
#for trace in perturbTraces:
# plotTrace(trace)
# plot each perturb surface
for surface in perturbSurfaces:
plotSurface(surface)
# show the figures
showFigures()
sys.exit(0)