def gamma_delta_bound_dist(): params = {} gammas = [i for i in range(1, 16)] deltas = [i for i in range(1, 16)] params['IA'], params['IB'], params['reps'] = 10, 10, 1000 val_keys = [ 'dist from continuous bound', 'dist from discrete bound', 'steps', 'log time' ] dataset = Dataset(val_keys) for i in util.rng(gammas): params['gamma'] = gammas[i] for j in util.rng(deltas): params['delta'] = deltas[j] run_sweep.one_param_one_data_instance(MODEL, dataset, params, verbose=True) plot_sweep.bound_comparison(dataset, params, 'gamma', 'delta', write_params_on_img=False)
def extract_one(name, sshot): data = get_features(sshot) if name == 'size': feat = [data[i]['size'] for i in rng(data)] elif name == 'branching ratio': feat = [data[i]['branching ratio'] for i in rng(data)] else: assert (False) #feature name not recognized return feat
def drawG_by_dendos(G,localD, globalD, params): # pass a subgraph induced by union seed community local + global plt.figure(figsize=(10,6)) node_size, nalpha, ealpha = 200, .5, .2 pos = get_layout(params,G) nodes = list(G.nodes()) #for node in nodes: # assert(node in np.array(localD).flatten() or node in np.array(globalD).flatten()) labels = {n:'' for n in G.nodes()} colors, bs, rs = [],{n:0 for n in G.nodes()},{n:0 for n in G.nodes()} for i in rng(localD): for n in localD[i]: if n in nodes: bs[n] = (len(localD)-i)/len(localD) labels[n] += 'L' for i in rng(globalD): for n in globalD[i]: if n in nodes: rs[n] = (len(globalD)-i)/len(globalD) if labels[n] not in ['G','LG']: labels[n] += 'G' labels[params['seeds'][0]] = 'Seed' alphas, sizes = [],[] for i in rng(nodes): node = nodes[i] if node == params['seeds'][0]: colors += [(0,0,1)] alphas += [1] sizes += [1000] else: colors += [(rs[node],bs[node],0)] alphas += [max(math.pow(bs[node],.3),.05)] sizes += [max(rs[node],.05)*1000] nx.draw_networkx_nodes(G, pos, nodelist=nodes, node_color=colors, node_size=node_size, alpha=nalpha) #labels = {n:labels[n] for n in G.nodes()} labels = {n:n for n in G.nodes()} nx.draw_networkx_labels(G, pos, labels, font_size=8, font_color='black') plt.title("Comparison of Hierarchical Communities", fontsize=26) elist = sorted(list(G.edges())) nx.draw_networkx_edges(G, pos, arrows=True, edgelist=elist, alpha=ealpha) if params['save_fig']: tstamp = timestamp() plt.savefig(params['output_path']+'/'+str(tstamp)+'_dendos.png') else: plt.show() plt.clf() plt.close()
def add_val(self, params, SCNs, val_key): # downside of this implementation is that don't know which characteristics are computed first # and have to recalc common things such as #steps and time # if too slow add a pre-feature calculation step ################################## DISCRETE MODEL CHARACTERISTICS ################################## if val_key == 'steps': steps = [scn.steps for scn in SCNs] return all_stats(steps, params) elif val_key == 'probability off-diag': init = params['IA']/(params['IA']+params['IB']) As,Bs = [scn.mols['A'] for scn in SCNs], [scn.mols['B'] for scn in SCNs] pr = [As[i]/(As[i]+Bs[i]) > init for i in rng(As)] return all_stats(pr, params) elif val_key == 'Percent Y0': ratios = [] for scn in SCNs: if scn.mols['Y0']==0: ratios += [0] else: ratios += [scn.mols['Y0']/(scn.mols['Y0']+scn.mols['Y1'])] # faster if know that sum != 0: #ratios = [scn.mols['Y0']/(scn.mols['Y0']+scn.mols['Y1']) for scn in SCNs] return all_stats(ratios, params) elif val_key == 'dist from discrete bound': discrete_bound = bounds.discrete(params['gamma'], params['delta'],params['IA'],params['IB']) avg_steps = avg([scn.steps for scn in SCNs]) return math.log(discrete_bound - avg_steps,10), None, None, None, None, None ################################## CONTINUOUS MODEL CHARACTERISTICS ################################## elif val_key in ['time', 'Convergence Time']: stop_ts = [scn.time for scn in SCNs] return all_stats(stop_ts, params) elif val_key in ['time', 'Log Convergence Time']: stop_ts = [math.log(scn.time,10) for scn in SCNs] return all_stats(stop_ts, params) elif val_key == 'log time': stop_ts = [math.log(scn.time,10) for scn in SCNs] return all_stats(stop_ts, params) elif val_key == 'expected time': #appears to be the same as time, runs faster stop_ts = [scn.time_mean_appx for scn in SCNs] return all_stats(stop_ts, params) elif val_key == 'dist from continuous bound': cont_bound = bounds.continuous(params['gamma'], params['delta'],params['IA'],params['IB']) avg_t = avg([scn.time for scn in SCNs]) #note that curr using exact time, may go faster with expected time return math.log(cont_bound - avg_t,10), None, None, None, None, None else: raise KeyError("Unrecognized value %s, use recognized value keys when initializing Dataset (see Dataset.add_val for existing options)." %(val_key))
def Rs_over_time(G,params,Rs): t = [i for i in rng(Rs)] plt.plot(t,Rs) plt.title("Local Modularity Score",fontsize=26) plt.xlabel("Iteration", fontsize=18) plt.ylabel("Local Modularity (R)",fontsize=18) if params['save_fig']: tstamp = timestamp() plt.savefig(params['output_path']+'/'+str(tstamp)+'_local_dR.png') else: plt.show() plt.clf() plt.close()
def drawG_by_comm_global(G,node_C,params): # color by seed/not_seed, alpha by rank assert(os.path.isdir(params['output_path'])) plt.figure(figsize=(10,6)) plt.title('Global Community Detection',fontsize=26) node_size, nalpha, ealpha = 40, .4, .1 pos = get_layout(params,G) nodes = list(G.nodes()) #colors = [COLORS[node_C[nodes[n]]%len(COLORS)] for n in rng(nodes)] normzd_C = {k:node_C[k] for k in node_C.keys()} normzd_C = {} i=0 for k in node_C.keys(): if node_C[k] not in normzd_C.keys(): normzd_C[node_C[k]] = i i+=1 # super messy color picking indx = [normzd_C[node_C[nodes[n]]]/len(normzd_C) for n in rng(nodes)] i1 = [(indx[n] // 0.1 / 10) for n in rng(nodes)] sub=[i1[i] for i in rng(i1)] for i in rng(i1): if i1[i] >= 1: sub[i]-=1 i2 = [10*((indx[n] // 0.01 / 100)-sub[n]) for n in rng(nodes)] i3 = [10*(((indx[n] // 0.001 / 1000)-sub[n])*10-i2[n]) for n in rng(nodes)] colors = [(i1[n],i2[n],i3[n]) for n in rng(nodes)] nx.draw_networkx_nodes(G, pos, nodelist=nodes, node_color=colors, node_size=node_size, alpha=nalpha) #labels = {n: G.nodes[n]['gene'] for n in G.nodes()} labels = {n:n for n in G.nodes()} # huh? #nx.draw_networkx_labels(G, pos, labels, font_size=8, font_color='black') elist = sorted(list(G.edges())) nx.draw_networkx_edges(G, pos, arrows=True, edgelist=elist, alpha=ealpha) if params['save_fig']: tstamp = timestamp() plt.savefig(params['output_path']+'/'+str(tstamp)+'_global.png') else: plt.show() plt.clf() plt.close()
def merge_repeats(merged_data, repeats_data, feature_names, CI=False): for name in feature_names: for metric in repeats_data[name].keys(): #merged_data[name][metric]['avg'] = util.avg(repeats_data[name][metric]) a = np.array(repeats_data[name][metric]) if a.size != 0: mean = np.mean(a) std = np.std(a) else: mean, std = 0, 0 merged_data[name][metric]['avg'] += [mean] merged_data[name][metric]['std'] += [std] #print('avg, std', merged_data[name][metric]['avg'],merged_data[name][metric]['std'],a,'\n') if CI: conf_interval1 = st.t.interval( 0.68, len(a) - 1, loc=mean, scale=st.sem(a)) #1 standard devs conf_interval2 = st.t.interval( 0.95, len(a) - 1, loc=mean, scale=st.sem(a)) #2 standard devs conf_interval3 = st.t.interval( 0.997, len(a) - 1, loc=mean, scale=st.sem(a)) #3 standard devs intervals, stats = [ conf_interval1, conf_interval2, conf_interval3 ], [['top1', 'btm1'], ['top2', 'btm2'], ['top3', 'btm3']] for i in util.rng(intervals): interval, stat = intervals[i], stats[i] a_trimd = [] for ele in a: if ele > interval[0] and ele < interval[1]: a_trimd += [ele] if np.count_nonzero(a_trimd) == 0: conf_min, conf_max = 0, 0 else: conf_min, conf_max = min(a_trimd), max(a_trimd) merged_data[name][metric][stat[0]] += [conf_max] merged_data[name][metric][stat[1]] += [conf_min]
def hist(): print("\nComparing two simulations using histogram.\n") # compares 2 runs, no averaging NADs = [1.0E+4, 1.0E+5] labels = ['[NAD] = ' + str(NADs[0]), '[NAD] = ' + str(NADs[1])] all_params = [] feature_names = ['size', 'branching ratio'] shots = [] data = {n: [] for n in feature_names} for i in rng(NADs): params['NAD'] = NADs[i] sshot = run_sim(params) for feat in feature_names: data[feat] += [features.extract_one(feat, sshot)] all_params += [params] util.pickle_it(all_params, data) plot.hist_first_and_last(data, params, feature_names, labels) # 1 img per feature
def prob_ratio(): params = {} params['gamma'], params['delta'], params['reps'] = 1, 0, 1000 max_iter = 10000 period = 100 IA_base = 3 IB_base = 1 scale = [i for i in range(10, 21)] val_keys = ['steps', 'probability off-diag'] dataset = Dataset(val_keys) for i in util.rng(scale): print("\rScale is %s of %s " % (i, len(scale)), end="") params['IA'] = IA_base * scale[i] params['IB'] = IB_base * scale[i] run_sweep.one_param_mult_data_instances(MODEL, dataset, params, period, max_iter) plot_sweep.pr_ratio_3d(dataset, params, write_params_on_img=False)
def alpha_test(): # TODO: refactor this DELTA_INSTEAD = False # just check gamma, delta symmetry base_gammas, base_delta, IA, IB, repitions = [.1, 10, 20], 1, 80, 60, 1000 if DELTA_INSTEAD: base_deltas, base_gamma = [1, 4, 8], 1 COLORS = [ 'blue', 'red', 'green', 'purple', 'cyan', 'orange', 'brown', 'magenta', 'yellow', 'grey' ] dkeys = ['steps', 'time', 'time_appx'] vkeys = ['scale', 'color', 'prediction'] data = {k: {'avg': [], 'var': []} for k in dkeys} variables = {k: [] for k in vkeys} legend_eles = [] for i in util.rng(base_gammas): if DELTA_INSTEAD: base_delta = base_deltas[i] else: base_gamma = base_gammas[i] color = COLORS[i] legend_eles += [ Line2D([0], [0], color=color, lw=4, label='base gamma = ' + str(base_gamma)) ] #Line2D([0], [0], marker='o', color='w', label='Scatter', markerfacecolor='g', markersize=15), for j in range(1, 10): print("\rBase gamma = %s, scale = %s" % (base_gamma, j), end="") gamma, delta = base_gamma * j, base_delta * j one_data = run_sweep.run_one_param(gamma, delta, IA, IB, repitions) variables['scale'] += [j] variables['color'] += [color] for k in one_data.keys(): data[k]['avg'] += [one_data[k]['avg']] data[k]['var'] += [one_data[k]['var']] if j == 1: variables['prediction'] += [one_data['time']['avg']] base_t = one_data['time']['avg'] else: variables['prediction'] += [1 / j * base_t] params = base_gammas, base_delta, IA, IB, repitions plot_sweep.features(data, variables, legend_eles, params, write_params_on_img=True)
def sweep(): # compares many parameters and averages each over many runs print("\nRunning parameter sweep with repeats.\n") NADs = [(5**i) for i in range(2, 6)] #PARGs = [(10**i) for i in range(0,3)] #PARGs = [(10**i) for i in range(-10,-5)] all_params = [] feature_names = ['size', 'branching ratio'] stats = { 'avg': [], 'std': [], 'top1': [], 'top2': [], 'top3': [], 'btm1': [], 'btm2': [], 'btm3': [] } merged_data = { n: { 'avg': deepcopy(stats), 'var': deepcopy(stats), 'max': deepcopy(stats), 'iod': deepcopy(stats), '1:2': deepcopy(stats) } for n in feature_names } shots = [] for i in rng(NADs): params['NAD'] = NADs[i] print("[NAD] = ", NADs[i]) #params['PARG'] = PARGs[i] #print("[PARG] = ",PARGs[i]) #params['cut_rate'] = PARGs[i] #print("cut_rate = ",PARGs[i]) repeats_data = { n: { 'avg': [], 'var': [], 'max': [], 'iod': [], '1:2': [] } for n in feature_names } # Format: data[feature_name][stat]. Example: data['size']['avg'] for r in range(params['repeats']): sshot = run_sim(params) features.extract_stats(repeats_data, feature_names, sshot) features.merge_repeats(merged_data, repeats_data, feature_names) all_params += [params] util.pickle_it(all_params, merged_data) plot.param_sweep(merged_data, params, NADs, '[NAD]', feature_names) #features * stats imgs
def drawG_by_rank(G,params, topK): # color by seed/not_seed, alpha by rank assert(os.path.isdir(params['output_path'])) plt.figure(figsize=(10,6)) node_size, nalpha, ealpha = 800, .3, .3 #node_alpha not curr used # positions for all nodes if params['draw_layout'] == 'circular': pos = nx.circular_layout(G) elif params['draw_layout'] == 'spring': pos = nx.spring_layout(G) else: assert(False) #unknown draw layout nodes = list(G.nodes()) colors, alphas = [], [] max_rank = max(G.nodes[nodes[i]]['rank'] for i in rng(nodes)) for i in rng(nodes): if G.nodes[nodes[i]]['seed']: G.nodes[nodes[i]]['alpha'] = 1 alphas += [1] colors+=['green'] else: colors+=['blue'] #G.nodes[nodes[i]]['alpha'] = G.nodes[nodes[i]]['rank']/max_rank #for the edges G.nodes[nodes[i]]['alpha'] = G.nodes[nodes[i]]['cardinal_rank'] #for the edges alphas += [G.nodes[nodes[i]]['cardinal_rank']]#/max_rank] #may not work..also gotta add LOG SCALING nx.draw_networkx_nodes(G, pos, nodelist=nodes, node_color=colors, node_size=node_size, alpha=alphas) #labels = {n: G.nodes[n]['gene'] for n in G.nodes()} topK2 = topK.tolist() top = topK2 + params['seeds'] labels = {n:n for n in top} #huh? nx.draw_networkx_labels(G, pos, labels, font_size=8, font_color='blue') #edge_alphas = [] ecolors = [] elist = sorted(list(G.edges())) for i in rng(elist): n1,n2 = elist[i][0],elist[i][1] #edge_alphas += [min(G.nodes[n1]['alpha'],G.nodes[n2]['alpha'])] ecolors += [(0,0,0,avg([G.nodes[n1]['alpha'],G.nodes[n2]['alpha']]))] nx.draw_networkx_edges(G, pos, arrows=True, edgelist=elist, edge_color=ecolors) #,alpha=edge_alphas) if params['save_fig']: now = datetime.now() curr_date = str(date.today()).strip('2020-') curr_time = str(datetime.now().strftime("%H-%M-%S")) plt.savefig(params['output_path']+'/'+curr_date+'_'+curr_time+'_rankedG.png') else: plt.show() plt.clf() plt.close()
def drawG_by_comm_local(G,params): # color by seed/not_seed, alpha by rank assert(os.path.isdir(params['output_path'])) node_size, nalpha, ealpha = 200, .5, .2 plt.figure(figsize=(10,6)) plt.title('Local Community Detection from Seed',fontsize=26) custom_lines = [Line2D([0], [0], color='green', lw=8,alpha=nalpha), Line2D([0], [0], color='purple', lw=8,alpha=nalpha), Line2D([0], [0], color='blue', lw=8,alpha=nalpha), Line2D([0], [0], color='grey', lw=8,alpha=nalpha)] ax = plt.gca() ax.legend(custom_lines, ['Seed', 'Inner Community','Border Community', 'Connected Outsiders']) pos = get_layout(params,G) nodes = list(G.nodes()) colors, alphas = [], [] dels = [] for i in rng(nodes): if G.nodes[nodes[i]]['seed']: colors+=['green'] elif G.nodes[nodes[i]]['status'] == 'C': colors+=['purple'] elif G.nodes[nodes[i]]['status'] == 'B': colors+=['blue'] elif G.nodes[nodes[i]]['status'] == 'U': colors+=['grey'] else: colors+=['white'] dels += [nodes[i]] for d in dels: nodes.remove(d) if params['draw_pagerank']: min_alpha = .1 nalpha=[] for i in rng(nodes): if G.nodes[nodes[i]]['seed']: G.nodes[nodes[i]]['alpha'] = 1 nalpha += [1] else: #G.nodes[nodes[i]]['alpha'] = G.nodes[nodes[i]]['rank']/max_rank #for the edges G.nodes[nodes[i]]['alpha'] = G.nodes[nodes[i]]['cardinal_rank'] #for the edges nalpha += [max(math.pow(G.nodes[nodes[i]]['cardinal_rank'],.3),min_alpha)]#/max_rank] #may not work..also gotta add LOG SCALING nx.draw_networkx_nodes(G, pos, nodelist=nodes, node_color=colors, node_size=node_size, alpha=nalpha) #labels = {n: G.nodes[n]['gene'] for n in G.nodes()} labels = {n:n for n in G.nodes()} # huh? nx.draw_networkx_labels(G, pos, labels, font_size=8, font_color='black') elist = sorted(list(G.edges())) nx.draw_networkx_edges(G, pos, arrows=True, edgelist=elist, alpha=ealpha) if params['save_fig']: tstamp = timestamp() plt.savefig(params['output_path']+'/'+str(tstamp)+'_local.png') else: plt.show() plt.clf() plt.close()
def over_param_2ds(x_params, x_key, y_key, z_params, z_key, dataset, params, write_params_on_img=True, prediction=None): # should make clearer # z_params is the value of the parameter that is varied for each line plot plt.figure(1, [16, 12]) handles = [] for i in util.rng(z_params): c = COLORS[i % len(COLORS)] y_avg = np.array(dataset.vals[y_key]['avg']) y_var = np.array(dataset.vals[y_key]['var']) y_max, y_min = np.array(dataset.vals[y_key]['max']), np.array( dataset.vals[y_key]['min']) y_max_sd, y_min_sd = np.array(dataset.vals[y_key]['max_sd']), np.array( dataset.vals[y_key]['min_sd']) plt.plot(x_params, y_avg, alpha=.8, linewidth=2, color=c) #top, btm = y_avg+y_var,y_avg-y_var top, btm = y_max_sd, y_min_sd #top,btm = y_max,y_min plt.fill_between(np.array(x_params), top, btm, alpha=.2, color=c) handles += ['Simulation'] #handles += [z_key + ' = ' + str(z_params[i])] if prediction != None: plt.plot(x_params, prediction, alpha=.8, linewidth=2, color='black', linestyle='--') handles += ['Prediction'] #legend=plt.legend(handles, fontsize=14) #plt.setp(legend.get_title(),fontsize=16) plt.xlabel(x_key, fontsize=20) plt.xticks(fontsize=12) plt.ylabel(y_key, fontsize=20) plt.yticks(fontsize=12) if write_params_on_img: ax = plt.gca() #ax.text(0,-.2,-1,'PARAMS' + str(params)) plt.title('Parameters: ' + str(params)[:80] + '\n' + str(params)[80:160] + '\n' + str(params)[160:], fontsize=10) if params['save_fig']: title = params['out_dir'] + util.timestamp( ) + '_' + x_key + '_' + y_key + '_' + z_key + '.png' plt.savefig(title) else: plt.show() plt.clf() plt.close()
def over_time_2ds(x_key, y_key, z_params, z_key, datasets, params, predictions=None): # z_params is the value of the parameter that is varied for each line plot # TODO: # rcparams for font # write_params_on_img as param # dpi plt.figure(1, [16, 12]) handles = [] for i in util.rng(z_params): dataset = datasets[i] c = COLORS[i % len(COLORS)] linestyle = LINESTYLES[i % len(LINESTYLES)] x = np.array(dataset.vals[x_key]['avg']) y_avg = np.array(dataset.vals[y_key]['avg']) y_var = np.array(dataset.vals[y_key]['var']) y_max, y_min = np.array(dataset.vals[y_key]['max_conf']), np.array( dataset.vals[y_key]['min_conf']) plt.plot(x, y_avg, alpha=.8, linewidth=2, color=c, linestyle=linestyle) plt.fill_between(np.array(x), y_max, y_min, alpha=.2, color=c, linestyle=linestyle) handles += [z_key + ' = ' + str(z_params[i])] plt.grid(alpha=.2) plt.axhline(y=1, color='grey', linestyle='--', alpha=.5) legend = plt.legend(handles, fontsize=14) plt.setp(legend.get_title(), fontsize=16) #plt.xlabel(x_key,fontsize=20) plt.xlabel('Time', fontsize=20) plt.xticks(fontsize=12) #plt.ylabel(y_key,fontsize=20) plt.ylabel('Accuracy', fontsize=20) plt.yticks(fontsize=12) if params['write_params_on_img']: ax = plt.gca() #ax.text(0,-.2,-1,'PARAMS' + str(params)) plt.title('Parameters: ' + str(params)[:80] + '\n' + str(params)[80:160] + '\n' + str(params)[160:], fontsize=10) else: plt.title('Accuracy of NAND Gate with Amplifier', fontsize=20) if params['save_fig']: title = params['out_dir'] + util.timestamp( ) + '_' + x_key + '_' + y_key + '_' + z_key + '.png' plt.savefig(title, dpi=300) else: plt.show() plt.clf() plt.close()