Skip to content

Commit 4237c65

Browse files
committed
wip: move first nipype commands to a group command
1 parent 0f9a620 commit 4237c65

File tree

9 files changed

+180
-204
lines changed

9 files changed

+180
-204
lines changed

bin/nipype_cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!python
22
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
45
import sys
56
from nipype.utils.nipype_cmd import main
67

8+
import click
9+
710
if __name__ == '__main__':
811
main(sys.argv)

bin/nipype_crash_search

Lines changed: 0 additions & 82 deletions
This file was deleted.

bin/nipype_display_crash

Lines changed: 0 additions & 85 deletions
This file was deleted.

bin/nipype_display_pklz

Lines changed: 0 additions & 36 deletions
This file was deleted.

nipype/scripts/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

nipype/scripts/cli.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!python
2+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3+
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
5+
import click
6+
7+
8+
@click.group()
9+
def cli():
10+
pass
11+
12+
13+
@cli.command()
14+
@click.argument('logdir', type=str)
15+
@click.option('-r', '--regex', type=str, default='*',
16+
help='Regular expression to be searched in each traceback.')
17+
def search(logdir, regex):
18+
"""Search for tracebacks content.
19+
20+
Search for traceback inside a folder of nipype crash log files that match
21+
a given regular expression.
22+
23+
Examples:
24+
nipype search -d nipype/wd/log -r '.*subject123.*'
25+
"""
26+
import re
27+
from .crash_files import iter_tracebacks
28+
29+
rex = re.compile(regex, re.IGNORECASE)
30+
for file, trace in iter_tracebacks(logdir):
31+
if rex.search(trace):
32+
click.echo("-" * len(file))
33+
click.echo(file)
34+
click.echo("-" * len(file))
35+
click.echo(trace)
36+
37+
38+
@cli.command()
39+
@click.argument('crashfile', type=str)
40+
@click.option('-r', '--rerun', is_flag=True, flag_value=True,
41+
help='Rerun crashed node.')
42+
@click.option('-d', '--debug', is_flag=True, flag_value=True,
43+
help='Enable Python debugger when re-executing.')
44+
@click.option('-i', '--ipydebug', is_flag=True, flag_value=True,
45+
help='Enable IPython debugger when re-executing.')
46+
@click.option('--dir', type=str,
47+
help='Directory where to run the node in.')
48+
def crash(crashfile, rerun, debug, ipydebug, directory):
49+
"""Display Nipype crash files.
50+
51+
For certain crash files, one can rerun a failed node in a temp directory.
52+
53+
Examples:
54+
nipype crash crashfile.pklz
55+
nipype crash crashfile.pklz -r -i
56+
nipype crash crashfile.pklz -r -i
57+
"""
58+
from .crash_files import display_crash_file
59+
60+
debug = 'ipython' if ipydebug else debug
61+
if debug == 'ipython':
62+
import sys
63+
from IPython.core import ultratb
64+
sys.excepthook = ultratb.FormattedTB(mode='Verbose',
65+
color_scheme='Linux',
66+
call_pdb=1)
67+
display_crash_file(crashfile, rerun, debug, directory)
68+
69+
70+
@cli.command()
71+
@click.argument('pklz_file', type=str)
72+
def show(pklz_file):
73+
"""Print the content of Nipype node .pklz file.
74+
75+
Examples:
76+
nipype show node.pklz
77+
"""
78+
from pprint import pprint
79+
from ..utils.filemanip import loadpkl
80+
81+
pkl_data = loadpkl(pklz_file)
82+
pprint(pkl_data)

nipype/scripts/crash_files.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""Utilities to manipulate and search through .pklz crash files."""
2+
3+
import re
4+
import sys
5+
import os.path as op
6+
from glob import glob
7+
8+
from traits.trait_errors import TraitError
9+
from nipype.utils.filemanip import loadcrash
10+
11+
12+
def load_pklz_traceback(crash_filepath):
13+
"""Return the traceback message in the given crash file."""
14+
try:
15+
data = loadcrash(crash_filepath)
16+
except TraitError as te:
17+
return str(te)
18+
except:
19+
raise
20+
else:
21+
return '\n'.join(data['traceback'])
22+
23+
24+
def iter_tracebacks(logdir):
25+
"""Return an iterator over each file path and
26+
traceback field inside `logdir`.
27+
Parameters
28+
----------
29+
logdir: str
30+
Path to the log folder.
31+
32+
field: str
33+
Field name to be read from the crash file.
34+
35+
Yields
36+
------
37+
path_file: str
38+
39+
traceback: str
40+
"""
41+
crash_files = sorted(glob(op.join(logdir, '*.pkl*')))
42+
43+
for cf in crash_files:
44+
yield cf, load_pklz_traceback(cf)
45+
46+
47+
def display_crash_file(crashfile, rerun, debug, directory):
48+
"""display crash file content and rerun if required"""
49+
from nipype.utils.filemanip import loadcrash
50+
51+
crash_data = loadcrash(crashfile)
52+
node = None
53+
if 'node' in crash_data:
54+
node = crash_data['node']
55+
tb = crash_data['traceback']
56+
print("\n")
57+
print("File: %s" % crashfile)
58+
59+
if node:
60+
print("Node: %s" % node)
61+
if node.base_dir:
62+
print("Working directory: %s" % node.output_dir())
63+
else:
64+
print("Node crashed before execution")
65+
print("\n")
66+
print("Node inputs:")
67+
print(node.inputs)
68+
print("\n")
69+
print("Traceback: ")
70+
print(''.join(tb))
71+
print ("\n")
72+
73+
if rerun:
74+
if node is None:
75+
print("No node in crashfile. Cannot rerun")
76+
return
77+
print("Rerunning node")
78+
node.base_dir = directory
79+
node.config = {'execution': {'crashdump_dir': '/tmp'}}
80+
try:
81+
node.run()
82+
except:
83+
if debug and debug != 'ipython':
84+
import pdb
85+
pdb.post_mortem()
86+
else:
87+
raise
88+
print("\n")

0 commit comments

Comments
 (0)