栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
  • 背景
  • 配准
  • 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('errn', 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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835669.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号