Published
Edited
Jan 3, 2022
Insert cell
Insert cell
Insert cell
kg = py`from os.path import dirname
import os
import io
namespaces = {
"foaf": "http://xmlns.com/foaf/0.1/",
"gorm": "http://example.org/sagas#",
"rel": "http://purl.org/vocab/relationship/",
}

kg = ${kglab}.KnowledgeGraph(
name = "Happy Vikings KG example for SKOS/OWL inference",
namespaces=namespaces,
)
with open("gorm.ttl", "wb") as f:
f.write(${await kgzip
.file("kglab-main/dat/gorm.ttl")
.arrayBuffer()}.tobytes())

kg.load_rdf("gorm.ttl")
kg`
Insert cell
py`repr(${kg})`
Insert cell
py`
text = ${kg}.save_rdf_text()
text`
Insert cell
Insert cell
py`
${pd}.set_option("max_rows", None)

measure = ${kglab}.Measure()
measure.measure_graph(${kg})

{"edges": measure.get_edge_count(),
"nodes": measure.get_node_count()}
`
Insert cell
Insert cell
ancestor_df = py`sparql = """
SELECT ?elder ?viking
WHERE {
?elder gorm:ancestorOf ?viking
}
ORDER BY ASC(?viking)
"""

df = ${kg}.query_as_df(sparql)
df`
Insert cell
// ancestor_df.to_html() /* blank */
Insert cell
Insert cell
spouse_df = py`
sparql = """
SELECT ?viking1 ?viking2
WHERE {
?viking1 gorm:spouseOf ?viking2
}
"""

df = ${kg}.query_as_df(sparql)
df`
Insert cell
table(spouse_df.to_dict("records").toJs().map(Object.fromEntries))
Insert cell
Insert cell
fighting_df = py`sparql = """
SELECT ?viking ?hobby
WHERE {
?viking foaf:topic_interest ?hobby .
gorm:Fighting skos:narrower ?hobby
}
ORDER BY ASC(?viking)
"""

df = ${kg}.query_as_df(sparql)
df`
Insert cell
table(fighting_df.to_dict("records").toJs().map(Object.fromEntries))
Insert cell
Insert cell
Insert cell
kg_after_infer = py`
kg = ${kg.copy()}
kg.infer_owlrl_closure()
kg`
Insert cell
py`
kg = ${kg_after_infer}
text = kg.save_rdf_text()
text`
Insert cell
Insert cell
py`
measure = ${kglab}.Measure()
measure.measure_graph(${kg_after_infer})

{"edges": measure.get_edge_count(),
"nodes": measure.get_node_count()}`
Insert cell
Insert cell
closure_ancestors_df = py`
kg = ${kg_after_infer}
sparql = """
SELECT ?elder_viking ?viking
WHERE {
?elder_viking gorm:ancestorOf ?viking
}
ORDER BY ASC(?viking)
"""

df = kg.query_as_df(sparql)
df`
Insert cell
table(closure_ancestors_df.to_dict("records").toJs().map(Object.fromEntries))
Insert cell
closure_spouse_df = py`
kg = ${kg_after_infer}
sparql = """
SELECT ?viking1 ?viking2
WHERE {
?viking1 gorm:spouseOf ?viking2
}
"""

df = kg.query_as_df(sparql)
df`
Insert cell
table(closure_spouse_df.to_dict("records").toJs().map(Object.fromEntries))
Insert cell
Insert cell
Insert cell
spo_tally = py`
measure = ${kglab}.Measure()
measure.measure_graph(${kg_after_infer})
measure.s_gen.get_tally()
`
Insert cell
spo_tally.to_dict().toJs()
Insert cell
p_tally = py`
measure = ${kglab}.Measure()
measure.measure_graph(${kg_after_infer})
measure.p_gen.get_tally()
`
Insert cell
p_tally.to_dict().toJs()
Insert cell
o_tally = py`
measure = ${kglab}.Measure()
measure.measure_graph(${kg_after_infer})
measure.o_gen.get_tally()
`
Insert cell
o_tally.to_dict().toJs()
Insert cell
Insert cell
kg_after_skosify = py`
kg = ${kg_after_infer.copy()}
kg.infer_skos_related()
kg.infer_skos_hierarchical(narrower=True)
kg.infer_skos_transitive(narrower=True)
kg
`
Insert cell
py`
measure = ${kglab}.Measure()
measure.measure_graph(${kg_after_skosify})

{"edges": measure.get_edge_count(),
"nodes": measure.get_node_count()}
`
Insert cell
Insert cell
fight_skosify = py`
kg= ${kg_after_skosify}

sparql = """
SELECT ?viking ?hobby
WHERE {
?viking foaf:topic_interest ?hobby .
gorm:Fighting skos:narrower ?hobby
}
ORDER BY ASC(?viking)
"""

df = kg.query_as_df(sparql)
df`
Insert cell
table(fight_skosify.to_dict("records").toJs().map(Object.fromEntries))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
pd = py`import pandas as pd
pd
`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
micropipPyvis = py` #depends on ${packagesPy}
# 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")
`
Insert cell
kglab = py` #depends on ${micropipPyvis}


import kglab
# ${otherKglabDependencies} ${kglabDependency} ${micropipDependenciesPy}
kglab
`
Insert cell
py`dir(${kglab})`
Insert cell
py`repr(${kglab})`
Insert cell
Insert cell
import { py, pyodide } 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