Skip to content

ENH: Detect values for EulerNumber interface #3173

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

Merged
merged 4 commits into from
Feb 24, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_EulerNumber_inputs():


def test_EulerNumber_outputs():
output_map = dict(out_file=dict(extensions=None,),)
output_map = dict(defects=dict(), euler=dict(),)
outputs = EulerNumber.output_spec()

for key, metadata in list(output_map.items()):
Expand Down
16 changes: 16 additions & 0 deletions nipype/interfaces/freesurfer/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,19 @@ def test_mrisexpand(tmpdir):
assert op.dirname(if_out_file) == op.dirname(fsavginfo["smoothwm"])
# Node places output in working directory
assert op.dirname(nd_out_file) == nd_res.runtime.cwd


@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
def test_eulernumber(tmpdir):
# grab a surface from fsaverage
fssrc = FreeSurferSource(
subjects_dir=fs.Info.subjectsdir(), subject_id="fsaverage", hemi="lh"
)
pial = fssrc.run().outputs.pial
assert isinstance(pial, str), "Problem when fetching surface file"

eu = fs.EulerNumber()
eu.inputs.in_file = pial
res = eu.run()
assert res.outputs.defects == 0
assert res.outputs.euler == 2
19 changes: 17 additions & 2 deletions nipype/interfaces/freesurfer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2592,7 +2592,9 @@ class EulerNumberInputSpec(FSTraitedSpec):


class EulerNumberOutputSpec(TraitedSpec):
out_file = File(exists=False, desc="Output file for EulerNumber")
euler = traits.Int(desc="Euler number of cortical surface. A value of 2 signals a "
"topologically correct surface model with no holes")
defects = traits.Int(desc="Number of defects")


class EulerNumber(FSCommand):
Expand All @@ -2612,9 +2614,22 @@ class EulerNumber(FSCommand):
input_spec = EulerNumberInputSpec
output_spec = EulerNumberOutputSpec

def _run_interface(self, runtime):
runtime = super()._run_interface(runtime)
self._parse_output(runtime.stdout, runtime.stderr)
return runtime

def _parse_output(self, stdout, stderr):
"""Parse stdout / stderr and extract defects"""
m = re.search(r'(?<=total defect index = )\d+', stdout or stderr)
if m is None:
raise RuntimeError("Could not fetch defect index")
self._defects = int(m.group())

def _list_outputs(self):
outputs = self._outputs().get()
outputs["out_file"] = os.path.abspath(self.inputs.in_file)
outputs["defects"] = self._defects
outputs["euler"] = 2 - (2 * self._defects)
return outputs


Expand Down