Ejemplo n.º 1
0
def check_example_camerini1980ranking(
    solver: rank.DescendSpanningArborescences,
    attr: str,
    attr_label: Union[str, None],
):
    """Test the example in [camerini1980ranking]_ for ranking SAs.

    Args:
        solver: the generator to return SAs.
        attr: the edge attribute used to in determining optimality.
        attr_label: the edge attribute used as unique ID for edge.
    """
    assert solver.msa.size(weight=attr) == 28

    res2 = next(solver)[0]
    assert res2.size(weight=attr) == 26

    res3 = next(solver)[0]
    assert res3.size(weight=attr) == 19

    res4 = next(solver)[0]
    assert res4.size(weight=attr) == 17

    res5 = next(solver)[0]
    assert res5.size(weight=attr) == 15

    if attr_label:
        labels = lambda g: get_labels(g, attr_label)
        assert labels(solver.msa) == {"e1", "e3", "e5"}
        assert labels(res2) == {"e1", "e5", "e6"}
        assert labels(res3) == {"e2", "e4", "e6"}
        assert labels(res4) == {"e1", "e2", "e3"}
        assert labels(res5) == {"e1", "e2", "e6"}

    assert nx.is_frozen(solver.raw)
Ejemplo n.º 2
0
def test_ascend_sa(dg: nx.MultiDiGraph):
    """Check the generator to ascend spanning arborescences."""
    solver = rank.AscendSpanningArborescences(dg,
                                              root="b1",
                                              attr=_ATTR,
                                              attr_label=_ATTR_LABEL)
    assert solver.msa.size(weight=_ATTR) == 15
    assert (solver.msa.edges["b1", "b2"]["weight"] == 6
            and solver.msa.edges["b1", "b4"]["weight"] == 1
            and solver.msa.edges["b4", "b3"]["weight"] == 8)

    res2 = next(solver)[0]
    assert res2.size(weight=_ATTR) == 17

    res3 = next(solver)[0]
    assert res3.size(weight=_ATTR) == 19

    res4 = next(solver)[0]
    assert res4.size(weight=_ATTR) == 26

    res5 = next(solver)[0]
    assert res5.size(weight=_ATTR) == 28

    # Check if all the SAs are correct.
    labels = lambda x: get_labels(x, _ATTR_LABEL)
    assert labels(res5) == {"e1", "e3", "e5"}
    assert labels(res4) == {"e1", "e5", "e6"}
    assert labels(res3) == {"e2", "e4", "e6"}
    assert labels(res2) == {"e1", "e2", "e3"}
    assert labels(solver.msa) == {"e1", "e2", "e6"}

    assert nx.is_frozen(solver.raw)
Ejemplo n.º 3
0
    def pformat(self):
        """Pretty formats your graph into a string.

        This pretty formatted string representation includes many useful
        details about your graph, including; name, type, frozeness, node count,
        nodes, edge count, edges, graph density and graph cycles (if any).
        """
        lines = []
        lines.append("Name: %s" % self.name)
        lines.append("Type: %s" % type(self).__name__)
        lines.append("Frozen: %s" % nx.is_frozen(self))
        lines.append("Nodes: %s" % self.number_of_nodes())
        for n in self.nodes_iter():
            lines.append("  - %s" % n)
        lines.append("Edges: %s" % self.number_of_edges())
        for (u, v, e_data) in self.edges_iter(data=True):
            if e_data:
                lines.append("  %s -> %s (%s)" % (u, v, e_data))
            else:
                lines.append("  %s -> %s" % (u, v))
        lines.append("Density: %0.3f" % nx.density(self))
        cycles = list(nx.cycles.recursive_simple_cycles(self))
        lines.append("Cycles: %s" % len(cycles))
        for cycle in cycles:
            buf = six.StringIO()
            buf.write("%s" % (cycle[0]))
            for i in range(1, len(cycle)):
                buf.write(" --> %s" % (cycle[i]))
            buf.write(" --> %s" % (cycle[0]))
            lines.append("  %s" % buf.getvalue())
        return os.linesep.join(lines)
Ejemplo n.º 4
0
    def clean_up(self):
        if nx.is_frozen(self.G):
            return

        if self.use_virtual_nodes:
            if self.verbose > 0:
                print("Remove virtual nodes")

                print("\nCurrent graph:")
                print("Nodes: {}".format(self.G.number_of_nodes()))
                print("Edges: {}".format(self.G.number_of_edges()))

            self.G.remove_nodes_from(self.virtual_nodes)
            self.assignments = np.delete(self.assignments, self.virtual_nodes)
            self.fixed = np.delete(self.fixed, self.virtual_nodes)

            if self.verbose > 0:
                print("\nVirtual nodes removed:")
                print("Nodes: {}".format(self.G.number_of_nodes()))
                print("Edges: {}".format(self.G.number_of_edges()))

        # Add partition attribute to nodes
        for i in range(0, len(self.assignments)):
            self.G.add_nodes_from([i], partition=str(self.assignments[i]))

        # Remove original node/edge weights
        for node in self.G.nodes_iter(data=True):
            if 'weight_orig' in node[1]:
                del node[1]['weight_orig']
        for edge in self.G.edges_iter(data=True):
            if 'weight_orig' in edge[2]:
                del edge[2]['weight_orig']

        # Freeze Graph from further modification
        self.G = nx.freeze(self.G)
Ejemplo n.º 5
0
    def _remove_alarms_of_other_projects(self, graph, current_project_id,
                                         is_admin_project):
        """Removes wrong alarms from the graph

        Removes alarms of other tenants from the graph, In case the tenant is
        admin then project_id can also be None.

        :type graph: NXGraph
        :type current_project_id: string
        :type is_admin_project: boolean
        """
        alarms_to_remove = []
        for alarm in graph.get_vertices(query_dict=base.ALARMS_ALL_QUERY):
            if not alarm.get(VProps.PROJECT_ID, None):
                cat_filter = {VProps.VITRAGE_CATEGORY: EntityCategory.RESOURCE}
                resource_neighbors = \
                    self.entity_graph.neighbors(alarm.vertex_id,
                                                vertex_attr_filter=cat_filter)
                if len(resource_neighbors) > 0:
                    resource_proj_id = \
                        resource_neighbors[0].get(VProps.PROJECT_ID, None)
                    cond1 = is_admin_project and resource_proj_id and \
                        resource_proj_id != current_project_id
                    cond2 = not is_admin_project and \
                        (not resource_proj_id or
                         resource_proj_id != current_project_id)
                    if cond1 or cond2:
                        alarms_to_remove.append(alarm)

        if alarms_to_remove and is_frozen(graph._g):
            graph._g = graph._g.copy()

        for alarm in alarms_to_remove:
            graph.remove_vertex(alarm)
Ejemplo n.º 6
0
def pformat(graph):
    """Pretty formats your graph into a string representation that includes
    details about your graph, including; name, type, frozeness, node count,
    nodes, edge count, edges, graph density and graph cycles (if any).
    """
    lines = []
    lines.append("Name: %s" % graph.name)
    lines.append("Type: %s" % type(graph).__name__)
    lines.append("Frozen: %s" % nx.is_frozen(graph))
    lines.append("Nodes: %s" % graph.number_of_nodes())
    for n in graph.nodes_iter():
        lines.append("  - %s" % n)
    lines.append("Edges: %s" % graph.number_of_edges())
    for (u, v, e_data) in graph.edges_iter(data=True):
        if e_data:
            lines.append("  %s -> %s (%s)" % (u, v, e_data))
        else:
            lines.append("  %s -> %s" % (u, v))
    lines.append("Density: %0.3f" % nx.density(graph))
    cycles = list(nx.cycles.recursive_simple_cycles(graph))
    lines.append("Cycles: %s" % len(cycles))
    for cycle in cycles:
        buf = six.StringIO()
        buf.write(str(cycle[0]))
        for i in range(1, len(cycle)):
            buf.write(" --> %s" % (cycle[i]))
        buf.write(" --> %s" % (cycle[0]))
        lines.append("  %s" % buf.getvalue())
    return "\n".join(lines)
Ejemplo n.º 7
0
    def append_loci(self, loci, **kwargs):
        if nx.is_frozen(self.DAG):
            raise AttributeError('The structure has been fixed')

        name = loci.Name
        if name in self.DAG:
            if not isinstance(self.DAG.nodes[name]['loci'], ExoValueLoci):
                raise KeyError('Duplicated variable name')

        self.DAG.add_node(name, loci=loci, **kwargs)
        self.DAG.remove_in_edges(name)

        new_pa = list()
        for pa in loci.Parents:
            if pa not in self.DAG:
                self.append_loci(ExoValueLoci(pa))
                new_pa.append(pa)
            self.DAG.add_edge(pa, name)

        # Check acyclic or not
        if not self.DAG.check_acyclic():
            self.DAG.remove_node(name)
            for pa in new_pa:
                self.DAG.remove_node(pa)
            raise AttributeError('The node causes cyclic paths')
Ejemplo n.º 8
0
def recurse(G, comm_limit):  # RECURSIVE CALLS TO FORM COMUNITIES
    if comm_limit <= 0:
        return
    else:
        comm_limit -= 1

    key, value = comparator(G)
    if value == -1:
        return
    print '\n', "MAX_EDGE_BETWEENESS_FOUND_FOR:", key
    all_comms = communities(G, key)
    print "COMMUNITIES_FOUND_DUE_TO_SPLIT_THROUGH:", key, " ; SPLIT_LEVEL:", comm_limit + 1, '\n'
    Graph_ar = []
    das = 1
    for i in all_comms:
        print "COMMUNITY ", das, ' : ', i
        das += 1
        Graph_ar.append(G.subgraph(i))

    for i in Graph_ar:
        if len(i) <= 1:
            continue
        j = nx.Graph(i)
        if nx.is_frozen(j):
            print "FROZEN_ERROR"
            continue
        recurse(j, comm_limit)
Ejemplo n.º 9
0
    def apply_edge_attr_filter(graph, edge_attr_filter):
        edges = graph._g.edges(data=True, keys=True)
        edges_to_remove = [(u, v, k) for (u, v, k, d) in edges
                           if not check_filter(d, edge_attr_filter)]

        if edges_to_remove and is_frozen(graph._g):
            graph._g = graph._g.copy()

        for source, target, key in edges_to_remove:
            graph._g.remove_edge(u=source, v=target, key=key)
Ejemplo n.º 10
0
def test_descend_sa(dg: nx.MultiDiGraph):
    """Check the generator to descend spanning arborescences.

    Args:
        dg: case in [camerini1980ranking]_ with 4 buses and 6 edges.
    """
    solver = rank.DescendSpanningArborescences(dg,
                                               root="b1",
                                               attr=_ATTR,
                                               attr_label=_ATTR_LABEL)
    check_example_camerini1980ranking(solver, _ATTR, _ATTR_LABEL)
    assert not nx.is_frozen(dg)
Ejemplo n.º 11
0
 def __resGraphs(self, residualEdges):
     nds = []
     for e in residualEdges:
         nds += list(e)
     g2 = self.G.subgraph(set(nds))
     assert nx.is_frozen(g2)
     # To “unfreeze” a graph you must make a copy by creating a new graph object:
     g2 = nx.DiGraph(g2)
     edges_should_be_removed = []
     for e in g2.edges:
         if e not in residualEdges:
             edges_should_be_removed.append(e)
     g2.remove_edges_from(edges_should_be_removed)
     return g2
Ejemplo n.º 12
0
 def shave(self, seed_cpds):
     '''
     shave off nodes that do not connect seeds, i.e.
     any node with degree = 1 and is not a seed, iteratively.
     '''
     if nx.is_frozen(self.graph):
         gg = self.graph
         self.graph = gg  ## not sure why graph gets frozen but unfreeze it
     nonseeds = [x for x in self.graph.nodes() if x not in seed_cpds]
     excessive = [x for x in nonseeds if self.graph.degree(x) == 1]
     while excessive:
         for x in excessive:
             self.graph.remove_node(x)
         nonseeds = [x for x in self.graph.nodes() if x not in seed_cpds]
         excessive = [x for x in nonseeds if self.graph.degree(x) == 1]
Ejemplo n.º 13
0
def _common_format(g, edge_notation):
    lines = []
    lines.append("Name: %s" % g.name)
    lines.append("Type: %s" % type(g).__name__)
    lines.append("Frozen: %s" % nx.is_frozen(g))
    lines.append("Density: %0.3f" % nx.density(g))
    lines.append("Nodes: %s" % g.number_of_nodes())
    for n in g.nodes_iter():
        lines.append("  - %s" % n)
    lines.append("Edges: %s" % g.number_of_edges())
    for (u, v, e_data) in g.edges_iter(data=True):
        if e_data:
            lines.append("  %s %s %s (%s)" % (u, edge_notation, v, e_data))
        else:
            lines.append("  %s %s %s" % (u, edge_notation, v))
    return lines
Ejemplo n.º 14
0
def test_descend_sa_label_auto(dg_label: nx.DiGraph):
    """Check if the graph is labelled automatically.

    Note:
        ``label_`` is the default label name in the algorithm, no matter
        if there are existing attribute values. That is, if there are
        values for the ``label_`` attribute and ``attr_label`` is set to
        ``label_``, those values will be updated by automatically
        generated values.
    """
    solver = rank.DescendSpanningArborescences(
        dg_label,
        root="b1",
        attr=_ATTR,
        attr_label="label_",
    )
    assert solver.msa.size(weight=_ATTR) == 28
    check_example_camerini1980ranking(solver, _ATTR, None)
    assert not nx.is_frozen(dg_label)
Ejemplo n.º 15
0
def _common_format(g, edge_notation):
    lines = []
    lines.append("Name: %s" % g.name)
    lines.append("Type: %s" % type(g).__name__)
    lines.append("Frozen: %s" % nx.is_frozen(g))
    lines.append("Density: %0.3f" % nx.density(g))
    lines.append("Nodes: %s" % g.number_of_nodes())
    for n, n_data in g.nodes_iter(data=True):
        if n_data:
            lines.append("  - %s (%s)" % (n, n_data))
        else:
            lines.append("  - %s" % n)
    lines.append("Edges: %s" % g.number_of_edges())
    for (u, v, e_data) in g.edges_iter(data=True):
        if e_data:
            lines.append("  %s %s %s (%s)" % (u, edge_notation, v, e_data))
        else:
            lines.append("  %s %s %s" % (u, edge_notation, v))
    return lines
Ejemplo n.º 16
0
def getGraphDegree(graph):
    assert (nx.is_frozen(graph))
    # pinNeighbors = collections.defaultdict(lambda: []) #pinId : list(pint node degrees)
    # boardNeighbors = collections.defaultdict(lambda: []) #pinId : list(board node degrees)

    pinOut = collections.defaultdict(lambda: [])
    # boardOut = collections.defaultdict(int)

    for k, node in enumerate(list(graph.nodes())):
        if config.pin_token not in node: continue  #only count pins

        currPinList = []
        currBoardList = []

        for i, neighbor in enumerate(nx.all_neighbors(graph, node)):
            #this line of code shouldn't ever hit, keeping it here for sanity sake
            if config.board_token not in neighbor:
                assert (False)
                continue  #only count boards
            # boardNeighbors[node].append(graph.degree(pin_neighbors))
            currBoardList.append(graph.degree(neighbor))

            for j, pin_neighbors in enumerate(nx.all_neighbors(
                    graph, neighbor)):
                #this condition should also never trigger
                if config.pin_token not in pin_neighbors:
                    assert (False)
                    continue  #only count second degree pins
                currPinList.append(graph.degree(pin_neighbors))
                # pinNeighbors[node].append(graph.degree(pin_neighbors))

        currPinList = np.array(currPinList)
        currBoardList = np.array(currBoardList)
        pinOut[node] = [graph.degree(node),
            currPinList.mean(), currPinList.std(),\
             currBoardList.mean(), currBoardList.std()]

        # if k == 10:
        # break

    return pinOut
Ejemplo n.º 17
0
def pformat(graph):
    lines = []
    lines.append("Name: %s" % graph.name)
    lines.append("Type: %s" % type(graph).__name__)
    lines.append("Frozen: %s" % nx.is_frozen(graph))
    lines.append("Nodes: %s" % graph.number_of_nodes())
    for n in graph.nodes_iter():
        lines.append("  - %s" % n)
    lines.append("Edges: %s" % graph.number_of_edges())
    for (u, v, e_data) in graph.edges_iter(data=True):
        reason = e_data.get('reason', '??')
        lines.append("  %s -> %s (%s)" % (u, v, reason))
    cycles = list(nx.cycles.recursive_simple_cycles(graph))
    lines.append("Cycles: %s" % len(cycles))
    for cycle in cycles:
        buf = six.StringIO()
        buf.write(str(cycle[0]))
        for i in range(1, len(cycle)):
            buf.write(" --> %s" % (cycle[i]))
        buf.write(" --> %s" % (cycle[0]))
        lines.append("  %s" % buf.getvalue())
    return "\n".join(lines)
Ejemplo n.º 18
0
    def __init__(self,
                 graph: Graph,
                 assignment: List[int],
                 components: Optional[DefaultDict[int, Component]] = None,
                 component_scores: Optional[Dict[int, Dict[str,
                                                           float]]] = None):
        if not Chromosome.objectives:
            raise ClassNotInitializedException(Chromosome)

        if not is_frozen(graph):
            self._graph = freeze(graph)
        else:
            self._graph = graph

        if 'order' not in graph.graph:
            # require an order on the graph for consistency
            # order[i] = j implies that vertex i is located at index j in vertex_set
            self._graph.graph['order'] = {
                vertex: idx
                for idx, vertex in enumerate(self._graph)
            }

        self._assignment: List[int] = assignment
        self._components: Dict[int, Component] = defaultdict(set)
        self._component_scores: Dict[int, Dict[str, float]] = {}
        self._scores: List[float] = None

        if components:
            self._components = components
        else:
            self._rebuild_components()

        if component_scores:
            self._component_scores = component_scores
        else:
            self._compute_component_scores()

        self._compute_scores()
Ejemplo n.º 19
0
 def test_is_frozen(self):
     assert_equal(nx.is_frozen(self.G), False)
     G = nx.freeze(self.G)
     assert_equal(G.frozen, nx.is_frozen(self.G))
     assert_equal(G.frozen, True)
Ejemplo n.º 20
0
 def is_frozen(self):
     return nx.is_frozen(self.DAG)
Ejemplo n.º 21
0
 def test_is_frozen(self):
     assert not nx.is_frozen(self.G)
     G = nx.freeze(self.G)
     assert G.frozen == nx.is_frozen(self.G)
     assert G.frozen
Ejemplo n.º 22
0
def reroot_neuron(x, new_root, inplace=False):
    """ Reroot neuron to new root.

    Parameters
    ----------
    x :        CatmaidNeuron | CatmaidNeuronList
               List must contain a SINGLE neuron.
    new_root : int | str
               Node ID or tag of the node to reroot to.
    inplace :  bool, optional
               If True the input neuron will be rerooted.

    Returns
    -------
    CatmaidNeuron
               Rerooted neuron. Only if ``inplace=False``.

    See Also
    --------
    :func:`~pymaid.CatmaidNeuron.reroot`
                Quick access to reroot directly from CatmaidNeuron/List
                objects.

    Examples
    --------
    >>> n = pymaid.get_neuron(16)
    >>> # Reroot neuron to its soma
    >>> n2 = pymaid.reroot_neuron(n, n.soma)

    """

    if new_root is None:
        raise ValueError('New root can not be <None>')

    if isinstance(x, core.CatmaidNeuron):
        pass
    elif isinstance(x, core.CatmaidNeuronList):
        if x.shape[0] == 1:
            x = x.loc[0]
        else:
            raise Exception('{0} neurons provided. Please provide only '
                            'a single neuron!'.format(x.shape[0]))
    else:
        raise Exception('Unable to process data of type "{0}"'.format(type(x)))

    # If new root is a tag, rather than a ID, try finding that node
    if isinstance(new_root, str):
        if new_root not in x.tags:
            logger.error('#{}: Found no treenodes with tag {} - please double '
                         'check!'.format(x.skeleton_id, new_root))
            return
        elif len(x.tags[new_root]) > 1:
            logger.error('#{}: Found multiple treenodes with tag {} - please '
                         'double check!'.format(x.skeleton_id, new_root))
            return
        else:
            new_root = x.tags[new_root][0]

    if not inplace:
        x = x.copy()

    # Skip if new root is among the existing roots
    if any(x.root == new_root):
        if not inplace:
            return x
        else:
            return

    if x.igraph and config.use_igraph:
        # Prevent warnings in the following code - querying paths between
        # unreachable nodes will otherwise generate a runtime warning
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")

            # Find paths to all roots
            path = x.igraph.get_shortest_paths(x.igraph.vs.find(node_id=new_root),
                                               [x.igraph.vs.find(node_id=r) for r in x.root])
            epath = x.igraph.get_shortest_paths(x.igraph.vs.find(node_id=new_root),
                                                [x.igraph.vs.find(node_id=r) for r in x.root],
                                                output='epath')

        # Extract paths that actually worked (i.e. within a continuous fragment)
        path = [p for p in path if p][0]
        epath = [p for p in epath if p][0]

        edges = [(s, t) for s, t in zip(path[:-1], path[1:])]

        weights = [x.igraph.es[e]['weight'] for e in epath]

        # Get all weights and append inversed new weights
        all_weights = x.igraph.es['weight'] + weights

        # Add inverse edges: old_root->new_root
        x.igraph.add_edges([(e[1], e[0]) for e in edges])

        # Re-set weights
        x.igraph.es['weight'] = all_weights

        # Remove new_root->old_root
        x.igraph.delete_edges(edges)

        # Get degree of old root for later categorisation
        old_root_deg = len(x.igraph.es.select(_target=path[-1]))

        # Translate path indices to treenode IDs
        ix2id = {ix: n for ix, n in zip(x.igraph.vs.indices,
                                        x.igraph.vs.get_attribute_values('node_id'))}
        path = [ix2id[i] for i in path]
    else:
        # If this NetworkX graph is just an (immutable) view, turn it into a
        # full, independent graph
        if float(nx.__version__) < 2.2:
            if isinstance(x.graph, nx.classes.graphviews.ReadOnlyGraph):
                x.graph = nx.DiGraph(x.graph)
        elif hasattr(x.graph, '_NODE_OK'):
            x.graph = nx.DiGraph(x.graph)
        elif nx.is_frozen(x.graph):
            x.graph = nx.DiGraph(x.graph)

        g = x.graph

        # Walk from new root to old root and remove edges along the way
        parent = next(g.successors(new_root), None)
        if not parent:
            # new_root is already the root
            return
        path = [new_root]
        weights = []
        while parent is not None:
            weights.append(g[path[-1]][parent]['weight'])
            g.remove_edge(path[-1], parent)
            path.append(parent)
            parent = next(g.successors(parent), None)

        # Invert path and add weights
        new_edges = [(path[i + 1], path[i],
                      {'weight': weights[i]}) for i in range(len(path) - 1)]

        # Add inverted path between old and new root
        g.add_edges_from(new_edges)

        # Get degree of old root for later categorisation
        old_root_deg = g.in_degree(path[-1])

    # Propagate changes in graph back to treenode table
    x.nodes.set_index('treenode_id', inplace=True)
    x.nodes.loc[path[1:], 'parent_id'] = path[:-1]
    if old_root_deg == 1:
        x.nodes.loc[path[-1], 'type'] = 'slab'
    elif old_root_deg > 1:
        x.nodes.loc[path[-1], 'type'] = 'branch'
    else:
        x.nodes.loc[path[-1], 'type'] = 'end'
    x.nodes.reset_index(drop=False, inplace=True)

    # Set new root's parent to None
    x.nodes.parent_id = x.nodes.parent_id.astype(object)
    x.nodes.loc[x.nodes.treenode_id == new_root, 'parent_id'] = None

    if x.igraph and config.use_igraph:
        x._clear_temp_attr(exclude=['igraph', 'classify_nodes'])
    else:
        x._clear_temp_attr(exclude=['graph', 'classify_nodes'])

    if not inplace:
        return x
    else:
        return
Ejemplo n.º 23
0
def split_into_training_test_sets(g, test_set_ratio):

    print("+ Getting the gcc of the original graph.")
    # Keep the original graph
    train_g = g.copy()
    train_g.remove_edges_from(nx.selfloop_edges(train_g))  # remove self loops
    train_g = train_g.subgraph(max(nx.connected_components(train_g), key=len))
    if nx.is_frozen(train_g):
        train_g = nx.Graph(train_g)
    print("\t- Completed!")

    print("+ Relabeling.")
    node2newlabel = {
        node: str(nodeIdx)
        for nodeIdx, node in enumerate(train_g.nodes())
    }
    train_g = nx.relabel_nodes(G=train_g, mapping=node2newlabel, copy=True)
    print("\t- Completed!")

    num_of_nodes = train_g.number_of_nodes()
    nodelist = list(train_g.nodes())

    print("+ Splitting into train and test sets.")
    num_of_edges = train_g.number_of_edges()

    edges = list(train_g.edges())

    test_size = int(test_set_ratio * num_of_edges)

    test_g = nx.Graph()
    test_g.add_nodes_from(train_g.nodes())

    count = 0
    idx = 0
    perm = np.arange(len(edges))
    while (count < test_size and count < num_of_edges):
        if count % 10000 == 0:
            print("{}/{}".format(count, test_size))
        # Remove the chosen edge
        chosen_edge = edges[perm[idx]]
        train_g.remove_edge(chosen_edge[0], chosen_edge[1])
        if chosen_edge[1] in nx.connected._plain_bfs(train_g, chosen_edge[0]):
            test_g.add_edge(chosen_edge[0], chosen_edge[1])
            count += 1
        else:
            train_g.add_edge(chosen_edge[0], chosen_edge[1])

        idx += 1
    print("--> Completed!")

    if count != test_size:
        raise ValueError("Enough positive edge samples could not be found!")

    # Generate the negative samples
    print("\+ Generating negative samples")
    count = 0
    negative_samples_idx = [[] for _ in range(num_of_nodes)]
    negative_samples = []
    while count < 2 * test_size:
        if count % 10000 == 0:
            print("{}/{}".format(count, 2 * test_size))
        uIdx = np.random.randint(num_of_nodes - 1)
        vIdx = np.random.randint(uIdx + 1, num_of_nodes)

        if vIdx not in negative_samples_idx[uIdx]:
            negative_samples_idx[uIdx].append(vIdx)

            u = nodelist[uIdx]
            v = nodelist[vIdx]

            negative_samples.append((u, v))

            count += 1

    train_neg_samples = negative_samples[:test_size]
    test_neg_samples = negative_samples[test_size:test_size * 2]

    return train_g, test_g, train_neg_samples, test_neg_samples
Ejemplo n.º 24
0
>>>>>>> 4c2410df82583108d45234d551042650277ca759
		if np.random.rand() < test_ratio:
			train_g.remove_edge(edge[0], edge[1])
		else:
			test_g.remove_edge(edge[0], edge[1])

	# Get the greatest components and relabel
	if not nx.is_connected(train_g):
		print("First size of train graph: {}".format(train_g.number_of_nodes()))
		train_g = train_g.subgraph(max(nx.connected_components(train_g), key=len))
		print("Gcc size of train graph: {}".format(train_g.number_of_nodes()))
		

		train_nodes = list(train_g.nodes())		
		node2newlabel = dict(zip(train_nodes, range(train_g.number_of_nodes())))
		if nx.is_frozen(train_g):
			print("Graph is frozen!")
			train_g = nx.Graph(train_g)
		nx.relabel_nodes(train_g, node2newlabel, copy=False)
		
		test_g = test_g.subgraph(train_nodes)
		if nx.is_frozen(test_g):
			print("Graph is frozen!")
			test_g = nx.Graph(test_g)
		nx.relabel_nodes(test_g, node2newlabel, copy=False)

	return train_g, test_g


def split(graph_path, output_folder ):
Ejemplo n.º 25
0
 def test_is_frozen(self):
     assert_equal(networkx.is_frozen(self.G), False)
     G=networkx.freeze(self.G)
     assert_equal(G.frozen, networkx.is_frozen(self.G))
     assert_equal(G.frozen,True)
Ejemplo n.º 26
0
 def test_is_frozen(self):
     assert nx.is_frozen(self.G) == False
     G = nx.freeze(self.G)
     assert G.frozen == nx.is_frozen(self.G)
     assert G.frozen == True
Ejemplo n.º 27
0
def getClusterCoef(graph, nodeDict):
    assert (nx.is_frozen(graph))
    #we can do this because the graph is static
    neighborDict = {}  #cache of all such boards

    for k, node in enumerate(list(graph.nodes())):
        if config.pin_token not in node: continue

        boardList = set()
        k_i = 0.

        for i, neighbor in enumerate(nx.all_neighbors(graph, node)):
            #this condition sohuldn't ever trigger
            if config.board_token not in neighbor:
                assert (False)
                continue
            boardList.add(neighbor)
            k_i += graph.degree(neighbor)

        if k_i == 1 or k_i == 0:
            nodeDict[node].append(0.)
            continue

        e_i = 0.  #computing e_i here

        neighborList = list(nx.all_neighbors(graph, node))

        # for i, neighbor_b1 in enumerate(nx.all_neighbors(graph, node)):
        # for j, neighbor_b2 in enumerate(nx.all_neighbors(graph, node)):
        # if neighbor_b1 == neighbor_b2: continue
        for i in range(len(neighborList)):
            neighbor_b1 = neighborList[i]
            if neighbor_b1 in neighborDict:
                b1_neighbors = neighborDict[neighbor_b1]
            else:
                b1_neighbors = set(nx.all_neighbors(graph, neighbor_b1))
                neighborDict[neighbor_b1] = b1_neighbors

            for j in range(len(neighborList[i + 1:])):
                neighbor_b2 = neighborList[j]
                if neighbor_b2 in neighborDict:
                    b2_neighbors = neighborDict[neighbor_b2]
                else:
                    b2_neighbors = set(nx.all_neighbors(graph, neighbor_b2))
                    neighborDict[neighbor_b2] = b2_neighbors

                #this code is SOOO slow
                # e_i += len(set(list(nx.all_neighbors(graph, neighbor_b1)) + list(nx.all_neighbors(graph, neighbor_b2))))-1

                union = b1_neighbors.union(b2_neighbors)

                # if len(union) < 1:
                # assert(len(union)>0)

                e_i += len(union) - 1
        # print "e_i is: ", e_i

        # for j, pin_neighbors in enumerate(nx.all_neighbors(graph, neighbor)):
        #condition shouldn't trigger
        # if config.pin_token not in pin_neighbors:
        # assert(False)
        # continue

        # for l, board_pin_neighbor in enumerate(nx.all_neighbors(graph, pin_neighbors)):
        # if board_pin_neighbor in boardList:
        # e_i += 1

        clust_coef = e_i / (2. * k_i * (k_i - 1))
        # if clust_coef >=1. or clust_coef<=0.:
        # assert(clust_coef <=1. and clust_coef >= 0.)
        nodeDict[node].append(clust_coef)
    return nodeDict