Skip to content

Commit

Permalink
[TEST] adding tests for 98BT
Browse files Browse the repository at this point in the history
  • Loading branch information
bclenet committed Feb 6, 2024
1 parent c04ba9c commit acd4548
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 20 deletions.
8 changes: 4 additions & 4 deletions narps_open/pipelines/team_98BT.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ def get_subject_information(event_file: str):
condition_names = ['gamble']

# Create empty lists
onset = []
duration = []
onsets = []
durations = []
weights_gain = []
weights_loss = []
answers = []
Expand All @@ -444,8 +444,8 @@ def get_subject_information(event_file: str):
# Create Bunch
return Bunch(
conditions = ['gamble'],
onsets = onsets,
durations = durations,
onsets = [onsets],
durations = [durations],
amplitudes = None,
tmod = None,
pmod = [
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from pytest import helpers
from pathvalidate import is_valid_filepath
from numpy import isclose

from narps_open.pipelines import Pipeline
from narps_open.runner import PipelineRunner
Expand All @@ -23,6 +24,15 @@
# Init configuration, to ensure it is in testing mode
Configuration(config_type='testing')

@helpers.register
def compare_float_2d_arrays(array_1, array_2):
""" Assert array_1 and array_2 are close enough """

assert len(array_1) == len(array_2)
for reference_array, test_array in zip(array_1, array_2):
assert len(reference_array) == len(test_array)
assert isclose(reference_array, test_array).all()

@helpers.register
def test_pipeline_outputs(pipeline: Pipeline, number_of_outputs: list):
""" Test the outputs of a Pipeline.
Expand Down
87 changes: 71 additions & 16 deletions tests/pipelines/test_team_98BT.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,26 @@
pytest -q test_team_98BT.py -k <selected_test>
"""

from pytest import helpers, mark
from os.path import join

from pytest import helpers, mark, fixture
from nipype import Workflow
from nipype.interfaces.base import Bunch

from narps_open.utils.configuration import Configuration
from narps_open.pipelines.team_98BT import PipelineTeam98BT

TEMPORARY_DIR = join(Configuration()['directories']['test_runs'], 'test_98BT')

@fixture
def remove_test_dir():
""" A fixture to remove temporary directory created by tests """

rmtree(TEMPORARY_DIR, ignore_errors = True)
mkdir(TEMPORARY_DIR)
yield # test runs here
rmtree(TEMPORARY_DIR, ignore_errors = True)

class TestPipelinesTeam98BT:
""" A class that contains all the unit tests for the PipelineTeam98BT class."""

Expand Down Expand Up @@ -58,7 +73,7 @@ def test_outputs():
helpers.test_pipeline_outputs(pipeline, [0,0,36,84,18])

@staticmethod
@mark.pipeline_test
@mark.unit_test
def test_fieldmap_info():
""" Test the get_fieldmap_info method """

Expand All @@ -67,29 +82,69 @@ def test_fieldmap_info():
filedmap_file_2 = join(
Configuration()['directories']['test_data'], 'pipelines', 'phasediff_2.json')

pipeline = PipelineTeam98BT()
test_result = pipeline.get_fieldmap_info(filedmap_file_1, ['magnitude_1', 'magnitude_2'])
assert test_result[0] == [0.0, 0.0]
assert test_result[1] == 'magnitude_2'
test_result = pipeline.get_fieldmap_info(filedmap_file_2, ['magnitude_1', 'magnitude_2'])
assert test_result[0] == [0.0, 0.0]
test_result = PipelineTeam98BT.get_fieldmap_info(
filedmap_file_1, ['magnitude_1', 'magnitude_2'])
assert test_result[0] == (0.00492, 0.00738)
assert test_result[1] == 'magnitude_1'
test_result = PipelineTeam98BT.get_fieldmap_info(
filedmap_file_2, ['magnitude_1', 'magnitude_2'])
assert test_result[0] == (0.00492, 0.00738)
assert test_result[1] == 'magnitude_2'

@staticmethod
@mark.pipeline_test
def test_fieldmap_info():
""" Test the get_fieldmap_info method """

@staticmethod
@mark.pipeline_test
def test_parameters_files():
@mark.unit_test
def test_parameters_files(remove_test_dir):
""" Test the get_parameters_files method """
"""
parameters_file = join(
Configuration()['directories']['test_data'], 'pipelines', 'confounds.tsv')
reference_file = join(
Configuration()['directories']['test_data'], 'pipelines', 'team_98BT', 'parameters.tsv')
# Get new parameters file
PipelineTeam98BT.get_parameters_file(
parameters_file, wc2_file, motion_corrected_files, 'sid', TEMPORARY_DIR)
# Check parameters file was created
created_parameters_file = join(
TEMPORARY_DIR, 'parameters_files', 'parameters_file_sub-sid.tsv')
assert exists(created_parameters_file)
# Check contents
assert cmp(reference_file, created_confounds_file)
"""

@staticmethod
@mark.pipeline_test
@mark.unit_test
def test_subject_information():
""" Test the get_subject_information method """

# Get test files
test_file = join(Configuration()['directories']['test_data'], 'pipelines', 'events.tsv')

bunch = PipelineTeam98BT.get_subject_information(test_file)

# Compare bunches to expected
assert isinstance(bunch, Bunch)
assert bunch.conditions == ['gamble']
helpers.compare_float_2d_arrays(bunch.onsets, [
[4.071, 11.834, 19.535, 27.535, 36.435]])
helpers.compare_float_2d_arrays(bunch.durations, [
[4.0, 4.0, 4.0, 4.0, 4.0]])
assert bunch.amplitudes == None
assert bunch.tmod == None
assert bunch.regressor_names == None
assert bunch.regressors == None
pmod = bunch.pmod[0]
assert isinstance(pmod, Bunch)
assert pmod.name == ['gain', 'loss', 'answer']
assert pmod.poly == [1, 1, 1]
helpers.compare_float_2d_arrays(pmod.param, [
[14.0, 34.0, 38.0, 10.0, 16.0],
[6.0, 14.0, 19.0, 15.0, 17.0],
[1, 1, 0, 0, 0]
])

@staticmethod
@mark.pipeline_test
def test_execution():
Expand Down
24 changes: 24 additions & 0 deletions tests/test_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,30 @@ def download(self):
class TestConftest:
""" A class that contains all the unit tests for the conftest module."""

@staticmethod
@mark.unit_test
def test_compare_float_2d_arrays():
""" Test the compare_float_2d_arrays helper """

array_1 = [[5.0, 0.0], [1.0, 2.0]]
array_2 = [[5.0, 0.0], [1.0]]
with raises(AssertionError):
helpers.compare_float_2d_arrays(array_1, array_2)

array_1 = [[6.0, 0.0], [1.0]]
array_2 = [[6.0, 0.0], [1.0, 2.0]]
with raises(AssertionError):
helpers.compare_float_2d_arrays(array_1, array_2)

array_1 = [[7.10001, 0.0], [1.0, 2.0]]
array_2 = [[7.10001, 0.0], [1.0, 2.00003]]
with raises(AssertionError):
helpers.compare_float_2d_arrays(array_1, array_2)

array_1 = [[10.0000200, 15.10], [1.0, 2.0]]
array_2 = [[10.00002, 15.10000], [1.0, 2.000003]]
helpers.compare_float_2d_arrays(array_1, array_2)

@staticmethod
@mark.unit_test
def test_test_outputs(set_test_directory):
Expand Down

0 comments on commit acd4548

Please sign in to comment.