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
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
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
('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([