Skip to content

Fixtures #1765

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 5 commits into from
Dec 29, 2016
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
36 changes: 6 additions & 30 deletions nipype/interfaces/freesurfer/tests/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,16 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
import os
from shutil import rmtree
import nibabel as nif
import numpy as np
from tempfile import mkdtemp
import pytest
import nipype.interfaces.freesurfer as freesurfer


@pytest.fixture()
def create_files_in_directory(request):
outdir = os.path.realpath(mkdtemp())
cwd = os.getcwd()
os.chdir(outdir)
filelist = ['a.nii', 'b.nii']
for f in filelist:
hdr = nif.Nifti1Header()
shape = (3, 3, 3, 4)
hdr.set_data_shape(shape)
img = np.random.random(shape)
nif.save(nif.Nifti1Image(img, np.eye(4), hdr),
os.path.join(outdir, f))

def clean_directory():
if os.path.exists(outdir):
rmtree(outdir)
os.chdir(cwd)
import pytest
from nipype.testing.fixtures import create_files_in_directory

request.addfinalizer(clean_directory)
return (filelist, outdir, cwd)
import nipype.interfaces.freesurfer as freesurfer


@pytest.mark.skipif(freesurfer.no_freesurfer(), reason="freesurfer is not installed")
def test_robustregister(create_files_in_directory):
filelist, outdir, cwd = create_files_in_directory
filelist, outdir = create_files_in_directory

reg = freesurfer.RobustRegister()

Expand All @@ -62,7 +38,7 @@ def test_robustregister(create_files_in_directory):

@pytest.mark.skipif(freesurfer.no_freesurfer(), reason="freesurfer is not installed")
def test_fitmsparams(create_files_in_directory):
filelist, outdir, cwd = create_files_in_directory
filelist, outdir = create_files_in_directory

fit = freesurfer.FitMSParams()

Expand All @@ -85,7 +61,7 @@ def test_fitmsparams(create_files_in_directory):

@pytest.mark.skipif(freesurfer.no_freesurfer(), reason="freesurfer is not installed")
def test_synthesizeflash(create_files_in_directory):
filelist, outdir, cwd = create_files_in_directory
filelist, outdir = create_files_in_directory

syn = freesurfer.SynthesizeFLASH()

Expand Down
74 changes: 11 additions & 63 deletions nipype/interfaces/freesurfer/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,18 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
from __future__ import print_function, division, unicode_literals, absolute_import
from builtins import open

import os
from tempfile import mkdtemp
from shutil import rmtree

import numpy as np

import nibabel as nif
import pytest
from nipype.interfaces.base import TraitError
from nipype.testing.fixtures import (create_files_in_directory_plus_dummy_file,
create_surf_file_in_directory)

from nipype.interfaces.base import TraitError
import nipype.interfaces.freesurfer as fs


@pytest.fixture()
def create_files_in_directory(request):
outdir = os.path.realpath(mkdtemp())
cwd = os.getcwd()
os.chdir(outdir)
filelist = ['a.nii', 'b.nii']
for f in filelist:
hdr = nif.Nifti1Header()
shape = (3, 3, 3, 4)
hdr.set_data_shape(shape)
img = np.random.random(shape)
nif.save(nif.Nifti1Image(img, np.eye(4), hdr),
os.path.join(outdir, f))
with open(os.path.join(outdir, 'reg.dat'), 'wt') as fp:
fp.write('dummy file')
filelist.append('reg.dat')

def clean_directory():
if os.path.exists(outdir):
rmtree(outdir)
os.chdir(cwd)

request.addfinalizer(clean_directory)
return (filelist, outdir, cwd)


@pytest.fixture()
def create_surf_file(request):
outdir = os.path.realpath(mkdtemp())
cwd = os.getcwd()
os.chdir(outdir)
surf = 'lh.a.nii'
hdr = nif.Nifti1Header()
shape = (1, 100, 1)
hdr.set_data_shape(shape)
img = np.random.random(shape)
nif.save(nif.Nifti1Image(img, np.eye(4), hdr),
os.path.join(outdir, surf))

def clean_directory():
if os.path.exists(outdir):
rmtree(outdir)
os.chdir(cwd)

request.addfinalizer(clean_directory)
return (surf, outdir, cwd)


@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
def test_sample2surf(create_files_in_directory):
def test_sample2surf(create_files_in_directory_plus_dummy_file):

s2s = fs.SampleToSurface()
# Test underlying command
Expand All @@ -76,7 +24,7 @@ def test_sample2surf(create_files_in_directory):
with pytest.raises(ValueError): s2s.run()

# Create testing files
files, cwd, oldwd = create_files_in_directory
files, cwd = create_files_in_directory_plus_dummy_file

# Test input settings
s2s.inputs.source_file = files[0]
Expand Down Expand Up @@ -107,7 +55,7 @@ def set_illegal_range():


@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
def test_surfsmooth(create_surf_file):
def test_surfsmooth(create_surf_file_in_directory):

smooth = fs.SurfaceSmooth()

Expand All @@ -118,7 +66,7 @@ def test_surfsmooth(create_surf_file):
with pytest.raises(ValueError): smooth.run()

# Create testing files
surf, cwd, oldwd = create_surf_file
surf, cwd = create_surf_file_in_directory

# Test input settings
smooth.inputs.in_file = surf
Expand All @@ -139,7 +87,7 @@ def test_surfsmooth(create_surf_file):


@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
def test_surfxfm(create_surf_file):
def test_surfxfm(create_surf_file_in_directory):

xfm = fs.SurfaceTransform()

Expand All @@ -150,7 +98,7 @@ def test_surfxfm(create_surf_file):
with pytest.raises(ValueError): xfm.run()

# Create testing files
surf, cwd, oldwd = create_surf_file
surf, cwd = create_surf_file_in_directory

# Test input settings
xfm.inputs.source_file = surf
Expand All @@ -170,7 +118,7 @@ def test_surfxfm(create_surf_file):


@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
def test_surfshots(create_files_in_directory):
def test_surfshots(create_files_in_directory_plus_dummy_file):

fotos = fs.SurfaceSnapshots()

Expand All @@ -181,7 +129,7 @@ def test_surfshots(create_files_in_directory):
with pytest.raises(ValueError): fotos.run()

# Create testing files
files, cwd, oldwd = create_files_in_directory
files, cwd = create_files_in_directory_plus_dummy_file

# Test input settins
fotos.inputs.subject_id = "fsaverage"
Expand Down
31 changes: 1 addition & 30 deletions nipype/interfaces/fsl/tests/test_dti.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,14 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
from builtins import open, range

import os

from tempfile import mkdtemp
from shutil import rmtree

import numpy as np

import nibabel as nb

import nipype.interfaces.fsl.dti as fsl
from nipype.interfaces.fsl import Info, no_fsl
from nipype.interfaces.base import Undefined

import pytest


@pytest.fixture(scope="module")
def create_files_in_directory(request):
outdir = os.path.realpath(mkdtemp())
cwd = os.getcwd()
os.chdir(outdir)
filelist = ['a.nii', 'b.nii']
for f in filelist:
hdr = nb.Nifti1Header()
shape = (3, 3, 3, 4)
hdr.set_data_shape(shape)
img = np.random.random(shape)
nb.save(nb.Nifti1Image(img, np.eye(4), hdr),
os.path.join(outdir, f))

def clean_directory():
rmtree(outdir)
os.chdir(cwd)

request.addfinalizer(clean_directory)
return (filelist, outdir)
from nipype.testing.fixtures import create_files_in_directory


# test dtifit
Expand Down
31 changes: 2 additions & 29 deletions nipype/interfaces/fsl/tests/test_epi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,13 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
import os

from tempfile import mkdtemp
from shutil import rmtree

import numpy as np

import nibabel as nb

import pytest
from nipype.testing.fixtures import create_files_in_directory

import nipype.interfaces.fsl.epi as fsl
from nipype.interfaces.fsl import no_fsl


@pytest.fixture(scope="module")
def create_files_in_directory(request):
outdir = os.path.realpath(mkdtemp())
cwd = os.getcwd()
os.chdir(outdir)
filelist = ['a.nii', 'b.nii']
for f in filelist:
hdr = nb.Nifti1Header()
shape = (3, 3, 3, 4)
hdr.set_data_shape(shape)
img = np.random.random(shape)
nb.save(nb.Nifti1Image(img, np.eye(4), hdr),
os.path.join(outdir, f))

def clean_directory():
rmtree(outdir)
os.chdir(cwd)

request.addfinalizer(clean_directory)
return (filelist, outdir)


# test eddy_correct
@pytest.mark.skipif(no_fsl(), reason="fsl is not installed")
def test_eddy_correct2(create_files_in_directory):
Expand Down
Loading