Skip to content

Commit 038b9e9

Browse files
committed
fix: cleaned prov encoding to store dictionaries and lists as prov json
1 parent c021039 commit 038b9e9

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

nipype/interfaces/base.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -701,18 +701,28 @@ def safe_encode(x):
701701
return pm.Literal(int(x), pm.XSD['integer'])
702702
if isinstance(x, (float,)):
703703
return pm.Literal(x, pm.XSD['float'])
704+
if isinstance(x, dict):
705+
outdict = {}
706+
for key, value in x.items():
707+
encoded_value = safe_encode(value)
708+
if isinstance(encoded_value, (pm.Literal,)):
709+
outdict[key] = encoded_value.json_representation()
710+
else:
711+
outdict[key] = encoded_value
712+
return pm.Literal(json.dumps(outdict), pm.XSD['string'])
713+
if isinstance(x, list):
714+
outlist = []
715+
for value in x:
716+
encoded_value = safe_encode(value)
717+
if isinstance(encoded_value, (pm.Literal,)):
718+
outlist.append(encoded_value.json_representation())
719+
else:
720+
outlist.append(encoded_value)
721+
return pm.Literal(json.dumps(outlist), pm.XSD['string'])
704722
try:
705-
if isinstance(x, dict):
706-
outdict = {}
707-
for key, value in x.items():
708-
outdict[key] = safe_encode(value)
709-
return pm.Literal(json.dumps(outdict), pm.XSD['string'])
710-
if isinstance(x, list):
711-
outlist = [safe_encode(value) for value in x]
712-
return pm.Literal(json.dumps(outlist), pm.XSD['string'])
713-
return pm.Literal(dumps(x),
714-
nipype['pickle'])
715-
except TypeError:
723+
return pm.Literal(dumps(x), nipype['pickle'])
724+
except TypeError, e:
725+
iflogger.info(e)
716726
return pm.Literal("Could not encode", pm.XSD['string'])
717727

718728

0 commit comments

Comments
 (0)