Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions mne/commands/mne_coreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,42 @@ def run():
"the data source panel and the coregistration panel "
"into a single panel with tabs.")
parser.add_option("--no-guess-mri", dest="guess_mri_subject",
action='store_false', default=True,
action='store_false', default=None,
help="Prevent the GUI from automatically guessing and "
"changing the MRI subject when a new head shape source "
"file is selected.")
parser.add_option("--head-opacity", type=float, default=1.,
parser.add_option("--head-opacity", type=float, default=None,
dest="head_opacity",
help="The opacity of the head surface, in the range "
"[0, 1].")
parser.add_option("--high-res-head",
action='store_true', default=False, dest="high_res_head",
help="Use a high-resolution head surface.")
parser.add_option("--low-res-head",
action='store_true', default=False, dest="low_res_head",
help="Default to using a low-resolution head surface.")
help="Use a low-resolution head surface.")
parser.add_option('--verbose', action='store_true', dest='verbose',
help='Turn on verbose mode.')

options, args = parser.parse_args()

if options.low_res_head:
if options.high_res_head:
raise ValueError("Can't specify --high-res-head and "
"--low-res-head at the same time.")
head_high_res = False
elif options.high_res_head:
head_high_res = True
else:
head_high_res = None

with ETSContext():
mne.gui.coregistration(options.tabbed, inst=options.inst,
subject=options.subject,
subjects_dir=options.subjects_dir,
guess_mri_subject=options.guess_mri_subject,
head_opacity=options.head_opacity,
head_high_res=not options.low_res_head,
head_high_res=head_high_res,
verbose=options.verbose)
if is_main:
sys.exit(0)
Expand Down
70 changes: 48 additions & 22 deletions mne/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@
#
# License: BSD (3-clause)

import os

from ..utils import _check_mayavi_version, verbose, get_config
from ._backend import _testing_mode


def _initialize_gui(frame, view=None):
"""Initialize GUI depending on testing mode."""
if _testing_mode(): # open without entering mainloop
return frame.edit_traits(view=view), frame
else:
frame.configure_traits(view=view)
return frame


def combine_kit_markers():
Expand All @@ -18,15 +30,14 @@ def combine_kit_markers():
from ._backend import _check_backend
_check_backend()
from ._marker_gui import CombineMarkersFrame
gui = CombineMarkersFrame()
gui.configure_traits()
return gui
frame = CombineMarkersFrame()
return _initialize_gui(frame)


@verbose
def coregistration(tabbed=False, split=True, scene_width=None, inst=None,
subject=None, subjects_dir=None, guess_mri_subject=True,
scene_height=None, head_opacity=1., head_high_res=True,
subject=None, subjects_dir=None, guess_mri_subject=None,
scene_height=None, head_opacity=None, head_high_res=None,
verbose=None):
"""Coregister an MRI with a subject's head shape.

Expand Down Expand Up @@ -64,11 +75,14 @@ def coregistration(tabbed=False, split=True, scene_width=None, inst=None,
Specify a minimum height for the 3d scene (in pixels).
Default is None, which uses ``MNE_COREG_SCENE_WIDTH`` config value
(which defaults to 400).
head_opacity : float
The default opacity of the head surface in the range [0., 1.]
(default: 1.).
head_high_res : bool
Use a high resolution head surface (default True).
head_opacity : float | None
The opacity of the head surface in the range [0., 1.].
Default is None, which uses ``MNE_COREG_HEAD_OPACITY`` config value
(which defaults to 1.).
head_high_res : bool | None
Use a high resolution head surface.
Default is None, which uses ``MNE_COREG_HEAD_HIGH_RES`` config value
(which defaults to True).
verbose : bool, str, int, or None
If not None, override default verbose level (see :func:`mne.verbose`
and :ref:`Logging documentation <tut_logging>` for more).
Expand All @@ -81,21 +95,35 @@ def coregistration(tabbed=False, split=True, scene_width=None, inst=None,
subjects for which no MRI is available
<http://www.slideshare.net/mne-python/mnepython-scale-mri>`_.
"""
config = get_config(home_dir=os.environ.get('_MNE_FAKE_HOME_DIR'))
prepare_bem = config.get('MNE_COREG_PREPARE_BEM', 'true') == 'true'
if guess_mri_subject is None:
guess_mri_subject = config.get(
'MNE_COREG_GUESS_MRI_SUBJECT', 'true') == 'true'
if head_high_res is None:
head_high_res = config.get('MNE_COREG_HEAD_HIGH_RES', 'true') == 'true'
if head_opacity is None:
head_opacity = config.get('MNE_COREG_HEAD_OPACITY', 1.)
if scene_width is None:
scene_width = get_config('MNE_COREG_SCENE_WIDTH', 500)
scene_width = config.get('MNE_COREG_SCENE_WIDTH', 500)
if scene_height is None:
scene_height = get_config('MNE_COREG_SCENE_HEIGHT', 400)
scene_height = config.get('MNE_COREG_SCENE_HEIGHT', 400)
if subjects_dir is None:
if 'SUBJECTS_DIR' in config:
subjects_dir = config['SUBJECTS_DIR']
elif 'MNE_COREG_SUBJECTS_DIR' in config:
subjects_dir = config['MNE_COREG_SUBJECTS_DIR']
head_opacity = float(head_opacity)
scene_width = int(scene_width)
scene_height = int(scene_height)
_check_mayavi_version()
from ._backend import _check_backend
_check_backend()
from ._coreg_gui import CoregFrame, _make_view
view = _make_view(tabbed, split, scene_width, scene_height)
gui = CoregFrame(inst, subject, subjects_dir, guess_mri_subject,
head_opacity, head_high_res)
gui.configure_traits(view=view)
return gui
frame = CoregFrame(inst, subject, subjects_dir, guess_mri_subject,
head_opacity, head_high_res, prepare_bem)
return _initialize_gui(frame, view)


def fiducials(subject=None, fid_file=None, subjects_dir=None):
Expand All @@ -120,9 +148,8 @@ def fiducials(subject=None, fid_file=None, subjects_dir=None):
from ._backend import _check_backend
_check_backend()
from ._fiducials_gui import FiducialsFrame
gui = FiducialsFrame(subject, subjects_dir, fid_file=fid_file)
gui.configure_traits()
return gui
frame = FiducialsFrame(subject, subjects_dir, fid_file=fid_file)
return _initialize_gui(frame)


def kit2fiff():
Expand All @@ -137,6 +164,5 @@ def kit2fiff():
from ._backend import _check_backend
_check_backend()
from ._kit2fiff_gui import Kit2FiffFrame
gui = Kit2FiffFrame()
gui.configure_traits()
return gui
frame = Kit2FiffFrame()
return _initialize_gui(frame)
7 changes: 7 additions & 0 deletions mne/gui/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#
# License: BSD (3-clause)

import os


def _check_backend():
from ..utils import _check_pyface_backend
Expand All @@ -25,3 +27,8 @@ def _check_backend():
"recommend using qt4 which can be enabled by installing the "
"pyside package." % backend)
warning(None, msg, "Pyface Backend Warning")


def _testing_mode():
"""Determine if we're running tests."""
return os.getenv('_MNE_GUI_TESTING_MODE', '') == 'true'
56 changes: 36 additions & 20 deletions mne/gui/_coreg_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@
translation, scaling, rotation_angles, Transform)
from ..coreg import (fit_matched_points, fit_point_cloud, scale_mri,
_find_fiducials_files, _point_cloud_error)
from ..utils import get_subjects_dir, logger
from ..utils import logger, set_config
from ._fiducials_gui import MRIHeadWithFiducialsModel, FiducialsPanel
from ._file_traits import trans_wildcard, InstSource, SubjectSelectorPanel
from ._viewer import (defaults, HeadViewController, PointObject, SurfaceObject,
_testing_mode)
from ._viewer import defaults, HeadViewController, PointObject, SurfaceObject


laggy_float_editor = TextEditor(auto_set=False, enter_set=True, evaluate=float)
Expand Down Expand Up @@ -230,7 +229,7 @@ def _get_processed_mri_points(self):
points[hair] += self.mri.norms[hair] * scaled_hair_dist
return points
else:
error(None, "Norms missing form bem, can't grow hair")
error(None, "Norms missing from bem, can't grow hair")
self.grow_hair = 0
return self.mri.points

Expand Down Expand Up @@ -312,11 +311,8 @@ def _get_raw_subject(self):
# subject name guessed based on the inst file name
if '_' in self.hsp.inst_fname:
subject, _ = self.hsp.inst_fname.split('_', 1)
if not subject:
subject = None
else:
subject = None
return subject
if subject:
return subject

@on_trait_change('raw_subject')
def _on_raw_subject_change(self, subject):
Expand Down Expand Up @@ -550,6 +546,11 @@ def close(self, info, is_ok): # noqa: D102
"processed.", "Saving Still in Progress")
return False
else:
# store configuration, but don't prevent from closing on error
try:
info.object.save_config()
except Exception as exc:
warnings.warn("Error saving GUI configuration:\n%s" % (exc,))
return True


Expand Down Expand Up @@ -1239,28 +1240,26 @@ def _subject_panel_default(self):
return SubjectSelectorPanel(model=self.model.mri.subject_source)

def _fid_panel_default(self):
panel = FiducialsPanel(model=self.model.mri, headview=self.headview)
return panel
return FiducialsPanel(model=self.model.mri, headview=self.headview)

def _coreg_panel_default(self):
panel = CoregPanel(model=self.model)
return panel
return CoregPanel(model=self.model)

def _headview_default(self):
return HeadViewController(scene=self.scene, system='RAS')

def __init__(self, raw=None, subject=None, subjects_dir=None,
guess_mri_subject=True, head_opacity=1.,
head_high_res=True): # noqa: D102
head_high_res=True, prepare_bem=True): # noqa: D102
super(CoregFrame, self).__init__(guess_mri_subject=guess_mri_subject)
self.subject_panel.model.use_high_res_head = head_high_res
self.model.prepare_bem_model = prepare_bem
if not 0 <= head_opacity <= 1:
raise ValueError(
"head_opacity needs to be a floating point number between 0 "
"and 1, got %r" % (head_opacity,))
self._initial_head_opacity = head_opacity

subjects_dir = get_subjects_dir(subjects_dir)
if (subjects_dir is not None) and os.path.isdir(subjects_dir):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're getting rid of this because the default is already taken elsewhere?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.model.mri.subjects_dir = subjects_dir

Expand Down Expand Up @@ -1361,14 +1360,12 @@ def _init_plot(self):
self.sync_trait('hsp_visible', p, 'visible', mutual=False)

on_pick = self.scene.mayavi_scene.on_mouse_pick
if not _testing_mode():
self.picker = on_pick(self.fid_panel._on_pick, type='cell')
self.picker = on_pick(self.fid_panel._on_pick, type='cell')

self.headview.left = True
self.scene.disable_render = False
if not _testing_mode(): # when testing, scene.camera is None
self.scene.render()
self.scene.camera.focal_point = (0., 0., 0.)
self.scene.render()
self.scene.camera.focal_point = (0., 0., 0.)
self.view_options_panel = ViewOptionsPanel(mri_obj=self.mri_obj,
hsp_obj=self.hsp_obj)

Expand Down Expand Up @@ -1414,3 +1411,22 @@ def _on_fid_file_loaded(self):

def _view_options_fired(self):
self.view_options_panel.edit_traits()

def save_config(self, home_dir=None):
"""Write configuration values."""
set_config('MNE_COREG_GUESS_MRI_SUBJECT',
str(self.model.guess_mri_subject).lower(),
home_dir, set_env=False)
set_config('MNE_COREG_HEAD_HIGH_RES',
str(self.model.mri.use_high_res_head).lower(),
home_dir, set_env=False)
set_config('MNE_COREG_HEAD_OPACITY',
str(self.mri_obj.opacity),
home_dir, set_env=False)
set_config('MNE_COREG_PREPARE_BEM',
str(self.model.prepare_bem_model).lower(),
home_dir, set_env=False)
if self.model.mri.subjects_dir:
set_config('MNE_COREG_SUBJECTS_DIR',
self.model.mri.subjects_dir,
home_dir, set_env=False)
8 changes: 5 additions & 3 deletions mne/gui/_file_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,11 @@ class SubjectSelectorPanel(HasPrivateTraits):
subjects = DelegatesTo('model')
use_high_res_head = DelegatesTo('model')

create_fsaverage = Button("Copy 'fsaverage' to subjects directory",
desc="Copy the files for the fsaverage subject "
"to the subjects directory.")
create_fsaverage = Button(
"Copy 'fsaverage' to subjects directory",
desc="Copy the files for the fsaverage subject to the subjects "
"directory. This button is disabled if a subject called "
"fsaverage already exists in the selected subjects-directory.")

view = View(VGroup(Label('Subjects directory and subject:',
show_label=True),
Expand Down
Loading