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

Add serialisation using JSON-LD 1.1 #55

Merged
merged 11 commits into from
Jun 2, 2019
24 changes: 20 additions & 4 deletions nidmresults/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
@copyright: University of Warwick 2013-2014
"""


from prov.model import ProvBundle, ProvDocument
import os
import datetime
Expand All @@ -22,7 +21,10 @@
import tempfile
import zipfile
from builtins import input

# Needed for export using JSON-LD 1.1
import pyld as ld
import json
import sys

class NIDMExporter():

Expand Down Expand Up @@ -685,13 +687,27 @@ def save_prov_to_files(self, showattributes=False):
with open(ttl_file, 'w') as ttl_fid:
ttl_fid.write(ttl_txt)

# print(json_context)
jsonld_file = os.path.join(self.export_dir, 'nidm.json')
# JSON-LD (deprecated kept for background compatibility w/ viewers)
jsonld_file = os.path.join(self.export_dir, 'nidm_deprecated.json')
jsonld_txt = self.doc.serialize(format='rdf', rdf_format='json-ld',
context=json_context)
with open(jsonld_file, 'w') as jsonld_fid:
jsonld_fid.write(jsonld_txt)

# JSON-LD using specification 1.1 (a.k.a "nice" JSON-LD)
jsonld_11 = json.dumps(ld.jsonld.compact(
json.loads(jsonld_txt), "http://purl.org/nidash/context"))

# If python 2 convert string to unicode to avoid
# 'must be unicode not str' error
if (sys.version_info < (3, 0)):
jsonld_11 = unicode(jsonld_11)

jsonld_11_file = os.path.join(self.export_dir, 'nidm.json')

with open(jsonld_11_file, "w") as fid:
fid.write(jsonld_11)

# provjsonld_file = os.path.join(self.export_dir, 'nidm.provjsonld')
# provjsonld_txt = self.doc.serialize(format='jsonld')
# with open(provjsonld_file, 'w') as provjsonld_fid:
Expand Down
19 changes: 19 additions & 0 deletions nidmresults/objects/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ def __init__(self, oid=None):
oid = NIIRI.qname(Identifier(oid))
self.id = oid

# the next three methods build a .file property
# with a dedicated function call on "set" that can
# be used to implement external ID mappings
# e.g. DataLad can replace '_map_fileid()' to provides
# its internal file IDs based on the filename associated
# with a NIDMFile object
@property
def file(self):
return getattr(self, '_file', None)

@file.setter
def file(self, fileobj):
if isinstance(fileobj, NIDMFile):
self._map_fileid(fileobj)
self._file = fileobj

def _map_fileid(self, fileobj):
pass

def __str__(self):
value = ""
if hasattr(self, 'value'):
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ rdflib-jsonld
prov>=1.5.0
nibabel
numpy
future
future
requests
pyld