Esempio n. 1
0
def plot_line(gr, ax, key):
    ax.cla()
    ax.set_title(' {0} vs  score'.format(key[0]))
    z = np.array([np.sum(list(p.dtc.scores.values())) for p in gr])
    x = np.array([p.dtc.attrs[key[0]] for p in gr])

    ax.plot(x, z)
    ax.set_xlim(np.min(x), np.max(x))
    ax.set_ylim(np.min(z), np.max(z))
    return ax
Esempio n. 2
0
def check_line(line, gr, newrange, key):
    # Is this a concave down shape (optima in the middle)
    # Or is the most negative value on an edge?
    # if it's on the edge calculate a new parameter value to explore
    range_adj = False
    min_ = np.min(line)
    cl = [g.dtc.attrs[key] for g in gr]
    new = None
    index = None
    new_param_val = None
    if line[0] == min_:
        attrs = gr[0].dtc.attrs[key]
        # quantity may not be negative yet
        # don't force it to be straight away
        # avoided forcing rapid sign reversal by keeping old value as an offset

        remin = attrs - 10 * np.abs(attrs)
        if remin == 0.0:
            remin = -1.0

        cl.insert(0, remin)
        cl = sorted(cl)
        newrange[key] = cl
        range_adj = True
        new_param_val = cl[0]
        index = 0
    if line[-1] == min_:
        attrs = gr[-1].dtc.attrs[key]
        # quantity might not be positve yet
        # don't force it to be straight away
        # avoided forcing rapid sign reversal by keeping old value as an offset

        remax = attrs + np.abs(attrs) * 10
        if remax == 0.0:
            remax = 1.0

        cl.append(remax)
        cl = sorted(cl)
        newrange[key] = cl
        range_adj = True
        new_param_val = cl[-1]
        index = -1

    return (newrange, range_adj, new_param_val, index)
Esempio n. 3
0
def check_line(line,newrange):
    range_adj = False
    keys = keys[0]
    min_ = np.min(line)
    print(min_,line[0],line[1],'diff?')
    if line[0] == min_:
        #print('hit')
        attrs = gr[0].dtc.attrs[keys]
        remin = - np.abs(attrs)*10
        remax = np.abs(gr[-1].dtc.attrs[keys])*10
        nr = np.linspace(remin,remax,10)
        newrange[keys] = nr
        range_adj = True

    if line[-1] == min_:
        #print('hit')
        attrs = gr[-1].dtc.attrs[keys]
        remin = - np.abs(attrs)*10
        remax = np.abs(gr[-1].dtc.attrs[keys])*10
        nr = np.linspace(remin,remax,10)
        newrange[keys] = nr
        range_adj = True
Esempio n. 4
0
def check_line(line, gr, newrange):
    range_adj = False
    key = list(newrange.keys())[0]
    #keys = keys[0]
    min_ = np.min(line)
    print(min_, line[0], line[1], 'diff?')
    if line[0] == min_:
        #print('hit')
        attrs = gr[0].dtc.attrs[key]
        remin = -np.abs(attrs) * 10
        remax = np.abs(gr[-1].dtc.attrs[key]) * 10
        nr = np.linspace(remin, remax, 3)
        newrange[key] = nr
        range_adj = True

    if line[-1] == min_:
        #print('hit')
        attrs = gr[-1].dtc.attrs[key]
        remin = -np.abs(attrs) * 10
        remax = np.abs(gr[-1].dtc.attrs[key]) * 10
        nr = np.linspace(remin, remax, 3)
        newrange[key] = nr
        range_adj = True
    return (newrange, range_adj)
Esempio n. 5
0
def evidence(matrix,hof):
    dim = np.shape(matrix)[0]
    print(dim)
    cnt = 0
    fig,ax = plt.subplots(dim,dim,figsize=(10,10))
    flat_iter = []
    newrange = {}
    for i,k in enumerate(matrix):
        for j,r in enumerate(k):

            keys = list(r[0])
            gr = r[1]
            line = [ np.sum(list(g.dtc.scores.values())) for g in gr]
            print(line)
            if i==j:
                keys = keys[0]
                min_ = np.min(line)
                print(min_,line[0],line[1],'diff?')
                if line[0] == min_:
                    print('hit')
                    attrs = gr[0].dtc.attrs[keys]
                    remin = - np.abs(attrs)*10
                    remax = np.abs(gr[-1].dtc.attrs[keys])*10
                    nr = np.linspace(remin,remax,10)
                    newrange[keys] = nr

                if line[-1] == min_:
                    print('hit')
                    attrs = gr[-1].dtc.attrs[keys]
                    remin = - np.abs(attrs)*10
                    remax = np.abs(gr[-1].dtc.attrs[keys])*10
                    nr = np.linspace(remin,remax,10)
                    newrange[keys] = nr

                print(newrange,'newrange')
    return newrange
Esempio n. 6
0
def grids(hof, tests, params):
    dim = len(hof[0].dtc.attrs.keys())
    flat_iter = iter([(i, ki, j, kj)
                      for i, ki in enumerate(hof[0].dtc.attrs.keys())
                      for j, kj in enumerate(hof[0].dtc.attrs.keys())])
    matrix = [[0 for x in range(dim)] for y in range(dim)]
    plt.clf()

    fig, ax = plt.subplots(dim, dim, figsize=(10, 10))
    cnt = 0
    mat = np.zeros((dim, dim))
    df = pd.DataFrame(mat)

    for i, freei, j, freej in flat_iter:
        free_param = set([
            freei, freej
        ])  # construct a small-set out of the indexed keys 2. If both keys are
        # are the same, this set will only contain one index
        bs = set(
            hof[0].dtc.attrs.keys()
        )  # construct a full set out of all of the keys available, including ones not indexed here.
        diff = bs.difference(
            free_param)  # diff is simply the key that is not indexed.
        # hc is the dictionary of parameters to be held constant
        # if the plot is 1D then two parameters should be held constant.
        hc = {}
        for d in diff:
            hc[d] = hof[0].dtc.attrs[d]

        cpparams = {}
        if i == j:
            assert len(free_param) == len(hc) - 1
            assert len(hc) == len(free_param) + 1
            # zoom in on optima
            cpparams['freei'] = (np.min(params[freei]), np.max(params[freei]))

            gr = run_grid(10,
                          tests,
                          provided_keys=freei,
                          hold_constant=hc,
                          mp_in=params)
            # make a psuedo test, that still depends on input Parametersself.
            # each test evaluates a normal PDP.
            fp = list(copy.copy(free_param))
            ax[i, j] = plot_line(gr, ax[i, j], fp)
        if i > j:
            assert len(free_param) == len(hc) + 1
            assert len(hc) == len(free_param) - 1
            cpparams['freei'] = (np.min(params[freei]), np.max(params[freei]))
            cpparams['freej'] = (np.min(params[freej]), np.max(params[freej]))

            gr = run_grid(10,
                          tests,
                          provided_keys=list((freei, freej)),
                          hold_constant=hc,
                          mp_in=params)
            fp = list(copy.copy(free_param))
            ax[i, j] = plot_surface(gr, ax[i, j], fp, imshow=False)

        if i < j:
            free_param = list(copy.copy(list(free_param)))
            if len(free_param) == 2:
                ax[i, j] = plot_scatter(hof, ax[i, j], free_param)
        # To Pandas:
        k = 0
        df.insert(i, j, k, free_param)
        k = 1
        df.insert(i, j, k, hc)
        k = 2
        df.insert(i, j, k, cpparams)
        k = 3
        df.insert(i, j, k, gr)
        '''
        where, i and j are indexs to the 3 by 3 (9 element) subplot matrix,
        and `k`-dim-0 is the parameter(s) that were free to vary (this can be two free in the case for i<j,
        or one free to vary for i==j).
        `k`-dim-1, is the parameter(s) that were held constant.
        `k`-dim-2 `cpparams` is a per parameter dictionary, whose values are tuples that mark the edges of (free)
        parameter ranges. `k`-dim-3 is the the grid that results from varying those parameters
        (the grid results can either be square (if len(free_param)==2), or a line (if len(free_param==1)).
        '''
    plt.savefig(str('cross_section_and_surfaces.png'))
    return matrix, df
Esempio n. 7
0
def grids(hof, tests, ranges, us, history):
    '''
    Obtain using the best candidate Gene (HOF, NU-tests, and expanded parameter ranges found via
    exploring extreme edge cases of parameters

    plot a error surfaces, and cross sections, about the optima in a 3by3 subplot matrix.

    where, i and j are indexs to the 3 by 3 (9 element) subplot matrix,
    and `k`-dim-0 is the parameter(s) that were free to vary (this can be two free in the case for i<j,
    or one free to vary for i==j).
    `k`-dim-1, is the parameter(s) that were held constant.
    `k`-dim-2 `cpparams` is a per parameter dictionary, whose values are tuples that mark the edges of (free)
    parameter ranges. `k`-dim-3 is the the grid that results from varying those parameters
    (the grid results can either be square (if len(free_param)==2), or a line (if len(free_param==1)).
    '''
    #scores = hof[0].dtc.scores = OrderedDict(hof[0].dtc.scores)
    hof[0].dtc.attrs = OrderedDict(hof[0].dtc.attrs)
    attrs = hof[0].dtc.attrs
    dim = len(hof[0].dtc.attrs.keys())
    best_param_keys = hof[0].dtc.attrs.keys()
    flat_iter = iter([(i, freei, j, freej)
                      for i, freei in enumerate(best_param_keys)
                      for j, freej in enumerate(best_param_keys)])

    plt.clf()
    fig0, ax0 = plt.subplots(dim, dim, figsize=(10, 10))
    #fig1,ax1 = plt.subplots(dim,dim,figsize=(10,10))

    cnt = 0
    temp = []
    loc_key = {}

    for k, v in attrs.items():
        loc_key[k] = attrs[k]
        ranges[k] = (loc_key[k] - 1 * np.abs(ranges[k]),
                     loc_key[k] + 1 * np.abs(ranges[k]))

    for i, freei, j, freej in flat_iter:
        if i == j:
            free_param = freei
        else:
            free_param = [freei, freej]
        free_param_set = set(
            free_param
        )  # construct a small-set out of the indexed keys 2. If both keys are
        # are the same, this set will only contain one index
        bs = set(
            best_param_keys
        )  # construct a full set out of all of the keys available, including ones not indexed here.
        diff = bs.difference(
            free_param_set)  # diff is simply the key that is not indexed.
        # hc is the dictionary of parameters to be held constant
        # if the plot is 1D then two parameters should be held constant.
        hc = {}
        for d in diff:
            hc[d] = hof[0].dtc.attrs[d]

        cpparams = {}

        cpparams['freei'] = (np.min(ranges[freei]), np.max(ranges[freei]))

        # make a psuedo test, that still depends on input Parametersself.
        # each test evaluates a normal PDP.
        fp = list(copy.copy(free_param))
        #plot_line_ss(gr,ax,key,hof,free,constant)
        if i == j:
            gr = run_simple_grid(10, tests, ranges, freei, hold_constant=hc)

            ax0[i, j] = plot_line_ss(gr, ax0[i, j], freei, hof, hc)

        if i > j:
            #assert len(free_param) == len(hc) + 1
            #assert len(hc) == len(free_param) - 1
            cpparams['freei'] = (np.min(ranges[freei]), np.max(ranges[freei]))
            cpparams['freej'] = (np.min(ranges[freej]), np.max(ranges[freej]))
            free_params = [freei, freej]
            gr = run_simple_grid(10,
                                 tests,
                                 ranges,
                                 free_params,
                                 hold_constant=hc)
            #gr = run_grid(10,tests,, hold_constant = hc, mp_in = params)
            fp = list(copy.copy(free_param))
            #ax0[i,j],img = plot_surface(gr,ax0[i,j],fp,hc,imshow=True)
            ax0[i, j], img = plot_surface(gr, ax0[i, j], fp, hc, imshow=True)

            #plt.colorbar(img, ax = ax0[i,j])
            #ax1[i,j] = plot_surface(gr,ax1[i,j],fp,imshow=False)

        if i < j:
            free_param = list(copy.copy(list(free_param)))
            #if len(free_param) == 2:

            ax0[i, j] = plot_scatter(history, ax0[i, j], free_param, hc)
            #ax0[i,j] = ps(fig0,ax1[i,j],freei,freej,history)

            cpparams['freei'] = (np.min(ranges[freei]), np.max(ranges[freei]))
            cpparams['freej'] = (np.min(ranges[freej]), np.max(ranges[freej]))
            gr = hof

        limits_used = (us[str(freei)], us[str(freej)])
        scores = [g.dtc.get_ss() for g in gr]
        params_ = [g.dtc.attrs for g in gr]

        # To Pandas:
        # https://stackoverflow.com/questions/28056171/how-to-build-and-fill-pandas-dataframe-from-for-loop#28058264
        temp.append({
            'i': i,
            'j': j,
            'free_param': free_param,
            'hold_constant': hc,
            'param_boundaries': cpparams,
            'scores': scores,
            'params': params_,
            'ga_used': limits_used,
            'grid': gr
        })
        #print(temp)
        #intermediate = pd.DataFrame(temp)blah
        with open('intermediate.p', 'wb') as f:
            pickle.dump(temp, f)

    #df = pd.DataFrame(temp)
    plt.savefig(str('cross_section_and_surfaces.png'))
    return temp
Esempio n. 8
0
    ('TC_burst', (200, 1.6, -60, -50, 35, 0.01, 15, -60, 10, 6)),
    ('RTN', (40, 0.25, -65, -45, 0, 0.015, 10, -55, 50, 7)),
    ('RTN_burst', (40, 0.25, -65, -45, 0, 0.015, 10, -55, 50, 7))
])

import numpy as np
reduced_dict = OrderedDict([
    (k, []) for k in ['C', 'k', 'vr', 'vt', 'vPeak', 'a', 'b', 'c', 'd']
])

#OrderedDict
for i, k in enumerate(reduced_dict.keys()):
    for v in reduced2007.values():
        reduced_dict[k].append(v[i])

explore_param = {k: (np.min(v), np.max(v)) for k, v in reduced_dict.items()}

opt_keys = [str('vr'), str('a'), str('b')]
nparams = len(opt_keys)

##
# find an optima copy and paste reverse search here.
##

#IB = mparams[param_dict['IB']]
RS = {}
IB = {}
TC = {}
CH = {}
RTN_burst = {}
cells = OrderedDict([