/
exhaustive_search.py
351 lines (290 loc) · 11 KB
/
exhaustive_search.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
#from neuronunit.optimization import get_neab
#tests = get_neab.tests
import pdb
import multiprocessing
from collections import OrderedDict
from neuronunit.optimization.model_parameters import model_params
from neuronunit.optimization import data_transport_container
from neuronunit.optimization.optimization_management import nunit_evaluation, update_deap_pop
from neuronunit.optimization.optimization_management import update_dtc_pop
import numpy as np
from collections import OrderedDict
import copy
from copy import deepcopy
import math
import dask.bag as db
from sklearn.model_selection import ParameterGrid
import scipy
import pickle
import os
import numpy as np
npartitions = multiprocessing.cpu_count()
npart = multiprocessing.cpu_count()
import shelve
import os
from numba import jit
class WSListIndividual(list):
"""Individual consisting of list with weighted sum field"""
def __init__(self, *args, **kwargs):
"""Constructor"""
self.rheobase = None
self.dtc = None
super(WSListIndividual, self).__init__(*args, **kwargs)
@jit
def reduce_params(model_params,nparams):
key_list = list(model_params.keys())
reduced_key_list = key_list[0:nparams]
subset = { k:model_params[k] for k in reduced_key_list }
return subset
#@jit
def chunks(l, n):
# For item i in a range that is a length of l,
return [ l[:][i:i+n] for i in range(0, len(l), n) ]
#@jit
def build_chunk_grid(npoints, free_params, hold_constant = None, mp_in = None):
#grid_points, maps = create_grid(mp_in, npoints = npoints, free_params = free_params)
temp = OrderedDict(grid_points[0]).keys()
tds = list(temp)
# old stable approach
if len(grid_points[0].keys())>1:
pops = [ WSListIndividual(g.values()) for g in grid_points ]
else:
pops = [ val for g in grid_points for val in g.values() ]
return pops,tds
# new approach, merited? Older works better
'''
pops = []
for g in grid_points:
pre_pop = list(g.values())
pop = WSListIndividual(pre_pop)
pops.append(pop)
# divide population into chunks that reflect the number of CPUs.
# don't want lists of lengths 1 that are awkward to iterate over.
# so check if there would be a chunk of list length 1, and if so divide by a different numberself.
# that is still dependant on CPU number.
if len(pops) % npartitions != 1:
pops_ = chunks(pops,npartitions)
else:
pops_ = chunks(pops,npartitions-2)
if len(pops_) > 1:
assert pops_[0] != pops_[1]
return pops_, tds
'''
#@jit
def sample_points(iter_dict, npoints=3):
replacement = {}
for p in range(0,len(iter_dict)):
for k,v in iter_dict.items():#popitem(last=False)
if len(v) == 2:
sample_points = list(np.linspace(v[0],v[1],npoints))
else:
v = np.array(v)
sample_points = list(np.linspace(v.max(),v.min(),npoints))
replacement[k] = sample_points
return replacement
'''
@jit
def sample_points(iter_dict, npoints=2):
replacement = {}
for p in range(0,len(iter_dict)):
k,v = iter_dict.popitem(last=False)
v[0] = v[0]*1.0/3.0
v[1] = v[1]*2.0/3.0
#if len(v) == 2:
# sample_points = list(np.linspace(v[0],v[1],npoints))
#else:
# v = np.array(v)
sample_points = list(np.linspace(v.max(),v.min(),npoints))
replacement[k] = sample_points
return replacement
'''
@jit
def update_dtc_grid(item_of_iter_list):
dtc = data_transport_container.DataTC()
dtc.attrs = deepcopy(item_of_iter_list)
dtc.scores = {}
dtc.rheobase = None
dtc.evaluated = False
dtc.backend = 'NEURON'
return dtc
@jit
def create_a_map(subset):
maps = {}
for k,v in subset.items():
maps[k] = {}
for ind,j in enumerate(subset[k]):
maps[k][j] = ind
return maps
#@jit
#def create_grid(mp_in=None,npoints=3,free_params=None,ga=None):
'''
Description, create a grid of evenly spaced samples in model parameters to search over.
Inputs: npoints, type: Integer: number of sample points per parameter
nparams, type: Integer: number of parameters to use, conflicts, with next argument.
nparams, iterates through a list of parameters, and assigns the nparams to use via stupid counting.
provided keys: explicitly define state the model parameters that are used to create the grid of samples, by
keying into an existing of parameters.
This method needs the user of the method to declare a dictionary of model parameters in a path:
neuronunit.optimization.model_parameters.
Miscallenous, once grid created by this function
has been evaluated using neuronunit it can be used for informing a more refined second pass fine grained grid
# smaller is a dictionary thats not necessarily as big
# as the grid defined in the model_params file. Its not necessarily
# a smaller dictionary, if it is smaller it is reduced by reducing sampling
# points.
if type(mp_in) is type(None):
from neuronunit.models.NeuroML2 import model_parameters as modelp
mp_in = OrderedDict(modelp.model_params)
pdb.set_trace()
whole_p_set = {}
sp = sample_points(copy.copy(mp_in), npoints=2)
whole_p_set = OrderedDict(sp)
print(type(free_params), 'free_params')
if type(free_params) is type(dict):
subset = OrderedDict( {k:whole_p_set[k] for k in list(free_params.keys())})
elif len(free_params) == 1 or type(free_params) is type(str('')):
subset = OrderedDict( {free_params: whole_p_set[free_params] } )
else:
subset = OrderedDict( {k:whole_p_set[k] for k in free_params})
print('subset is wrong')
pdb.set_trace()
maps = create_a_map(subset)
if type(ga) is not type(None):
if npoints > 1:
for k,v in subset.items():
v[0] = v[0]*1.0/3.0
v[1] = v[1]*2.0/3.0
'''
#@jit
def add_constant(hold_constant,pop):
hold_constant = OrderedDict(hold_constant)
for p in pop:
for k,v in hold_constant.items():
p[k] = v
for k in hold_constant.keys():
td.append(k)
return pop,td
def create_grid(mp_in = None, npoints = 3, free_params = None, ga = None):
'''
check for overlap in parameter space.
'''
if len(free_params)> 1:
subset = OrderedDict(free_params)
else:
subset = {free_params[0]:None}
ndim = len(subset)
#nsteps = np.floor(float(npoints)/float(ndim))
if type(mp_in) is not type(None):
for k,v in mp_in.items():
if k in free_params:
subset[k] = np.linspace(np.min(free_params[k]),np.max(free_params[k]), npoints)
#import pdb; pdb.set_trace()
#subset[k] = ( np.min(free_params[k]),np.max(free_params[k]) )
else:
subset[k] = v
# The function of maps is to map floating point sample spaces onto a monochromataic matrix indicies.
grid = list(ParameterGrid(subset))
return grid
@jit
def tfg2i(x, y, z):
'''
translate_float_grid_to_index
Takes x, y, z values as lists and returns a 2D numpy array
'''
dx = abs(np.sort(list(set(x)))[1] - np.sort(list(set(x)))[0])
dy = abs(np.sort(list(set(y)))[1] - np.sort(list(set(y)))[0])
i = ((x - min(x)) / dx).astype(int) # Longitudes
j = ((y - max(y)) / dy).astype(int) # Latitudes
grid = np.nan * np.empty((len(set(j)),len(set(i))))
grid[j, i] = z # if using latitude and longitude (for WGS/West)
return grid
def tfc2i(x, y, z,err):
'''
translate_float_cube_to_index
Takes x, y, z values as lists and returns a 2D numpy array
'''
dx = abs(np.sort(list(set(x)))[1] - np.sort(list(set(x)))[0])
dy = abs(np.sort(list(set(y)))[1] - np.sort(list(set(y)))[0])
dz = abs(np.sort(list(set(z)))[1] - np.sort(list(set(y)))[0])
i = ((x - min(x)) / dx).astype(int) # Longitudes
j = ((y - max(y)) / dy).astype(int) # Latitudes
k = ((z - max(z)) / dz).astype(int) # Latitudes
grid = np.nan * np.empty((len(set(i)),len(set(j)), len(set(k)) ))
grid[i,j,k] = err # if using latitude and longitude (for WGS/West)
return
@jit
def transdict(dictionaries):
from collections import OrderedDict
mps = OrderedDict()
sk = sorted(list(dictionaries.keys()))
for k in sk:
mps[k] = dictionaries[k]
tl = [ k for k in mps.keys() ]
return mps, tl
def run_rick_grid(rick_grid, tests,td):
consumable = iter(rick_grid)
grid_results = []
results = update_deap_pop(consumable, tests, td)
#import pdb
#pdb.set_trace()
if type(results) is not None:
grid_results.extend(results)
return grid_results
def run_simple_grid(npoints, tests, ranges, free_params, hold_constant = None):
subset = OrderedDict()
for k,v in ranges.items():
if k in free_params:
subset[k] = ( np.min(ranges[k]),np.max(ranges[k]) )
# The function of maps is to map floating point sample spaces onto a monochromataic matrix indicies.
subset = OrderedDict(subset)
subset = sample_points(subset, npoints = npoints)
grid_points = list(ParameterGrid(subset))
td = grid_points[0].keys()
if type(hold_constant) is not type(None):
grid_points = add_constant(hold_constant,grid_points)
if len(td) > 1:
consumable = [ WSListIndividual(g.values()) for g in grid_points ]
else:
consumable = [ val for g in grid_points for val in g.values() ]
grid_results = []
td = list(td)
if len(consumable) <= 16:
consumable = consumable
results = update_deap_pop(consumable, tests, td)
if type(results) is not None:
grid_results.extend(results)
if len(consumable) > 16:
consumable = chunks(consumable,8)
for sub_pop in consumable:
sub_pop = sub_pop
#sub_pop = [[i] for i in sub_pop ]
#sub_pop = WSListIndividual(sub_pop)
results = update_deap_pop(sub_pop, tests, td)
if type(results) is not None:
grid_results.extend(results)
return grid_results
def run_grid(npoints, tests, provided_keys = None, hold_constant = None, ranges=None):
subset = mp_in[provided_keys]
consumable_ ,td = build_chunk_grid(npoints,provided_keys)
cnt = 0
grid_results = []
if type(hold_constant) is not type(None):
td, hc = add_constant(hold_constant,consumable_,td)
consumable = iter(consumable_)
use_cache = None
s = None
for sub_pop in consumable:
results = update_deap_pop(sub_pop, tests, td)
if type(results) is not None:
grid_results.extend(results)
if type(use_cache) is not type(None):
if type(s) is not type(None):
s['consumable'] = consumable
s['cnt'] = cnt
s['grid_results'] = grid_results
s['sub_pop'] = sub_pop
cnt += 1
print('done_block_of_N_cells: ',cnt)
if type(s) is not type(None):
s.close()
return grid_results