navis 1.4.0
  • Install
  • Quickstart
  • Tutorials
  • API
  • Changelog
  • Github
  • Ecosystem
  • Site
    • Page
        • navis.models.network_models.TraversalModel
          • TraversalModel
            • TraversalModel.__init__()

    navis.models.network_models.TraversalModel¶

    class navis.models.network_models.TraversalModel(edges, seeds, source='source', target='target', weights='weight', max_steps=15, traversal_func=None)[source]¶

    Model for traversing a network starting with given seed nodes.

    What this does:

    1. Grab all already visited nodes (starting with seeds in step 1)

    2. Find all downstream nodes of these

    3. Probabilistically traverse based on the weight of the connecting edges

    4. Add those newly visited nodes to the pool & repeat from beginning

    5. Stop when every (connected) neuron was visited or we reached max_steps

    Parameters:
    • edges (pandas.DataFrame) – DataFrame representing an edge list. Must minimally have a source and target column.

    • seeds (iterable) – Seed nodes for traversal. Nodes that aren’t found in edges['source'] will be (silently) removed.

    • weights (str, optional) – Name of a column in edges used as weights. If not provided, all edges will be given a weight of 1. If using the default activation function the weights need to be between 0 and 1.

    • max_steps (int) – Limits the number of steps for each iteration.

    • traversal_func (callable, optional) – Function that determines whether a given edge will be traversed or not in a given step. Must take numpy array (N, 1) of edge weights and return an array with True/False of equal size. Defaults to random_linear_activation_function() which will linearly scale probability of traversal from 0 to 100% between edges weights 0 to 0.3.

    Examples

    >>> from navis.models import TraversalModel
    >>> import networkx as nx
    >>> import numpy as np
    >>> # Generate a random graph
    >>> G = nx.fast_gnp_random_graph(1000, .2, directed=True)
    >>> # Turn into edge list
    >>> edges = nx.to_pandas_edgelist(G)
    >>> # Add random edge weights
    >>> edges['weight'] = np.random.random(edges.shape[0])
    >>> # Initialize model
    >>> model = TraversalModel(edges, seeds=list(G.nodes)[:10])
    >>> # Run model on 2 cores
    >>> model.run_parallel(n_cores=2, iterations=100)
    >>> # Get a summary
    >>> model.summary.tail()                                    
          layer_min  layer_max  layer_mean  layer_median
    node
    995           2          2        2.00             2
    996           2          3        2.33             2
    997           2          2        2.00             2
    998           2          2        2.00             2
    999           2          2        2.00             2
    

    Above Graph was traversed quickly (3 steps max). Let’s adjust the traversal function:

    >>> from navis.models import random_linear_activation_function
    >>> # Use a lower probability for activation
    >>> def my_act(x):
    ...     return random_linear_activation_function(x, max_w=10)
    >>> model = TraversalModel(edges, seeds=list(G.nodes)[:10],
    ...                        traversal_func=my_act)
    >>> res = model.run(iterations=100)
    >>> res.tail()                                              
          layer_min  layer_max  layer_mean  layer_median
    node
    995           2          4       3.210           3.0
    996           2          4       3.280           3.0
    997           2          4       3.260           3.0
    998           2          4       3.320           3.0
    999           2          4       3.195           3.0
    

    Initialize model.

    __init__(edges, seeds, source='source', target='target', weights='weight', max_steps=15, traversal_func=None)[source]¶

    Initialize model.

    Methods

    __init__(edges, seeds[, source, target, ...])

    Initialize model.

    initializer()

    make_summary()

    Generate summary.

    run([iterations, return_iterations])

    Run model (single process).

    run_parallel([n_cores, iterations])

    Run model using parallel processes.

    Attributes

    has_results

    Check if model has results.

    n_nodes

    Return unique nodes in network.

    summary

    Per-node summary.

    Back to top

    Source

    © Copyright 2018, Philipp Schlegel.
    Created using Sphinx 5.3.0.