|
| 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