def get_benchmark_network(name): """ Get benchmark networks for comparing link prediction methods. Args: name (str): Name of the benchmark network to get Returns: (object): Parsed network """ if name == 'erdos-renyi': # Erdos-Renyi random graph NUM_NODES = 25000 AVERAGE_DEGREE = 10 from benchmark_graphs import erdos_renyi network, _ = erdos_renyi(num_nodes=NUM_NODES, average_degree=AVERAGE_DEGREE) return network elif name == 'gnutella': # Gnutella peer-to-peer file sharing network return parse_network.parse_network('../data/gnutella', create_using=nx.Graph) elif name == 'facebook': # Facebook social circles network return parse_network.parse_network('../data/circles', create_using=nx.Graph) elif name == 'nec': # nec overlay map return parse_network.parse_network('../data/nec', create_using=nx.Graph) else: raise(ValueError('Unknown network specified'))
def main(): """ Split data into training and test sets, construct features and evaluate classifiers. """ from sklearn.ensemble import RandomForestClassifier # Parse network. network = parse_network.parse_network('../data/aps_2008_2013', create_using=nx.Graph) le, _ = get_label_encoder_and_bow(network) # Get training and test data. train_idxs, test_idxs = get_tts(network) data_train, target_train = get_features(network, train_idxs) data_test, target_test = get_features(network, test_idxs) # Initialize random-forest classifier. clf_rf = RandomForestClassifier() # Compute classification repots and write to file. clf_report_rf = evaluate_model(data_train, target_train, data_test, target_test, clf_rf) clf_report_maj = classification_report(target_test, majority_neigh(network, test_idxs)) with open('../results/res_classification.txt', 'w') as f: f.write(clf_report_rf + '\n') f.write(clf_report_maj)
def lancichinetti(mu): """ Return Lancichinetti benchmark graph with specified mu parameter. Args: mu (float): The mu parameter. Returns: (tuple): Parsed graph with specified mu parameter and ground truth in required format """ # Get path. fmt = '{:<04}' f_tmp = fmt.format(mu).replace('.', '') f = 'LFR_' + f_tmp[:2] + '_' + f_tmp[2:] # Load and parse graph. Get ground truth in required format. graph = parse_network.parse_network('../data/LFR/' + f, create_using=nx.Graph) attrs = nx.get_node_attributes(graph, 'data') ground_truth = [{ node_idx for node_idx, label in attrs.items() if label == comm_label } for comm_label in set(attrs.values())] # Return graph and ground truth in required format. return graph, ground_truth
def bottlenose_dolphins(): """ Parse and return Lusseau bottlenose dolphins network. Returns: (tuple): Parsed network and ground truth in required format """ # Load and parse graph. Get ground truth in required format. graph = parse_network.parse_network('../data/dolphins', create_using=nx.Graph) attrs = nx.get_node_attributes(graph, 'data') ground_truth = [{ node_idx for node_idx, label in attrs.items() if label == comm_label } for comm_label in set(attrs.values())] # Return graph and ground truth in required format. return graph, ground_truth
assignFlux = args.assignFlux runWhich = args.runWhich os.makedirs(outDir, exist_ok=True) ## get stoichiometric matrix --------------------------------------------------------------------------- print('\n\nParsing network') print('.' * 50) # get the stoichiometric matrix of a pathway iniMetabs = iniMetabs.split(',') if iniMetabs else [] finMetabs = finMetabs.split(',') if finMetabs else [] exBalMetabs = exBalMetabs.split(',') if exBalMetabs else [] exOptMetabs = exOptMetabs.split(',') if exOptMetabs else [] S4Bal, S4Opt, enzymeInfo, metabInfo = parse_network( reactionFile, iniMetabs, finMetabs, exBalMetabs, exOptMetabs) S4BalFull = get_full_stoichiometric_matrix( S4Bal, metabInfo ) # S4BalFull also includes input and output reactions of the pathway # get flux distribution in steady state if assignFlux: speEnz, speFlux = assignFlux.split(':') speFlux = float(speFlux) Vss = get_steady_state_net_fluxes(S4BalFull, enzymeInfo, metabInfo, speEnz, speFlux) else: Vss = get_steady_state_net_fluxes(S4BalFull, enzymeInfo, metabInfo)
# Randomly sample specified number of nodes. sample = random.sample(list(graph.nodes()), num_to_mark) # Go over sampled nodes and mark randomly chosen neighbors. for node in sample: marked.append(random.choice(list(graph.neighbors(node)))) # Return list of marked nodes. return marked if __name__ == '__main__': # Parse network. PATH = '../data/social' graph = parse_network.parse_network(PATH, create_using=nx.Graph) # Fraction of nodes to mark and number of runs to perform. FRAC_TO_MARK = 0.1 NUM_RUNS = 30 # Initialize results aggregate. aggr1 = 0 aggr2 = 0 for idx in range(NUM_RUNS): # Randomly mark specified fraction of nodes. marked_scheme1 = mark_random_nodes( graph, int(round(graph.number_of_nodes() * FRAC_TO_MARK)))
0, list(filter(lambda x: x[0] == include_additional, sorted_nodes))[0][1]) return x, y if __name__ == '__main__': import matplotlib.pyplot as plt from scipy import stats # Set name of dolphin of interest. DOLPHIN_NAME = 'SN100' # Parse the bottlenose dolphin network as well as associated data. graph = parse_network.parse_network("../data/dolphins", create_using=nx.Graph) # Get index of node corresponding to the dolphin of interest. idx_dolphin = [ el[0] for el in nx.get_node_attributes(graph, 'name').items() if el[1] == 'SN100' ][0] ### Bar charts of centralities ### importances_degree_centrality = node_importances(graph, 'degree_centrality') rank1 = node_rank(graph, idx_dolphin, 'degree_centrality') x_bar1, y_bar1 = data_most_important(importances_degree_centrality, 10, include_additional=idx_dolphin) fig1, ax1 = plt.subplots()