PyLiPD utilities ===============+

The following describes the utilities supporting the main Pylipd PyLiPD User APIs:

RDFGraph (pylipd.utils.rdf_graph.RDFGraph)

class pylipd.rdf_graph.RDFGraph(graph=None)

The RDF Graph class contains an RDF Graph using the RDFLib library, and allows querying over it

Examples

from pylipd.utils.rdf_graph import RDFGraph

# Load RDF file into graph
rdf = RDFGraph()
rdf.load(["../examples/rdf/graph.ttl"])
(result, result_df) = rdf.query("SELECT ?s ?p ?o WHERE {?s ?p ?o} LIMIT 10")
print(result_df)
                                                   s  \
0                          http://example.org/bob#me   
1                          http://example.org/bob#me   
2                          http://example.org/bob#me   
3              http://www.wikidata.org/entity/Q12418   
4                          http://example.org/bob#me   
5              http://www.wikidata.org/entity/Q12418   
6  http://data.europeana.eu/item/04802/243FA86189...   

                                                 p  \
0  http://www.w3.org/1999/02/22-rdf-syntax-ns#type   
1                      http://schema.org/birthDate   
2         http://xmlns.com/foaf/0.1/topic_interest   
3                   http://purl.org/dc/terms/title   
4                  http://xmlns.com/foaf/0.1/knows   
5                 http://purl.org/dc/terms/creator   
6                 http://purl.org/dc/terms/subject   

                                               o  
0               http://xmlns.com/foaf/0.1/Person  
1                                     1990-07-04  
2          http://www.wikidata.org/entity/Q12418  
3                                      Mona Lisa  
4                    http://example.org/alice#me  
5  http://dbpedia.org/resource/Leonardo_da_Vinci  
6          http://www.wikidata.org/entity/Q12418  

Methods

clear()

Clears the graph

copy()

Makes a copy of the object

get(ids)

Get id(s) from the graph and returns the LiPD object

get_all_graph_ids()

Get all Graph ids

load(files[, graphid])

Loads a RDF file into the graph

merge(rdf)

Merges the current LiPD object with another LiPD object

pop(ids)

Pops graph(s) from the combined graph and returns the popped RDF Graph

query(query[, remote, result])

Once data is loaded into the graph (or remote endpoint set), one can make SparQL queries to the graph

remove(ids)

Removes ids(s) from the graph

serialize()

Returns RDF quad serialization of the current combined Graph .

set_endpoint(endpoint)

Sets a SparQL endpoint for a remote Knowledge Base (example: GraphDB)

clear()

Clears the graph

copy()

Makes a copy of the object

Returns:

a copy of the original object

Return type:

pylipd.utils.rdf_graph.RDFGraph

get(ids)

Get id(s) from the graph and returns the LiPD object

Parameters:

ids (str or list of str) – graph id(s) to get.

Returns:

RDFGraph object with the retrieved graph(s)

Return type:

pylipd.utils.utils.rdf_graph.RDFGraph

Examples

from pylipd.utils.rdf_graph import RDFGraph

# Fetch RDF graph data for given id(s)
rdf = RDFGraph()
rdf.load(["../examples/rdf/graph.ttl"], graphid="http://example.org/graph")
rdf.get("http://example.org/graph")
<pylipd.utils.rdf_graph.RDFGraph at 0x7fb6c3a7b2b0>
get_all_graph_ids()

Get all Graph ids

Returns:

  • ids (list)

  • A list of graph ids

Examples

from pylipd.utils.rdf_graph import RDFGraph

# Fetch RDF Graph Data
rdf = RDFGraph()
rdf.load(["../examples/rdf/graph.ttl"], graphid="http://example.org/graph")
print(rdf.get_all_graph_ids())
['http://example.org/graph']
load(files, graphid=None)

Loads a RDF file into the graph

Parameters:

rdf_file (str) – Path to the RDF file

Examples

from pylipd.utils.rdf_graph import RDFGraph

# Load RDF file into graph
rdf = RDFGraph()
rdf.load(["../examples/rdf/graph.ttl"])
(result, result_df) = rdf.query("SELECT ?s ?p ?o WHERE {?s ?p ?o} LIMIT 10")
print(result_df)
                                                   s  \
0                          http://example.org/bob#me   
1                          http://example.org/bob#me   
2                          http://example.org/bob#me   
3              http://www.wikidata.org/entity/Q12418   
4                          http://example.org/bob#me   
5              http://www.wikidata.org/entity/Q12418   
6  http://data.europeana.eu/item/04802/243FA86189...   

                                                 p  \
0  http://www.w3.org/1999/02/22-rdf-syntax-ns#type   
1                      http://schema.org/birthDate   
2         http://xmlns.com/foaf/0.1/topic_interest   
3                   http://purl.org/dc/terms/title   
4                  http://xmlns.com/foaf/0.1/knows   
5                 http://purl.org/dc/terms/creator   
6                 http://purl.org/dc/terms/subject   

                                               o  
0               http://xmlns.com/foaf/0.1/Person  
1                                     1990-07-04  
2          http://www.wikidata.org/entity/Q12418  
3                                      Mona Lisa  
4                    http://example.org/alice#me  
5  http://dbpedia.org/resource/Leonardo_da_Vinci  
6          http://www.wikidata.org/entity/Q12418  
merge(rdf)

Merges the current LiPD object with another LiPD object

Parameters:

rdf (pylipd.rdf_graph.RDFGraph) – RDFGraph object to merge with

Returns:

merged RDFGraph object

Return type:

pylipd.utils.rdf_graph.RDFGraph

pop(ids)

Pops graph(s) from the combined graph and returns the popped RDF Graph

Parameters:

ids (str or list of str) – rdf id(s) to be popped.

Returns:

RDFGraph object with the popped graph(s)

Return type:

pylipd.utils.rdf_graph.RDFGraph

Examples

from pylipd.utils.rdf_graph import RDFGraph

# Pop RDF graph data for given id(s)
rdf = RDFGraph()
rdf.load(["../examples/rdf/graph.ttl"], graphid="http://example.org/graph")
popped = rdf.pop("http://example.org/graph")
query(query, remote=False, result='sparql')

Once data is loaded into the graph (or remote endpoint set), one can make SparQL queries to the graph

Parameters:
  • query (str) – SparQL query

  • remote (bool) – (Optional) If set to True, the query will be made to the remote endpoint (if set)

  • result (str) – (Optional) Result return type

Returns:

  • result (dict) – Dictionary of sparql variable and binding values

  • result_df (pandas.Dataframe) – Return the dictionary above as a pandas.Dataframe

Examples

from pylipd.utils.rdf_graph import RDFGraph

rdf = RDFGraph()
rdf.load(["../examples/rdf/graph.ttl"])
query = """PREFIX le: <http://linked.earth/ontology#>
        select ?s ?p ?o where {
            ?s ?p ?o
        } LIMIT 10 """
result, result_df = rdf.query(query)
print(result_df)
                                                   s  \
0                          http://example.org/bob#me   
1                          http://example.org/bob#me   
2                          http://example.org/bob#me   
3              http://www.wikidata.org/entity/Q12418   
4                          http://example.org/bob#me   
5              http://www.wikidata.org/entity/Q12418   
6  http://data.europeana.eu/item/04802/243FA86189...   

                                                 p  \
0  http://www.w3.org/1999/02/22-rdf-syntax-ns#type   
1                      http://schema.org/birthDate   
2         http://xmlns.com/foaf/0.1/topic_interest   
3                   http://purl.org/dc/terms/title   
4                  http://xmlns.com/foaf/0.1/knows   
5                 http://purl.org/dc/terms/creator   
6                 http://purl.org/dc/terms/subject   

                                               o  
0               http://xmlns.com/foaf/0.1/Person  
1                                     1990-07-04  
2          http://www.wikidata.org/entity/Q12418  
3                                      Mona Lisa  
4                    http://example.org/alice#me  
5  http://dbpedia.org/resource/Leonardo_da_Vinci  
6          http://www.wikidata.org/entity/Q12418  
remove(ids)

Removes ids(s) from the graph

Parameters:

ids (str or list of str) – graph id(s) to be removed

Examples

from pylipd.utils.rdf_graph import RDFGraph

# Remove RDF graph data for given id(s)
rdf = RDFGraph()
rdf.load(["../examples/rdf/graph.ttl"], graphid="http://example.org/graph")
rdf.remove("http://example.org/graph")
serialize()

Returns RDF quad serialization of the current combined Graph .. rubric:: Examples

from pylipd.utils.rdf_graph import RDFGraph

# Fetch RDF data
rdf = RDFGraph()
rdf.load(["../examples/rdf/graph.ttl"], graphid="http://example.org/graph")
nquads = rdf.serialize()
print(nquads[:10000])
print("...")
<http://example.org/bob#me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> <http://example.org/graph> .
<http://example.org/bob#me> <http://schema.org/birthDate> "1990-07-04"^^<http://www.w3.org/2001/XMLSchema#date> <http://example.org/graph> .
<http://example.org/bob#me> <http://xmlns.com/foaf/0.1/topic_interest> <http://www.wikidata.org/entity/Q12418> <http://example.org/graph> .
<http://www.wikidata.org/entity/Q12418> <http://purl.org/dc/terms/title> "Mona Lisa" <http://example.org/graph> .
<http://example.org/bob#me> <http://xmlns.com/foaf/0.1/knows> <http://example.org/alice#me> <http://example.org/graph> .
<http://www.wikidata.org/entity/Q12418> <http://purl.org/dc/terms/creator> <http://dbpedia.org/resource/Leonardo_da_Vinci> <http://example.org/graph> .
<http://data.europeana.eu/item/04802/243FA8618938F4117025F17A8B813C5F9AA4D619> <http://purl.org/dc/terms/subject> <http://www.wikidata.org/entity/Q12418> <http://example.org/graph> .


...
set_endpoint(endpoint)

Sets a SparQL endpoint for a remote Knowledge Base (example: GraphDB)

Parameters:

endpoint (str) – URL for the SparQL endpoint

Examples

from pylipd.utils.rdf_graph import RDFGraph

# Fetch LiPD data from remote RDF Graph
rdf = RDFGraph()
rdf.set_endpoint("https://linkedearth.graphdb.mint.isi.edu/repositories/LiPDVerse2")
(result, result_df) = rdf.query("SELECT ?s ?p ?o WHERE {?s ?p ?o} LIMIT 10")