Get a sequence of commands from nipype workflow

Is it somehow possible to get a sequence of commands from the nipype workflow? I know that each Node with command line interface generates its own command.txt, but what’s the easiest way to gather them in one place?
Ideally, I would also like to see python utility functions operations made.

@achetverikov there currently isn’t, but you can hack together a little script that does this.

For example:

from glob import glob

def grab_command(fl): 
   with open(fl) as fp:      
       lines = fp.readlines()
   for line in lines:      
       if line.startswith('* command'):
           return line.split('* command : ')[1].strip()

workdir = '/node/working/directory/*/_report/report.rst'

# you may need to add additional paths if you have mapnodes
reports = glob(workdir)

commands = []

for report in reports:
   cmd = grab_command(report)
   if cmd:
       commands.append(cmd)

print(commands)

By parsing the report.rst, you can also get the python functions wrapped within a Function interface.

1 Like

This may not respect dependencies very well, as I don’t think glob makes any order guarantees. As a quick hack, you could do:

reports = sorted(glob(workdir), key=os.path.getmtime)

A more reliable (albeit more complicated) approach would be to recreate the execution graph, and walk the working directory according to a topological sort. Probably the easiest way to do that would be to copy the Linear plugin, but instead of running just print or otherwise save the commands.

2 Likes