final version to send
parent
55911adcde
commit
3b4d0e3200
File diff suppressed because one or more lines are too long
@ -1,56 +1,49 @@
|
||||
#! /usr/bin/python3
|
||||
|
||||
import networkx as nx
|
||||
from utils import *
|
||||
import warnings
|
||||
import time
|
||||
import random
|
||||
import argparse
|
||||
warnings.filterwarnings("ignore")
|
||||
import networkx as nx
|
||||
from utils import *
|
||||
|
||||
"""
|
||||
Standard function to compute the omega index for a given graph. To see the implementation of the omega index, refer to the networkx documentation
|
||||
|
||||
def random_sample(graph, k):
|
||||
nodes = list(graph.nodes())
|
||||
n = int(k*len(nodes))
|
||||
nodes_sample = random.sample(nodes, n)
|
||||
https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.smallworld.omega.html#networkx.algorithms.smallworld.omega
|
||||
|
||||
G = graph.subgraph(nodes_sample)
|
||||
This file has been created to be used with the server. It takes as input the name of the graph and the percentage of nodes to be sampled. It then computes the omega index for the sampled graph and returns the result. Run
|
||||
|
||||
if not nx.is_connected(G):
|
||||
print("Graph is not connected. Taking the largest connected component")
|
||||
connected = max(nx.connected_components(G), key=len)
|
||||
G_connected = graph.subgraph(connected)
|
||||
```
|
||||
./omega_sampled_server.py -h
|
||||
```
|
||||
|
||||
print(nx.is_connected(G_connected))
|
||||
to see the list of available graphs and the other parameters that can be passed as input.
|
||||
"""
|
||||
|
||||
print("Number of nodes in the sampled graph: ", G.number_of_nodes())
|
||||
print("Number of edges in the sampled graph: ", G.number_of_edges())
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
return G_connected
|
||||
parser.add_argument("graph", help="Name of the graph to be used.", choices=['checkins-foursquare', 'checkins-gowalla', 'checkins-brightkite', 'friends-foursquare', 'friends-gowalla', 'friends-brightkite'])
|
||||
parser.add_argument("--k", help="Percentage of nodes to be sampled. Needs to be a float between 0 and 1", default=0)
|
||||
parser.add_argument("--niter", help="Number of rewiring per edge. Needs to be an integer. Default is 5", default=5)
|
||||
parser.add_argument("--nrand", help="Number of random graphs. Needs to be an integer. Default is 5", default=5)
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("graph", help="Name of the graph to be used. Options are 'checkins-foursquare', 'checkins-gowalla', 'checkins-brightkite', 'friends-foursquare', 'friends-gowalla', 'friends-brightkite'")
|
||||
parser.add_argument("k", help="Percentage of nodes to be sampled. Needs to be a float between 0 and 1")
|
||||
parser.add_argument("--niter", help="Number of rewiring per edge. Needs to be an integer. Default is 5", default=5)
|
||||
parser.add_argument("--nrand", help="Number of random graphs. Needs to be an integer. Default is 5", default=5)
|
||||
parser.add_help = True
|
||||
args = parser.parse_args()
|
||||
parser.add_help = True
|
||||
args = parser.parse_args()
|
||||
|
||||
# the name of the graph is the first part of the input string
|
||||
name = args.graph.split('-')[1]
|
||||
if 'checkins' in args.graph:
|
||||
# the name of the graph is the first part of the input string
|
||||
name = args.graph.split('-')[1]
|
||||
if 'checkins' in args.graph:
|
||||
G = create_graph_from_checkins(name)
|
||||
elif 'friends' in args.graph:
|
||||
elif 'friends' in args.graph:
|
||||
G = create_friendships_graph(name)
|
||||
G.name = str(args.graph) + " Checkins Graph"
|
||||
|
||||
# sample the graph
|
||||
G_sample = random_sample(G, float(args.k))
|
||||
|
||||
# compute omega
|
||||
start = time.time()
|
||||
print("\nComputing omega for graph: ", G.name)
|
||||
omega = nx.omega(G_sample, niter = int(args.niter), nrand = int(args.nrand))
|
||||
end = time.time()
|
||||
print("Omega coefficient for graph {}: {}".format(G.name, omega))
|
||||
print("Time taken: ", round(end-start,2))
|
||||
G.name = str(args.graph) + " Checkins Graph"
|
||||
|
||||
# sample the graph
|
||||
G_sample = random_sample(G, float(args.k)) # function from utils.py, check it out there
|
||||
|
||||
# compute omega
|
||||
start = time.time()
|
||||
print("\nComputing omega for graph: ", G.name)
|
||||
omega = nx.omega(G_sample, niter = int(args.niter), nrand = int(args.nrand))
|
||||
end = time.time()
|
||||
print("\nOmega coefficient for graph {}: {}".format(G.name, omega))
|
||||
print("Time taken: ", round(end-start,2), " seconds")
|
||||
|
Loading…
Reference in New Issue