Skip to content

Commit

Permalink
Merge pull request nipreps#403 from davhunt/master
Browse files Browse the repository at this point in the history
TEST/RF: Separate LTA length fixing and add doctest
  • Loading branch information
effigies committed Oct 3, 2019
2 parents d280796 + d4f32dc commit 232b6d6
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 13 deletions.
2 changes: 2 additions & 0 deletions niworkflows/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pandas as pd
import pytest
import tempfile
import pkg_resources

from .utils.bids import collect_data

Expand All @@ -22,6 +23,7 @@ def add_np(doctest_namespace):
doctest_namespace['Path'] = Path
doctest_namespace['datadir'] = data_dir
doctest_namespace['bids_collect_data'] = collect_data
doctest_namespace['test_data'] = pkg_resources.resource_filename('niworkflows', 'tests/data')

tmpdir = tempfile.TemporaryDirectory()
doctest_namespace['tmpdir'] = tmpdir.name
Expand Down
61 changes: 48 additions & 13 deletions niworkflows/interfaces/freesurfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,7 @@ def _post_run_hook(self, runtime):
if not isdefined(lta_file):
continue

lines = Path(lta_file).read_text().splitlines(keepends=True)

fixed = False
newfile = []
for line in lines:
if line.startswith('filename = ') and len(line.strip("\n")) >= 255:
fixed = True
newfile.append('filename = path_too_long\n')
else:
newfile.append(line)

if fixed:
Path(lta_file).write_text(''.join(newfile))
fix_lta_length(lta_file)

runtime = super(TruncateLTA, self)._post_run_hook(runtime)
return runtime
Expand Down Expand Up @@ -326,6 +314,53 @@ def _run_interface(self, runtime):
return runtime


def fix_lta_length(lta_file):
""" Fix the length of the filename field in an LTA file if too long.
Updates file in place.
Examples
--------
No changes are made to a valid transform file:
>>> valid_transform = Path(test_data) / 'valid_transform.lta'
>>> orig_contents = valid_transform.read_text()
>>> fix_lta_length(valid_transform)
False
>>> valid_transform.read_text() == orig_contents
True
Invalid transform files are files with > 255 characters in any line:
>>> invalid_transform = Path(test_data) / 'long_path_transform.lta'
>>> orig_contents = invalid_transform.read_text()
>>> any(len(line) > 255 for line in orig_contents.splitlines(keepends=True))
True
>>> local_copy = Path('invalid.lta')
>>> local_copy.write_text(orig_contents)
2120
>>> fix_lta_length(local_copy)
True
>>> local_copy.read_text() == orig_contents
False
"""
lines = Path(lta_file).read_text().splitlines(keepends=True)

fixed = False
newfile = []
for line in lines:
if line.startswith('filename = ') and len(line.strip("\n")) >= 255:
fixed = True
newfile.append('filename = path_too_long\n')
else:
newfile.append(line)

if fixed:
Path(lta_file).write_text(''.join(newfile))
return fixed


def inject_skullstripped(subjects_dir, subject_id, skullstripped):
mridir = op.join(subjects_dir, subject_id, 'mri')
t1 = op.join(mridir, 'T1.mgz')
Expand Down
31 changes: 31 additions & 0 deletions niworkflows/tests/data/long_path_transform.lta
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# transform file /N/dc2/scratch/davhunt/workflows/5d6705bee0ae0618d1ededa8/5d6db5f2e0ae06ba6cee3710/fmripworkdir/fmriprep_wf/single_subject_TTTEMPSUB_wf/func_preproc_ses_SSSES_task_balloonanalogrisktask_run_1_wf/bold_reg_wf/bbreg_wf/mri_coreg/registration.lta
# created by davhunt on Mon Sep 2 22:46:17 2019

type = 0 # LINEAR_VOX_TO_VOX
nxforms = 1
mean = 0.0000 0.0000 0.0000
sigma = 10000.0000
1 4 4
3.124938726425171e+00 -6.231633014976978e-03 2.379096671938896e-02 2.793729782104492e+01
1.451765652745962e-02 -1.255382895469666e+00 -3.662998914718628e+00 2.176610260009766e+02
1.317331008613110e-02 2.861747980117798e+00 -1.606821179389954e+00 3.680672836303711e+01
0.000000000000000e+00 0.000000000000000e+00 0.000000000000000e+00 9.999999403953552e-01
src volume info
valid = 1 # volume info valid
filename = /N/dc2/scratch/davhunt/workflows/5d6705bee0ae0618d1ededa8/5d6db5f2e0ae06ba6cee3710/fmripworkdir/fmriprep_wf/single_subject_TTTEMPSUB_wf/func_preproc_ses_SSSES_task_balloonanalogrisktask_run_1_wf/bold_reg_wf/bbreg_wf/mri_coreg/ref_bold_corrected_trans_masked.nii.gz
volume = 64 64 33
voxelsize = 3.125000000000000e+00 3.125000000000000e+00 4.000000000000000e+00
xras = -1.000000000000000e+00 0.000000000000000e+00 0.000000000000000e+00
yras = 0.000000000000000e+00 1.000000000000000e+00 0.000000000000000e+00
zras = 0.000000000000000e+00 0.000000000000000e+00 1.000000000000000e+00
cras = -2.068481445312500e+00 3.198078918457031e+01 -4.065056610107422e+01
dst volume info
valid = 1 # volume info valid
filename = /N/dc2/scratch/davhunt/workflows/5d6705bee0ae0618d1ededa8/5d6db5f2e0ae06ba6cee3710/fmripout/freesurfer/sub-TTTEMPSUB/mri/brainmask.mgz
volume = 256 256 256
voxelsize = 1.000000000000000e+00 1.000000000000000e+00 1.000000000000000e+00
xras = -9.999999403953552e-01 0.000000000000000e+00 0.000000000000000e+00
yras = 0.000000000000000e+00 0.000000000000000e+00 -9.999999403953552e-01
zras = 0.000000000000000e+00 9.999999403953552e-01 0.000000000000000e+00
cras = -3.000000000000000e+00 2.999969482421875e+00 -8.000038146972656e+00
subject sub-TTTEMPSUB
31 changes: 31 additions & 0 deletions niworkflows/tests/data/valid_transform.lta
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# transform file /N/dc2/scratch/davhunt/workflows/5d6705bee0ae0618d1ededa8/5d6db5f2e0ae06ba6cee3710/fmripworkdir/fmriprep_wf/single_subject_TTTEMPSUB_wf/func_preproc_ses_SSSES_task_balloonanalogrisktask_run_1_wf/bold_reg_wf/bbreg_wf/mri_coreg/registration.lta
# created by davhunt on Mon Sep 2 22:46:17 2019

type = 0 # LINEAR_VOX_TO_VOX
nxforms = 1
mean = 0.0000 0.0000 0.0000
sigma = 10000.0000
1 4 4
3.124938726425171e+00 -6.231633014976978e-03 2.379096671938896e-02 2.793729782104492e+01
1.451765652745962e-02 -1.255382895469666e+00 -3.662998914718628e+00 2.176610260009766e+02
1.317331008613110e-02 2.861747980117798e+00 -1.606821179389954e+00 3.680672836303711e+01
0.000000000000000e+00 0.000000000000000e+00 0.000000000000000e+00 9.999999403953552e-01
src volume info
valid = 1 # volume info valid
filename = path_too_long
volume = 64 64 33
voxelsize = 3.125000000000000e+00 3.125000000000000e+00 4.000000000000000e+00
xras = -1.000000000000000e+00 0.000000000000000e+00 0.000000000000000e+00
yras = 0.000000000000000e+00 1.000000000000000e+00 0.000000000000000e+00
zras = 0.000000000000000e+00 0.000000000000000e+00 1.000000000000000e+00
cras = -2.068481445312500e+00 3.198078918457031e+01 -4.065056610107422e+01
dst volume info
valid = 1 # volume info valid
filename = /N/dc2/scratch/davhunt/workflows/5d6705bee0ae0618d1ededa8/5d6db5f2e0ae06ba6cee3710/fmripout/freesurfer/sub-TTTEMPSUB/mri/brainmask.mgz
volume = 256 256 256
voxelsize = 1.000000000000000e+00 1.000000000000000e+00 1.000000000000000e+00
xras = -9.999999403953552e-01 0.000000000000000e+00 0.000000000000000e+00
yras = 0.000000000000000e+00 0.000000000000000e+00 -9.999999403953552e-01
zras = 0.000000000000000e+00 9.999999403953552e-01 0.000000000000000e+00
cras = -3.000000000000000e+00 2.999969482421875e+00 -8.000038146972656e+00
subject sub-TTTEMPSUB

0 comments on commit 232b6d6

Please sign in to comment.