def pre_run(tests): dim = len(hof[0].dtc.attrs.keys()) flat_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())] cnt = 0 for i, ki, j, kj in flat_iter: free_param = set([ ki, kj ]) # 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. # BD 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] if i == j: assert len(free_param) == len(hc) - 1 assert len(hc) == len(free_param) + 1 from neuronunit.models.NeuroML2 import model_parameters as modelp mp = copy.copy(modelp.model_params) gr = run_grid(3, tests, provided_keys=free_param, hold_constant=hc, mp_in=mp) # make a psuedo test, that still depends on input Parametersself. # each test evaluates a normal PDP. line = [np.sum(list(g.dtc.scores.values())) for g in gr] nr = {str(list(free_param)[0]): None} newrange, range_adj = check_line(line, gr, nr) while range_adj == True: mp = mp_process(newrange) gr = run_grid(3, tests, provided_keys=newrange, hold_constant=hc, mp_in=mp) # make a psuedo test, that still depends on input Parametersself. # each test evaluates a normal PDP. nr = {} line = [np.sum(list(g.dtc.scores.values())) for g in gr] newrange, range_adj = check_line(line, gr, newrange) mp = mp_process(newrange) with open('parameter_bf_ranges.p', 'wb') as f: pickle.dump(mp, f) import pdb pdb.set_trace() package = run_ga( mp, nparams * 2, 12, tests_, provided_keys=opt_keys) #, use_cache = True, cache_name='simple') return package
def plot_surface(gr, ax, keys, imshow=False): # from https://github.com/russelljjarvis/neuronunit/blob/dev/neuronunit/unit_test/progress_report_4thJuly.ipynb # Not rendered https://github.com/russelljjarvis/neuronunit/blob/dev/neuronunit/unit_test/progress_report_.ipynb gr = [g for g in gr if type(g.dtc) is not type(None)] gr = [g for g in gr if type(g.dtc.scores) is not type(None)] ax.cla() gr_ = [] index = 0 for i, g in enumerate(gr): if type(g.dtc) is not type(None): gr_.append(g) else: index = i xx = np.array([p.dtc.attrs[str(keys[0])] for p in gr]) yy = np.array([p.dtc.attrs[str(keys[1])] for p in gr]) zz = np.array([np.sum(list(p.dtc.scores.values())) for p in gr]) dim = len(xx) N = int(np.sqrt(len(xx))) X = xx.reshape((N, N)) Y = yy.reshape((N, N)) Z = zz.reshape((N, N)) if imshow == False: ax.pcolormesh(X, Y, Z, edgecolors='black') else: import seaborn as sns sns.set() ax = sns.heatmap(Z) ax.set_title(' {0} vs {1} '.format(keys[0], keys[1])) return ax
def grids(hof,tests): dim = len(hof[0].dtc.attrs.keys()) flat_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)) #fig1,ax1 = plt.subplots(dim,dim,figsize=(10,10)) cnt = 0 for i,ki,j,kj in flat_iter: free_param = set([ki,kj]) # 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. # BD 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] if i == j: assert len(free_param) == len(hc) - 1 assert len(hc) == len(free_param) + 1 gr = run_grid(10,tests,provided_keys = free_param ,hold_constant = hc) line = [ np.sum(list(g.dtc.scores.values())) for g in gr] # make a psuedo test, that still depends on input Parametersself. # each test evaluates a normal PDP. matrix[i][j] = ( free_param,gr ) 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 # what I want to do, I want to plot grid lines not a heat map. # I want to plot bd.attrs is cross hairs, # I want the range of the plot shown to be bigger than than the grid lines. gr = run_grid(10,tests,provided_keys = free_param ,hold_constant = hc) fp = list(copy.copy(free_param)) #if len(gr) == 100: ax[i,j] = plot_surface(gr,ax[i,j],fp,imshow=False) matrix[i][j] = ( free_param,gr ) if i < j: matrix[i][j] = ( free_param,gr ) fp = list(copy.copy(free_param)) if len(fp) == 2: ax[i,j] = plot_scatter(hof,ax[i,j],fp) plt.savefig(str('first_surfaces.png')) return matrix
def plot_scatter(hof, ax, keys): z = np.array([np.sum(list(p.dtc.scores.values())) for p in hof]) x = np.array([p.dtc.attrs[str(keys[0])] for p in hof]) if len(keys) != 1: y = np.array([p.dtc.attrs[str(keys[1])] for p in hof]) ax.cla() ax.set_title(' {0} vs {1} '.format(keys[0], keys[1])) ax.scatter(x, y, c=y, s=125) #, cmap='gray') return ax
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 plot_surface(gr, ax, keys, imshow=False): # from # https://github.com/russelljjarvis/neuronunit/blob/dev/neuronunit/unit_test/progress_report_4thJuly.ipynb # Not rendered # https://github.com/russelljjarvis/neuronunit/blob/dev/neuronunit/unit_test/progress_report_.ipynb gr = [g for g in gr if type(g.dtc) is not type(None)] gr = [g for g in gr if type(g.dtc.scores) is not type(None)] ax.cla() #gr = [ g gr_ = [] index = 0 for i, g in enumerate(gr): if type(g.dtc) is not type(None): gr_.append(g) else: index = i z = [np.sum(list(p.dtc.scores.values())) for p in gr] x = [p.dtc.attrs[str(keys[0])] for p in gr] y = [p.dtc.attrs[str(keys[1])] for p in gr] # impute missings if len(x) != 100: delta = 100 - len(x) for i in range(0, delta): x.append(np.mean(x)) y.append(np.mean(y)) z.append(np.mean(z)) xx = np.array(x) yy = np.array(y) zz = np.array(z) dim = len(xx) N = int(np.sqrt(len(xx))) X = xx.reshape((N, N)) Y = yy.reshape((N, N)) Z = zz.reshape((N, N)) if imshow == False: ax.pcolormesh(X, Y, Z, edgecolors='black') else: import seaborn as sns sns.set() ax = sns.heatmap(Z) #ax.imshow(Z) #ax.pcolormesh(xi, yi, zi, edgecolors='black') ax.set_title(' {0} vs {1} '.format(keys[0], keys[1])) return ax
def check_range(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] print(line) if i==j: line = [ np.sum(list(g.dtc.scores.values())) for g in gr] (newrange, range_adj) = check_line(line,newrange) print(newrange,'newrange') return (newrange, range_adj)
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
def get_justas_plot(history): # try: import plotly.plotly as py from plotly.offline import download_plotlyjs, init_notebook_mode, plot #, iplot import plotly.graph_objs as go import cufflinks as cf cf.go_offline() gr = [v for v in history.genealogy_history.values()] gr = [g for g in gr if type(g.dtc) is not type(None)] gr = [g for g in gr if type(g.dtc.scores) is not type(None)] keys = list(gr[0].dtc.attrs.keys()) xx = np.array([p.dtc.attrs[str(keys[0])] for p in gr]) yy = np.array([p.dtc.attrs[str(keys[1])] for p in gr]) zz = np.array([p.dtc.attrs[str(keys[2])] for p in gr]) ee = np.array([np.sum(list(p.dtc.scores.values())) for p in gr]) #pdb.set_trace() # z_data = np.array((xx,yy,zz,ee)) list_of_dicts = [] for x, y, z, e in zip(list(xx), list(yy), list(zz), list(ee)): list_of_dicts.append({ keys[0]: x, keys[1]: y, keys[2]: z, str('error'): e }) z_data = pd.DataFrame(list_of_dicts) data = [go.Surface(z=z_data.as_matrix())] layout = go.Layout( width=1000, height=1000, autosize=False, title='Sciunit Errors', scene=dict( xaxis=dict( title=str(keys[0]), #gridcolor='rgb(255, 255, 255)', #zerolinecolor='rgb(255, 255, 255)', #showbackground=True, #backgroundcolor='rgb(230, 230,230)' ), yaxis=dict( title=str(keys[1]), #gridcolor='rgb(255, 255, 255)', #zerolinecolor='rgb(255, 255, 255)', #showbackground=True, #backgroundcolor='rgb(230, 230,230)' ), zaxis=dict( title=str(keys[2]), #gridcolor='rgb(255, 255, 255)', #zerolinecolor='rgb(255, 255, 255)', #showbackground=True, #backgroundcolor='rgb(230, 230,230)' ), aspectratio=dict(x=1, y=1, z=0.7), aspectmode='manual'), margin=dict(l=65, r=50, b=65, t=90)) fig = go.Figure( data=data, layout=layout ) #,xTitle=str(keys[0]),yTitle=str(keys[1]),title='SciUnitOptimization') plot(fig, filename='sciunit-score-3d-surface.html')