def spring_layout(ndf, ldf, iterations=1000): print("Running spring Layout") nw = bn.buildNetworkX(ldf) # remove isolated nodes and clusters for layout giant_component_nodes = max(nx.connected_components(nw), key=len) giant_component = nw.subgraph(giant_component_nodes) layout = nx.spring_layout( giant_component, k=0.2, weight='weight', iterations=iterations) # k is spacing 0-1, default 0.1 x = {n: layout[n][0] for n in giant_component.nodes()} y = {n: layout[n][1] for n in giant_component.nodes()} ndf['x_spring'] = ndf['id'].map(x) ndf['y_spring'] = ndf['id'].map(y) # place all disconnected nodes at 0,0 ndf['x_spring'].fillna(0) ndf['y_spring'].fillna(0) return ndf
def plot_network(ndf, edf, plot_name, x='x_tsne', y='y_tsne', colorBy='Cluster', sizeBy='ClusterCentrality', sizeScale=100): # draw network colored by creative style and save image # ndf = nodes dataframe # ldf = links dataframe # plotname = name of file to save image (pdf) # dependency: https://github.com/foodwebster/Tag2Network nw = bn.buildNetworkX(edf) # build networkX graph object node_sizes = ndf.loc[:, sizeBy] * sizeScale node_sizes_array = node_sizes.values # convert sizeBy col to array for sizing dn.draw_network_categorical(nw, ndf, node_attr=colorBy, plotfile=plot_name, x=x, y=y, node_size=node_sizes_array)
#from Network.BuildNetwork import addLouvainClusters #from Network.ClusteringProperties import basicClusteringProperties pd.set_option('display.expand_frame_repr', False) # expand display of data columns if screen is wide wd = pl.Path.cwd() datapath = wd / "raw_data" / "10_yrs_of_TED_Network.xlsx" resultspath = wd / "processed_data" ## read network file ndf = pd.read_excel(datapath, sheet_name="Nodes") ldf = pd.read_excel(datapath, sheet_name="Links") # create networkx object nw = bn.buildNetworkX(ldf) def force_directed(ndf, ldf, iterations=100, plot=True): ## add force-directed layout coordinates and draw bn.add_force_directed_layout(ndf, linksdf=ldf, nw=None, iterations=iterations) if plot: bn.draw_network_categorical(nw, ndf, node_attr="keyword_theme", x='x_force_directed', y='y_force_directed', plotfile="network_fd.pdf")