def test_possible_paths():
    """Test to generate all possible paths."""
    paths = PathCollection()
    paths.add('a', 'a', 'b', 'b', 'a')

    null = NullModel()
    assert len(null.possible_relations(paths, length=3)) == 16
def test_fit_path_collection():
    """Fit PathCollection to a HON"""
    paths = PathCollection()
    paths.add('a', 'c', 'd', uid='acd', frequency=10)
    paths.add('b', 'c', 'e', uid='bce', frequency=10)

    hon = HigherOrderNetwork()
    hon.fit(paths, order=0)

    assert hon.order == 0
    assert hon.number_of_nodes() == 5
    assert hon.number_of_edges() == 0

    hon = HigherOrderNetwork()
    hon.fit(paths, order=1)

    assert hon.order == 1
    assert hon.number_of_nodes() == 5
    assert hon.number_of_edges() == 4

    hon = HigherOrderNetwork()
    hon.fit(paths, order=2)

    assert hon.order == 2
    assert hon.number_of_nodes() == 4
    assert hon.number_of_edges() == 2

    hon = HigherOrderNetwork.from_paths(paths, order=2)

    assert hon.order == 2
    assert hon.number_of_nodes() == 4
    assert hon.number_of_edges() == 2
def test_outdegrees():
    """Fit PathCollection to a HON"""
    paths = PathCollection()
    paths.add('a', 'c', 'd', uid='acd', frequency=10)
    paths.add('b', 'c', 'e', uid='bce', frequency=10)

    hon = HigherOrderNetwork()
    hon.fit(paths, order=2)

    assert sum(hon.outdegrees().values()) == 2
Example #4
0
    def _(self,
          data: Network,
          order: Optional[int] = None,
          subpaths: bool = True) -> None:
        paths = PathCollection(directed=data.directed,
                               multipaths=data.multiedges)
        for edge in data.edges:
            paths.add(*edge, count=data.edges.counter[edge.uid])

        self.fit(paths, order=order)
    def routes_from(self, v, node_mapping=None) -> PathCollection:
        """
        Constructs all paths from node v to any leaf nodes

        Parameters
        ----------
        v:  str
            uid of node from which to start
        node_mapping: dict
            an optional mapping from node to a different set.

        Returns
        -------
        list
            a list of lists, where each list contains one path from the source
            node v until a leaf node is reached
        """

        if node_mapping is None:
            node_mapping = {w.uid: w.uid for w in self.nodes}

        paths = PathCollection()

        # Collect temporary paths, indexed by the target node
        temp_paths = defaultdict(list)
        temp_paths[v] = [[v]]

        # set of unprocessed nodes
        queue = {v}
        # print('Queue = ', queue)

        while queue:
            # take one unprocessed node
            x = queue.pop()
            # print('Dequeued ', x)
            # successors of x expand all temporary
            # paths, currently ending in x
            if len(self.successors[x]) > 0:
                for w in self.successors[x]:
                    for p in temp_paths[x]:
                        temp_paths[w.uid].append(p + [w.uid])
                    # print('Adding ', w.uid)
                    queue.add(w.uid)
                del temp_paths[x]
            #print('Queue = ', queue)

        # flatten list
        for possible_paths in temp_paths.values():
            for path in possible_paths:
                if node_mapping:
                    path = [node_mapping[k] for k in path]
                paths.add(path, count=1, uid='-'.join(path))

        return paths
Example #6
0
def test_PathCollection_add_tuple():
    """Add path tuple to the path collection."""

    paths = PathCollection()
    paths.add(('a', 'b'), ('a', 'b', 'c'))

    assert len(paths.nodes) == 3
    # assert len(paths.edges) == 2
    assert len(paths) == 2

    paths.add('a', 'b', 'c')
    assert paths.counter[paths['a', 'b', 'c'].uid] == 2
Example #7
0
def test_PathCollection_remove_edges():
    """Remove edge path from the path collection."""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    paths = PathCollection()
    paths.add(e, f, uid='p1')

    paths.remove(e, f)
    assert len(paths) == 0
    assert 'p1' not in paths

    paths.add(e, f, uid='p1')
    paths.remove('p1')
    assert len(paths) == 0

    paths.add(e, f, uid='p1')
    paths.remove(e, f)
    assert len(paths) == 0

    paths.add(e, f, uid='p1')
    paths.remove('e', 'f')
    assert len(paths) == 0
Example #8
0
def test_PathCollection_add_edges():
    """Add edge path to the path collection."""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    paths = PathCollection()
    paths.add(e, f, uid='p1')

    assert len(paths.nodes) == 2
    #     assert len(paths.edges) == 2
    assert len(paths) == 1
    assert 'p1' in paths

    paths.add(e, f, uid='p1')
    assert paths.counter['p1'] == 2

    paths.add(e, f)
    assert paths.counter['p1'] == 3

    with pytest.raises(Exception):
        paths.add(e, f, uid='p2')

    assert paths.counter['p1'] == 3
def test_basic():
    """Test basic functions"""

    paths = PathCollection()
    paths.add('a', 'c', 'd', uid='a-c-d', count=10)
    paths.add('b', 'c', 'e', uid='b-c-e', count=10)

    null = NullModel()
    null.fit(paths, order=2)

    # null = NullModel.from_paths(paths, order=2)

    assert null.number_of_edges() == 4
    assert null.number_of_nodes() == 4

    for e in null.edges.uids:
        assert null.edges.counter[e] == 5.0
Example #10
0
def test_degrees_of_reedom():
    """Tets degrees of freedom"""
    paths = PathCollection()
    paths.add('a', 'c', 'd', frequency=2)
    paths.add('b', 'c', 'e', frequency=2)

    null = NullModel.from_paths(paths, order=0)
    assert null.degrees_of_freedom() == 4

    null = NullModel.from_paths(paths, order=1)
    assert null.degrees_of_freedom() == 1

    null = NullModel.from_paths(paths, order=2)
    assert null.degrees_of_freedom() == 2

    null = NullModel.from_paths(paths, order=3)
    assert null.degrees_of_freedom() == 0
Example #11
0
def test_PathCollection_remove_path():
    """Remove path from the path collection."""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    p1 = Path(e, f, uid='p1')
    p2 = Path(e, uid='p2')
    p3 = Path(a, uid='p3')

    paths = PathCollection()
    paths.add(p1)

    paths.remove(p1)

    assert len(paths.nodes) == 0
    # assert len(paths.edges) == 2
    assert len(paths) == 0
    assert p1 not in paths
Example #12
0
def test_PathCollection_add_path():
    """Add path to the path collection."""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    p1 = Path(e, f, uid='p1')
    p2 = Path(e, uid='p2')
    p3 = Path(a, uid='p3')

    paths = PathCollection()
    paths.add(p1)

    paths.add(p1)
    assert paths.counter['p1'] == 2

    assert len(paths.nodes) == 2
    # assert len(paths.edges) == 2
    assert len(paths) == 1
    assert p1 in paths

    paths = PathCollection()
    paths.add(p1, p2)

    assert p1 in paths
    assert p2 in paths
Example #13
0
def test_fit_path_collection():
    """Fit PathCollection to a HON"""
    paths = PathCollection()
    a = Node('a')
    b = Node('b')
    c = Node('c')
    d = Node('d')
    e = Node('e')
    # paths.add(a, c, d, uid='acd', frequency=10)
    # paths.add(b, c, e, uid='bce', frequency=10)
    paths.add('a', c, 'd', 'f', uid='acd', count=10)
    paths.add('b', c, 'e', 'g', uid='bce', count=10)

    # paths.add('a', 'c', 'd', uid='acd', frequency=10)
    # paths.add('b', 'c', 'e', uid='bce', frequency=10)

    # print(paths.counter)
    # hon = HigherOrderNetwork()
    # hon.fit(paths, order=3)

    # print(hon.nodes['xxx'].objects)
    # print(hon.nodes.counter)
    # for e in hon.edges:
    #     print(e.first_order_relations)
    #     print()
    #     break

    paths = PathCollection()
    paths.add('a', 'c', 'b', uid='acb', count=10)
    paths.add('c', 'b', 'a', 'c', uid='cba', count=20)
    paths.add('a', 'b', 'a', 'c', uid='abac', count=30)
    # paths.add(a, 'b', 'c', 'd', 'e', 'f', uid='p1')
    # paths.add(a, 'b', 'c', 'd', 'e', 'x')
    hon = HigherOrderNetwork(uid='hon')
    hon.fit(paths, order=1)

    print(hon)

    # for n in hon.nodes:
    #     print((n, hon.indegrees()[n.uid], hon.outdegrees()[n.uid]))
    ##p = paths['p1'].subpaths(min_length=0, max_length=None, paths=True)
    # # print(paths)
    # print(hon.observed)
    # print(hon.subpaths)
    # print(hon.edges.counter)

    print('no log', hon.likelihood(paths, log=False))
    print('log', hon.likelihood(paths, log=True))
    import numpy as np
    print(np.exp(hon.likelihood(paths, log=True)))
Example #14
0
    def _(self, data: Network, order: Optional[int] = None) -> None:

        # Check order
        if order is not None:
            self._order = order

        if 0 <= self.order <= 1:
            super().fit(data, order=self.order)

        elif self.order > 1:

            # TODO: create function to transfer base data from PathCollection object
            # --- START ---
            nc = NodeCollection()
            for node in data.nodes.values():
                nc.add(node)

            ec = EdgeCollection(nodes=nc)
            for edge in data.edges.values():
                ec.add(edge)

            self._nodes = HigherOrderNodeCollection(nodes=nc, edges=ec)
            # --- END ---

            # get network data
            network = data

            # generate a path representation of the data
            paths = PathCollection(directed=network.directed,
                                   nodes=network.nodes,
                                   edges=network.edges)
            for edge in data.edges:
                paths.add(edge, frequency=edge.attributes.get('frequency', 1))

            self.calculate(network, paths)

        else:
            LOG.error('A Null Model with order %s is not supported',
                      self.order)
            raise AttributeError
Example #15
0
def _temp(self, **kwargs):
    """Convert a temproal netwok to paths."""
    from pathpy.models.directed_acyclic_graph import DirectedAcyclicGraph

    #paths = PathCollection(edges=self.edges.copy())
    paths = PathCollection()

    delta = kwargs.get('delta', 1)
    # generate a single time-unfolded DAG
    dag = DirectedAcyclicGraph.from_temporal_network(self, delta=delta)

    for root in dag.roots:
        causal_tree = _causal_tree(dag, root)

        _paths = causal_tree.to_paths()

        for path in _paths:
            edges = [e['original'] for e in path.edges]
            if edges not in paths:
                paths.add(*edges, frequency=1)
            else:
                paths[edges]['frequency'] += 1
    return paths
Example #16
0
def test_PathCollection_counter():
    """Test the counter of the path collection"""
    paths = PathCollection()
    paths.add('a', 'b', count=5)
    paths.add('a', 'b', count=7)
    assert paths.counter[paths['a', 'b'].uid] == 12

    p1 = Path('a', 'x', 'c', uid='a-x-c')
    p2 = Path('b', 'x', 'd', uid='b-x-d')
    pc = PathCollection(multipaths=True)
    pc.add(p1)
    pc.add(p2)
    pc.add(p2)

    assert 'a-x-c' and 'b-x-d' in pc.counter

    p3 = Path('b', 'x', 'd', uid='b-x-d-2')
    pc.add(p3)

    assert 'a-x-c' and 'b-x-d' and 'b-x-d-2' in pc.counter
Example #17
0
def test_PathCollection_add_str():
    """Add string path to the path collection."""

    paths = PathCollection()
    paths.add('a', 'b', 'c', uid='p1')

    assert len(paths.nodes) == 3
    # assert len(paths.edges) == 2
    assert len(paths) == 1
    assert 'p1' in paths

    paths.add('a', 'b', 'c', uid='p1')
    assert paths.counter['p1'] == 2

    paths.add('a', 'b', 'c')
    assert paths.counter['p1'] == 3
Example #18
0
def test_PathCollection_add_nodes():
    """Add node path to the path collection."""
    a = Node('a')
    b = Node('b')
    c = Node('c')

    paths = PathCollection()
    paths.add(a, b, c, uid='p1')

    assert len(paths.nodes) == 3
    # assert len(paths.edges) == 2
    assert len(paths) == 1
    assert 'p1' in paths

    paths.add(a, b, c, uid='p1')
    assert paths.counter['p1'] == 2

    paths.add(a, b, c)
    assert paths.counter['p1'] == 3
Example #19
0
def test_possible_paths():
    """Test to generate all possible paths."""
    paths = PathCollection()
    paths.add('a', 'a', 'b', 'b', 'a')

    assert len(NullModel.possible_paths(paths.edges, order=3)) == 16
Example #20
0
def test_PathCollection():
    """Test the paths object"""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    p1 = Path(e, f, uid='p1')
    p2 = Path(e, uid='p2')
    p3 = Path(a, uid='p3')

    paths = PathCollection()
    paths.add(p1)
    paths.add(p2)
    paths.add(p3)

    with pytest.raises(Exception):
        paths.add(p1)

    assert len(paths.nodes) == 3
    assert len(paths.edges) == 2
    assert len(paths) == 3
    assert p1 in paths
    assert p2 in paths
    assert p3 in paths

    assert 'p1' in paths
    assert 'p2' in paths
    assert 'p3' in paths

    assert (e, f) in paths
    assert ('e', 'f') in paths
    assert [e] in paths
    assert ['e'] in paths

    assert (a, b, c) in paths
    assert ('a', 'b', 'c') in paths
    assert (a, b) in paths
    assert ('a', 'b') in paths

    assert (a, ) in paths
    assert ('a', ) in paths
    assert [a] in paths
    assert ['a'] in paths

    # print(paths['p1'] == p1)
    #print(paths[p1] == p1)

    assert paths['a', 'b', 'c'] == p1
    assert paths['e', 'f'] == p1

    with pytest.raises(Exception):
        p = paths['x', 'y']

    g = Edge(b, c, uid='a')
    p4 = Path(g, uid='p4')
    paths.add(p4)

    # issue warning
    assert ['a'] in paths

    paths = PathCollection()
    paths.add(a, b)

    with pytest.raises(Exception):
        paths.add(a, b)

    paths = PathCollection()
    paths.add('a', 'b', 'c', uid='a-b-c')

    assert len(paths) == 1
    assert 'a-b-c' in paths
    assert 'a' and 'b' and 'c' in paths.nodes
    assert ('a', 'b') and ('b', 'c') in paths.edges

    paths = PathCollection()
    paths.add(p1, p2)

    assert len(paths) == 2

    paths = PathCollection()
    paths.add(('a', 'b', 'c'), ('a', 'b'))

    assert len(paths.nodes) == 3
    assert len(paths.edges) == 2
    assert len(paths) == 2

    paths = PathCollection()
    paths.add(e, f, uid='p1')

    assert len(paths) == 1
    assert len(paths.edges) == 2
    assert len(paths.nodes) == 3

    assert (e, f) in paths
    assert ('a', 'b', 'c') in paths

    with pytest.raises(Exception):
        paths.add(f, e, uid='p2')

    paths = PathCollection()
    paths.add('e1', uid='p1', nodes=False)

    assert len(paths) == 1
    assert len(paths.edges) == 1
    assert len(paths.nodes) == 2
    assert 'p1' in paths
    assert 'e1' in paths.edges

    paths = PathCollection()
    paths.add('e1', 'e2', uid='p1', nodes=False)

    assert len(paths) == 1
    assert len(paths.edges) == 2
    assert len(paths.nodes) == 3
    assert 'p1' in paths
    assert 'e1' and 'e2' in paths.edges

    assert paths.edges['e1'].w == paths.edges['e2'].v

    paths = PathCollection()
    paths.add(('e1', 'e2'), ('e3', 'e4'), nodes=False)

    assert len(paths.nodes) == 6
    assert len(paths.edges) == 4
    assert len(paths) == 2

    paths = PathCollection()
    paths.add(p1, p2, p3)

    assert len(paths.nodes) == 3
    assert len(paths.edges) == 2
    assert len(paths) == 3

    paths.remove(p3)
    assert len(paths.nodes) == 3
    assert len(paths.edges) == 2
    assert len(paths) == 2
    assert p3 not in paths

    paths.remove('p1')
    assert len(paths.nodes) == 3
    assert len(paths.edges) == 2
    assert len(paths) == 1
    assert p1 not in paths

    paths = PathCollection()
    paths.add(('a', 'b', 'c'), ('a', 'b'))

    assert len(paths) == 2

    paths.remove('a', 'b')

    assert len(paths) == 1

    paths = PathCollection()
    paths.add(('a', 'b'), ('b', 'c'), ('c', 'd'))
    paths.remove(('a', 'b'), ('b', 'c'))

    assert len(paths) == 1
    assert ('a', 'b') not in paths
    assert ('b', 'c') not in paths
    assert ('c', 'd') in paths

    paths = PathCollection()
    paths.add(('e1', 'e2'), ('e2', 'e3'), ('e3', 'e4'), nodes=False)

    assert len(paths) == 3
    assert len(paths.edges) == 4

    paths.remove('e1', 'e2')
    assert len(paths) == 2

    paths.remove(('e2', 'e3'), ('e3', 'e4'))
    assert len(paths) == 0

    paths = PathCollection()
    paths.add('a', 'b', uid='p1')
    paths.add('b', 'c', uid='p2')
    paths.add('c', 'd', uid='p3')
    paths.add('d', 'e', uid='p4')

    assert len(paths) == 4

    paths.remove('p1')

    assert len(paths) == 3

    paths.remove('p2', 'p3')

    assert len(paths) == 1
Example #21
0
def test_PathCollection():
    """Test the paths object"""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    p1 = Path(e, f, uid='p1')
    p2 = Path(e, uid='p2')
    p3 = Path(a, uid='p3')

    paths = PathCollection()
    paths.add(p1)
    paths.add(p2)
    paths.add(p3)

    paths.add(p1)
    assert paths.counter['p1'] == 2

    assert len(paths.nodes) == 3
    # assert len(paths.edges) == 2
    assert len(paths) == 3
    assert p1 in paths
    assert p2 in paths
    assert p3 in paths

    assert 'p1' in paths
    assert 'p2' in paths
    assert 'p3' in paths

    assert (e, f) in paths
    assert ('e', 'f') in paths
    assert (e, ) in paths
    assert ('e', ) in paths
    assert (a, ) in paths
    assert ('a', ) in paths

    a = Node('a')
    b = Node('b')
    c = Node('c')

    p1 = Path(a, b, c, uid='p1')
    p2 = Path(a, b, uid='p2')
    p3 = Path(a, uid='p3')

    paths = PathCollection()
    paths.add(p1)
    paths.add(p2)
    paths.add(p3)

    assert (a, b, c) in paths
    assert ('a', 'b', 'c') in paths
    assert (a, b) in paths
    assert ('a', 'b') in paths

    assert (a, ) in paths
    assert ('a', ) in paths
    # assert [a] in paths
    # assert ['a'] in paths

    assert paths['a', 'b', 'c'] == p1
    assert paths['a', 'b'] == p2

    with pytest.raises(Exception):
        p = paths['x', 'y']

    p4 = Path(b, c, uid='p4')
    # with pytest.raises(Exception):
    paths.add(p4)
    assert paths.counter['p4'] == 1

    paths = PathCollection()
    paths.add(a, b)

    # with pytest.raises(Exception):
    paths.add(a, b)
    assert paths.counter[paths['a', 'b'].uid] == 2

    paths = PathCollection()
    paths.add('a', 'b', 'c', uid='a-b-c')

    assert len(paths) == 1
    assert 'a-b-c' in paths
    assert 'a' and 'b' and 'c' in paths.nodes

    paths = PathCollection()
    paths.add(p1, p2)

    assert len(paths) == 2

    paths = PathCollection()
    paths.add(('a', 'b', 'c'), ('a', 'b'))

    assert len(paths.nodes) == 3
    #assert len(paths.edges) == 2
    assert len(paths) == 2

    paths = PathCollection()
    paths.add(e, f, uid='p1')

    assert len(paths) == 1
    #assert len(paths.edges) == 2
    assert len(paths.nodes) == 2

    assert (e, f) in paths
    #assert ('a', 'b', 'c') in paths

    # with pytest.raises(Exception):
    paths.add(f, e, uid='p2')

    #     paths = PathCollection()
    #     paths.add('e1', uid='p1', nodes=False)

    #     assert len(paths) == 1
    #     assert len(paths.edges) == 1
    #     assert len(paths.nodes) == 2
    #     assert 'p1' in paths
    #     assert 'e1' in paths.edges

    #     paths = PathCollection()
    #     paths.add('e1', 'e2', uid='p1', nodes=False)

    #     assert len(paths) == 1
    #     assert len(paths.edges) == 2
    #     assert len(paths.nodes) == 3
    #     assert 'p1' in paths
    #     assert 'e1' and 'e2' in paths.edges

    #     assert paths.edges['e1'].w == paths.edges['e2'].v

    #     paths = PathCollection()
    #     paths.add(('e1', 'e2'), ('e3', 'e4'), nodes=False)

    #     assert len(paths.nodes) == 6
    #     assert len(paths.edges) == 4
    #     assert len(paths) == 2

    paths = PathCollection()
    paths.add(p1, p2, p3)

    assert len(paths.nodes) == 3
    assert len(paths) == 3

    paths.remove(p3)
    assert len(paths.nodes) == 3
    #assert len(paths.edges) == 2
    assert len(paths) == 2
    assert p3 not in paths

    paths.remove('p1')
    assert len(paths.nodes) == 2
    #assert len(paths.edges) == 2
    assert len(paths) == 1
    assert p1 not in paths

    paths = PathCollection()
    paths.add(('a', 'b', 'c'), ('a', 'b'))

    assert len(paths) == 2

    paths.remove('a', 'b')

    assert len(paths) == 1

    paths = PathCollection()
    paths.add(('a', 'b'), ('b', 'c'), ('c', 'd'))
    paths.remove(('a', 'b'), ('b', 'c'))

    assert len(paths) == 1
    assert ('a', 'b') not in paths
    assert ('b', 'c') not in paths
    assert ('c', 'd') in paths

    #     paths = PathCollection()
    #     paths.add(('e1', 'e2'), ('e2', 'e3'), ('e3', 'e4'), nodes=False)

    #     assert len(paths) == 3
    #     assert len(paths.edges) == 4

    #     paths.remove('e1', 'e2')
    #     assert len(paths) == 2

    #     paths.remove(('e2', 'e3'), ('e3', 'e4'))
    #     assert len(paths) == 0

    paths = PathCollection()
    paths.add('a', 'b', uid='p1')
    paths.add('b', 'c', uid='p2')
    paths.add('c', 'd', uid='p3')
    paths.add('d', 'e', uid='p4')

    assert len(paths) == 4

    paths.remove('p1')

    assert len(paths) == 3

    paths.remove(('p2', 'p3'))