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

Prefixes (Namespaces) for alphanumeric identifiers not passed to turtle serialisation #96

Open
cmaumet opened this issue Oct 19, 2016 · 4 comments

Comments

@cmaumet
Copy link
Contributor

cmaumet commented Oct 19, 2016

Hi @satra, @trungdong,

I am trying to use Namespace to specify prefixes for our alphanumeric identifiers and though this works fine with provn, it can't get it to work with the turtle serialisation. Any help would be greatly appreciated!

Here is a minimal example:

from prov.model import ProvDocument, Namespace, QualifiedName
import urllib.request
import csv

# Get a list of preferred prefixes form online csv file
csv_url = "https://raw.githubusercontent.com/incf-nidash/nidm/master/nidm/nidm-results/terms/prefixes.csv"
prefix_file = urllib.request.urlopen(
    csv_url).read().decode('utf-8').splitlines()
prefixes = dict()
reader = csv.reader(prefix_file)
for alphanum_id, prefix, uri in reader:
    prefixes[uri] = Namespace(prefix, uri)['']

## Example of prefixes dictionnary
# prefixes = dict(
#     [("http://purl.org/nidash/nidm#NIDM_0000170",
#       Namespace('nidm_groupName',
#                 'http://purl.org/nidash/nidm#NIDM_0000170')['']),
#      ("http://purl.org/nidash/nidm#NIDM_0000165",
#       Namespace('nidm_NIDMResultsExporter',
#                 'http://purl.org/nidash/nidm#NIDM_0000165')[''])],
#     )

g = ProvDocument()

group_name_uri = "http://purl.org/nidash/nidm#NIDM_0000170"
ex = Namespace('ex', 'http://example/')

g.entity(ex['group1'], {prefixes[group_name_uri]: "Group 1"})

print(g.serialize(format='provn'))
print("---")
print(g.serialize(format='rdf', rdf_format="turtle"))

and the output:

document
  prefix ex <http://example/>
  prefix nidm_groupName <http://purl.org/nidash/nidm#NIDM_0000170>

  entity(ex:group1, [nidm_groupName:="Group 1"])
endDocument

---
@prefix ex: <http://example/> .
@prefix nidm_groupName: <http://purl.org/nidash/nidm#NIDM_0000170> .
@prefix ns1: <http://purl.org/nidash/nidm#> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:group1 a prov:Entity ;
    ns1:NIDM_0000170 "Group 1"^^xsd:string .

I would like ns1:NIDM_0000170 to be replaced by nidm_groupName: in the turtle document.

@satra
Copy link
Contributor

satra commented Oct 19, 2016

PR submitted: RDFLib/rdflib#660

@satra
Copy link
Contributor

satra commented Oct 19, 2016

@cmaumet - i tested with this.

from prov.model import ProvDocument, Namespace, QualifiedName
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/incf-nidash/nidm/master/nidm/nidm-results/terms/prefixes.csv')
df['Namespace'] = df[['Preferred prefix', 'URI']].apply(lambda x: Namespace(x['Preferred prefix'], x['URI']), axis=1)
prefixes = dict(df[['URI', 'Namespace']].values)
g = ProvDocument()

group_name_uri = "http://purl.org/nidash/nidm#NIDM_0000170"

for prefix in prefixes:
    if prefix == group_name_uri:
        g.add_namespace(prefixes[prefix])

group_name_uri = "http://purl.org/nidash/nidm#NIDM_0000170"
ex = Namespace('ex', 'http://example/')
g.entity(ex['group1'], {group_name_uri: "Group 1"})

print(g.serialize(format='provn'))
print("---")
print(g.serialize(format='rdf', rdf_format="turtle"))

@cmaumet
Copy link
Contributor Author

cmaumet commented Oct 20, 2016

Thank you @satra!

@satra
Copy link
Contributor

satra commented May 4, 2017

this has been superceded by this PR, which takes performance into account: RDFLib/rdflib#649

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants