Beispiel #1
0
def sample_paths_from_temporal_network_dag(tempnet, delta=1, num_roots=1, max_subpath_length=None):
    """
    Estimates the frequency of causal paths in a temporal network assuming a
    maximum temporal distance of delta between consecutive
    time-stamped links on a path. This method first creates a directed and acyclic
    time-unfolded graph based on the given parameter delta. This directed acyclic
    graph is used to calculate causal paths for a given delta, randomly sampling num_roots
    roots in the time-unfolded DAG.

    Parameters
    ----------
    tempnet : pathpy.TemporalNetwork
        TemporalNetwork to extract the time-respecting paths from
    delta : int
        Indicates the maximum temporal distance up to which time-stamped
        links will be considered to contribute to a causal path.
        For (u,v;3) and (v,w;7) a causal path (u,v,w) is generated
        for 0 < delta <= 4, while no causal path is generated for
        delta > 4. Every time-stamped edge is a causal path of
        length one. Default value is 1.
    num_roots : int
        The number of randomly chosen roots that will be used to estimate path statistics.

    Returns
    -------
    Paths
        An instance of the class Paths, which can be used to generate higher- and multi-order
        models of causal paths in temporal networks.
    """
    # generate a single time-unfolded DAG
    Log.add('Constructing time-unfolded DAG ...')
    dag, node_map = DAG.from_temporal_network(tempnet, delta)
    # dag.topsort()
    # assert dag.is_acyclic
    Log.add('finished.')
    print(dag)

    causal_paths = Paths()
    
    # For each root in the time-unfolded DAG, we generate a
    # causal tree and use it to count all causal paths
    # that originate at this root
    current_root = 1
    Log.add('Generating causal trees for {0} root nodes ...'.format(num_roots))
    import random
    for root in random.sample(dag.roots, num_roots):
        causal_tree, causal_mapping = generate_causal_tree(dag, root, node_map)
        if num_roots > 10:
            step = num_roots/10
            if current_root % step == 0:
                Log.add('Analyzing tree {0}/{1} ...'.format(current_root, num_roots))
        # elevate Logging level
        x = Log.min_severity
        Log.set_min_severity(Severity.WARNING)

        # calculate all unique longest path in causal tree
        causal_paths += paths_from_dag(causal_tree, causal_mapping, repetitions=False, max_subpath_length=max_subpath_length)
        current_root += 1

        # restore log level
        Log.set_min_severity(x)
    Log.add('finished.')
    
    return causal_paths
Beispiel #2
0
def paths_from_temporal_network_dag(tempnet, delta=1, max_subpath_length=None):
    """
    Calculates the frequency of causal paths in a temporal network assuming a 
    maximum temporal distance of delta between consecutive
    time-stamped links on a path. This method first creates a directed and acyclic
    time-unfolded graph based on the given parameter delta. This directed acyclic
    graph is used to calculate all time-respecting paths for a given delta.
    I.e., for time-stamped links (a,b,1), (b,c,5), (b,d,7) and delta = 5 the
    time-respecting path (a,b,c) will be found.

    Parameters
    ----------
    tempnet : pathpy.TemporalNetwork
        TemporalNetwork to extract the time-respecting paths from
    delta : int
        Indicates the maximum temporal distance up to which time-stamped
        links will be considered to contribute to a causal path.
        For (u,v;3) and (v,w;7) a causal path (u,v,w) is generated
        for 0 < delta <= 4, while no causal path is generated for
        delta > 4. Every time-stamped edge is a causal path of
        length one. Default value is 1.
    max_subpath_length : int
        Can be used to limit the calculation of sub path statistics to a given
        maximum length. This is useful as statistics of sub paths of length k
        are only needed to fit higher-order model with order k and larger. If model
        selection is limited to a maximum order K, we can set the maximum sub path length
        to K. Default is None, which means all subpaths are calculated.

    Returns
    -------
    Paths
        An instance of the class Paths, which can be used to generate higher- and multi-order
        models of causal paths in temporal networks.

    Examples
    ---------
    >>> t = pp.TemporalNetwork()
    >>> t.add_edge('a', 'b', 1)
    >>> t.add_edge('b', 'a', 3)
    >>> t.add_edge('b', 'c', 3)
    >>> t.add_edge('d', 'c', 4)
    >>> t.add_edge('c', 'd', 5)
    >>> t.add_edge('c', 'b', 6)

    >>> >>>causal_paths = pp.path_extraction.paths_from_temporal_network_dag(t, delta=2)
    >>> [Severity.INFO]	Constructing time-unfolded DAG ...
    >>> [Severity.INFO]	finished.
    >>> [Severity.INFO]	Generating causal trees for 2 root nodes ...
    >>> [Severity.INFO]	finished.
    >>> print(causal_paths)
    >>> Total path count: 		4.0 
    >>> [Unique / Sub paths / Total]: 	[4.0 / 24.0 / 28.0]
    >>> Nodes:				    4 
    >>> Edges:				    6
    >>> Max. path length:		3
    >>> Avg path length:		2.25 
    >>> Paths of length k = 0		0.0 [ 0.0 / 13.0 / 13.0 ]
    >>> Paths of length k = 1		0.0 [ 0.0 / 9.0 / 9.0 ]
    >>> Paths of length k = 2		3.0 [ 3.0 / 2.0 / 5.0 ]
    >>> Paths of length k = 3		1.0 [ 1.0 / 0.0 / 1.0 ]

    >>> The calculated (longest) causal paths in this example are:
    >>> (a, b, c, d), (d, c, b), (d, c, d), (a, b, a)
    """
    # generate a single time-unfolded DAG
    Log.add('Constructing time-unfolded DAG ...')
    dag, node_map = DAG.from_temporal_network(tempnet, delta)
    Log.add('finished.')
    print(dag)

    causal_paths = Paths()
    
    # For each root in the time-unfolded DAG, we generate a
    # causal tree and use it to count all causal paths
    # that originate at this root
    num_roots = len(dag.roots)
    current_root = 1
    Log.add('Generating causal trees for {0} root nodes ...'.format(num_roots))
    for root in dag.roots:
        causal_tree, causal_mapping = generate_causal_tree(dag, root, node_map)
        if num_roots > 10:
            step = num_roots/10
            if current_root % step == 0:
                Log.add('Analyzing tree {0}/{1} ...'.format(current_root, num_roots))
        # elevate Logging level
        x = Log.min_severity
        Log.set_min_severity(Severity.WARNING)

        # calculate all unique longest path in causal tree
        causal_paths += paths_from_dag(causal_tree, causal_mapping, repetitions=False, max_subpath_length=max_subpath_length)
        current_root += 1

        # restore log level
        Log.set_min_severity(x)
    Log.add('finished.')
    
    return causal_paths