我有一个命令 nipype.interface.afni.Warp 它给了我以下 python 终端输出:
190920-12:22:00,333 nipype.interface INFO:
stderr 2019-09-20T12:22:00.333467:++ 3dWarp: AFNI version=AFNI_19.2.21 (Aug 29 2019) [64-bit]
190920-12:22:00,334 nipype.interface INFO:
stderr 2019-09-20T12:22:00.334117:++ Authored by: RW Cox
190920-12:22:00,365 nipype.interface INFO:
stderr 2019-09-20T12:22:00.365105:++ Using minimum spacing of 1.000000 mm for new grid spacing
190920-12:22:03,252 nipype.interface INFO:
stderr 2019-09-20T12:22:03.252756:++ Output dataset /media/sf_Ubuntu_files/dicomtest/warp_test.nii.gz
190920-12:22:03,253 nipype.interface INFO:
stdout 2019-09-20T12:22:03.253083:# mat44 Obliquity Transformation ::
190920-12:22:03,253 nipype.interface INFO:
stdout 2019-09-20T12:22:03.253083: 1.000000 -0.000000 0.000000 0.000000
190920-12:22:03,253 nipype.interface INFO:
stdout 2019-09-20T12:22:03.253083: 0.000000 0.999592 -0.028568 -1.842994
190920-12:22:03,253 nipype.interface INFO:
stdout 2019-09-20T12:22:03.253083: -0.000000 0.028568 0.999592 3.788057
我想在“# mat44 Obliquity Transformation ::”行下方捕获矩阵并将其写入文件。我已经在 bash 中完成了这项工作,如下所示:
3dWarp -flags_and_stuff | \grep -A 4 '# mat44 Obliquity Transformation ::' > $filename.1D
但是我想用 python 来编写上面的 bash 命令。
按照这篇博客文章的步骤,我尝试了这个:
command = ['python3' ,"nipype.interfaces.afni.Warp('more stuff').run()"]
my_env = os.environ.copy()
my_env["PATH"] = "/usr/sbin:/sbin:" + my_env["PATH"]
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=my_env)
但是当我输入时,p.communicate()
我得到:
>>> p.communicate()
(b"python3: can't open file 'nipype.interfaces.afni.Warp(<stuff>).run()': [Errno 2] No such file or directory\n", None)
如何在 python 中完成这项工作?还是在 bash 中执行更好?我正在编写的脚本将使用这条线数千次,所以无论最快(我假设这也意味着最“pythonic”)的方法是什么。