예제 #1
0
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
예제 #2
0
 def custom_code(invalid_ind):
     if self.backend is None:
         invalid_pop = list(update_deap_pop(invalid_ind, self.error_criterion, td = self.td))
     else:
         invalid_pop = list(update_deap_pop(invalid_ind, self.error_criterion, td = self.td, backend = self.backend))
     assert len(invalid_pop) != 0
     invalid_dtc = [ i.dtc for i in invalid_pop if hasattr(i,'dtc') ]
     fitnesses = list(map(evaluate, invalid_dtc))
     return fitnesses
예제 #3
0
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
예제 #4
0
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
예제 #5
0
        def custom_code(invalid_ind, as_log=None):
            if type(as_log) is not type(None):
                for p in invalid_ind:
                    for gene in p:
                        gene = np.log(gene)

            if self.backend is None:
                invalid_pop = update_deap_pop(invalid_ind,
                                              self.error_criterion,
                                              td=self.td)
            else:

                invalid_pop = update_deap_pop(invalid_ind,
                                              self.error_criterion,
                                              td=self.td,
                                              backend=self.backend)
            assert len(invalid_pop) != 0
            invalid_dtc = [i.dtc for i in invalid_pop if hasattr(i, 'dtc')]
            fitnesses = list(map(evaluate, invalid_dtc))
            return (invalid_pop, fitnesses)
예제 #6
0
 def custom_code(invalid_ind):
     #from dask.distributed import Client
     #c = Client()
     #c.restart()
     #assert len(invalid_ind)!= 0
     from neuronunit.optimization.optimization_management import evaluate, map_wrapper, update_deap_pop
     return_package = list(
         update_deap_pop(invalid_ind, self.error_criterion,
                         td=self.td))  #, CPU_NUM=self.CPU_NUM))
     invalid_dtc = [i[0] for i in return_package]
     invalid_ind = [i[1] for i in return_package]
     fitnesses = list(map_wrapper(evaluate, invalid_dtc))  #.compute())
     return fitnesses
예제 #7
0
def run_grid(nparams, npoints, tests, provided_keys=None, hold_constant=None):
    consumable_, td = build_chunk_grid(npoints, nparams, provided_keys)
    if type(hold_constant) is not type(None):
        hc = list(hold_constant.values())[0]
        for c in consumable_:
            for i in c:
                i.append(hc)
        td.append(list(hold_constant.keys())[0])
    cnt = 0
    grid_results = []
    consumable = iter(consumable_)
    for sub_pop in consumable:
        grid_results.extend(update_deap_pop(sub_pop, tests, td))
        #assert len(grid_results[0].dtc.attrs.keys()) == 3
        assert len(grid_results[0].dtc.scores.keys()) >= 2

        with open('grid_cell_results' + str(nparams) + str('.p'), 'wb') as f:
            pickle.dump(grid_results, f)
        with open('iterator_state' + str(nparams) + str('.p'), 'wb') as f:
            pickle.dump([sub_pop, tests, cnt], f)
        cnt += 1
        print('done_block_of_N_cells: ', cnt)
    return grid_results
예제 #8
0
def pre_run(tests, opt_keys):
    # algorithmically find the the edges of parameter ranges, via a course grained
    # sampling of extreme parameter values
    # to find solvable instances of Izhi-model, (models with a rheobase value).
    nparams = len(opt_keys)
    from neuronunit.models.NeuroML2 import model_parameters as modelp
    mp = copy.copy(modelp.model_params)
    mp['b'] = [-0.5, 500.0]
    mp['vr'] = [-100.0, 10.0]
    mp['a'] = [-10, 5]
    cnt = 0
    fc = {}  # final container

    for key in opt_keys:
        cnt = 0
        print(key, mp)
        gr = run_grid(3, tests, provided_keys=key, mp_in=mp)
        line = [g.dtc.get_ss() for g in gr]
        nr = {key: None}
        _, range_adj, new, index = check_line(line, gr, nr, key)
        while range_adj == True:
            # while the sampled line is not concave (when minimas are at the edges)
            # sample a point to a greater extreme
            gr_ = update_deap_pop(new, tests, key)
            param_line = [g.dtc.attrs[key] for g in gr]

            temp = list(mp[key])
            inter = None

            if index == 0:
                p0 = (gr_.dtc.attrs[key], gr_.dtc.get_ss())
                p1 = (gr[0].dtc.attrs[key], gr[0].dtc.get_ss())
                inter = interpolate(p0, p1)
                print(inter)

                gr.insert(index, gr_)
                temp.insert(index, new)
            elif index == -1:
                p0 = (gr_.dtc.attrs[key], gr_.dtc.get_ss())

                p1 = (gr[-1].dtc.attrs[key], gr[-1].dtc.get_ss())
                inter = interpolate(p0, p1)
                print(inter)

                gr.append(gr_)
                temp.append(gr_)

            mp[key] = np.array(temp)
            line = [g.dtc.get_ss() for g in gr]

            _, range_adj, new, index = check_line(line, gr, mp, key)
            cnt += 1
            with open('temp_range.p', 'wb') as f:
                pickle.dump(mp, f)
            if type(inter) is not type(None):
                with open('temp_inter.p', 'wb') as f:
                    pickle.dump([mp, inter], f)

        param_line = [g.dtc.attrs[key] for g in gr]
        line = [g.dtc.get_ss() for g in gr]
        plt.clf()
        plt.plot(param_line, line)
        plt.savefig('check_' + str(key) + '.png')

        fc[key] = {}
        fc[key]['line'] = line
        fc[key]['range'] = mp
        fc[key]['cnt'] = cnt
    return fc, mp