文章目录

  • 背景
  • 配准
  • N4biasfiledcorrection

背景

项目需求:对脑影像T1结构像进行配准和偏差矫正。
采用方法:调用远程linux的ants包+使用nipype进行配准和N4矫正(本项目中有pyqt5的需求,至于怎么在pycharm下配置自行百度。)
Note:nipype提供了安装的docker,同组的师兄说可以直接下载docker就不用非得在linux上运行ants这些,但是本人对docker不熟悉(师兄也没用)就不打算这么做了。先安排个坑:docker的配置与使用,之后项目需要打包成docker环境再来补一下。
首先pycharm连接远端环境+本地写代码+远程跑,具体方法:https://www.cnblogs.com/Red-Heart-Match/p/16013112.html

配准

找到ants的安装目录,到bin文件夹下,如下图所示:

bin文件夹底下放置了我们需要运行的命令。可以看出来,有.sh结尾的也有直接一条命令的,以.sh结尾的是sh文件,需要使用source才能运行;直接一条命令的不需要source,只需要告诉os它们的路径即可运行。
一下为代码示例:

import os
from nipype.interfaces.ants import RegistrationSynQuick
import torch
import subprocess
from PyQt5.QtWidgets import QMessageBox, QProgressBar
from PyQt5 import QtWidgets
import sys
import logging
from PyQt5.QtCore import QThreaddef registration(fix_image, move_image, output_path):reg = RegistrationSynQuick()output_path = output_pathfix_image = fix_imagemove_image = move_imagereg.inputs.fixed_image = fix_imagereg.inputs.moving_image = move_imagereg.inputs.output_prefix = output_pathreg.inputs.num_threads = 2cmdline = reg.cmdlineprint(cmdline)try:# 注意这里需要export出ANTs的路径,并且后面也需要加上source xx.sh(因为是.sh 需要source)cmd = 'export ANTSPATH=/home/limpid/ANTs/install/bin && source $ANTSPATH/{0}'.format(cmdline)# cmd = 'source ~/.bashrc && {0}'.format(cmdline)task = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)res = task.stdout.decode('utf-8')with open('out.txt', 'w+') as file:file.write(res)# task.wait()QMessageBox.information(QtWidgets.QWidget(), 'messagebox', 'finished! See output file', QMessageBox.Yes)return 1except BaseException as err:print('err\n', err)QMessageBox.information(QtWidgets.QWidget(), 'messagebox', 'error, see log file', QMessageBox.Yes)return -1

N4biasfiledcorrection

import os
import subprocess
from PyQt5.QtWidgets import QMessageBox
from PyQt5 import QtWidgets
import SimpleITK as sitk
from nipype.interfaces.ants import N4BiasFieldCorrectiondef n4correction(in_file, out_path, out_file, image_type=sitk.sitkFloat64):''':param in_file::param out_file::param image_type::return:'''correct = N4BiasFieldCorrection()correct.inputs.input_image = in_filecorrect.inputs.output_image = os.path.join(out_path, out_file)try:cmdline = correct.cmdline# 注意这就不需要进行source,因为N4biasfieldcorrection是二进制文件 可以直接运行cmd = 'export ANTSPATH=/home/limpid/ANTs/install/bin && $ANTSPATH/{0}'.format(cmdline)print(cmd)task = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)res = task.stdout.decode('utf-8')with open('out.txt', 'w+') as file:file.write(res)QMessageBox.information(QtWidgets.QWidget(), 'messagebox', 'finished! See output file', QMessageBox.Yes)return 1except BaseException as err:print('err', err)QMessageBox.warning(QtWidgets.QWidget(), 'messagebox', 'error, see log file', QMessageBox.Yes)return -1

python远程使用ants中的配准命令和N4biasfiledcorrection注意点相关推荐

  1. python远程登录linux命令,Python+requests通过paramiko远程登录Linux执行sh命令

    在做自动化测试过程中, 偶尔需要登录Linux服务器执行一些sh命令.Python的paramiko模块, 遵循SSH2协议, 支持以加密和认证的方式远程连接服务器, 可用来连接远程linux主机, ...

  2. python调用shell用什么类_python脚本中调用shell命令

    在python脚本中调用类uninx上的系统命令或工具.有下面两种方法 os.system(string cmd | scriptname.sh) 参数可以是shell命令,也可以是shell脚本. ...

  3. python中执行linux命令(调用linux命令)_Python调用Linux bash命令

    import subprocess as sup  # 以下注释很多(为了自己以后不忘), 如果只是想在python中执行Linux命令, 看前5行就够了 # 3.5版本之后官方推荐使用sup.run ...

  4. python调用shell命令-在Python中执行shell命令的6种方法,你都知道吗?

    原标题:在Python中执行shell命令的6种方法,你都知道吗? Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库.今天我们就讲解 ...

  5. python使用教程cmd啥意思-对python中执行DOS命令的3种方法总结

    1. 使用os.system("cmd") 特点是执行的时候程序会打出cmd在Linux上执行的信息. import os os.system("ls") 2. ...

  6. python调用shell命令-python中执行shell命令的几个方法小结

    最近有个需求就是页面上执行shell命令,第一想到的就是os.system, 复制代码 代码如下: os.system('cat /proc/cpuinfo') 但是发现页面上打印的命令执行结果 0或 ...

  7. Python中调用Linux命令并获取返回值

    方法一.使用os模块的system方法: os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shell指令成功执行,256/512表示未找到,该方法适用于sh ...

  8. python getopts_linux bash shell 中getopts 命令 和 python 中 getopt 函数的比较总结

    在 python 中有个获取命令行参数的函数叫 getopt(args, shortopts, longopts=[]) 通常我们使用的时候是如下的形式: import sys import geto ...

  9. Python中执行外部命令

    有很多需求需要在Python中执行shell命令.启动子进程,并捕获命令的输出和退出状态码,类似于Java中的Runtime类库. subprocess模块的使用: Python使用最广泛的是标准库的 ...

最新文章

  1. python 获得两个数组(List)交集、并集与差集
  2. arcgis server账号需要设置地图缓存的访问权限
  3. 将字符串和数字合并动态写入
  4. ISA Server服务器故障恢复一例系统盘符更换之后的应对方法
  5. android.net.wifi的简单使用方法
  6. camera (14)---智能手机双摄像头原理解析:RGB +Depth
  7. js选中文字兼容性解决
  8. Load data local infile
  9. 《网络对抗》 逆向及Bof基础实践
  10. selenium 模拟登录qq空间
  11. 标识符,注释,常量,变量
  12. 云剪贴板:以备不时之需
  13. vue中添加mp3音频文件
  14. 135编辑器导出html,135编辑器教程|三步教你搞定表格样式
  15. 家乡的春节html,家乡的春节作文(通用10篇)
  16. Vrep的matlab接口
  17. linux下调整虚拟内存,Linux 如何调整虚拟内存大小
  18. pta 地下迷宫探索
  19. jscc控制器说明书_JSCC精研SPC系列调速器
  20. 织梦手机版list.php,织梦手机静态页生成插件

热门文章

  1. 【Chrome-crx】键盘模拟鼠标点击网页按钮
  2. 让我们来谈谈python中的prettyprint和pprint
  3. c#轻松实现磁性窗口
  4. odoo常见的装饰方法
  5. 噪音测试标准:行业标准
  6. 前端开发需要学什么(数据可视化)
  7. icomoon 下载及使用
  8. dell 2950 LED报W1228 ROMB Batt 24hr 警告处理
  9. 电脑动态图制作的方法技巧
  10. iOS 14.2 内置原生壁纸,安排上吧!