-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelparametrization.py
194 lines (157 loc) · 7.85 KB
/
modelparametrization.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
import matplotlib.gridspec as gridspec
from topo.command import wipe_out_activity, clear_event_queue
import topo
import pylab
import numpy
class ModelRecording(object):
"""
sheets - list of sheets of the model that need to be reset
retina - retina name of the model
reset_homeo - HACKY
if reset_homeo != None it will reset homeostatic
plasticity. reset_homeo tells the order of the homeo transfer function
in the list of .output_fns
sheets_to_record - sheets that we want to record
sslice - the slice of activity and projection activity that are to be recorded (None is all)
"""
def __init__(self, sheets, retina, sheets_to_record=None, sslice=None, reset_homeo=None):
self.sheets = sheets
self.retina = retina
self.reset_homeo = reset_homeo
self.sslice = sslice
self.sheets_to_record = sheets_to_record
def pre_test(self):
self.plastic = {}
for s in self.sheets:
self.plastic[s] = topo.sim[s].plastic
topo.sim[s].plastic = False
wipe_out_activity()
clear_event_queue()
topo.sim.state_push()
# HACKY
if self.reset_homeo != None:
for s in self.sheets:
topo.sim[s].output_fns[self.reset_homeo].old_a*=0
def post_test(self):
topo.sim.state_pop()
for s in self.sheets:
topo.sim[s].plastic = self.plastic[s]
def present_stimulus_sequence(self,interval,stimuli):
import numpy
self.records = {}
ip = topo.sim[self.retina].input_generator
self.pre_test()
for s in stimuli:
print numpy.mean(topo.sim["V1Exc"].output_fns[0].t)
print numpy.mean(topo.sim["V1Inh"].output_fns[0].t)
if s!= None:
topo.sim[self.retina].set_input_generator(s)
topo.sim.run(interval)
self.record()
else:
topo.sim.run(interval)
self.record()
self.post_test()
topo.sim[self.retina].set_input_generator(ip)
def record(self):
if self.sheets_to_record == None:
sh = self.sheets
else:
sh = self.sheets_to_record
for s in sh + [self.retina]:
if not self.records.has_key(s):
self.records[s] = {}
if not self.records[s].has_key('activity'):
self.records[s]['activity'] = []
if self.sslice == None:
self.records[s]["activity"].append(topo.sim[s].activity.copy())
else:
self.records[s]["activity"].append(topo.sim[s].activity[self.sslice[0],self.sslice[1]].copy())
for s in sh:
if not self.records[s].has_key('projections'):
self.records[s]['projections'] = {}
for p in topo.sim[s].projections().keys():
if not self.records[s]['projections'].has_key(p):
self.records[s]['projections'][p] = []
if self.sslice == None:
self.records[s]['projections'][p].append(topo.sim[s].projections()[p].activity.copy())
else:
self.records[s]['projections'][p].append(topo.sim[s].projections()[p].activity[self.sslice[0],self.sslice[1]].copy())
def plot_activity(self,gs,index):
"""
Plots the activities in all layers to the stimulus number index in the list of stimuli that were presented
"""
gs = gridspec.GridSpecFromSubplotSpec(1, len(self.records.keys()), subplot_spec=gs)
for i,s in enumerate(self.records.keys()):
ax = pylab.subplot(gs[0,i])
ax.imshow(self.records[s]['activity'][index],cmap='gray')
pylab.title(str(numpy.max(self.records[s]['activity'][index])) + " : " + str(numpy.min(self.records[s]['activity'][index])))
class ModelParametrization(object):
"""
This class can run iteratively all combinations of parameter_values corresponding
to parameters and for each such combination it executes the model_measurment_function
parameters - the list of parameters that should be scanned
parameter_values - list of lists. The outer list len is the same as len of parameters
each sublist contains values that should be explored for the given parameter
model_measurement_function - the function that when called will perform the measurement of the model
plotting_function - function that plots the results after each measurement.
plotting_params - the parameters to be passed to plotting function
directory - where to save results
"""
def __init__(self,parameters,parameter_values,model_measurement_function,plotting_function,plotting_params,directory):
self.parameters = parameters
self.parameter_values = parameter_values
self.plotting_function = plotting_function
self.plotting_params = plotting_params
self.model_measurement_function = model_measurement_function
self.directory = directory
self.init_vals = [eval(p) for p in self.parameters]
def go(self,initial_run=False):
if initial_run:
topo.sim.run(1.0)
self.run_combinations(self.parameter_values)
self.set_params(self.init_vals)
def set_params(self,values):
for p,v in zip(self.parameters,values):
print p
print v
exec(p + ' = v')
@staticmethod
def set_parameters(parameters,values):
"""
Helper function, that makes it easy to set simulation with a particular combination of parameter values
"""
for p,v in zip(parameters,values):
exec(p + ' = v')
def execute_model_measurement(self,params):
self.model_measurement_function()
self.fig = pylab.figure(figsize = (14,12),facecolor='w')
gs = gridspec.GridSpec(1, 1)
gs.update(left=0.05, right=0.95, top=0.95, bottom=0.05)
self.plotting_function(gs[0,0],**self.plotting_params)
st = ""
for p in params:
st = st + ' ' + str(p)
pylab.savefig(self.directory + '/' + st + '.png');
def _run_combinations_rec(self, param, params, index):
if(len(params) == index):
self.set_params(param)
self.execute_model_measurement(param)
return
a = params[index]
for p in a:
new_param = param + [p]
self._run_combinations_rec(new_param, params, index + 1)
def run_combinations(self, params):
"""
this function runs function func with all combinations of params defined in the array params, eg.
params = [[1,2,3],[1,2,3]...]
"""
run_combinations_counter = 0
self._run_combinations_rec([], params, 0)
# various plotting functions
def explore_initial_activity(parameters,parameter_values,sheets,sheets_to_record,retina,reset_homeo,interval,directory):
mr = ModelRecording(sheets, retina, sheets_to_record = sheets_to_record, reset_homeo = reset_homeo)
f = lambda : mr.present_stimulus_sequence(interval,[None])
mp = ModelParametrization(parameters,parameter_values,f,mr.plot_activity,{'index' : 0},directory)
mp.go(initial_run=True)