Skip to content

Commit

Permalink
Merge branch 'nice_jsonld' into next_release
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaumet committed May 29, 2019
2 parents d5beaf2 + 83fe039 commit 139fe71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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
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

0 comments on commit 139fe71

Please sign in to comment.