Skip to content

Commit c69d4ad

Browse files
authored
Merge pull request #3026 from oesteban/fix/3014
ENH: Lightweight node cache checking
2 parents 4c414b8 + e7a6200 commit c69d4ad

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

nipype/pipeline/engine/nodes.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,27 +293,29 @@ def is_cached(self, rm_outdated=False):
293293
"""
294294
outdir = self.output_dir()
295295

296-
# Update hash
297-
hashed_inputs, hashvalue = self._get_hashval()
298-
299296
# The output folder does not exist: not cached
300-
if not op.exists(outdir):
301-
logger.debug('[Node] Directory not found "%s".', outdir)
297+
if not op.exists(outdir) or \
298+
not op.exists(op.join(outdir, 'result_%s.pklz' % self.name)):
299+
logger.debug('[Node] Not cached "%s".', outdir)
302300
return False, False
303301

304-
hashfile = op.join(outdir, '_0x%s.json' % hashvalue)
305-
cached = op.exists(hashfile)
306-
307-
# Check if updated
302+
# Check if there are hashfiles
308303
globhashes = glob(op.join(outdir, '_0x*.json'))
309304
unfinished = [
310305
path for path in globhashes
311306
if path.endswith('_unfinished.json')
312307
]
313308
hashfiles = list(set(globhashes) - set(unfinished))
309+
310+
# Update hash
311+
hashed_inputs, hashvalue = self._get_hashval()
312+
313+
hashfile = op.join(outdir, '_0x%s.json' % hashvalue)
314314
logger.debug('[Node] Hashes: %s, %s, %s, %s',
315315
hashed_inputs, hashvalue, hashfile, hashfiles)
316316

317+
cached = hashfile in hashfiles
318+
317319
# No previous hashfiles found, we're all set.
318320
if cached and len(hashfiles) == 1:
319321
assert(hashfile == hashfiles[0])
@@ -387,17 +389,17 @@ def hash_exists(self, updatehash=False):
387389
return cached, self._hashvalue, hashfile, self._hashed_inputs
388390

389391
def run(self, updatehash=False):
390-
"""Execute the node in its directory.
392+
"""
393+
Execute the node in its directory.
391394
392395
Parameters
393396
----------
394-
395397
updatehash: boolean
396398
When the hash stored in the output directory as a result of a previous run
397399
does not match that calculated for this execution, updatehash=True only
398400
updates the hash without re-running.
399-
"""
400401
402+
"""
401403
if self.config is None:
402404
self.config = {}
403405
self.config = merge_dict(deepcopy(config._sections), self.config)
@@ -441,6 +443,11 @@ def run(self, updatehash=False):
441443
for outdatedhash in glob(op.join(self.output_dir(), '_0x*.json')):
442444
os.remove(outdatedhash)
443445

446+
# _get_hashval needs to be called before running. When there is a valid (or seemingly
447+
# valid cache), the is_cached() member updates the hashval via _get_hashval.
448+
# However, if this node's folder doesn't exist or the result file is not found, then
449+
# the hashval needs to be generated here. See #3026 for a larger context.
450+
self._get_hashval()
444451
# Hashfile while running
445452
hashfile_unfinished = op.join(
446453
outdir, '_0x%s_unfinished.json' % self._hashvalue)

0 commit comments

Comments
 (0)