-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #288 from populse/add_mrtrix
add mrtrix in capsul
- Loading branch information
Showing
9 changed files
with
405 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
'ants', | ||
'fsl', | ||
'matlab', | ||
'mrtrix', | ||
'spm'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import | ||
|
||
import os | ||
from capsul import engine | ||
import six | ||
|
||
|
||
def init_settings(capsul_engine): | ||
with capsul_engine.settings as settings: | ||
settings.ensure_module_fields('mrtrix', | ||
[dict(name='directory', | ||
type='string', | ||
description='Directory where mrtrix is installed') | ||
]) | ||
|
||
# init a single config | ||
config = settings.config('mrtrix', 'global') | ||
if not config: | ||
settings.new_config('mrtrix', 'global', | ||
{capsul_engine.settings.config_id_field: | ||
'mrtrix'}) | ||
|
||
|
||
def check_notably_invalid_config(conf): | ||
''' | ||
Checks if the given module config is obviously invalid, for instance | ||
if a mandatory path is not filled | ||
Returns | ||
------- | ||
invalid: list | ||
list of invalid config keys | ||
''' | ||
return getattr(conf, 'directory', None) | ||
|
||
|
||
def activate_configurations(): | ||
''' | ||
Activate the mrtrix module (set env variables) from the global | ||
configurations, in order to use them via | ||
:mod:`capsul.in_context.mrtrix` functions | ||
''' | ||
conf = engine.configurations.get('capsul.engine.module.mrtrix', {}) | ||
mrtrix_dir = conf.get('directory') | ||
if mrtrix_dir: | ||
os.environ['MRTRIXPATH'] = six.ensure_str(mrtrix_dir) | ||
elif 'MRTRIXPATH' in os.environ: | ||
del os.environ['MRTRIXPATH'] | ||
|
||
|
||
def edition_widget(engine, environment, config_id='mrtrix'): | ||
''' Edition GUI for mrtrix config - see | ||
:class:`~capsul.qt_gui.widgets.settings_editor.SettingsEditor` | ||
''' | ||
from soma.qt_gui.controller_widget import ScrollControllerWidget | ||
from soma.controller import Controller | ||
import types | ||
import traits.api as traits | ||
|
||
def validate_config(widget): | ||
widget.update_controller() | ||
controller = widget.controller_widget.controller | ||
with widget.engine.settings as session: | ||
conf = session.config(config_id, widget.environment) | ||
values = {'config_id': config_id} | ||
for k in ['directory']: | ||
value = getattr(controller, k) | ||
if value is traits.Undefined: | ||
value = None | ||
values[k] = value | ||
if conf is None: | ||
session.new_config(config_id, widget.environment, values) | ||
else: | ||
for k, value in values.items(): | ||
if k == 'config_id': | ||
continue | ||
setattr(conf, k, values[k]) | ||
|
||
controller = Controller() | ||
|
||
controller.add_trait('directory', | ||
traits.Directory(traits.Undefined, | ||
desc='Directory where mrtrix is installed')) | ||
|
||
conf = engine.settings.select_configurations( | ||
environment, {'mrtrix': 'any'}) | ||
if conf: | ||
fconf = conf.get('capsul.engine.module.mrtrix', {}) | ||
controller.directory = fconf.get('directory', traits.Undefined) | ||
|
||
widget = ScrollControllerWidget(controller, live=True) | ||
widget.engine = engine | ||
widget.environment = environment | ||
widget.accept = types.MethodType(validate_config, widget) | ||
|
||
return widget |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
Specific subprocess-like functions to call mrtrix taking into account | ||
configuration stored in ExecutionContext. To functions and class in | ||
this module it is mandatory to activate an ExecutionContext (using a | ||
with statement). For instance:: | ||
from capsul.engine import capsul_engine | ||
from capsul.in_context.mrtrix import mrtrix_check_call | ||
ce = capsul_engine() | ||
with ce: | ||
mrtrix_check_call(['mrinfo', '/somewhere/myimage.nii']) | ||
For calling mrtrix command with this module, the first argument of | ||
command line must be the mrtrix executable without any path. | ||
The appropriate path is added from the configuration | ||
of the ExecutionContext. | ||
''' | ||
|
||
from __future__ import absolute_import | ||
|
||
import os | ||
import os.path as osp | ||
import soma.subprocess | ||
from soma.utils.env import parse_env_lines | ||
import six | ||
|
||
mrtrix_runtime_env = None | ||
|
||
|
||
def mrtrix_command_with_environment(command, use_runtime_env=True): | ||
''' | ||
Given an mrtrix command where first element is a command name without | ||
any path. Returns the appropriate command to call taking into account | ||
the mrtrix configuration stored in the | ||
activated ExecutionContext. | ||
''' | ||
|
||
if use_runtime_env and mrtrix_runtime_env: | ||
c0 = list(osp.split(command[0])) | ||
c0 = osp.join(*c0) | ||
cmd = [c0] + command[1:] | ||
return cmd | ||
|
||
mrtrix_dir = os.environ.get('MRTRIXPATH') | ||
if mrtrix_dir: | ||
shell = os.environ.get('SHELL', '/bin/sh') | ||
if shell.endswith('csh'): | ||
cmd = [shell, '-c', | ||
'setenv MRTRIXPATH "{0}"; setenv PATH "{0}:$PATH";exec {1} '.format( | ||
mrtrix_dir, command[0]) + \ | ||
' '.join("'%s'" % i.replace("'", "\\'") for i in command[1:])] | ||
else: | ||
cmd = [shell, '-c', | ||
'export MRTRIXPATH="{0}"; export PATH="{0}:$PATH"; exec {1} '.format( | ||
mrtrix_dir, command[0]) + \ | ||
' '.join("'%s'" % i.replace("'", "\\'") for i in command[1:])] | ||
|
||
return cmd | ||
|
||
|
||
def mrtrix_env(): | ||
''' | ||
get mrtrix env variables | ||
process | ||
''' | ||
global mrtrix_runtime_env | ||
|
||
if mrtrix_runtime_env is not None: | ||
return mrtrix_runtime_env | ||
|
||
mrtrix_dir = os.environ.get('MRTRIXPATH') | ||
kwargs = {} | ||
|
||
cmd = mrtrix_command_with_environment(['env'], use_runtime_env=False) | ||
new_env = soma.subprocess.check_output(cmd, **kwargs).decode( | ||
'utf-8').strip() | ||
new_env = parse_env_lines(new_env) | ||
env = {} | ||
for l in new_env: | ||
name, val = l.strip().split('=', 1) | ||
name = six.ensure_str(name) | ||
val = six.ensure_str(val) | ||
if name not in ('_', 'SHLVL') and (name not in os.environ | ||
or os.environ[name] != val): | ||
env[name] = val | ||
|
||
# add PATH | ||
if mrtrix_dir: | ||
env['PATH'] = os.pathsep.join([mrtrix_dir, os.environ.get('PATH', '')]) | ||
# cache dict | ||
mrtrix_runtime_env = env | ||
return env | ||
|
||
|
||
class MrtrixPopen(soma.subprocess.Popen): | ||
''' | ||
Equivalent to Python subprocess.Popen for mrtrix commands | ||
''' | ||
def __init__(self, command, **kwargs): | ||
cmd = mrtrix_command_with_environment(command) | ||
super(MrtrixPopen, self).__init__(cmd, **kwargs) | ||
|
||
|
||
def mrtrix_call(command, **kwargs): | ||
''' | ||
Equivalent to Python subprocess.call for mrtrix commands | ||
''' | ||
cmd = mrtrix_command_with_environment(command) | ||
return soma.subprocess.call(cmd, **kwargs) | ||
|
||
|
||
def mrtrix_check_call(command, **kwargs): | ||
''' | ||
Equivalent to Python subprocess.check_call for mrtrix commands | ||
''' | ||
cmd = mrtrix_command_with_environment(command) | ||
return soma.subprocess.check_call(cmd, **kwargs) | ||
|
||
|
||
def mrtrix_check_output(command, **kwargs): | ||
''' | ||
Equivalent to Python subprocess.check_output for mrtrix commands | ||
''' | ||
cmd = mrtrix_command_with_environment(command) | ||
return soma.subprocess.check_output(cmd, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.