Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new recipe to create an ego graph from nodes #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions custom-recipes/ego-graph-from-nodes/recipe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"meta": {
"label": "Ego graph from nodes",
"description": "Returns induced subgraph of neighbors centered at selected nodes within a given radius",
"icon": "icon-link"
},

"kind": "PYTHON",

"selectableFromDataset" : "Input Dataset",
"inputRoles" : [
{
"name": "Input Dataset",
"arity": "UNARY",
"required": true,
"acceptsDataset": true
}
],
"outputRoles" : [
{
"name": "Output Dataset",
"arity": "UNARY",
"required": true,
"acceptsDataset": true
}
],

"params": [
{
"name": "source_nodes",
"label": "Source nodes",
"type": "COLUMN",
"columnRole":"Input Dataset"
},
{
"name": "target_nodes",
"label": "Target nodes",
"type": "COLUMN",
"columnRole":"Input Dataset"
},
{
"name": "edges_label",
"label": "Edges label",
"type": "COLUMNS",
"columnRole":"Input Dataset"
},
{
"name": "weighted",
"label": "Weighted graph",
"type": "BOOLEAN",
"defaultValue": false,
"description": ""
},
{
"name": "nodes",
"label": "Nodes",
"type": "STRINGS",
"description": ""
},
{
"name": "ego_graph_radius",
"label": "Ego graph radius",
"type": "INT",
"defaultValue": 2,
"description": ""
}
],

"resourceKeys": []
}
72 changes: 72 additions & 0 deletions custom-recipes/ego-graph-from-nodes/recipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
from dataiku.customrecipe import get_recipe_config
from graph_analytics_utils import get_input_dataset, get_output_dataset
from graph_analytics_constants import Constants
import pandas as pd
import networkx as nx
from networkx.algorithms import union
import logging
import time

def create_ego_graph(graph, node, radius=1, predecessors=False):
try:
# Create a directed ego-graph from node to get it's successors
node_ego_graph = nx.ego_graph(graph, node, radius)

# Reverse graph to get level 1 predecessors
if predecessors:
tmp_graph = graph.reverse()
predecessors_ego_graph = nx.ego_graph(tmp_graph, node, radius=1)
predecessors_ego_graph = predecessors_ego_graph.reverse()
node_ego_graph.update(predecessors_ego_graph)

return node_ego_graph

# Return an empty directed graph if node is not found in graph
except:
logging.warn("Ego graph - Node " + node + " not found in graph")
return nx.DiGraph()


# Read recipe config
input_dataset = get_input_dataset('Input Dataset')
output_dataset = get_output_dataset('Output Dataset')

recipe_config = get_recipe_config()

# List of necessary columns
columns = []
columns.append(recipe_config['source_nodes'])
columns.append(recipe_config['target_nodes'])
for col in recipe_config['edges_label']:
columns.append(col)

# Recipe input
input_df = input_dataset.get_dataframe(columns=columns)
logging.info("Ego graph - Dataset loaded")

# Delete nulls
input_df = input_df[(input_df[recipe_config['source_nodes']].notnull()) & (input_df[recipe_config['target_nodes']].notnull())]
logging.info("Ego graph - Null values removed")

# Dedup
deduplicated_df = input_df.groupby(columns).size().reset_index().rename(columns={0: 'w'})
logging.info("Ego graph - Deduplicated dataset created")

# Creating the directed graph
graph = nx.from_pandas_edgelist(deduplicated_df, recipe_config['source_nodes'], recipe_config['target_nodes'], recipe_config['edges_label'], nx.DiGraph)
logging.info("Base NetworkX graph created")

node_ego_graph = nx.DiGraph()
for node in recipe_config["nodes"]:
node_ego_graph.update(create_ego_graph(graph, node, recipe_config["ego_graph_radius"], True))

# Write output dataframe
output_df = pd.DataFrame(list(node_ego_graph.edges(data=True)))
output_df.columns = [recipe_config['source_nodes'], recipe_config['target_nodes'], "Edges labels"]

logging.info("Ego graph - Ego graph computed")

output_dataset.write_with_schema(output_df)