-
Notifications
You must be signed in to change notification settings - Fork 521
/
base.py
147 lines (133 loc) · 4.37 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
import os.path as op
import glob
from ... import logging
from ...utils.filemanip import simplify_list
from ..base import traits, File, Directory, TraitedSpec, OutputMultiPath
from ..freesurfer.base import FSCommand, FSTraitedSpec
iflogger = logging.getLogger("nipype.interface")
class WatershedBEMInputSpec(FSTraitedSpec):
subject_id = traits.Str(
argstr="--subject %s",
mandatory=True,
desc="Subject ID (must have a complete Freesurfer directory)",
)
subjects_dir = Directory(
exists=True,
mandatory=True,
usedefault=True,
desc="Path to Freesurfer subjects directory",
)
volume = traits.Enum(
"T1",
"aparc+aseg",
"aseg",
"brain",
"orig",
"brainmask",
"ribbon",
argstr="--volume %s",
usedefault=True,
desc='The volume from the "mri" directory to use (defaults to T1)',
)
overwrite = traits.Bool(
True,
usedefault=True,
argstr="--overwrite",
desc="Overwrites the existing files",
)
atlas_mode = traits.Bool(
argstr="--atlas",
desc="Use atlas mode for registration (default: no rigid alignment)",
)
class WatershedBEMOutputSpec(TraitedSpec):
mesh_files = OutputMultiPath(
File(exists=True),
desc=(
"Paths to the output meshes (brain, inner "
"skull, outer skull, outer skin)"
),
)
brain_surface = File(
exists=True, loc="bem/watershed", desc="Brain surface (in Freesurfer format)"
)
inner_skull_surface = File(
exists=True,
loc="bem/watershed",
desc="Inner skull surface (in Freesurfer format)",
)
outer_skull_surface = File(
exists=True,
loc="bem/watershed",
desc="Outer skull surface (in Freesurfer format)",
)
outer_skin_surface = File(
exists=True,
loc="bem/watershed",
desc="Outer skin surface (in Freesurfer format)",
)
fif_file = File(
exists=True,
loc="bem",
altkey="fif",
desc='"fif" format file for EEG processing in MNE',
)
cor_files = OutputMultiPath(
File(exists=True),
loc="bem/watershed/ws",
altkey="COR",
desc='"COR" format files',
)
class WatershedBEM(FSCommand):
"""Uses mne_watershed_bem to get information from dicom directories
Examples
--------
>>> from nipype.interfaces.mne import WatershedBEM
>>> bem = WatershedBEM()
>>> bem.inputs.subject_id = 'subj1'
>>> bem.inputs.subjects_dir = '.'
>>> bem.cmdline
'mne watershed_bem --overwrite --subject subj1 --volume T1'
>>> bem.run() # doctest: +SKIP
"""
_cmd = "mne watershed_bem"
input_spec = WatershedBEMInputSpec
output_spec = WatershedBEMOutputSpec
_additional_metadata = ["loc", "altkey"]
def _get_files(self, path, key, dirval, altkey=None):
globsuffix = "*"
globprefix = "*"
keydir = op.join(path, dirval)
if altkey:
key = altkey
globpattern = op.join(keydir, "".join((globprefix, key, globsuffix)))
return glob.glob(globpattern)
def _list_outputs(self):
outputs = self.output_spec().get()
subjects_dir = self.inputs.subjects_dir
subject_path = op.join(subjects_dir, self.inputs.subject_id)
output_traits = self._outputs()
mesh_paths = []
for k in list(outputs.keys()):
if k != "mesh_files":
val = self._get_files(
subject_path,
k,
output_traits.traits()[k].loc,
output_traits.traits()[k].altkey,
)
if val:
value_list = simplify_list(val)
if isinstance(value_list, list):
out_files = []
for value in value_list:
out_files.append(op.abspath(value))
elif isinstance(value_list, (str, bytes)):
out_files = op.abspath(value_list)
else:
raise TypeError
outputs[k] = out_files
if not k.rfind("surface") == -1:
mesh_paths.append(out_files)
outputs["mesh_files"] = mesh_paths
return outputs