-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add mrtrix in capsul #288
Merged
Merged
add mrtrix in capsul #288
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose that mrtrix is supposed to have a unique cif, not the same as ant. Any value would be possible here as long as it is unique among all other values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a unique hard-coded CIF will just forbid to have several installs / versions of Mrtrix configured in Capsul, but it will probably be OK.