Published
Edited
Jan 3, 2022
1 star
Insert cell
Insert cell
Insert cell
kg = py`
from os.path import dirname
import os, io

namespaces = {
"nom": "http://example.org/#",
"wtm": "http://purl.org/heals/food/",
"ind": "http://purl.org/heals/ingredient/",
"skos": "http://www.w3.org/2004/02/skos/core#",
}

kg = ${kglab}.KnowledgeGraph(
name = "A recipe KG example based on Food.com",
base_uri = "https://www.food.com/recipe/",
namespaces = namespaces,
)

with io.open('recipes.ttl','wb') as f:
f.write(${await kgzip
.file("kglab-main/dat/recipes.ttl")
.arrayBuffer()}.tobytes())

kg.load_rdf("recipes.ttl") ;
kg
`
Insert cell
Insert cell
Insert cell
sparql = `
SELECT ?subject ?object
WHERE {
?subject rdf:type wtm:Recipe .
?subject wtm:hasIngredient ?object .
}`
Insert cell
Insert cell
subgraph = py`

subgraph = ${kglab}.SubgraphMatrix(${kg}, ${sparql})
subgraph`
Insert cell
nx_graph = py`
nx_graph = ${subgraph}.build_nx_graph(${nx}.DiGraph(), bipartite=True)
nx_graph`
Insert cell
Insert cell
Insert cell
py`${nx}.density(${nx_graph})`
Insert cell
Insert cell
Insert cell
py`
butter_node = ${kg}.get_ns("ind").Butter
butter_id = ${subgraph}.transform(butter_node)

edges = list(${nx}.bfs_edges(${nx_graph}, source=butter_id, depth_limit=2))
"num edges:", len(edges)`
Insert cell
Insert cell
py`
out = []
butter_node = ${kg}.get_ns("ind").Butter
butter_id = ${subgraph}.transform(butter_node)

nx_graph = ${nx_graph}
subgraph = ${subgraph}
nx = ${nx}
for edge in nx.bfs_edges(nx_graph.to_undirected(), source=butter_id, depth_limit=2):
s_id, o_id = edge

s_node = subgraph.inverse_transform(s_id)
s_label = subgraph.n3fy(s_node)

o_node = subgraph.inverse_transform(o_id)
o_label = subgraph.n3fy(o_node)

# for brevity's sake, only show non-butter nodes
if s_node != butter_node:
out.append(dict({"s_label":s_label,"o_label": o_label}))
out
`
Insert cell
Insert cell
Insert cell
ind_rec_nodes = py`
from networkx.algorithms import bipartite

rec_nodes, ind_nodes = bipartite.sets(${nx_graph})

{"rec_nodes": rec_nodes, "ingredients": ind_nodes}
`
Insert cell
Insert cell
py`
out = []
for triad, occurrences in ${nx}.triadic_census(${nx_graph}).items():
if occurrences > 0:
out.append("triad {:>4}: {:7d} occurrences".format(triad, occurrences))
out
`
Insert cell
Insert cell
Insert cell
Insert cell
ind_nodes = py`ind_nodes = ${await ind_rec_nodes.get("ingredients")}
ind_nodes`
Insert cell
centrality = py`
results = ${nx}.degree_centrality(${nx_graph})
ind_rank = {}
ind_nodes = ${await ind_rec_nodes.get("ingredients")}

out = []

for node_id, rank in sorted(results.items(), key=lambda item: item[1], reverse=True):
if node_id in ind_nodes:
ind_rank[node_id] = rank
node = ${subgraph}.inverse_transform(node_id)
label = ${subgraph}.n3fy(node)
out.append("{:6.3f} {}".format(rank, label))
out, results, ind_rank
`
Insert cell
ind_rank = centrality[2]
Insert cell
Insert cell
py`
nx = ${nx}
color = [ "red" if n in ${ind_nodes} else "blue" for n in ${nx_graph}.nodes()]

nx.draw(${nx_graph}, node_color=color, edge_color="gray", with_labels=True)
${plt}.show()
`
Insert cell
Insert cell
core_g = py`
core_g = ${nx}.k_core(${nx_graph})
core_g
`
Insert cell
core_g_NodeView = py`
node_view = ${core_g}.nodes()
repr(node_view)`
Insert cell
py`
ind_rank = ${ind_rank}
color = [ "red" if n in ${ind_nodes} else "blue" for n in ${core_g} ]
size = [ ind_rank[n] * 800 if n in ${ind_nodes} else 1 for n in ${core_g} ]

${nx}.draw(${core_g}, node_color=color, node_size=size, edge_color="gray", with_labels=True)
${plt}.show()`
Insert cell
popularIngredients = py`
ind_rank = ${ind_rank}
core_g = ${core_g}
subgraph = ${subgraph}
out = ['\\n node_id rank label']

for node_id, rank in sorted(ind_rank.items(), key=lambda item: item[1], reverse=True):
if node_id in core_g:
node = subgraph.inverse_transform(node_id)
label = subgraph.n3fy(node)
out.append("{:3} {:6.3f} {}".format(node_id, rank, label))

# join the output into one block of text
'\\n'.join(out)
`
Insert cell
Insert cell
pageRanks = py`
nx = ${nx}
subgraph = ${subgraph}
nx_graph = ${nx_graph}
core_g = ${core_g}
ind_nodes = ${ind_nodes}
out = ['\\nnode_id rank page_rank label']

page_rank = nx.pagerank(nx_graph)

for node_id, rank in sorted(page_rank.items(), key=lambda item: item[1], reverse=True):
if node_id in core_g and node_id in ind_nodes:
node = subgraph.inverse_transform(node_id)
label = subgraph.n3fy(node)
out.append("{:3} {:6.3f} {:6.3f} {}".format(node_id, rank, page_rank[node_id], label))

# join the output into one block of text
'\\n'.join(out)
`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
nx = py`import networkx as nx
nx`
Insert cell
kglab = py`
# make completely sure pyvis got included
import micropip
micropip.install("https://thadk.net/pyodide_packages/pyodide_18_1/pyvis-0.1.9-py2.py3-none-any.whl")

import kglab
# ${otherKglabDependencies} ${kglabDependency} ${micropipDependenciesPy} ${packagesPy}
kglab
`
Insert cell
py`dir(${kglab})`
Insert cell
py`repr(${kglab})`
Insert cell
Insert cell
/* Patch plt.show() to work in Observable from https://observablehq.com/@gnestor/pyodide-demo */
plt = py`
from matplotlib import pyplot as plt
import types
import io
import base64
from js import document

def show(self):
buf = io.BytesIO()
self.savefig(buf, format='png')
buf.seek(0)
img_str = 'data:image/png;base64,' + base64.b64encode(buf.read()).decode('UTF-8')
el = document.createElement('img')
el.src = img_str
return el

plt._show = types.MethodType(plt.show, plt)
plt.show = types.MethodType(show, plt)

plt`
Insert cell
import { py, pyodide, downloadPyoFile } from "@thadk/pyodide-18"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more