cosifer.collections package¶
Submodules¶
cosifer.collections.graph module¶
Graph class.
-
class
cosifer.collections.graph.
Graph
(adjacency, labels=None, undirected=True)¶ Bases:
object
Lightweight class for handling cosifer graph objects. If the graph is undirected the adjacency matrix is stored as a sparse lower triangular matrix.
-
n
¶ number of nodes.
- Type
int
-
labels_to_indices
¶ label to index mapping.
- Type
pd.Series
-
indices_to_labels
¶ index to lable mapping.
- Type
pd.Series
-
adjacency
¶ sparse adjacency.
- Type
spicy.sparse.csr_matrix
-
undirected
¶ flag indicating whether edges are directed.
- Type
bool
-
get_scaled_adjacency
()¶ Get a min-max scaled version of the adjacency.
- Returns
the min-max scaled adjacency.
- Return type
spicy.sparse.csr_matrix
-
indices_to_labels
= None
-
labels_to_indices
= None
-
n
= 0
-
set_adjacency_from_numpy
(adjacency)¶ Set the adjacency from a numpy ndarray.
- Parameters
adjacency (np.ndarray) – adjacency matrix.
- Raises
RuntimeError – in case of inconsitencies in the sizes.
-
set_adjacency_from_pandas
(adjacency)¶ Set the adjacency from a pandas dataframe.
- Parameters
adjacency (pd.DataFrame) – adjacency matrix.
- Raises
RuntimeError – in case of inconsitencies in the sizes.
-
set_adjacency_from_sparse
(adjacency)¶ Set the adjacency from a sparse matrix.
- Parameters
adjacency (spicy.sparse.csr_matrix) – adjacency matrix.
- Raises
RuntimeError – in case of inconsitencies in the sizes.
-
set_labels
(labels)¶ Set the node labels.
- Parameters
labels (iterable) – labels to set.
- Raises
RuntimeError – in case of inconsitenties between the labels and the graph nodes.
-
to_interaction_table
(scaled=True, interaction_symbol='<->')¶ Convert the graph to an interaction table.
- Parameters
scaled (bool, optional) – flag to activate min-max scaling of the edges. Defaults to True.
interaction_symbol (str, optional) – symbol to depict interactions between labels. Defaults to ‘<->’.
- Returns
- the table containing the interactions reported in
the graph.
- Return type
-
cosifer.collections.interaction_table module¶
Interation table class.
-
class
cosifer.collections.interaction_table.
InteractionTable
(df=Empty DataFrame Columns: [e1, e2, intensity] Index: [], labels=None, interaction_symbol=None, force_undirected=False)¶ Bases:
object
Interaction table class representation of an edge list.
-
df
¶ underlying dataframe containing the edge list.
- Type
pd.DataFrame
-
labels
¶ node labels.
- Type
iterable
-
apply_filter
(labels=None, prune_labels=True, indices=None, threshold=0.0, top_n=None)¶ Apply a filter to get an InteractionTable.
- Parameters
labels (iterable, optional) – node labels to select. Defaults to None.
prune_labels (bool, optional) – prune labels not present in the final table. Defaults to True.
indices (itarable, optional) – indices to filter rows. Defaults to None.
threshold (float, optional) – threshold for the edge weights. Defaults to 0.0.
top_n (int, optional) – number of top interactions to keep. Defaults to None.
- Returns
a filtered interaction table.
- Return type
-
get_df_dict
(threshold=None)¶ Get a dictionary to represent the edge list.
- Parameters
threshold (float, optional) – threshold to filter edge by intensity. Defaults to None.
- Returns
a dictionary representing then edge list dataframe.
- Return type
dict
-
to_edge_list
(interaction_symbol='<->', weights=True)¶ Returns an edge list containing the interactions.
- Parameters
interaction_symbol (str, optional) – Symbol separating the labels in the index of the edge list dataframe. Defaults to ‘<->’.
weights (bool, optional) – Flag indicating whether weights are returned. Defaults to True.
- Returns
a list of edges represented by tuples.
- Return type
list
-
to_graph
(undirected=True, imposed_labels=None)¶ Get a graph from the stored edges.
- Parameters
undirected (bool, optional) – flag to indicate whether the interactions are undirected. Defaults to True.
imposed_labels (iterable, optional) – node label to consider. Defaults to None.
- Returns
a graph.
- Return type
-
-
cosifer.collections.interaction_table.
directed_to_undirected_interactions
(directed_interactions)¶ Processing of a directed table, discarding directions and keeping only the maximum edge value. It will drop and reindex with integers the table. :param: directed_interactions: directed interactions table. :return: an undirected version of the given table.
- Parameters
directed_interactions (pd.DataFrame) – directed interactions dataframe.
- Returns
undirected interactions dataframe
- Return type
pd.DataFrame
-
cosifer.collections.interaction_table.
interaction_table_from_dict
(interaction_dictionary)¶ Construct an InteractionTable from a dictionary.
- Parameters
interaction_dictionary (dict) – an interaction dictionary. The dictionary should contain two keys: ‘labels’, containing the node labels, and ‘interactions’, an object that can be used to construct a dataframe representing the edge list.
- Returns
the interaction table.
- Return type
-
cosifer.collections.interaction_table.
interaction_table_from_edge_list
(interaction_list)¶ Construct an InteractionTable from a list.
- Parameters
interaction_list (list) – an edge list containing tuples.
- Returns
the interaction table for the provided edge list.
- Return type
-
cosifer.collections.interaction_table.
interaction_table_from_gzip
(filepath)¶ Construct an InteractionTable from a gzipped file containing an edge list.
- Parameters
filepath (str) – path to the gipped file.
- Returns
the interaction table from the gzipped edge list.
- Return type
-
cosifer.collections.interaction_table.
interaction_table_to_edge_list
(interaction_table, interaction_symbol='<->', weights=True)¶ Convert an InteractionTable to an edge list.
- Parameters
interaction_table (InteractionTable) – an interaction table.
interaction_symbol (str, optional) – Symbol separating the labels in the index of the edge list dataframe. Defaults to ‘<->’.
weights (bool, optional) – Flag indicating whether weights are returned. Defaults to True.
- Returns
a list of edges represented by tuples.
- Return type
list
-
cosifer.collections.interaction_table.
process_group
(row)¶ Process a grouped edge list dataframe
- Parameters
row (pd.Series) – a dataframe row.
- Returns
a list with entitity labels and weight.
- Return type
list
-
cosifer.collections.interaction_table.
process_index
(index, intensity, interaction_symbol)¶ Process index to get edge tuple. Disregard edge weight.
- Parameters
index (str) – index of the edge list dataframe.
intensity (float) – intensity of the edge.
interaction_symbol (str) – symbol used to separate node labels.
- Returns
a tuple containing edge labels.
- Return type
tuple
-
cosifer.collections.interaction_table.
process_index_with_weights
(index, intensity, interaction_symbol)¶ Process index to get edge tuple including edge weight.
- Parameters
index (str) – index of the edge list dataframe.
intensity (float) – intensity of the edge.
interaction_symbol (str) – symbol used to separate node labels.
- Returns
a tuple containing edge labels and weight.
- Return type
tuple