예제 #1
0
def force_directed(ndf, ldf, iterations=1000):
    ## add force-directed layout coordinate
    bn.add_force_directed_layout(ndf,
                                 linksdf=ldf,
                                 nw=None,
                                 iterations=iterations)
    return ndf
예제 #2
0
def tsne_layout(ndf, ldf, plot=True):
    ## add tsne-layout coordinates and draw
    bn.add_layout(ndf, linksdf=ldf, nw=None)
    ndf.rename(columns={"x": "x_tsne", "y": "y_tsne"}, inplace=True)
    if plot:
        bn.draw_network_categorical(nw,
                                    ndf,
                                    node_attr="keyword_theme",
                                    x='x_tsne',
                                    y='y_tsne',
                                    plotfile="network_tsne.pdf")
    return ndf
예제 #3
0
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")
    return ndf
예제 #4
0
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
예제 #5
0
def build_network(df, attr, blacklist=[], idf=True, linksPer=3, minTags=1):
    '''
    Run basic 'build tag network' without any plotting or layouts or file outputs.
    Resulting network can then be enriched and decorated before writing final files. 
    
    df = nodes dataframe
    attr = tag attribute to use for linking
    blacklist = tags to blacklist from linking
    linksPer = avg links per node
    minTags = exclude any nodes with fewer than min Tags
    
    Returns: nodes and links dataframes
    Dependency: https://github.com/foodwebster/Tag2Network
    '''
    print("\nBuild Network")
    df[attr] = df[attr].fillna("")
    df = df[df[attr] !=
            '']  # remove any recipients which have no tags for linking
    df = df.reset_index(drop=True)
    taglist = attr + "_list"  # name new column of tag attribute converted into list
    df[taglist] = df[attr].apply(
        lambda x: x.split('|'))  # convert tag string to list
    df[taglist] = df[taglist].apply(
        lambda x: [s for s in x if s not in blacklist
                   ])  # only keep keywords not in blacklist
    df[attr] = df[taglist].apply(lambda x: "|".join(
        x))  # re-join tags into string with blacklist removed
    ndf, ldf = bn.buildTagNetwork(
        df,
        tagAttr=taglist,
        dropCols=[],
        outname=None,
        nodesname=None,
        edgesname=None,
        plotfile=None,  #str(networkpath/'RecipientNetwork.pdf'), 
        idf=idf,
        toFile=False,
        doLayout=False,
        linksPer=linksPer,
        minTags=1)

    return ndf, ldf
예제 #6
0
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)
예제 #7
0
def tsne_layout(ndf, ldf):
    ## add tsne-layout coordinates and draw
    # dependency: https://github.com/foodwebster/Tag2Network
    bn.add_layout(ndf, linksdf=ldf, nw=None)
    ndf.rename(columns={"x": "x_tsne", "y": "y_tsne"}, inplace=True)
    return ndf
예제 #8
0
#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")