def plot_set(ax, graph, group, color): """ Plots the (I(C), E(C)) points for all communities in the group """ I_values = [] E_values = [] for c in group: I, E, rubish = CD.I_E(graph, c) E_values.append(E) I_values.append(I) ax.plot(I_values, E_values, color + 'o')
def gen_path_single(graph, seed, name, ax, metric, comp, ylim=[0, .3], legend=False, width=0.01, param=None): """ Generates and plots the I E path and manages the space Parameters ---------- graph : a networkx graph seed : a list of nodes to start from name : the title of the graph ax : the subplot to put everything metric : the single community metric to optimize comp : the comparison function either CD.compare_min or max legend : whether or not to show the legend width : the width of the arrow head """ # plot metric optimized path (I_path, E_path, order) = CD.path_I_E(graph, seed[:], metric, comp, param=param) print "Last point for ", name, " I ", I_path[-1], " E ", E_path[-1] CD.plot_path(I_path[:], E_path[:], ax, 'r', name, width) # plot corner cases ax.plot(1, 0, 'kD', label='Ideal', markersize=10) (graph_I, graph_E) = CD.I_E(graph, graph.nodes()) ax.plot(graph_I, 0, 'mD', label='Entire Graph', markersize=10) # set labels etc ax.set_title(name, fontsize=24) plt.xticks([0, .3, .7, 1], ['0', '0.3', '0.7', '1']) plt.yticks(ylim, [str(y) for y in ylim]) ax.set_xlim(-0.01, 1.01) ax.set_ylim(ylim[0] - width / 2., ylim[1] + width / 2.) ax.set_xlabel(r'$I(C)$', fontsize=24) ax.set_ylabel(r'$E(C)$', fontsize=24) if legend: handles, labels = ax.get_legend_handles_labels() ax.legend(handles, labels, loc=2) plt.show() # save the figure plt.savefig(name + ".eps") plt.savefig(name + ".pdf")
def gen_path_set(graph, I_path, E_path, S_path, name, ylim=[0, 1], legend=False, width=0.01, fig=False, color='r'): """ Given an I E S path plots Parameters ---------- I_path : a list of I(S) values E_path : a list of E(S) values S_path : a list of |S| values name : the title and saving file name ylim : the maximum value of E(S) legend : whether or not to show legend (not likely) width : the width of the arrow head """ if not fig: fig = plt.figure() ax = fig.add_subplot(111) CD.plot_path(I_path[:], E_path[:], ax, color, name, width) # plot corner cases ax.plot(1, 0, 'kD', label='Ideal', markersize=10) (graph_I, graph_E) = CD.I_E(graph, graph.nodes()) ax.plot(graph_I, 0, 'mD', label='Entire Graph', markersize=10) # set the dimensions and labels ax.set_title(name, fontsize=24) plt.xticks([0, .3, .7, 1], ['0', '0.3', '0.7', '1']) plt.yticks(ylim, [str(y) for y in ylim]) ax.set_xlim(-.01, 1.01) ax.set_ylim(ylim[0] - width / 2., ylim[1] + width / 2.) ax.set_xlabel(r'$I(S)$', fontsize=24) ax.set_ylabel(r'$E(S)$', fontsize=24) if legend: handles, labels = ax.get_legend_handles_labels() ax.legend(handles, labels, loc=2) plt.show() # save the figure plt.savefig(name + ".eps") plt.savefig(name + ".pdf")
def path_I_E(graph, seed, f, compare, param=None, stop=100): """Traces the path of the best f metric community through the IE plane Parameters ---------- graph - a networkx graph seed - a set of nodes to start from f - a metric to optimize compare - the function that determines if the metric is being improved param - a box to hand in any parameters f depends on, such as in linearity Returns ------- I_values - a list of the subsequent I values E_values - a list of the subsequent E values order - the order in which nodes are added Method ------ At each stage, finds a node that if added to the set decreases conductance """ all_nodes = set(graph.nodes()) I_values = [] E_values = [] order = seed[:] addition = True while addition != None and len(order) < stop: (next_I, next_E) = CD.I_E(graph, order) I_values.append(next_I) E_values.append(next_E) addition = greedy_selection(graph, order, f, compare, param) if type(addition) == int: order.append(addition) elif addition != None: addition = None if len(order) == 100: print "The metric was terminated by the maximum number of steps" return I_values, E_values, order