Comman Line Interface

85 views
Skip to first unread message

Yasin Yazıcı

unread,
Jul 7, 2014, 1:49:27 AM7/7/14
to nipy...@googlegroups.com
Hi,

I have some basic questions about CommandLine interface. The code above is used as template for this post. Name of the script is CommanLineInterface.py and terminal is used to run it on a Ubuntu desktop.

from nipype.interfaces.base import CommandLine
cli
= CommandLine(command='...')
print cli.cmdline
cli
.run()


When command = 'pwd', it shows current location and the result is the same with typing 'pwd' to terminal.

When command = 'gzip example.py', it compresses example.py and the result is the same with typing 'gzip example.py' to terminal.

When command = 'gzip', it gets stuck at   File "/usr/local/lib/python2.7/dist-packages/nipype/interfaces/base.py", line 1126, in _process
    res = select.select(streams, [], [], timeout)
However, this is not exactly the same with typing gzip to terminal :
          gzip: compressed data not written to a terminal. Use -f to force compression. For help, type: gzip -h


When command = 'matlab', it gets stuck at the same line. When I type 'matlab' to terminal it gets open.

When command = 'mipav', it gets stuck at the same line . When I type 'mipav' to terminal it gets open.

I just thought whatever you write into "
(command='...')" gives the same response with typing it into terminal directly. However, the result are not he same. How CommanLine exactly works? What should I do to replicate the same behaviour?

Kind Regards
Yasin

Satrajit Ghosh

unread,
Jul 7, 2014, 2:52:00 PM7/7/14
to nipy-user
hi yasin,

gzip, matlab, (i don't know about mipav) are all interactive commands when typed at a terminal - because they determine the pty type and try to alter their behavior. 

commandline executables should be able to run and exit gracefully or with an error when the full command is specified. if they start looking for terminal input or generate a gui to perform operations, then it won't work properly.

so interactive processes should be avoided with command line, unless there is a way to launch them and then exit from the process.

cheers,

satra

--

---
You received this message because you are subscribed to the Google Groups "NiPy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nipy-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yasin Yazıcı

unread,
Jul 15, 2014, 1:09:12 AM7/15/14
to nipy...@googlegroups.com

Hi Satra

I have also another question that is related with command line (at least I think so). The code below name fslhd.py which I wrapped. It calls the fslhd from fsl and print its output to 'info.txt' by using '>' operator.

from nipype.interfaces.fsl.base import FSLCommand, FSLCommandInputSpec
from nipype.interfaces.base import TraitedSpec, File, traits, isdefined
import os

class FslHdInputSpec(FSLCommandInputSpec):

    xml_style
= traits.Bool(desc='print an XML-style', argstr='-x', position=0)

    in_file
= File(desc="input file", exists=True, argstr="%s", position=1, mandatory=True)

    out_text_name
= traits.Str(desc='output text name', argstr='> %s', position=2)

   
#out_file = File(desc="output file", argstr="%s")

class FslHdOutputSpec(TraitedSpec):
    out_file
= File(desc = "out file", exists = True)

class FslHd(FSLCommand):
    input_spec
= FslHdInputSpec
    output_spec
= FslHdOutputSpec
    cmd
= 'fslhd'


if __name__ == '__main__':

    f
= FslHd(xml_style = True , in_file = '/new_home/intern2012/yasin/data_sink/T2_N3rescalenorm_std.nii', out_text_name = 'info.txt')
   
print f.cmdline
    f
.run()





When I call it from terminal (python flshd.py), it prints a 'info.txt' as I wanted, but when it is implemented into a node as below it does not print the 'info.txt'.  The partial code below works without error but doesn't print a text file.

# fslhd
fslhd
= pe.Node(interface = fslhd.FslHd(), name = 'fslhd')
fslhd
.inputs.xml_style = True
fslhd
.inputs.in_file = '/new_home/intern2012/yasin/data_sink/T2_N3rescalenorm_std.nii'
fslhd
.inputs.out_text_name = 'info.txt'

fslhd
.run()

As far as I know, in the first situation the code runs through 'if __name__ == '__main__':' . In the second one it does not import 'if __name__ == '__main__':'. However non-important part is included into node. I could not understand where the problem is.

Regards
Yasin


Yasin Yazıcı

unread,
Jul 15, 2014, 11:08:29 PM7/15/14
to nipy...@googlegroups.com
Hi,

I got the answer. I didn't include ' _list_outputs(self)'

Regards
Yasin

Satrajit Ghosh

unread,
Jul 16, 2014, 9:39:12 AM7/16/14
to nipy-user
hi yasin,

something like this could use the new traits metadata. changes made are bold faced.

from nipype.interfaces.fsl.base import FSLCommand, FSLCommandInputSpec
from nipype.interfaces.base import TraitedSpec, File, traits, isdefined
import os

class FSLHdInputSpec(FSLCommandInputSpec):

    xml_style = traits.Bool(desc='print an XML-style', argstr='-x', position=0)
    in_file = File(desc="input file", exists=True, argstr="%s", position=1, mandatory=True)
    out_text_file = File(desc="output file name", argstr="> %s", position=-1, hash_files=False,
                         name_source='in_file', name_template='%s_out.txt',
                         keep_extension=True
)

class FSLHdOutputSpec(TraitedSpec):
    out_text_file = File(desc = "out file", exists = True)

class FSLHd(FSLCommand):
    input_spec = FSLHdInputSpec
    output_spec = FSLHdOutputSpec
    cmd = 'fslhd'

running it:

In [4]: res = FSLHd().run(in_file='MMRR21_template_to_MNI152.nii.gz')

In [5]: res.outputs
Out[5]:
out_text_file = /Users/satra/Downloads/MMRR21_template_to_MNI152_out.txt

In [6]: cat MMRR21_template_to_MNI152_out.txt
filename       MMRR21_template_to_MNI152.nii.gz
sizeof_hdr     348
data_type      FLOAT32
dim0           3
...


cheers,

satra

Reply all
Reply to author
Forward
0 new messages