Beispiel #1
0
def print_graph_detail(graph):
    """
    格式化显示Graph参数
    :param graph:
    :return:
    """
    import networkx as nx
    dst = {
        "nodes": nx.number_of_nodes(graph),
        "edges": nx.number_of_edges(graph),
        "selfloops": nx.number_of_selfloops(graph),
        "isolates": nx.number_of_isolates(graph),
        "覆盖度": 1 - nx.number_of_isolates(graph) / nx.number_of_nodes(graph),
    }
    print_table(dst)
Beispiel #2
0
def get_network_statistics(G, year=None):
    # G.name = year
    n_connected_components = 1
    for C in (G.subgraph(c).copy()
              for c in sorted(nx.connected_components(G), key=len)):
        largest_C = C
        n_connected_components -= 1
    maxK = sorted(G.degree, key=lambda x: x[1], reverse=True)[0]
    info = {
        'Number of Isolates':
        nx.number_of_isolates(G),
        'No. of nodes':
        float(nx.info(G).split('\n')[-3].split(':')[-1].strip()),
        'No. of edges':
        float(nx.info(G).split('\n')[-2].split(':')[-1].strip()),
        'Average no. of edges':
        len(G.edges) / len(G.nodes),
        'Average degree':
        float(nx.info(G).split('\n')[-1].split(':')[-1].strip()),
        'Max Degree':
        maxK[1],
        'Average Clustering coefficient':
        nx.average_clustering(G),
        'No. of nodes in largest connected component':
        len(largest_C.nodes),
        'Diameter of largest connected component':
        nx.diameter(largest_C),
        'Average shortest path length of LCC':
        nx.average_shortest_path_length(largest_C)
    }
    df_stats = pd.DataFrame.from_records([info]).T
    df_stats = df_stats.reset_index()
    df_stats.columns = ['Attributes', 'Statistics']
    return df_stats
Beispiel #3
0
def info(G):
    """
  Compute and print out standard statistics of undirected multigraph G.
  """
    tic = time()
    print("{0:>15s} | '{1:s}'".format('Graph', G.name.replace('_', '-')))
    multi = False
    for edge in G.edges():
        if G.number_of_edges(edge[0], edge[1]) > 1:
            multi = True
            break
    print("{0:>15s} | '{1:s}'".format('Type', '===' if multi else '---'))
    print("{0:>15s} | {1:,d} ({2:,d})".format('Nodes', G.number_of_nodes(),
                                              nx.number_of_isolates(G)))
    print("{0:>15s} | {1:,d} ({2:,d})".format('Edges', G.number_of_edges(),
                                              nx.number_of_selfloops(G)))
    ks = [k for _, k in G.degree()]
    print("{0:>15s} | {1:.1f} ({2:,d}, {3:,d})".format(
        'Degree', 2.0 * G.number_of_edges() / G.number_of_nodes(), min(ks),
        max(ks)))
    print("{0:>15s} | {1:.8f}".format(
        'Density', 2.0 * G.number_of_edges() / G.number_of_nodes() /
        (G.number_of_nodes() - 1.0)))
    CCs = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)
    print("{0:>15s} | {1:.1f}% ({2:,d})".format(
        'Components', 100.0 * CCs[0].number_of_nodes() / G.number_of_nodes(),
        len(CCs)))
    d, D = dists(CCs[0])
    print("{0:>15s} | {1:.3f} ({2:,d})".format('Distances', d, D))
    print("{0:>15s} | {1:.6f}".format('Clustering',
                                      nx.average_clustering(nx.Graph(G))))
    print("{0:>15s} | {1:.1f} sec\n".format('Time', time() - tic))
Beispiel #4
0
def calculate_networks_indicators(graph):
    """计算基本网络指标"""
    degree_centrality = nx.degree_centrality(graph)
    nodes = list(degree_centrality.keys())
    betweenness_centrality = nx.betweenness_centrality(graph, weight='weight')
    network_indicators = pd.DataFrame({
        'nodes':
        nodes,
        'degree_centrality': [degree_centrality[node] for node in nodes],
        'betweenness_centrality':
        [betweenness_centrality[node] for node in nodes]
    })

    network_indicators['local_reaching_centrality'] = [
        nx.local_reaching_centrality(graph, node, weight='weight')
        for node in nodes
    ]
    constraint = nx.constraint(graph, weight='weight')
    network_indicators['constraint'] = [constraint[node] for node in nodes]
    effective_size = nx.effective_size(graph, weight='weight')
    network_indicators['effective_size'] = [
        effective_size[node] for node in nodes
    ]
    triangles = nx.triangles(graph)
    network_indicators['triangles'] = [triangles[node] for node in nodes]
    clustering = nx.clustering(graph, weight='weight')
    network_indicators['clustering'] = [clustering[node] for node in nodes]

    weight_dict = {
        item[0]: item[1]
        for item in nx.degree(graph, weight='weight')
    }
    degree_dict = {item[0]: item[1] for item in nx.degree(graph)}
    average_weight_dict = {
        weight_key:
        (weight_dict[weight_key] /
         degree_dict[weight_key] if degree_dict[weight_key] != 0 else 0)
        for weight_key in weight_dict.keys()
    }
    network_indicators['tie_strength'] = [
        average_weight_dict[node] for node in nodes
    ]
    network_indicators['number_of_node'] = nx.number_of_nodes(graph)
    network_indicators['density'] = nx.density(graph)
    cliques = nx.graph_clique_number(graph)
    if cliques >= 3:
        network_indicators['cliques'] = cliques
    else:
        network_indicators['cliques'] = 0
    network_indicators['efficiency'] = nx.global_efficiency(graph)
    network_indicators['isolates'] = nx.number_of_isolates(graph)

    network_indicators = network_indicators[[
        'nodes', 'degree_centrality', 'betweenness_centrality',
        'local_reaching_centrality', 'constraint', 'effective_size',
        'triangles', 'clustering', 'tie_strength', 'number_of_node', 'density',
        'cliques', 'efficiency', 'isolates'
    ]]
    return network_indicators
 def print_statistics(self):
     message("-------------------------------------------")
     message("-- Graph Statistics -----------------------")
     message()
     message("   isolates:", nx.number_of_isolates(self.graph))
     message("    density:", nx.density(self.graph))
     message("    bridges:", len(list(nx.bridges(self.graph))))
     message("    cliques:", nx.graph_clique_number(self.graph))
     message(" conn-comps:", nx.number_connected_components(self.graph))
     message()
     message("-------------------------------------------")
Beispiel #6
0
def macroscopic_measurements(G, start, final_leaders):
    """
         Function which computes the macroscopic attributes of the newtork:
         1. total nodes
         2. total edges
         3. graph's density
         4. number of isolated nodes
         5. number of self loops
         6. average_shortest_path
     """

    number_of_total_nodes = len(G)
    number_of_total_edges = len(G.edges)
    sys.stderr.write("Nodes and edges' number calculated! Runtime: %s\n" %
                     (time.time() - start))

    # fraction of existing edges out of all potentially possiblie edges => number between 0 e 1
    graph_density = nx.density(G)
    sys.stderr.write("Graph's density calculated! Runtime: %s\n" %
                     (time.time() - start))

    # check if there are isolated nodes [a node with no neighbors (that is, with degree zero)]
    isolated_nodes = nx.number_of_isolates(G)
    sys.stderr.write("Isolates nodes calculated! Runtime: %s\n" %
                     (time.time() - start))

    # check if there are self-loops (edge that has the same node at both ends)
    self_loops = G.number_of_selfloops()
    sys.stderr.write("Self loops calculated! Runtime: %s\n" %
                     (time.time() - start))

    G.remove_edges_from(G.selfloop_edges())  # remove self-loops

    f = open("netstats_directed_old/directed_graph_macro_stats", 'a')
    f.write("Number nodes = %s \n" % number_of_total_nodes)
    f.write("\n")
    f.write("Number edges = %s \n" % number_of_total_edges)
    f.write("\n")
    f.write("Density = %s \n" % graph_density)
    f.write("\n")
    f.write("Isolated nodes = %s \n" % isolated_nodes)
    f.write("\n")
    f.write("Self-loops = %s \n" % self_loops)
    f.write("\n")
    f.close()
Beispiel #7
0
def display_info(sn_graph):
    try:
        print(nx.info(sn_graph))
        #Betweennss
        betweenness = nx.betweenness_centrality(sn_graph, normalized=True).values()
        print("betweeness centrality (maximum): ", np.max(np.array(list(betweenness))))
        print("betweenness centrality (average): ", np.mean(np.array(list(betweenness))))
        #Closeness
        closeness = nx.closeness_centrality(sn_graph).values()
        print("closeness centrality (maximum): ", np.max(np.array(list(closeness))))
        print("closeness centrality (average): ", np.mean(np.array(list(closeness))))
        #Any seprate node?
        print("Any seprate node: ", nx.number_of_isolates(sn_graph))
        #BigestSubgraph
        subgraphs = sorted(nx.connected_component_subgraphs(sn_graph), key=len, reverse=True)
        print("Biggest sub component : ", subgraphs[0].order(), subgraphs[0].size())
        #diameter
        diameter=nx.diameter(sn_graph)
        print("graph diameter : ",diameter)
    except :
        print("Measure Error - diameter!!! ")
Beispiel #8
0
    def __init__(self, lines: list[LineString]):
        """Construct a directed graph from a set of LineStrings

        Parameters:
            lines: geometries in the network

        Returns:
            directed network graph

        """
        super().__init__()

        # add the nodes
        endpoints = []
        for line in lines:
            endpoints.append(line.coords[0])
            endpoints.append(line.coords[-1])
        for i, p in enumerate(set(endpoints)):
            self.add_node(i, geom=Point(p))

        # add the edges
        for line in lines:
            from_node = None
            to_node = None
            for n, data in self.nodes(data=True):
                p = data['geom']
                if p.equals(Point(line.coords[0])):
                    from_node = n
                elif p.equals(Point(line.coords[-1])):
                    to_node = n
            self.add_edge(from_node, to_node, geom=line, len=line.length, meas=measure(line))

        if nx.number_of_isolates(self) > 1:
            warnings.warn(ISOLATED_NODES)
        # if len(list(nx.connected_components(self.to_undirected()))) > 1:
        if nx.number_connected_components(self.to_undirected()) > 1:
            warnings.warn(MULTIPLE_SUBGRAPHS)

        self.vertices = self._vertices()
def metrics(G):
    # drawing the full network
    plot_graph(G)
    show()

    print(nx.info(G))

    clos = nx.closeness_centrality(G).values()
    bet = nx.betweenness_centrality(G, normalized=True).values()

    print("isolated nodes: ", nx.number_of_isolates(G))
    print("Density:", nx.density(G))
    print("Diameter:", nx.diameter(G))
    print("Connectivity:", nx.node_connectivity(G))
    print("Average short path", nx.average_shortest_path_length(G))
    print("Assortativity:", nx.degree_assortativity_coefficient(G))
    giant = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)
    print("Size of biggest GCC (nodes): ", giant[0].order())
    print("Max betweeness", np.max(np.array(list(bet))))
    print("Max closeness", np.max(np.array(list(clos))))
    print("AVG betweeness", np.mean(np.array(list(bet))))
    print("AVG closeness", np.mean(np.array(list(clos))))
    PlotMostImp(G)
Beispiel #10
0
"""#Criando Grafo"""

G = nx.DiGraph()
G.add_nodes_from(nodes.name)

G.add_edges_from([(s,t) for s,t in zip(edges.name_source, edges.name_target)]) # adicionando as arestas

nx.write_graphml(G, "dependencies_py.graphml")

nx.set_node_attributes(G, pd.Series(list(nodes.position.str.split(",")), index=nodes.name).to_dict(), 'pos')

print(nx.number_of_nodes(G))
print(nx.number_of_edges(G))
print(G.nodes.data())

print(nx.number_of_isolates(G)) #nós isolados
 G.remove_nodes_from(list(nx.isolates(G)))# removendo nós isolados

print(nx.number_of_nodes(G))
print(nx.number_of_edges(G))
print(G.nodes)

"""# ANÁLISES, MÉTRICAS, GRAU

##Matrizes e mais
"""

print(list(G.adj['labkit'])) # or list(G.neighbors(1))
print(nx.center(nx.Graph(G))) # ['beautifulsoup4', 'requests', 'six', 'docopt', 'docutils', 'gevent', 'pycrypto', 'distribute', 'lxml', 'argparse', 'pyyaml', 'jinja2', 'simplejson', 'mock', 'numpy', 'sphinx', 'python-dateutil', 'flake8', 'sqlalchemy', 'twisted', 'babel', 'psycopg2', 'click', 'flask', 'pillow', 'pytz', 'pep8']

"""##Densidade"""
        sparcity = []

        for entity in natsorted(os.listdir(simulation)):
            path_entity = simulation + '/' + entity + '/'

            if os.path.isdir(path_entity):
                print(entity)

                simulated_matrix = np.load(path_entity + 'sim_fc.npy')
                J = np.loadtxt(path_entity + 'J_ij.csv', delimiter=',')
                critical_temperature = np.loadtxt(path_entity + 'ctem.csv',
                                                  delimiter=',')

                c, r = correlation_function(simulated_matrix, J)

                print(nx.number_of_isolates(nx.Graph(J)))

                index_ct = find_nearest(ts, critical_temperature)
                dimensionality = dim(c, r, index_ct)
                if dimensionality != 3:  # Outliear
                    dimensionality_sim.append(dimensionality)
                    critical_temperature_sim.append(critical_temperature)
                    size_sim.append(J.shape[-1])
                    degrees = sorted(d for n, d in nx.Graph(J).degree())
                    degree_sim.append(np.mean(degrees))
                    sparcity.append(nx.density(nx.Graph(J)) * 100)

        #dimensionality_.append(np.mean(dimensionality_sim))
        results.append([
            np.mean(dimensionality_sim),
            np.mean(size_sim),
Beispiel #12
0
def create_gml(json_paths, target_paths, output_dir, taxon_file=None):
    """Create solution graph from miscoto output and compute stats

    Args:
        json_paths (str): {target: path_to_corresponding_json}
        target_paths (str): {target: path_to_corresponding_sbml}
        output_dir (str): results directory
        taxon_file (str): mpwt taxon file for species in sbml folder
    """
    miscoto_stat_output = os.path.join(output_dir, 'miscoto_stats.txt')
    key_species_stats_output = os.path.join(output_dir,
                                            'key_species_stats.tsv')
    key_species_json = os.path.join(output_dir, 'key_species.json')

    gml_output = os.path.join(output_dir, 'gml')

    if not utils.is_valid_dir(gml_output):
        logger.critical('Impossible to access/create output directory')
        sys.exit(1)

    len_min_sol = {}
    len_union = {}
    len_intersection = {}
    len_solution = {}
    len_target = {}

    target_categories = {}
    for target in target_paths:
        target_categories[target] = sbml_management.get_compounds(
            target_paths[target])

    if taxon_file:
        taxon_named_species, all_taxons = get_taxon(taxon_file)
    else:
        taxon_named_species = None
        all_taxons = None

    key_species_data = {}
    miscoto_stat_output_datas = []

    for target_category in target_categories:
        key_species_data[target_category] = {}
        key_species_data[target_category]['essential_symbionts'] = {}
        key_species_data[target_category]['alternative_symbionts'] = {}
        target_output_gml_path = os.path.join(gml_output,
                                              target_category + '.gml')
        with open(json_paths[target_category]) as json_data:
            dicti = json.load(json_data)

        G = nx.Graph()
        added_node = []
        species_weight = {}
        if dicti['still_unprod'] != []:
            logger.warning('ERROR ', dicti["still_unprod"], ' is unproducible')
        len_target[target_category] = len(dicti['newly_prod']) + len(
            dicti['still_unprod'])
        len_min_sol[target_category] = len(dicti['bacteria'])
        len_union[target_category] = len(dicti['union_bacteria'])
        len_intersection[target_category] = len(dicti['inter_bacteria'])
        key_species_types = {
            organism: 'ES' if organism in dicti['inter_bacteria'] else 'AS'
            for organism in dicti['union_bacteria']
        }
        if taxon_file:
            for taxon in all_taxons:
                key_species_data[target_category]['essential_symbionts'][
                    taxon] = [
                        organism for organism in key_species_types
                        if key_species_types[organism] == 'ES' and
                        taxon_named_species[organism].split('__')[0] == taxon
                    ]
                key_species_data[target_category]['alternative_symbionts'][
                    taxon] = [
                        organism for organism in key_species_types
                        if key_species_types[organism] == 'AS' and
                        taxon_named_species[organism].split('__')[0] == taxon
                    ]
        else:
            key_species_data[target_category]['essential_symbionts'][
                'data'] = [
                    organism for organism in key_species_types
                    if key_species_types[organism] == 'ES'
                ]
            key_species_data[target_category]['alternative_symbionts'][
                'data'] = [
                    organism for organism in key_species_types
                    if key_species_types[organism] == 'AS'
                ]

        len_solution[target_category] = len(dicti['enum_bacteria'])
        for sol in dicti['enum_bacteria']:
            if len(dicti['enum_bacteria'][sol]) > 1:
                for species_1, species_2 in combinations(
                        dicti['enum_bacteria'][sol], 2):
                    if species_1 not in added_node:
                        if taxon_file:
                            G.add_node(taxon_named_species[species_1],
                                       note=key_species_types[species_1])
                        else:
                            G.add_node(species_1,
                                       note=key_species_types[species_1])
                        added_node.append(species_1)
                    if species_2 not in added_node:
                        if taxon_file:
                            G.add_node(taxon_named_species[species_2],
                                       note=key_species_types[species_2])
                        else:
                            G.add_node(species_2,
                                       note=key_species_types[species_2])
                        added_node.append(species_2)
                    combination_species = '_'.join(
                        sorted([species_1, species_2]))
                    if combination_species not in species_weight:
                        species_weight[combination_species] = 1
                    else:
                        species_weight[combination_species] += 1
                    if taxon_file:
                        G.add_edge(taxon_named_species[species_1],
                                   taxon_named_species[species_2],
                                   weight=species_weight[combination_species])
                    else:
                        G.add_edge(species_1,
                                   species_2,
                                   weight=species_weight[combination_species])
            elif len(dicti['enum_bacteria'][sol]) == 1:
                species_1 = dicti['enum_bacteria'][sol][0]
                if species_1 not in added_node:
                    if taxon_file:
                        G.add_node(taxon_named_species[species_1],
                                   note=key_species_types[species_1])
                    else:
                        G.add_node(species_1,
                                   note=key_species_types[species_1])
                    added_node.append(species_1)

        # Check if all the nodes of G are not isolates.
        if len(G.nodes) == nx.number_of_isolates(G):
            logger.critical(
                r'/!\ Warning: All the nodes of the solution graph are isolated (they are not connected to other nodes). This lead to powergrasp creating san empty powergraph.'
            )
            logger.critical(
                'So m2m_analysis stops at the solution graph step.')
            sys.exit(1)

        miscoto_stat_output_datas.append([
            target_category,
            str(len_target[target_category]),
            str(len_min_sol[target_category]),
            str(len_union[target_category]),
            str(len_intersection[target_category]),
            str(len_solution[target_category])
        ])
        logger.info('######### Graph of ' + target_category + ' #########')
        logger.info('Number of nodes: ' + str(G.number_of_nodes()))
        logger.info('Number of edges: ' + str(G.number_of_edges()))
        nx.write_gml(G, target_output_gml_path)

    with open(miscoto_stat_output, 'w') as stats_output:
        statswriter = csv.writer(stats_output, delimiter="\t")
        statswriter.writerow([
            'categories', 'nb_target', 'size_min_sol', 'size_union',
            'size_intersection', 'size_enum'
        ])
        for miscoto_stat_output_data in miscoto_stat_output_datas:
            statswriter.writerow(miscoto_stat_output_data)

    with open(key_species_json, 'w') as json_output:
        json.dump(key_species_data, json_output, indent=4)

    with open(key_species_stats_output, 'w') as key_stat_output:
        key_stats_writer = csv.writer(key_stat_output, delimiter='\t')
        if all_taxons:
            key_stats_writer.writerow(
                ['target_categories', 'key_group', *sorted(all_taxons), 'Sum'])
        else:
            key_stats_writer.writerow(
                ['target_categories', 'key_group', 'data', 'Sum'])
        for target in key_species_data:
            if all_taxons:
                essential_counts = [
                    len(key_species_data[target]['essential_symbionts'][taxon])
                    for taxon in sorted(all_taxons)
                ]
                alternative_counts = [
                    len(key_species_data[target]['alternative_symbionts']
                        [taxon]) for taxon in sorted(all_taxons)
                ]
            else:
                essential_counts = [
                    len(key_species_data[target]['essential_symbionts']
                        ['data'])
                ]
                alternative_counts = [
                    len(key_species_data[target]['alternative_symbionts']
                        ['data'])
                ]
            key_counts = list(map(add, essential_counts, alternative_counts))
            key_stats_writer.writerow(
                [target, 'key_species', *key_counts,
                 sum(key_counts)])
            key_stats_writer.writerow([
                target, 'essential_symbionts', *essential_counts,
                sum(essential_counts)
            ])
            key_stats_writer.writerow([
                target, 'alternative_symbionts', *alternative_counts,
                sum(alternative_counts)
            ])
 def nr_isolated_nodes(self):
     return nx.number_of_isolates(self.graph)
Beispiel #14
0
def test_number_of_isolates():
    G = nx.Graph()
    G.add_edge(0, 1)
    G.add_nodes_from([2, 3])
    assert_equal(nx.number_of_isolates(G), 2)
Beispiel #15
0
 def print(graph, precision):
     if nx.number_of_isolates(graph) < 1:
         return Utils.print_set(
             nx.algorithms.covering.min_edge_cover(graph), precision)
     else:
         return "Graph has isolate vertice."
Beispiel #16
0
def stress(G,
           output_folder,
           fI,
           x_constraint=None,
           y_constraint=None,
           weight_threshold=0):
    # remove weak edges
    G2 = G.copy()
    for ij in G.edges:
        if G.edges[ij]['weight'] < weight_threshold:
            G2.remove_edge(*ij)

    # Make another copy
    G3 = G2.copy()
    # Check if any nodes have been isolated
    if nx.number_of_isolates(G2) != 0:
        # First remove nodes from x and y constraints
        for i in range(0, len(G2)):
            # Check if node is isolated
            if nx.is_isolate(G2, i):
                # Then check if node has x and y constraints
                if i in x_constraint:
                    del x_constraint[i]
                if i in y_constraint:
                    del y_constraint[i]

        # Make iterator over the isolates
        iso = nx.isolates(G2)
        # Use to remove nodes from graph
        G3.remove_nodes_from(iso)
        # Mapping should be empty initially
        mapping = {}
        # Counter
        k = 0
        # Loop over
        for i in range(0, len(G2)):
            # If isolate found
            if nx.is_isolate(G2, i):
                # Increment counter
                k = k + 1
            else:
                # Otherwise save to mapping
                mapping[i] = i - k
        # Use to relabel nodes and constraints
        G3 = nx.relabel_nodes(G3, mapping)
        # Loop over keys in mapping
        for i in mapping:
            # Check if key exists in constraints
            if i in x_constraint:
                x_constraint[mapping[i]] = x_constraint.pop(i)
            if i in y_constraint:
                y_constraint[mapping[i]] = y_constraint.pop(i)

    # length has decreased by 1 => This isn't the problem
    # compute layout
    X = _sgd(G3, y_constraint=y_constraint, x_constraint=x_constraint)

    # draw with colours
    cols_node = list(nx.get_node_attributes(G3, 'color').values())
    cols_edge = list(nx.get_edge_attributes(G3, 'color').values())
    widths_edge = list(nx.get_edge_attributes(G3, 'width').values())
    # Extract x and y labels of nodes
    xvs = nx.get_node_attributes(G3, 'x')
    yvs = nx.get_node_attributes(G3, 'y')
    # Make a copy of xvs to use as labels
    labels = yvs.copy()
    # Vector containing alphabet
    ab = list(string.ascii_lowercase)
    # Loop over x values
    for i in yvs:
        # Check if node is a strain
        if xvs[i] == 1.0:
            labels[i] = ab[yvs[i] - 1]

    nx.draw(G3,
            pos=X,
            node_color=cols_node,
            edge_color=cols_edge,
            width=widths_edge,
            labels=labels,
            arrows=False)
    plt.axis('equal')
    if weight_threshold != 0.0:
        plt.savefig(f'{output_folder}/{G3.graph["name"]}_stress_prun{fI}.png')
    else:
        plt.savefig(f'{output_folder}/{G3.graph["name"]}_stress{fI}.png')
    plt.close()
def min_edge_cover(G, matching_algorithm=None):
    """Returns a set of edges which constitutes
    the minimum edge cover of the graph.

    A smallest edge cover can be found in polynomial time by finding
    a maximum matching and extending it greedily so that all nodes
    are covered.

    Parameters
    ----------
    G : NetworkX graph
        An undirected bipartite graph.

    matching_algorithm : function
        A function that returns a maximum cardinality matching in a
        given bipartite graph. The function must take one input, the
        graph ``G``, and return a dictionary mapping each node to its
        mate. If not specified,
        :func:`~networkx.algorithms.bipartite.matching.hopcroft_karp_matching`
        will be used. Other possibilities include
        :func:`~networkx.algorithms.bipartite.matching.eppstein_matching`,
        or matching algorithms in the
        :mod:`networkx.algorithms.matching` module.

    Returns
    -------
    min_cover : set

        It contains all the edges of minimum edge cover
        in form of tuples. It contains both the edges `(u, v)` and `(v, u)`
        for given nodes `u` and `v` among the edges of minimum edge cover.

    Notes
    -----
    An edge cover of a graph is a set of edges such that every node of
    the graph is incident to at least one edge of the set.
    The minimum edge cover is an edge covering of smallest cardinality.

    Due to its implementation, the worst-case running time of this algorithm
    is bounded by the worst-case running time of the function
    ``matching_algorithm``.

    Minimum edge cover for bipartite graph can also be found using the
    function present in :mod:`networkx.algorithms.bipartite.covering`
    """
    if nx.number_of_isolates(G) > 0:
        # ``min_cover`` does not exist as there is an isolated node
        raise nx.NetworkXException(
            "Graph has a node with no edge incident on it, "
            "so no edge cover exists.")
    if matching_algorithm is None:
        matching_algorithm = partial(nx.max_weight_matching,
                                     maxcardinality=True)
    maximum_matching = matching_algorithm(G)
    # ``min_cover`` is superset of ``maximum_matching``
    try:
        min_cover = set(
            maximum_matching.items())  # bipartite matching case returns dict
    except AttributeError:
        min_cover = maximum_matching
    # iterate for uncovered nodes
    uncovered_nodes = set(G) - set([v for u, v in min_cover]) - set(
        [u for u, v in min_cover])
    for v in uncovered_nodes:
        # Since `v` is uncovered, each edge incident to `v` will join it
        # with a covered node (otherwise, if there were an edge joining
        # uncovered nodes `u` and `v`, the maximum matching algorithm
        # would have found it), so we can choose an arbitrary edge
        # incident to `v`. (This applies only in a simple graph, not a
        # multigraph.)
        u = arbitrary_element(G[v])
        min_cover.add((u, v))
        min_cover.add((v, u))
    return min_cover
Beispiel #18
0
def curve_fit_plot(G,
                   nrows=3,
                   ncols=3,
                   size=(15, 15),
                   nbins=12,
                   start_cutoff=0.1,
                   end_cutoff=0.3):
    """
	Plot in log-log scale nrows*ncols degree distributions fitted with curve_fit function
	
	The function fitted is: a*np.power(x,-b)
	
	a,b reported on each plot
	
	Return dataframe with statistical summary
	"""

    cut_off = np.linspace(start_cutoff, end_cutoff, nrows * ncols)

    _, plot = plt.subplots(nrows=nrows, ncols=ncols, figsize=size)
    subplots = plot.reshape(1, nrows * ncols)[0]

    stats = collections.defaultdict(list)
    fit = collections.defaultdict(list)

    for i, subplot in enumerate(subplots):

        F = slice_network(G, cut_off[i])

        stats['nodes'].append(len(F.nodes()))
        stats['edges'].append(len(F.edges()))
        stats['density'].append(nx.density(F))
        stats['isolates'].append(nx.number_of_isolates(F))

        F.remove_nodes_from(list(nx.isolates(F)))

        stats['nodes'].append(len(F.nodes()))
        stats['edges'].append(len(F.edges()))
        stats['density'].append(nx.density(F))
        stats['isolates'].append(nx.number_of_isolates(F))

        data = list(dict(nx.degree(F)).values())
        d, f = get_degree_dist(F)

        bins, dens = binned_values(F, nbins)

        powerlaw.plot_pdf(data,
                          linestyle='-.',
                          c='b',
                          label='fit log-bins',
                          alpha=0.6,
                          ax=subplot)
        subplot.scatter(bins, dens, s=10, c='b', label='binned data')
        subplot.scatter(d, f, s=10, c='r', alpha=0.3, label='data')
        subplot.set_xscale('log')
        subplot.set_yscale('log')
        subplot.title.set_text('Weight - slice: {}'.format(round(
            cut_off[i], 2)))
        subplot.set_ylim((min(f) - 10e-4, max(f) + 10e-1))
        subplot.set_xlim((min(d), max(d) + 200))
        subplot.legend()
        subplot.set_xlabel('Degree')
        subplot.set_ylabel('freq')

    ir = ['F', 'T']
    cf = np.sort(np.append(cut_off, cut_off))

    l = [(c, ir[i % 2]) for i, c in enumerate(cf)]

    df = pd.DataFrame(stats,
                      index=pd.MultiIndex.from_tuples(
                          l, names=['Weight', 'isolates removed']))

    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)

    return df
Beispiel #19
0
    return exact_bet_dict


# Param
num_of_graphs = 50
graph_types = ["ER", "SF", "GRP"]

for graph_type in graph_types:
    print("###################")
    print(f"Generating graph type : {graph_type}")
    print(f"Number of graphs to be generated:{num_of_graphs}")
    list_bet_data = list()
    print("Generating graphs and calculating centralities...")
    for i in range(num_of_graphs):
        print(f"Graph index:{i+1}/{num_of_graphs}", end='\r')
        g_nx = create_graph(graph_type)
        if nx.number_of_isolates(g_nx) > 0:
            g_nx.remove_nodes_from(list(nx.isolates(g_nx)))
            g_nx = nx.convert_node_labels_to_integers(g_nx)
        g_nkit = nx2nkit(g_nx)
        bet_dict = cal_exact_bet(g_nkit)
        list_bet_data.append([g_nx, bet_dict])

    fname = "./graphs/" + graph_type + ".pickle"

    with open(fname, "wb") as fopen:
        pickle.dump(list_bet_data, fopen)
    print("")
    print("Graphs saved")

print("End.")
Beispiel #20
0
            #Randomly add/remove edge and nodes.
            x = random.uniform(0, 1)
            if x > 0.5:
                if len(list(clone.edges)) == 0:
                    error = 1
                    break
                else:
                    node_1, node_2 = random.choice(list(clone.edges))
                    counter = counter + 1
                    if graph.has_edge(node_1, node_2):
                        clone.remove_edge(node_1, node_2)
            else:
                node_1 = random.choice(clone.nodes())
                node_2 = random.choice(clone.nodes())
                if node_1 != node_2 and not clone.has_edge(
                        node_1, node_2) and not graph.has_edge(node_1, node_2):
                    clone.add_edge(node_1, node_2)
                    counter = counter + 1
    if error == 0:
        #try:
        isolate = nx.isolates(clone)
        print(nx.number_of_isolates(clone))
        print("Added")
        if len(clone) == 0 or len(graph) == 0:
            continue
        if nx.is_connected(clone) and nx.is_connected(graph):
            index = transfomer(graph, clone, vals, index)
    #except:
        print("Error")
        continue
Beispiel #21
0

import networkx as nx
G = nx.DiGraph() #graph constructor
for u in range(len(a_links)): #scan the matrix table
    G.add_node(a_links[u]) #add all strings from a_links table as nodes
    for v in range(len(a_links)):
        if matrix[u][v] == 1: #if the is a non zero cell
            G.add_edge(a_links[u],a_links[v]) #make an edge between the urls2 article and the correspondant a_lists article
                


# In[5]:


nx.number_of_isolates(G)


# In[28]:


p = nx.clustering(G)
import operator
sorted_cluster_vl = sorted(p.items(), key = operator.itemgetter(1))
print(sorted_cluster_vl)


# In[29]:


c = nx.degree_centrality(G)
Beispiel #22
0
 def calculate(graph):
     if nx.number_of_isolates(graph) < 1 and nx.number_of_nodes(graph) > 1:
         return len(nx.algorithms.covering.min_edge_cover(graph))
     else:
         return 10**10
Beispiel #23
0
print("The number of nodes for SGI graph is: ", len(G_SGI.nodes))
print("The number of links for SGI graph is: ", len(G_SGI.edges))
# Intersection interactome
print("The number of nodes for I graph is: ", len(G_I.nodes))
print("The number of links for I graph is: ", len(G_I.edges))
# Union interactome
print("The number of nodes for U graph is: ", len(G_U.nodes))
print("The number of links for U graph is: ", len(G_U.edges))

# B. Number of Connected components
print("The number of connected components for SGI graph is: ", nx.number_connected_components(G_SGI))
print("The number of connected components for I graph is: ", nx.number_connected_components(G_I))
print("The number of connected components for U graph is: ", nx.number_connected_components(G_U))

# C. Number of isolated nodes
print("The number of isolated nodes for SGI graph is: ", nx.number_of_isolates(G_SGI))
print("The number of isolated nodes for I graph is: ", nx.number_of_isolates(G_I))
print("The number of isolated nodes for U graph is: ", nx.number_of_isolates(G_U))

# D. Average Path length

# SGI
PathLenSGI=[]
for g in nx.connected_component_subgraphs(G_SGI):
    PathLenSGI.append(nx.average_shortest_path_length(g)) 
print("The average path length for SGI graph is: ", PathLenSGI)
print(sum(PathLenSGI)/len(PathLenSGI))
# I
PathLenI=[]
for g in nx.connected_component_subgraphs(G_I):
    PathLenI.append(nx.average_shortest_path_length(g)) 
Beispiel #24
0
    def __init__(self, configs: Union[Configuration, dict, nx.DiGraph]):
        if type(configs) == nx.DiGraph:  # Assume we're creating a copy
            super().__init__(configs)
            return
        elif type(configs) == dict:
            configs = SmallWorldTopology.Configuration(**configs)

        super().__init__()
        self.__dict__.update(asdict(configs))

        assert (
            len(self.minicolumn_shape) == 3
        ), "Minicolumn shape must be of dimension 3 (3D)"
        assert (
            len(self.macrocolumn_shape) == 3
        ), "Macrocolumn shape must be of dimension 3 (3D)"

        # Initial neuron positions (all separated by neuron_spacing)
        i, j, k = np.multiply(self.macrocolumn_shape, self.minicolumn_shape)
        grid = np.mgrid[:i, :j, :k].reshape(3, -1)
        x, y, z = grid * self.neuron_spacing

        # Adding minicolumnSpacing (from random to small world topology)
        if self.minicolumn_spacing > 0:
            for d in range(3):  # For each dimension
                grid[d] //= self.minicolumn_shape[d]
            x += grid[0] * self.minicolumn_spacing
            y += grid[1] * self.minicolumn_spacing
            z += grid[2] * self.minicolumn_spacing

        positions = map(lambda p: {"position": p}, zip(x, y, z))
        self.add_nodes_from(zip(range(len(x)), positions))

        # Distance-based random connectivity
        positions = np.stack(np.asarray(self.nodes.data("position"))[:, 1])

        if (
            self.sparse_init
        ):  # Slower but iterative (for adjacency matrices that don't fit in memory)
            distances = pairwise_distances_chunked(
                positions,
                metric="euclidean",
                n_jobs=-1,
                reduce_func=lambda chunk, start: bsr_matrix(
                    np.random.random(chunk.shape)
                    < self.p_max * np.exp(-chunk / self.intracolumnar_sparseness)
                ),
                working_memory=self.mem_available,
            )
            adjacency_matrix = vstack(list(distances))
            adjacency_matrix.setdiag(0)  # Avoid self-connections
            self.add_edges_from(zip(*adjacency_matrix.nonzero()))
        else:
            distances = cdist(positions, positions, "euclidean")
            probabilities = self.p_max * np.exp(
                -distances / self.intracolumnar_sparseness
            )
            np.fill_diagonal(probabilities, 0)  # Avoid self-connections
            rand_matrix = np.random.random(probabilities.shape)
            i, j = np.nonzero(rand_matrix < probabilities)
            self.add_edges_from(zip(i, j))

        n_neurons = self.number_of_nodes()
        self.inhibitory_neurons = set(
            np.random.permutation(n_neurons)[: int(n_neurons * self.inhibitory_prob)]
        )

        for u, v in self.edges:
            if u in self.inhibitory_neurons:
                self.edges[u, v]["weight"] = -np.random.uniform(
                    *self.inhibitory_init_weight_range
                )
            else:
                self.edges[u, v]["weight"] = np.random.uniform(
                    *self.excitatory_init_weight_range
                )

        if self.spectral_radius_norm:
            spectral_radius = lambda matrix: np.max(np.abs(np.linalg.eigvals(matrix)))
            adj = nx.adjacency_matrix(self, weight="weight").todense()
            scale = 1.0 / spectral_radius(np.abs(adj))

            for i, (u, v) in enumerate(self.edges):
                self.edges[u, v]["weight"] = self.edges[u, v]["weight"] * scale

        if _logger.isEnabledFor(logging.INFO):
            # Some extra info about the topology
            out_degrees = np.array(self.out_degree())[:, 1]
            reporter.log_metrics(
                {
                    "number-of-neurons": n_neurons,
                    "number-of-synapses": self.number_of_edges(),
                    "excitatory-ratio": 100.0
                    * (1.0 - len(self.inhibitory_neurons) / n_neurons),
                    "avg-out-degree": np.mean(out_degrees),
                    "nb-out-degree-0": len(out_degrees) - np.count_nonzero(out_degrees),
                    "nb-isolates": nx.number_of_isolates(self),
                }
            )
import math
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

n = 150
g = nx.fast_gnp_random_graph(n, 0.02)
nx.draw_random(g)
##
##giant = sorted(nx.connected_component_subgraphs(g),key = len,reverse = True)
giant = max(nx.connected_component_subgraphs(g), key=len)
nx.draw_random(giant)
#%%
g_node = nx.number_of_nodes(g)
giant_node = nx.number_of_nodes(giant)
isolate_node = nx.number_of_isolates(g)
print(g_node, "\n")
print(giant_node, "\n")
print(isolate_node, "\n")
prob_g = math.log(g_node) / g_node
print(prob_g)
#%%
#g = nx.fast_gnp_random_graph(100,prob_isolate-0.1)
#nx.draw_random(g)
prob_g = math.log(g_node) / g_node
p = []
for i in range(4):
    prob_g = prob_g - 0.03
    p.append(prob_g)
prob_g1 = math.log(g_node) / g_node
for i in range(4):
Beispiel #26
0
def min_edge_cover(G, matching_algorithm=None):
    """Returns a set of edges which constitutes
    the minimum edge cover of the graph.

    A smallest edge cover can be found in polynomial time by finding
    a maximum matching and extending it greedily so that all nodes
    are covered.

    Parameters
    ----------
    G : NetworkX graph
        An undirected bipartite graph.

    matching_algorithm : function
        A function that returns a maximum cardinality matching in a
        given bipartite graph. The function must take one input, the
        graph ``G``, and return a dictionary mapping each node to its
        mate. If not specified,
        :func:`~networkx.algorithms.bipartite.matching.hopcroft_karp_matching`
        will be used. Other possibilities include
        :func:`~networkx.algorithms.bipartite.matching.eppstein_matching`,
        or matching algorithms in the
        :mod:`networkx.algorithms.matching` module.

    Returns
    -------
    min_cover : set

        It contains all the edges of minimum edge cover
        in form of tuples. It contains both the edges `(u, v)` and `(v, u)`
        for given nodes `u` and `v` among the edges of minimum edge cover.

    Notes
    -----
    An edge cover of a graph is a set of edges such that every node of
    the graph is incident to at least one edge of the set.
    The minimum edge cover is an edge covering of smallest cardinality.

    Due to its implementation, the worst-case running time of this algorithm
    is bounded by the worst-case running time of the function
    ``matching_algorithm``.

    Minimum edge cover for bipartite graph can also be found using the
    function present in :mod:`networkx.algorithms.bipartite.covering`
    """
    if nx.number_of_isolates(G) > 0:
        # ``min_cover`` does not exist as there is an isolated node
        raise nx.NetworkXException(
            "Graph has a node with no edge incident on it, "
            "so no edge cover exists.")
    if matching_algorithm is None:
        matching_algorithm = partial(nx.max_weight_matching,
                                     maxcardinality=True)
    maximum_matching = matching_algorithm(G)
    # ``min_cover`` is superset of ``maximum_matching``
    min_cover = set(maximum_matching.items())
    # iterate for uncovered nodes
    uncovered_nodes = set(G) - {v for u, v in min_cover}
    for v in uncovered_nodes:
        # Since `v` is uncovered, each edge incident to `v` will join it
        # with a covered node (otherwise, if there were an edge joining
        # uncovered nodes `u` and `v`, the maximum matching algorithm
        # would have found it), so we can choose an arbitrary edge
        # incident to `v`. (This applies only in a simple graph, not a
        # multigraph.)
        u = arbitrary_element(G[v])
        min_cover.add((u, v))
        min_cover.add((v, u))
    return min_cover
Beispiel #27
0
    g = nx.fast_gnp_random_graph(n, p)
    pos = layout(G)
    region += 1
    plt.subplot(region)
    plt.title(p)
    nx.draw(g, pos, node_size=15)
    all_components = sorted(nx.connected_component_subgraphs(g),
                            key=len,
                            reverse=True)
    giant = all_components[0]
    total_nodes = nx.number_of_nodes(g)
    giant_nodes = nx.number_of_nodes(giant)

    print("\nP =", p, "\nNumber of nodes in original graph :", total_nodes)
    print("Nodes in giant component =", giant_nodes)
    print('Number of isolates are :', nx.number_of_isolates(g))
#%%
#giant component
n = 150

pc = 1 / n
pmax = math.log(n) / float(n)

pvals = [pc, pmax]
region = 220
for pv in pvals:

    g = nx.fast_gnp_random_graph(n, pv)
    h = max(nx.connected_component_subgraphs(g), key=len)

    #plt.subplots_adjust(left=0,right=1,bottom=0,top=0.95,wspace=0.1,hspace=0.1)