Skip to content

Commit bb28cd1

Browse files
committed
TST: Add configuration module testing
1 parent 7379b58 commit bb28cd1

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed

nibabies/data/tests/config.toml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[environment]
2+
cpu_count = 8
3+
exec_docker_version = "20.10.12"
4+
exec_env = "nibabies-docker"
5+
free_mem = 0.4
6+
overcommit_policy = "heuristic"
7+
overcommit_limit = "50%"
8+
nipype_version = "1.6.1"
9+
templateflow_version = "0.7.2"
10+
version = "21.1.0"
11+
12+
[execution]
13+
bids_dir = "/data"
14+
bids_database_dir = "/tmp/bids_db"
15+
bids_description_hash = "c47e9ebb943ca662556808b2aeac3f6c8bb2a242696c32850c64ec47aba80d9e"
16+
boilerplate_only = false
17+
sloppy = true
18+
debug = []
19+
derivatives = [ "/opt/derivatives/precomputed",]
20+
fs_license_file = "/opt/freesurfer/license.txt"
21+
fs_subjects_dir = "/opt/subjects"
22+
layout = "BIDS Layout: .../data | Subjects: 1 | Sessions: 1 | Runs: 1"
23+
log_dir = "/tmp/logs"
24+
log_level = 20
25+
low_mem = false
26+
md_only_boilerplate = false
27+
nibabies_dir = "/out"
28+
notrack = false
29+
output_dir = "/out"
30+
me_output_echos = false
31+
output_layout = "bids"
32+
output_spaces = "MNIInfant:cohort-1:res-native"
33+
reports_only = false
34+
run_uuid = "20220323-202555_01a7d80d-7ff4-4b13-a99c-ec399045e9ff"
35+
segmentation_atlases_dir = "/opt/segmentations"
36+
participant_label = [ "01",]
37+
templateflow_home = "/home/nibabies/.cache/templateflow"
38+
work_dir = "/scratch"
39+
write_graph = false
40+
41+
[workflow]
42+
anat_only = false
43+
aroma_err_on_warn = false
44+
aroma_melodic_dim = -200
45+
bold2t1w_dof = 6
46+
bold2t1w_init = "register"
47+
cifti_output = false
48+
fd_radius = 45
49+
fmap_bspline = false
50+
force_syn = false
51+
hires = true
52+
ignore = [ "slicetiming",]
53+
longitudinal = false
54+
medial_surface_nan = false
55+
regressors_all_comps = false
56+
regressors_dvars_th = 1.5
57+
regressors_fd_th = 0.5
58+
run_reconall = true
59+
skull_strip_fixed_seed = false
60+
skull_strip_template = "UNCInfant:cohort-1"
61+
skull_strip_t1w = "force"
62+
slice_time_ref = 0.5
63+
spaces = "MNIInfant:cohort-1:res-native MNIInfant:cohort-1"
64+
use_aroma = false
65+
use_bbr = false
66+
use_syn_sdc = false
67+
68+
[nipype]
69+
crashfile_format = "txt"
70+
get_linked_libs = false
71+
memory_gb = 8
72+
nprocs = 4
73+
omp_nthreads = 2
74+
plugin = "MultiProc"
75+
resource_monitor = false
76+
stop_on_first_crash = false
77+
78+
[nipype.plugin_args]
79+
maxtasksperchild = 1
80+
raise_insufficient = false

nibabies/tests/__init__.py

Whitespace-only changes.

nibabies/tests/test_config.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
#
4+
# Copyright 2021 The NiPreps Developers <[email protected]>
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# We support and encourage derived works from this project, please read
19+
# about our expectations at
20+
#
21+
# https://www.nipreps.org/community/licensing/
22+
#
23+
"""Check the configuration module and file."""
24+
import os
25+
from pathlib import Path
26+
from pkg_resources import resource_filename as pkgrf
27+
from unittest.mock import patch
28+
29+
import pytest
30+
from toml import loads
31+
from niworkflows.utils.spaces import format_reference
32+
33+
from .. import config
34+
35+
36+
def _reset_config():
37+
"""
38+
Forcibly reload the configuration module to restore defaults.
39+
.. caution::
40+
`importlib.reload` creates new sets of objects, but will not remove
41+
previous references to those objects."""
42+
import importlib
43+
44+
importlib.reload(config)
45+
46+
47+
def test_reset_config():
48+
execution = config.execution
49+
setattr(execution, 'bids_dir', 'TESTING')
50+
assert config.execution.bids_dir == 'TESTING'
51+
_reset_config()
52+
assert config.execution.bids_dir is None
53+
# Even though the config module was reset,
54+
# previous references to config classes
55+
# have not been touched.
56+
assert execution.bids_dir == 'TESTING'
57+
58+
59+
def test_config_spaces():
60+
"""Check that all necessary spaces are recorded in the config."""
61+
filename = Path(pkgrf('nibabies', 'data/tests/config.toml'))
62+
settings = loads(filename.read_text())
63+
for sectionname, configs in settings.items():
64+
if sectionname != 'environment':
65+
section = getattr(config, sectionname)
66+
section.load(configs, init=False)
67+
config.nipype.init()
68+
config.loggers.init()
69+
config.init_spaces()
70+
71+
spaces = config.workflow.spaces
72+
assert "MNI152NLin6Asym:res-2" not in [str(s) for s in spaces.get_standard(full_spec=True)]
73+
74+
assert "MNI152NLin6Asym_res-2" not in [
75+
format_reference((s.fullname, s.spec))
76+
for s in spaces.references
77+
if s.standard and s.dim == 3
78+
]
79+
80+
config.workflow.use_aroma = True
81+
config.init_spaces()
82+
spaces = config.workflow.spaces
83+
84+
assert "MNI152NLin6Asym:res-2" in [str(s) for s in spaces.get_standard(full_spec=True)]
85+
86+
assert "MNI152NLin6Asym_res-2" in [
87+
format_reference((s.fullname, s.spec))
88+
for s in spaces.references
89+
if s.standard and s.dim == 3
90+
]
91+
92+
config.execution.output_spaces = None
93+
config.workflow.use_aroma = False
94+
config.workflow.age_months = None
95+
config.init_spaces()
96+
spaces = config.workflow.spaces
97+
98+
assert [str(s) for s in spaces.get_standard(full_spec=True)] == []
99+
assert [
100+
format_reference((s.fullname, s.spec))
101+
for s in spaces.references
102+
if s.standard and s.dim == 3
103+
] == []
104+
105+
# but adding age will populate output spaces with a default
106+
config.execution.output_spaces = None
107+
config.workflow.use_aroma = False
108+
config.workflow.age_months = 1
109+
config.init_spaces()
110+
spaces = config.workflow.spaces
111+
112+
assert [str(s) for s in spaces.get_standard(full_spec=True)] == []
113+
assert [
114+
format_reference((s.fullname, s.spec))
115+
for s in spaces.references
116+
if s.standard and s.dim == 3
117+
] == ['MNIInfant_cohort-1']
118+
_reset_config()
119+
120+
121+
@pytest.mark.parametrize(
122+
"master_seed,ants_seed,numpy_seed", [(1, 17612, 8272), (100, 19094, 60232)]
123+
)
124+
def test_prng_seed(master_seed, ants_seed, numpy_seed):
125+
"""Ensure seeds are properly tracked"""
126+
seeds = config.seeds
127+
with patch.dict(os.environ, {}):
128+
seeds.load({'_random_seed': master_seed}, init=True)
129+
assert getattr(seeds, 'master') == master_seed
130+
assert seeds.ants == ants_seed
131+
assert seeds.numpy == numpy_seed
132+
assert os.getenv("ANTS_RANDOM_SEED") == str(ants_seed)
133+
134+
_reset_config()
135+
for seed in ('_random_seed', 'master', 'ants', 'numpy'):
136+
assert getattr(config.seeds, seed) is None

0 commit comments

Comments
 (0)