python远程使用ants中的配准命令和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 QThread

def registration(fix_image, move_image, output_path):

    reg = RegistrationSynQuick()
    output_path = output_path
    fix_image = fix_image
    move_image = move_image

    reg.inputs.fixed_image = fix_image
    reg.inputs.moving_image = move_image
    reg.inputs.output_prefix = output_path
    reg.inputs.num_threads = 2

    cmdline = reg.cmdline
    print(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 1
    except 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 N4BiasFieldCorrection

def 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_file
    correct.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 1
    except BaseException as err:
        print('err', err)
        QMessageBox.warning(QtWidgets.QWidget(), 'messagebox', 'error, see log file', QMessageBox.Yes)
        return -1
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九久呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值