Skip to content

Commit

Permalink
Merge branch 'PM4PY-1521-utility-convert-SNA-to-NX' into 'integration'
Browse files Browse the repository at this point in the history
PM4PY-1521 Utility to convert a result from SNA to a NetworkX Graph / DiGraph

See merge request process-mining/pm4py/pm4py-core!576
  • Loading branch information
fit-sebastiaan-van-zelst committed Jan 6, 2022
2 parents a4cf385 + f8fc9e4 commit c15c889
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pm4py/algo/organizational_mining/sna/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
from typing import List, Any, Dict
from enum import Enum
from pm4py.util import exec_utils


class Parameters(Enum):
WEIGHT_THRESHOLD = "weight_threshold"


def sna_result_to_nx_graph(sna_results: List[List[Any]], parameters=None):
"""
Transforms the results of SNA to a NetworkX Graph / DiGraph object
(depending on the type of analysis).
Parameters
------------------
sna_results
Result of a SNA operation
parameters
Parameters of the algorithm, including:
- Parameters.WEIGHT_THRESHOLD => the weight threshold (used to filter out edges)
Returns
-----------------
nx_graph
NetworkX Graph / DiGraph
"""
if parameters is None:
parameters = {}

import networkx as nx
import numpy as np

weight_threshold = exec_utils.get_param_value(Parameters.WEIGHT_THRESHOLD, parameters, 0.0)
directed = sna_results[2]

rows, cols = np.where(sna_results[0] > weight_threshold)
edges = zip(rows.tolist(), cols.tolist())
if directed:
graph = nx.DiGraph()
else:
graph = nx.Graph()
labels = {}
nodes = []
for index, item in enumerate(sna_results[1]):
labels[index] = item
nodes.append(item)

edges = [(labels[e[0]], labels[e[1]]) for e in edges]

graph.add_nodes_from(nodes)
graph.add_edges_from(edges)

return graph


def cluster_affinity_propagation(sna_results: List[List[Any]], parameters=None) -> Dict[str, List[str]]:
Expand Down

0 comments on commit c15c889

Please sign in to comment.