Skip to content

Commit c7e3fdb

Browse files
committed
fix: provenance tracking improvements with hash generation
1 parent c9df801 commit c7e3fdb

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

nipype/pipeline/engine.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,10 @@ def run(self, plugin=None, plugin_args=None, updatehash=False):
689689
runner.run(execgraph, updatehash=updatehash, config=self.config)
690690
datestr = datetime.utcnow().strftime('%Y%m%dT%H%M%S')
691691
if str2bool(self.config['execution']['write_provenance']):
692-
write_workflow_prov(execgraph,
693-
os.path.join(self.base_dir,
694-
'workflow_provenance_%s' % datestr),
695-
format='all')
692+
prov_base = os.path.join(self.base_dir,
693+
'workflow_provenance_%s' % datestr)
694+
logger.info('Provenance file prefix: %s' % prov_base)
695+
write_workflow_prov(execgraph, prov_base, format='all')
696696
return execgraph
697697

698698
# PRIVATE API AND FUNCTIONS

nipype/pipeline/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,11 @@ def merge_dict(d1, d2, merge=lambda x, y: y):
10701070
return result
10711071

10721072

1073+
def merge_bundles(g1, g2):
1074+
for rec in g2.get_records():
1075+
g1._add_record(rec)
1076+
return g1
1077+
10731078
def write_workflow_prov(graph, filename=None, format='turtle'):
10741079
"""Write W3C PROV Model JSON file
10751080
"""
@@ -1102,12 +1107,12 @@ def write_workflow_prov(graph, filename=None, format='turtle'):
11021107
if isdefined(values):
11031108
subresult.outputs[key] = values[idx]
11041109
sub_bundle = ProvStore().add_results(subresult)
1105-
ps.g.add_bundle(sub_bundle)
1110+
ps.g = merge_bundles(ps.g, sub_bundle)
11061111
ps.g.wasGeneratedBy(sub_bundle, process)
11071112
else:
11081113
process.add_extra_attributes({pm.PROV["type"]: nipype_ns["Node"]})
11091114
result_bundle = ProvStore().add_results(result)
1110-
ps.g.add_bundle(result_bundle)
1115+
ps.g = merge_bundles(ps.g, result_bundle)
11111116
ps.g.wasGeneratedBy(result_bundle, process)
11121117
processes.append(process)
11131118

nipype/utils/provenance.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
get_id = lambda: niiri[uuid1().hex]
3232

3333
def get_attr_id(attr, skip=None):
34-
dictwithhash, hashval = get_hashval(attr, skip=None)
34+
dictwithhash, hashval = get_hashval(attr, skip=skip)
3535
return niiri[hashval]
3636

3737
max_text_len = 1024000
@@ -59,10 +59,19 @@ def get_hashval(inputdict, skip=None):
5959

6060
dict_withhash = {}
6161
dict_nofilename = OrderedDict()
62-
for name, val in sorted(inputdict.items()):
63-
if skip is not None and name in skip:
62+
keys = {}
63+
for key in inputdict:
64+
if skip is not None and key in skip:
6465
continue
65-
outname = name.get_uri()
66+
keys[key.get_uri()] = key
67+
for key in sorted(keys):
68+
val = inputdict[keys[key]]
69+
outname = key
70+
try:
71+
if isinstance(val, pm.URIRef):
72+
val = val.decode()
73+
except AttributeError:
74+
pass
6675
if isinstance(val, pm.QName):
6776
val = val.get_uri()
6877
if isinstance(val, pm.Literal):
@@ -180,14 +189,19 @@ def prov_encode(graph, value, create_container=True):
180189
entities = []
181190
for item in value:
182191
item_entity = prov_encode(graph, item)
183-
if 'file://' not in item_entity.get_value():
184-
raise ValueError('No file found')
185192
entities.append(item_entity)
193+
if isinstance(item, list):
194+
continue
195+
if not isinstance(item_entity.get_value()[0], basestring):
196+
raise ValueError('Not a string literal')
197+
if 'file://' not in item_entity.get_value()[0]:
198+
raise ValueError('No file found')
186199
id = get_id()
187200
entity = graph.collection(identifier=id)
188201
for item_entity in entities:
189-
graph.hadMember(id, item_entity.get_identifier())
190-
except ValueError:
202+
graph.hadMember(id, item_entity)
203+
except ValueError, e:
204+
iflogger.debug(e)
191205
entity = prov_encode(graph, value, create_container=False)
192206
else:
193207
entity = prov_encode(graph, value[0])

0 commit comments

Comments
 (0)