From 8c852acc5281f30b8bd3ef1288b19cfa18d310f1 Mon Sep 17 00:00:00 2001 From: cmaumet Date: Wed, 15 May 2019 15:09:00 +0200 Subject: [PATCH 1/9] Add export to JSON-LD file using 1.1 spec --- nidmresults/exporter.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/nidmresults/exporter.py b/nidmresults/exporter.py index 3d54af1..94394dc 100644 --- a/nidmresults/exporter.py +++ b/nidmresults/exporter.py @@ -22,6 +22,10 @@ import tempfile import zipfile from builtins import input +# Needed for export using JSON-LD 1.1 +import pyld as ld +# Needed to read context from URL (at some point will be embeded) +import urllib.request, json class NIDMExporter(): @@ -685,13 +689,23 @@ 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) + with urllib.request.urlopen("http://purl.org/nidash/context") as url: + context = json.loads(url.read().decode()) + + # context = {"@context": + # os.path.join(TPL_DIR, "..", "nidmr.json")} + foo = ld.jsonld.compact(jsonld_txt, context) + with open(self.file.replace('.ttl', '.json'), "w") as fid: + fid.write(json.dumps(foo, indent=2)) + # 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: From ecb4f4556871632016000b09524d41bdd6cdcf38 Mon Sep 17 00:00:00 2001 From: cmaumet Date: Wed, 15 May 2019 15:09:38 +0200 Subject: [PATCH 2/9] Add pyld to requirements --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 72d2c42..ba0ce77 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ rdflib-jsonld prov>=1.5.0 nibabel numpy -future \ No newline at end of file +future +pyld \ No newline at end of file From 63a9d8d53a99b2ac701d5dcaa16d86da0b0e6acb Mon Sep 17 00:00:00 2001 From: cmaumet Date: Wed, 15 May 2019 15:18:03 +0200 Subject: [PATCH 3/9] Directly read context from URL --- nidmresults/exporter.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/nidmresults/exporter.py b/nidmresults/exporter.py index 94394dc..39cb4d4 100644 --- a/nidmresults/exporter.py +++ b/nidmresults/exporter.py @@ -24,8 +24,7 @@ from builtins import input # Needed for export using JSON-LD 1.1 import pyld as ld -# Needed to read context from URL (at some point will be embeded) -import urllib.request, json +import json class NIDMExporter(): @@ -697,14 +696,11 @@ def save_prov_to_files(self, showattributes=False): jsonld_fid.write(jsonld_txt) # JSON-LD using specification 1.1 (a.k.a "nice" JSON-LD) - with urllib.request.urlopen("http://purl.org/nidash/context") as url: - context = json.loads(url.read().decode()) - - # context = {"@context": - # os.path.join(TPL_DIR, "..", "nidmr.json")} - foo = ld.jsonld.compact(jsonld_txt, context) - with open(self.file.replace('.ttl', '.json'), "w") as fid: - fid.write(json.dumps(foo, indent=2)) + jsonld_11 = ld.jsonld.compact( + json.loads(jsonld_txt), "http://purl.org/nidash/context") + jsonld_11_file = os.path.join(self.export_dir, 'nidm.json') + with open(jsonld_11_file, "w") as fid: + fid.write(json.dumps(jsonld_11, indent=2)) # provjsonld_file = os.path.join(self.export_dir, 'nidm.provjsonld') # provjsonld_txt = self.doc.serialize(format='jsonld') From 1e0cc96c9b2dba1c27fb7472991ddac66649ef64 Mon Sep 17 00:00:00 2001 From: cmaumet Date: Wed, 15 May 2019 15:51:35 +0200 Subject: [PATCH 4/9] fix PEP8 issue --- nidmresults/exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nidmresults/exporter.py b/nidmresults/exporter.py index 39cb4d4..f5a941a 100644 --- a/nidmresults/exporter.py +++ b/nidmresults/exporter.py @@ -23,7 +23,7 @@ import zipfile from builtins import input # Needed for export using JSON-LD 1.1 -import pyld as ld +import pyld as ld import json From 2f0f8f9634bf68db0929826c4581b1e0c03d9f16 Mon Sep 17 00:00:00 2001 From: cmaumet Date: Thu, 23 May 2019 11:26:16 +0200 Subject: [PATCH 5/9] Add requests as requirement (needed by pyld) --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index ba0ce77..b01a105 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ prov>=1.5.0 nibabel numpy future +requests pyld \ No newline at end of file From 395f0c71c35549feb3fbaff451e93dac1a6da5cf Mon Sep 17 00:00:00 2001 From: cmaumet Date: Fri, 24 May 2019 10:48:55 +0200 Subject: [PATCH 6/9] Compatibility py2 & py3 --- nidmresults/exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nidmresults/exporter.py b/nidmresults/exporter.py index f5a941a..bfde1f6 100644 --- a/nidmresults/exporter.py +++ b/nidmresults/exporter.py @@ -700,7 +700,7 @@ def save_prov_to_files(self, showattributes=False): json.loads(jsonld_txt), "http://purl.org/nidash/context") jsonld_11_file = os.path.join(self.export_dir, 'nidm.json') with open(jsonld_11_file, "w") as fid: - fid.write(json.dumps(jsonld_11, indent=2)) + json.dump(jsonld_11, fid, indent=2) # provjsonld_file = os.path.join(self.export_dir, 'nidm.provjsonld') # provjsonld_txt = self.doc.serialize(format='jsonld') From 48f70ab0bf039bedc051948730216765adda6b23 Mon Sep 17 00:00:00 2001 From: cmaumet Date: Fri, 24 May 2019 11:13:00 +0200 Subject: [PATCH 7/9] using __future__ to make all string unicode --> py2/py3 compatibility --- nidmresults/exporter.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nidmresults/exporter.py b/nidmresults/exporter.py index bfde1f6..54e1e32 100644 --- a/nidmresults/exporter.py +++ b/nidmresults/exporter.py @@ -8,7 +8,7 @@ @copyright: University of Warwick 2013-2014 """ - +from __future__ import unicode_literals from prov.model import ProvBundle, ProvDocument import os import datetime @@ -26,7 +26,6 @@ import pyld as ld import json - class NIDMExporter(): """ From 2b02544f64895eeb78f15cc50d22c5abc03d579a Mon Sep 17 00:00:00 2001 From: cmaumet Date: Fri, 24 May 2019 11:22:07 +0200 Subject: [PATCH 8/9] Another test --- nidmresults/exporter.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nidmresults/exporter.py b/nidmresults/exporter.py index 54e1e32..39d00c2 100644 --- a/nidmresults/exporter.py +++ b/nidmresults/exporter.py @@ -698,6 +698,8 @@ def save_prov_to_files(self, showattributes=False): jsonld_11 = ld.jsonld.compact( json.loads(jsonld_txt), "http://purl.org/nidash/context") jsonld_11_file = os.path.join(self.export_dir, 'nidm.json') + if isinstance(jsonld_11, str): + jsonld_11 = unicode(jsonld_11) with open(jsonld_11_file, "w") as fid: json.dump(jsonld_11, fid, indent=2) From 83fe0395fb012ad68eb1ee72e4ee0f9569b12985 Mon Sep 17 00:00:00 2001 From: cmaumet Date: Fri, 24 May 2019 15:44:40 +0200 Subject: [PATCH 9/9] unicode error --- nidmresults/exporter.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/nidmresults/exporter.py b/nidmresults/exporter.py index 39d00c2..0282041 100644 --- a/nidmresults/exporter.py +++ b/nidmresults/exporter.py @@ -8,7 +8,6 @@ @copyright: University of Warwick 2013-2014 """ -from __future__ import unicode_literals from prov.model import ProvBundle, ProvDocument import os import datetime @@ -25,6 +24,7 @@ # Needed for export using JSON-LD 1.1 import pyld as ld import json +import sys class NIDMExporter(): @@ -695,13 +695,18 @@ def save_prov_to_files(self, showattributes=False): jsonld_fid.write(jsonld_txt) # JSON-LD using specification 1.1 (a.k.a "nice" JSON-LD) - jsonld_11 = ld.jsonld.compact( - json.loads(jsonld_txt), "http://purl.org/nidash/context") - jsonld_11_file = os.path.join(self.export_dir, 'nidm.json') - if isinstance(jsonld_11, str): + 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: - json.dump(jsonld_11, fid, indent=2) + fid.write(jsonld_11) # provjsonld_file = os.path.join(self.export_dir, 'nidm.provjsonld') # provjsonld_txt = self.doc.serialize(format='jsonld')