Skip to content

Commit f8742ca

Browse files
authored
Entry points for testing accept configuration files at runtime (Inria-Empenn#242)
* Runner allows for configuration at runtime * Other scripts allow for configuration at runtime * Documentation update * New actions versions * Custom configuration allowed for testing * DOC + Adding config argument in narps_open_tester
1 parent e893baa commit f8742ca

File tree

6 files changed

+66
-13
lines changed

6 files changed

+66
-13
lines changed

docs/testing.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Tests can be launched manually or while using CI (Continuous Integration).
4242
* To specify a test -for which the name contains 'test_pattern'- inside a test file : `pytest test_file.py -k "test_pattern"`
4343
* To run a tests with a given mark 'mark' : `pytest -m 'mark'`
4444
* To create code coverage data : `coverage run -m pytest ./tests` then `coverage report` to see the code coverage result or `coverage xml` to output a .xml report file
45+
* To use a custom configuration file (other than `narps_open/utils/configuration/test_config.toml` that is used by default): `pytest --narps_open_config=<path_to_the_custom_config_file>`
4546

4647
## Command line tools
4748

@@ -62,6 +63,7 @@ The command line tool `narps_open_correlations` is also available and can be use
6263

6364
```bash
6465
narps_open_correlations -t 2T6S -n 60
66+
narps_open_correlations -t 2T6S -n 60 --config <path_to_a_custom_config_file>
6567
```
6668

6769
to get the correlation values for the results of a previously executed pipeline (here team 2T6S, with 60 subjects).
@@ -70,7 +72,7 @@ to get the correlation values for the results of a previously executed pipeline
7072

7173
* `pytest.ini` is a global configuration files for using pytest (see reference [here](https://docs.pytest.org/en/7.1.x/reference/customize.html)). It allows to [register markers](https://docs.pytest.org/en/7.1.x/example/markers.html) that help to better identify tests. Note that `pytest.ini` could be replaced by data inside `pyproject.toml` in the next versions.
7274
* `tests/conftest.py` defines common functions, parameters, and [helpers](https://pytest-helpers-namespace.readthedocs.io/en/latest/) that are later available to all tests
73-
* `narps_open/utils/configuration/testing_config.toml` sets the parameters for the `testing` configuration type (see how the [configuration](/docs/configuration.md) module works). This configuration type is automatically used for testing (as defined in `tests/conftest.py`).
75+
* `narps_open/utils/configuration/testing_config.toml` sets the parameters for the `testing` configuration type (see how the [configuration](/docs/configuration.md) module works). This configuration type is used by default for testing (as defined in `tests/conftest.py`), but a configuration can be passed at runtime using the `narps_open_config` option in the `pytest` command line.
7476

7577
## Writing tests
7678

narps_open/data/task.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
class TaskInformation(dict, metaclass=SingletonMeta):
1313
""" This class allows to access information about the task performed in NARPS """
1414

15-
task_information_file = join(Configuration()['directories']['dataset'], 'task-MGT_bold.json')
16-
17-
def __init__(self):
15+
def __init__(self, task_file=''):
1816
super().__init__()
1917

2018
# Load information from the task-MGT_bold.json file
21-
with open(self.task_information_file, 'rb') as file:
19+
if task_file:
20+
task_information_file = task_file # For testing purpose only
21+
else:
22+
task_information_file = join(
23+
Configuration()['directories']['dataset'], 'task-MGT_bold.json')
24+
with open(task_information_file, 'rb') as file:
2225
self.update(load(file))
2326

2427
# Compute derived information

narps_open/tester.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@ def main():
1717
parser = ArgumentParser(description='Test the pipelines from NARPS.')
1818
parser.add_argument('-t', '--team', type=str, required=True,
1919
help='the team ID', choices=get_implemented_pipelines())
20+
parser.add_argument('--config', type=str, required=False,
21+
help='custom configuration file to be used')
2022
arguments = parser.parse_args()
2123

22-
sys.exit(pytest.main([
24+
pytest_arguments = [
2325
'-s',
2426
'-q',
2527
'-x',
2628
f'tests/pipelines/test_team_{arguments.team}.py',
2729
'-m',
28-
'pipeline_test']))
30+
'pipeline_test']
31+
32+
if 'config' in arguments:
33+
pytest_arguments.append(f'--narps_open_config={arguments.config}')
34+
35+
sys.exit(pytest.main(pytest_arguments))
2936

3037
if __name__ == '__main__':
3138
main()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[general]
2+
title = "Testing configuration for the NARPS open pipelines project, to be used inside the nipype/nipype:py38 docker container"
3+
config_type = "custom"
4+
5+
[directories]
6+
dataset = "/work/data/original/ds001734/"
7+
reproduced_results = "/work/run/reproduced/"
8+
narps_results = "/work/data/results/"
9+
test_data = "/work/tests/test_data/"
10+
test_runs = "/work/run/"
11+
12+
[runner]
13+
nb_procs = 8 # Maximum number of threads executed by the runner
14+
nb_trials = 3 # Maximum number of executions to have the pipeline executed completely
15+
16+
[pipelines]
17+
remove_unused_data = true # set to true to activate remove nodes of pipelines
18+
19+
[results]
20+
neurovault_naming = true # true if results files are saved using the neurovault naming, false if they use naming of narps
21+
22+
[testing]
23+
24+
[testing.pipelines]
25+
nb_subjects_per_group = 4 # Compute first level analyses by subgroups of N subjects, to avoid lacking of disk and memory
26+
correlation_thresholds = [0.30, 0.70, 0.78, 0.85, 0.93] # Correlation between reproduced hypotheses files and results, respectively for [20, 40, 60, 80, 108] subjects.

tests/conftest.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,24 @@
2424
from narps_open.data.results import ResultsCollection
2525
from narps_open.data.participants import get_participants_subset
2626

27-
# Init configuration, to ensure it is in testing mode
28-
Configuration(config_type='testing')
27+
def pytest_addoption(parser):
28+
""" Add a configuration option for narps_open to the pytest command line """
29+
parser.addoption('--narps_open_config',
30+
action='store', help='NARPS Open Pipelines configuration file')
31+
32+
@fixture(scope='session', autouse=True)
33+
def load_configuration(request):
34+
""" Automatically load narps_open configuration from the corresponding
35+
pytest command line argument.
36+
"""
37+
config_file = request.config.getoption('--narps_open_config')
38+
39+
if config_file:
40+
# Init configuration with custom file
41+
Configuration(config_type='custom').config_file = config_file
42+
else:
43+
# Ensure it is in testing mode
44+
Configuration(config_type='testing')
2945

3046
@fixture
3147
def temporary_data_dir():

tests/data/test_task.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
from narps_open.data import task
1919

2020
@fixture(scope='function', autouse=True)
21-
def mock_task_data(mocker):
22-
""" Patch the json.load method to mock task data """
23-
mocker.patch.object(
24-
task.TaskInformation, 'task_information_file',
21+
def load_test_task_data():
22+
""" Init the TaskInformation class with task data for testing """
23+
task.TaskInformation(
2524
join(Configuration()['directories']['test_data'], 'data', 'task', 'task-info.json')
2625
)
2726

0 commit comments

Comments
 (0)