Projecting a subgraph from Neo4j to NetworkX with projx


In [1]:
%matplotlib inline

In [2]:
import networkx as nx
import projx as px
import matplotlib.pyplot as plt
from py2neo import Graph  # Supports streaming.

In [3]:
plt.rcParams['figure.figsize'] = 12, 7

In [ ]:
def clear_db(g):
    g.cypher.execute("START n = node(*) MATCH n-[r?]-() DELETE n, r;")

In [4]:
# Source graph. Dummy smoothie recipes.
g = Graph()

In [5]:
neo4j_etl = {
    "extractor": {
        "neo4j": {
            "query": "match (n)--(r:Recipe) return n, r"
        }
    }, 
    "transformers": [
        {
            "node": {
                "pattern": [{"node": {"alias": "n", "unique": "UniqueId"}}],
                "set": [
                    {"key": "name", "value_lookup": "n.UniqueId"},
                    {"key": "type", "value": "Ingredient"}
                ]
            },
        },
        {
            "node": {
                "pattern": [{"node": {"alias": "r", "unique": "UniqueId"}}],
                "set": [
                    {"key": "name", "value_lookup": "r.UniqueId"},
                    {"key": "type", "value": "Recipe"}
                ]
            },
        },
        {
            "edge": {
                "pattern": [
                    {"node": {"alias": "n", "unique": "UniqueId"}}, 
                    {"edge": {}}, 
                    {"node": {"alias": "r", "unique": "UniqueId"}}
                ] 
            }
        }
    ], 
    "loader": {
        "neo4j2nx": {}
    }
}

In [6]:
subgraph = px.execute_etl(neo4j_etl, g)
px.draw_simple_graph(subgraph)



In [7]:
neo4j_etl = {
    "extractor": {
        "neo4j": {
            "query": "match (n)--(r:Recipe)--(m) return n, r, m"
        }
    }, 
    "transformers": [
        {
            "node": {
                "pattern": [{"node": {"alias": "n", "unique": "UniqueId"}}],
                "set": [
                    {"key": "name", "value_lookup": "n.UniqueId"},
                    {"key": "type", "value": "Ingredient"}
                ]
            },
        },
        {
            "node": {
                "pattern": [{"node": {"alias": "m", "unique": "UniqueId"}}],
                "set": [
                    {"key": "name", "value_lookup": "m.UniqueId"},
                    {"key": "type", "value": "Ingredient"}
                ]
            },
        },
        {
            "edge": {
                "pattern": [
                    {"node": {"alias": "n", "unique": "UniqueId"}}, 
                    {"edge": {}}, 
                    {"node": {"alias": "m", "unique": "UniqueId"}}
                ], 
                "set": [
                    {"key": "name", "value_lookup": "r.UniqueId"}
                ], 
            }
        }
    ], 
    "loader": {
        "neo4j2nx": {}
    }
}

In [8]:
subgraph = px.execute_etl(neo4j_etl, g)
px.draw_simple_graph(subgraph)



In [9]:
neo4j_etl = {
    "extractor": {
        "neo4j": {
            "query": "match (n)--(r:Recipe {UniqueId: 'PinaColadaGreenSmoothie'})--(m) return n, r, m"
        }
    }, 
    "transformers": [
        {
            "node": {
                "pattern": [{"node": {"alias": "n", "unique": "UniqueId"}}],
                "set": [
                    {"key": "name", "value_lookup": "n.UniqueId"},
                    {"key": "type", "value": "Ingredient"}
                ]
            },
        },
        {
            "node": {
                "pattern": [{"node": {"alias": "m", "unique": "UniqueId"}}],
                "set": [
                    {"key": "name", "value_lookup": "m.UniqueId"},
                    {"key": "type", "value": "Ingredient"}
                ]
            },
        },
        {
            "edge": {
                "pattern": [
                    {"node": {"alias": "n", "unique": "UniqueId"}}, 
                    {"edge": {}}, 
                    {"node": {"alias": "m", "unique": "UniqueId"}}
                ], 
                "set": [
                    {"key": "name", "value_lookup": "r.UniqueId"}
                ], 
            }
        }
    ], 
    "loader": {
        "neo4j2nx": {}
    }
}

In [10]:
subgraph = px.execute_etl(neo4j_etl, g)
px.draw_simple_graph(subgraph)


Projecting a subgraph from Neo4j and writing an edgelist with projx


In [11]:
neo4j2edgelist_etl = {
    "extractor": {
        "neo4j": {
            "query": "match (n)--(r:Recipe) return n, r"
        }
    }, 
    "transformers": [
        {
            "edge": {
                "pattern": [
                    {"node": {"alias": "n", "unique": "UniqueId"}}, 
                    {"edge": {}}, 
                    {"node": {"alias": "r", "unique": "UniqueId"}}
                ]
            }
        }
    ], 
    "loader": {
        "neo4j2edgelist": {"delim": ",", "filename": "demo.csv", "newline": "\n"}
    }
}

In [12]:
px.execute_etl(neo4j2edgelist_etl, g)

In [13]:
with open('demo.csv', "r") as f:
    for l in f.readlines():
        print l


Cantaloupe,CantaloupeGrapeSweetnessGreenSmoothie

Spinach,CantaloupeGrapeSweetnessGreenSmoothie

Grapes,CantaloupeGrapeSweetnessGreenSmoothie

Water,CantaloupeGrapeSweetnessGreenSmoothie

AlmondMilk,CantaloupeGrapeSweetnessGreenSmoothie

CoconutOil,CantaloupeGrapeSweetnessGreenSmoothie

Banana,ChocolateCoveredCherryGreenSmoothie

Spinach,ChocolateCoveredCherryGreenSmoothie

CacaoPowder,ChocolateCoveredCherryGreenSmoothie

Cinnamon,ChocolateCoveredCherryGreenSmoothie

Cherries,ChocolateCoveredCherryGreenSmoothie

AlmondMilk,ChocolateCoveredCherryGreenSmoothie

Peaches,StrawberryPeachRefresherGreenSmoothie

Strawberry,StrawberryPeachRefresherGreenSmoothie

AlmondMilk,StrawberryPeachRefresherGreenSmoothie

BokChoy,StrawberryPeachRefresherGreenSmoothie

GroundCinnamon,ThanksgivingInYourMouthGreenSmoothie

Spinach,ThanksgivingInYourMouthGreenSmoothie

Water,ThanksgivingInYourMouthGreenSmoothie

GroundNutmeg,ThanksgivingInYourMouthGreenSmoothie

SweetPotato,ThanksgivingInYourMouthGreenSmoothie

Mango,ThanksgivingInYourMouthGreenSmoothie

AlmondMilk,ThanksgivingInYourMouthGreenSmoothie

Banana,KiwiStrawberryTwistGreenSmoothie

OrangeJuice,KiwiStrawberryTwistGreenSmoothie

Kiwi,KiwiStrawberryTwistGreenSmoothie

LemonJuice,KiwiStrawberryTwistGreenSmoothie

Strawberry,KiwiStrawberryTwistGreenSmoothie

Kale,KiwiStrawberryTwistGreenSmoothie

Spinach,SPASkinCleanseGreenSmoothie

CoconutWater,SPASkinCleanseGreenSmoothie

Pineapple,SPASkinCleanseGreenSmoothie

Avocado,SPASkinCleanseGreenSmoothie

Spinach,PinaColadaGreenSmoothie

CoconutWater,PinaColadaGreenSmoothie

Pineapple,PinaColadaGreenSmoothie

CoconutFlakes,PinaColadaGreenSmoothie

AlmondMilk,PinaColadaGreenSmoothie

Banana,CranberryKaleCoolerGreenSmoothie

Water,CranberryKaleCoolerGreenSmoothie

BloodOrange,CranberryKaleCoolerGreenSmoothie

Kale,CranberryKaleCoolerGreenSmoothie

CranberryJuice,CranberryKaleCoolerGreenSmoothie

Lime,CranberryKaleCoolerGreenSmoothie

Banana,BerryCherryJubileeGreenSmoothie

Spinach,BerryCherryJubileeGreenSmoothie

Water,BerryCherryJubileeGreenSmoothie

Raspberries,BerryCherryJubileeGreenSmoothie

Cherries,BerryCherryJubileeGreenSmoothie

Blueberry,BerryCherryJubileeGreenSmoothie

Spinach,StrawberryAndMintPopsicle

honey,StrawberryAndMintPopsicle

Mint,StrawberryAndMintPopsicle

CoconutMilk,StrawberryAndMintPopsicle

Strawberry,StrawberryAndMintPopsicle

Banana,HealingCranberryCleanserGreenSmoothie

Water,HealingCranberryCleanserGreenSmoothie

Kale,HealingCranberryCleanserGreenSmoothie

Orange,HealingCranberryCleanserGreenSmoothie

Cranberries,HealingCranberryCleanserGreenSmoothie

Banana,BerryProteinBashGreenSmoothie

Spinach,BerryProteinBashGreenSmoothie

Strawberry,BerryProteinBashGreenSmoothie

Almond,BerryProteinBashGreenSmoothie

AlmondMilk,BerryProteinBashGreenSmoothie

Blueberry,BerryProteinBashGreenSmoothie

Banana,KiwiBerryPunchGreenSmoothie

Rasberry,KiwiBerryPunchGreenSmoothie

Spinach,KiwiBerryPunchGreenSmoothie

Water,KiwiBerryPunchGreenSmoothie

Kiwi,KiwiBerryPunchGreenSmoothie

Avocado,KiwiBerryPunchGreenSmoothie

Blueberry,KiwiBerryPunchGreenSmoothie

Carrot,BeginnersLuckWithTopsGreenSmoothie

Banana,BeginnersLuckWithTopsGreenSmoothie

Spinach,BeginnersLuckWithTopsGreenSmoothie

Water,BeginnersLuckWithTopsGreenSmoothie

Pineapple,BeginnersLuckWithTopsGreenSmoothie

Mango,BeginnersLuckWithTopsGreenSmoothie

GroundCinnamon,ASweetPearGreenSmoothie

Banana,ASweetPearGreenSmoothie

Spinach,ASweetPearGreenSmoothie

Pear,ASweetPearGreenSmoothie

AlmondMilk,ASweetPearGreenSmoothie

Banana,StrawberryBananaBlueberryGreenSmoothie

OrangeJuice,StrawberryBananaBlueberryGreenSmoothie

Spinach,StrawberryBananaBlueberryGreenSmoothie

Water,StrawberryBananaBlueberryGreenSmoothie

Strawberry,StrawberryBananaBlueberryGreenSmoothie

Blueberry,StrawberryBananaBlueberryGreenSmoothie

Spinach,SunshineInAJarGreenSmoothie

NavelOrangeJuice,SunshineInAJarGreenSmoothie

Pineapple,SunshineInAJarGreenSmoothie

Collards,SunshineInAJarGreenSmoothie

Banana,AvoBananaKaleGreenSmoothie

Water,AvoBananaKaleGreenSmoothie

Kale,AvoBananaKaleGreenSmoothie

avocado,AvoBananaKaleGreenSmoothie

Spinach,PeachCoconutDreamGreenSmoothie

CoconutWater,PeachCoconutDreamGreenSmoothie

Grape,PeachCoconutDreamGreenSmoothie

Peache,PeachCoconutDreamGreenSmoothie

Banana,AlmondButterAndJellyGreenSmoothie

AlmondButter,AlmondButterAndJellyGreenSmoothie

Spinach,AlmondButterAndJellyGreenSmoothie

Grape,AlmondButterAndJellyGreenSmoothie

AlmondMilk,AlmondButterAndJellyGreenSmoothie

Banana,PomegranateCitrusPunchGreenSmoothie

OrangeJuice,PomegranateCitrusPunchGreenSmoothie

Spinach,PomegranateCitrusPunchGreenSmoothie

Water,PomegranateCitrusPunchGreenSmoothie

PomegranateSeeds,PomegranateCitrusPunchGreenSmoothie

Celery,MangoGingerImmuneSupportGreenSmoothie

Lemon,MangoGingerImmuneSupportGreenSmoothie

Parsley,MangoGingerImmuneSupportGreenSmoothie

Ginger,MangoGingerImmuneSupportGreenSmoothie

Water,MangoGingerImmuneSupportGreenSmoothie

CucumberPeeled,MangoGingerImmuneSupportGreenSmoothie

Mango,MangoGingerImmuneSupportGreenSmoothie

Cilantro,CilantroMangoDetoxGreenSmoothie

Spinach,CilantroMangoDetoxGreenSmoothie

Water,CilantroMangoDetoxGreenSmoothie

Pineapple,CilantroMangoDetoxGreenSmoothie

Avocado,CilantroMangoDetoxGreenSmoothie

Mango,CilantroMangoDetoxGreenSmoothie

Cantaloupe,FreeRadicalFightingSuperGreenSmoothie

Spinach,FreeRadicalFightingSuperGreenSmoothie

Mint,FreeRadicalFightingSuperGreenSmoothie

Water,FreeRadicalFightingSuperGreenSmoothie

Apple,FreeRadicalFightingSuperGreenSmoothie

LimeJuice,FreeRadicalFightingSuperGreenSmoothie

Blueberry,FreeRadicalFightingSuperGreenSmoothie

Banana,PineappleKaleCoconutOilGreenSmoothie

Water,PineappleKaleCoconutOilGreenSmoothie

Kale,PineappleKaleCoconutOilGreenSmoothie

Pineapple,PineappleKaleCoconutOilGreenSmoothie

CoconutOil,PineappleKaleCoconutOilGreenSmoothie

Banana,BeginnersLuck

Spinach,BeginnersLuck

Water,BeginnersLuck

Pineapple,BeginnersLuck

Mango,BeginnersLuck

Banana,CilantroLimeadeGreenSmoothie

Cilantro,CilantroLimeadeGreenSmoothie

Spinach,CilantroLimeadeGreenSmoothie

Ginger,CilantroLimeadeGreenSmoothie

Water,CilantroLimeadeGreenSmoothie

Lime,CilantroLimeadeGreenSmoothie


In [ ]: