<jupyter_start><jupyter_text>Importing required libraries for creating graph object<jupyter_code>from scipy.io import mmread
from Base import Graph
from IPython.display import display
import pandas as pd

from copy import deepcopy


#visualization libraries, not required as such for main algorithm
from pyvis.network import Network
import networkx as nx
from matplotlib import pyplot as plt<jupyter_output><empty_output><jupyter_text>Initialising and creating instances of graph object using different *.mtx files<jupyter_code># karate = mmread('soc-karate.mtx')
# webedu = mmread('web-edu.mtx')
# internet = mmread('tech-internet-as.mtx')

karate = mmread('../assets/S_soc-karate.mtx')
webedu = mmread('../assets/M_web-edu.mtx')
internet = mmread('../assets/L_tech-internet-as.mtx')
G1 = Graph(mtxfilepath='../assets/S_soc-karate.mtx')
G2 = Graph(sparse=webedu)
G3 = Graph(sparse=internet)
print(("-"*50)+"Graphs objects created"+("-"*50))
graphData = [['soc-karate.mtx', G1.graph.number_of_nodes(), G1.graph.number_of_edges(), G1.is_connected()],
            ['web-edu.mtx', G2.graph.number_of_nodes(), G2.graph.number_of_edges(), G2.is_connected()],
            ['tech-internet-as.mtx', G3.graph.number_of_nodes(), G3.graph.number_of_edges(), G3.is_connected()]]
            
display(pd.DataFrame(graphData, columns=["Name", "Size", 'Edges', "connected"]))<jupyter_output><empty_output><jupyter_text>Finding Centralities<jupyter_code># EGO centrality
# print(G.ego_centrality_node(4))
# print("ego graph made")
# Finding lfvc node

lfvc1 = G1.lfvc_node(0)
lfvc2 = G2.lfvc_node(0)
# lfvc3 = G3.lfvc_node(0)
print(lfvc1)
print(lfvc2)
# print(lfvc3)
# Finding nodes of interest
graphData = [['soc-karate.mtx', G1.nodes_of_interest()],
            ['web-edu.mtx', G2.nodes_of_interest()],
            ['tech-internet-as.mtx', G3.nodes_of_interest()]]
display(pd.DataFrame(graphData, columns=["Name", "Nodes of interest: "]))

# Finding Centralities of smallest size graph, i.e. soc-karate

print("soc-karate :")
dc1 = G1.degree_centrality()
cc1 = G1.closeness_centrality()
bc1 = G1.betweenness_centrality()
ec1 = G1.eigenvector_centrality()
clc1 = G1.clustering_coefficient_node(0)
lfvc_val = G1.lfvc()
nhc1 = G1.neighbourhood_hopset(0,2)


data = [['lfvc', lfvc_val],
        ['degree centrality', dc1],
        ['closeness centrality', cc1],
        ['betweenness centrality', bc1],
        ['eigenvector centrality', ec1],
        ['neighbouring hopset', nhc1],
        ['Clusters of node 1', clc1]]

display(pd.DataFrame(data, columns=["Centrality", "value"]))<jupyter_output><empty_output><jupyter_text>Finding nodes of interest<jupyter_code>nodes_interest1 = G1.nodes_of_interest()
nodes_interest2 = G2.nodes_of_interest()
nodes_interest3 = G3.nodes_of_interest()<jupyter_output><empty_output><jupyter_text>Centralities at nodes of interest<jupyter_code># Finding Centralities of medium size graph, i.e. web-edu

print("web-edu :")
for i in nodes_interest2:
    print("\nNode ", i)
    cc2 = G2.closeness_centrality_node(i)
    clc2 = G2.clustering_coefficient_node(i)
    ec2 = G2.ego_centrality_node(i)
    lfvc_val2 = G2.lfvc_node(i)
    nhc2 = G2.neighbourhood_hopset(i,2)
    eig_c2 = G2.eigenvector_centrality_node(i)

    data = [['lfvc', lfvc_val2],
        ['closeness centrality', cc2],
        ['Clusters of node 1', clc2],
        ['neighbouring hopset', nhc2],
        ['ego centrality', ec2],
        ['eigenvector centrality', eig_c2]]

    display(pd.DataFrame(data, columns=["Centrality", "value"]))

# Finding Centralities of largest size graph, i.e. tech-internet-as

print("tech-internet-as :")
for i in nodes_interest3:
    print("\nNode ", i)
    cc3 = G3.closeness_centrality_node(i)
    clc3 = G3.clustering_coefficient_node(i)
    ec3 = G3.ego_centrality_node(i)
    lfvc_val3 = G3.lfvc_node(i)
    nhc3 = G3.neighbourhood_hopset(i,2)
    eig_c3 = G3.eigenvector_centrality_node(i)

    data = [['lfvc', lfvc_val3],
        ['closeness centrality', cc3],
        ['Clusters of node 1', clc3],
        ['neighbouring hopset', nhc3],
        ['ego centrality', ec3],
        ['eigenvector centrality', eig_c3]]

    display(pd.DataFrame(data, columns=["Centrality", "value"]))



# dc3 = G3.degree_centrality()
# cc3 = G3.closeness_centrality()
# bc3 = G3.betweenness_centrality()
# eig_c3 = G3.eigenvector_centrality()
# clc3 = G3.clustering_coefficient_node(0)
# lfvc_val3 = G3.lfvc_node(0)
# nhc3 = G3.neighbourhood_hopset(0,2)
# print(("-"*100))
# print("lfvc")
# print(lfvc_val3)

# data = [[1, 'lfvc', lfvc_val3],
#         [2, 'degree centrality', len(dc3)],
#         [3, 'closeness centrality', len(cc3)],
#         [4, 'betweenness centrality', len(bc3)],
#         [5, 'eigenvector centrality', len(eig_c3)],
#         [6, 'neighbouring hopset', nhc3],
#         [7, 'Clusters of node 1', clc3]]

# print(tabulate(data, headers=["#", "Centrality", "len of array/value"]))<jupyter_output><empty_output><jupyter_text>Deep community detection using greedy lfvc<jupyter_code>filename = 'webedu_nt'
g_obj = G2
dc: tuple() = g_obj.greedy_community_detection(q=50, function='node_lfvc')<jupyter_output><empty_output><jupyter_text>Display community<jupyter_code>def community_visualizer_matplot(dc,G,layout = nx.kamada_kawai_layout):
    Gnx = G.graph
    pos=layout(Gnx) #change layout if needed
    # labels = nx.get_edge_attributes(Gnx,'weight')
    colors=['green' if i in dc else 'red' for i in range(len(Gnx.nodes))]
    # nx.draw_networkx_edge_labels(Gnx,pos,edge_labels=labels)
    nx.draw_networkx(Gnx,pos,node_color=colors,node_size=300,width=0.3)
    plt.show()
    return

def community_visualizer_pyvis(file,dc,G_obj):
    G = G_obj.graph
    nt = Network(height='100%', width='100%', bgcolor='#94b8b8', font_color='black')
    # nt = Network(height='750px', width='100%')

    colors = []
    for i in G.nodes:
        if(i in dc[0]):
            colors.append('blue')
        elif(i in dc[1]):
            colors.append('green')
        else:
            colors.append('red')
            
    nt.add_nodes([i for i in G.nodes],color = colors)
    for n1,n2 in G.edges:
        nt.add_edge(int(n1),int(n2))
    # print(nt)
    nt.barnes_hut(gravity=-80000, central_gravity=0.3, spring_length=250, spring_strength=0.001, damping=0.09, overlap=0)

    nt.toggle_physics(False)
    nt.inherit_edge_colors(False)
    nt.show_buttons(filter_=['physics']) #make =True for all buttons
    nt.show(file)


# community_visualizer_matplot(dc,G1,nx.spring_layout)
community_visualizer_pyvis('../assets/'+filename+'.html', dc, g_obj)<jupyter_output><empty_output><jupyter_text>Community reductionReducing Commnunity and representing it using single node in graph<jupyter_code>def reduce_community_graph(dc, g_obj: Graph):
    n_g: nx.Graph = deepcopy(g_obj.graph)
    om : set = dc[0]
    nu : set = dc[1].difference(om)
    nnode = max([x for x in g_obj.graph.nodes])+len(nu)
    n_g.add_node(nnode)
    for y in om:
        for x in nu:
            if(n_g.has_edge(y,x)):
                n_g.add_edge(y,nnode, weight=1)
                break

    n_g.remove_nodes_from(nu)
    n_nu = om.union([nnode])
    return ((om, n_nu), Graph(nx_graph=n_g))

rc, ng_obj = reduce_community_graph(dc, g_obj)
community_visualizer_pyvis('../assets/'+filename+'_rc.html', rc, ng_obj)<jupyter_output><empty_output><jupyter_text>Computing centralities of representative node<jupyter_code>nnode = list(rc[1].difference(rc[0]))[0]
print("\nNode ", nnode)
cc2 = ng_obj.closeness_centrality_node(nnode)
clc2 = ng_obj.clustering_coefficient_node(nnode)
ec2 = ng_obj.ego_centrality_node(nnode)
lfvc_val2 = ng_obj.lfvc_node(nnode)
nhc2 = ng_obj.neighbourhood_hopset(nnode,2)
eig_c2 = ng_obj.eigenvector_centrality_node(nnode)

data = [['lfvc', lfvc_val2],
    ['closeness centrality', cc2],
    ['Clusters of node 1', clc2],
    ['neighbouring hopset', nhc2],
    ['ego centrality', ec2],
    ['eigenvector centrality', eig_c2]]

display(pd.DataFrame(data, columns=["Centrality", "value"]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14854937118665174, 'fraction_numerical': 0.026024156393973354, 'mean_word_length': 4.53168044077135, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '498352', 'license': 'MIT', 'n_tokens_mistral': 3229, 'n_tokens_neox': 3105, 'n_words': 541, 'path': 'py-src/community_detect.ipynb', 'repo_name': 'appmonster007/project-megraphs', 'size': '8031'}