IndraNet Graphs (indra.assemblers.indranet)

A Networkx Representation of INDRA Statements

class indra.assemblers.indranet.net.IndraNet(incoming_graph_data=None, **attr)[source]

A Networkx representation of INDRA Statements.

classmethod digraph_from_df(df, flattening_method=None, weight_mapping=None)[source]

Create a digraph from a pandas DataFrame.

classmethod from_df(df)[source]

Create an IndraNet MultiDiGraph from a pandas DataFrame.

Returns an instance of IndraNet with graph data filled out from a dataframe containing pairwise interactions.

Parameters:df (pd.DataFrame) – A pandas.DataFrame with each row containing node and edge data for one edge. Indices are used to distinguish multiedges between a pair of nodes. Any columns not part of the mandatory columns are considered extra attributes. Columns starting with ‘agA_’ or ‘agB_’ (excluding the agA/B_name) will be added to its respective nodes as node attributes. Any other columns will be added as edge attributes.
Returns:An IndraNet object
Return type:IndraNet
classmethod signed_from_df(df, sign_dict=None, flattening_method=None, weight_mapping=None)[source]

Create a signed graph from a pandas DataFrame.

to_digraph(flattening_method=None, weight_mapping=None)[source]

Flatten the IndraNet to a DiGraph

Parameters:
  • flattening_method (str|function) – The method to use when updating the belief for the flattened edge
  • weight_mapping (function) – A function taking at least the graph G as an argument and returning G after adding edge weights as an edge attribute to the flattened edges using the reserved keyword ‘weight’.
Returns:

G – An IndraNet graph flattened to a DiGraph

Return type:

IndraNet(nx.DiGraph)

to_signed_graph(sign_dict=None, flattening_method=None, weight_mapping=None)[source]

Flatten the IndraNet to a signed graph.

Parameters:
  • sign_dict (dict) – A dictionary mapping a Statement type to a sign to be used for the edge. By default only Activation and IncreaseAmount are added as positive edges and Inhibition and DecreaseAmount are added as negative edges, but a user can pass any other Statement types in a dictionary.
  • flattening_method (str|function(G, edge)) –

    The method to use when updating the belief for the flattened edge.

    If a string is provided, it must be one of the predefined options ‘simple_scorer’ or ‘complementary_belief’.

    If a function is provided, it must take the flattened graph ‘G’ and an edge ‘edge’ to perform the belief flattening on and return a number:

    >>> def flattening_function(G, edge):
    ...     # Return the average belief score of the constituent edges
    ...     all_beliefs = [s['belief']
    ...         for s in G.edges[edge]['statements']]
    ...     return sum(all_beliefs)/len(all_beliefs)
    
  • weight_mapping (function(G)) –

    A function taking at least the graph G as an argument and returning G after adding edge weights as an edge attribute to the flattened edges using the reserved keyword ‘weight’.

    Example:

    >>> def weight_mapping(G):
    ...     # Sets the flattened weight to the average of the
    ...     # inverse source count
    ...     for edge in G.edges:
    ...         w = [1/s['evidence_count']
    ...             for s in G.edges[edge]['statements']]
    ...         G.edges[edge]['weight'] = sum(w)/len(w)
    ...     return G
    
Returns:

SG – An IndraNet graph flattened to a signed graph

Return type:

IndraNet(nx.MultiDiGraph)

class indra.assemblers.indranet.assembler.IndraNetAssembler(statements=None)[source]

Assembler to create an IndraNet object from a list of INDRA statements.

Parameters:statements (list[indra.statements.Statement]) – A list of INDRA Statements to be assembled.
model

An IndraNet graph object assembled by this class.

Type:IndraNet
add_statements(stmts)[source]

Add INDRA Statements to the assembler’s list of statements.

Parameters:stmts (list[indra.statements.Statement]) – A list of indra.statements.Statement to be added to the statement list of the assembler.
make_df(exclude_stmts=None, complex_members=3)[source]

Create a dataframe containing information extracted from assembler’s list of statements necessary to build an IndraNet.

Parameters:
  • exclude_stmts (list[str]) – A list of statement type names to not include into a dataframe.
  • complex_members (int) – Maximum allowed size of a complex to be included in the data frame. All complexes larger than complex_members will be rejected. For accepted complexes, all permutations of their members will be added as dataframe records.
Returns:

df – Pandas DataFrame object containing information extracted from statements.

Return type:

pd.DataFrame

make_model(exclude_stmts=None, complex_members=3, graph_type='multi_graph', sign_dict=None, belief_flattening=None, weight_flattening=None)[source]

Assemble an IndraNet graph object.

Parameters:
  • exclude_stmts (list[str]) – A list of statement type names to not include in the graph.
  • complex_members (int) – Maximum allowed size of a complex to be included in the graph. All complexes larger than complex_members will be rejected. For accepted complexes, all permutations of their members will be added as edges.
  • graph_type (str) – Specify the type of graph to assemble. Chose from ‘multi_graph’ (default), ‘digraph’, ‘signed’.
  • sign_dict (dict) – A dictionary mapping a Statement type to a sign to be used for the edge. This parameter is only used with the ‘signed’ option. See IndraNet.to_signed_graph for more info.
  • belief_flattening (str|function(G, edge)) –

    The method to use when updating the belief for the flattened edge.

    If a string is provided, it must be one of the predefined options ‘simple_scorer’ or ‘complementary_belief’.

    If a function is provided, it must take the flattened graph ‘G’ and an edge ‘edge’ to perform the belief flattening on and return a number:

    >>> def belief_flattening(G, edge):
    ...     # Return the average belief score of the constituent edges
    ...     all_beliefs = [s['belief']
    ...         for s in G.edges[edge]['statements']]
    ...     return sum(all_beliefs)/len(all_beliefs)
    
  • weight_flattening (function(G)) –

    A function taking at least the graph G as an argument and returning G after adding edge weights as an edge attribute to the flattened edges using the reserved keyword ‘weight’.

    Example:

    >>> def weight_flattening(G):
    ...     # Sets the flattened weight to the average of the
    ...     # inverse source count
    ...     for edge in G.edges:
    ...         w = [1/s['evidence_count']
    ...             for s in G.edges[edge]['statements']]
    ...         G.edges[edge]['weight'] = sum(w)/len(w)
    ...     return G
    
Returns:

model – IndraNet graph object.

Return type:

IndraNet

indra.assemblers.indranet.assembler.get_ag_ns_id(ag)[source]

Return a tuple of name space, id from an Agent’s db_refs.