def render_config_resource(): parser = argparse.ArgumentParser(epilog='This command will soon change its behaviour. Use `run-config` instead.') parser.add_argument('command') parser.add_argument('-cp', '--config_path', required=True) args = parser.parse_known_args()[0] read_config(args.config_path).get_resource(args.command)
def test_overwrite(): rm = read_config('expressions/literals.config').string_input( 'literals = 1') rm.literals with pytest.raises(RuntimeError): rm.import_config('expressions/literals.config') rm = read_config('expressions/literals.config').string_input('a = 2') rm.string_input('b = a + 1') rm.literals
def build_config(): parser = argparse.ArgumentParser( description= 'Read the `input` config and flatten all its imports in order to obtain ' 'an `output` config without dependencies.') parser.add_argument('input', help='Path to the input config file.') parser.add_argument('output', help='Path to the output config file.') parser.add_argument('entry_points', nargs='*', help='Names that should be kept during rendering.') args = parser.parse_args() output = Path(args.output) output.parent.mkdir(parents=True, exist_ok=True) read_config(args.input).save_config(output, args.entry_points or None)
def test_operators(): rm = read_config('expressions/operators.config') assert rm.arithmetic == [ 5, 6, 0.75, 0, 2, 6, -3, -5, 5, True, 63, 36, 27, 5, 3, True, False, False, False, True, True, False, True, False, True, True, True, True ] assert rm.priority == 1 + 2 * 3**4 + 1
def test_func_def(): rm = read_config('statements/funcdef.config') assert rm.f() == 1 assert rm.inc_first(['a', 'b', 'c']) == (2, 1, 1) assert rm.qsort([2, 1, 3, 10, 10]) == [1, 2, 3, 10, 10] assert rm.returner(2)() == 2 assert rm.h_with_defaults(0, n=3) == 1 assert rm.doc.__doc__ == 'docstring'
def test_attr_error(): rm = read_config('imports/imports.config') with pytest.raises(AttributeError): rm.undefined_value with pytest.raises(KeyError): rm['undefined_value'] with pytest.raises(ResourceError): rm.get_resource('undefined_value')
def test_lambda(): rm = read_config('expressions/lambda_.config') assert rm.b(2) == 8 assert rm.c(2, rm.a) == 8 assert rm.d(1)(2) == [1, 2] assert rm.e() == 8 assert rm.test == [1, 8, 32] assert rm.vararg(1, 2, 3) == (2, 3) assert rm.only_vararg(1) == (1, )
def build(self, config: PathLike, folder: PathLike): folder = Path(folder) for i, ids in enumerate(self.split): # TODO: move check to constructor if len(ids) != len(self.prefixes): raise ValueError( f"The number of identifier groups ({len(ids)}) " f"does not match the number of prefixes ({len(self.prefixes)})" ) local = folder / f'experiment_{i}' local.mkdir(parents=True) for val, prefix in zip(ids, self.prefixes): save(val, local / self._expand_prefix(prefix), indent=0) # resource manager is needed here, because there may be inheritance read_config(config).save_config(folder / 'resources.config')
def test_entry_points(subtests, tests_path): for path in tests_path.glob('**/*.config'): with subtests.test(filename=path.name): config = read_config(path) # for each entry point the config must be readable and give the same results for name in config._scope.keys(): source = config.render_config(name) assert config.render_config(name) == source assert read_string(source).render_config() == source, path
def test_import(): rm = read_config('imports/imports.config') assert rm.numpy == np try: rm.r rm.os rm.std rm.mean except BaseException: pytest.fail()
def test_lambda_args(): rm = read_config('expressions/lambda_.config') assert rm.with_default() == (1, 2) assert rm.keyword(y=1) == ((), 1) with pytest.raises(TypeError): rm.b(1, 2) try: rm.vararg(x=1) except BaseException: pytest.fail()
def get_resource_manager(source_path: str, shortcuts: dict = None, injections: dict = None) -> ResourceManager: """Read and parse a config. See ``resource_manager.read_config`` for details.""" return read_config(source_path, shortcuts={ **SHORTCUTS, **(shortcuts or {}) }, injections=injections)
def test_literals(): rm = read_config('expressions/literals.config') assert rm.literals == [ True, False, None, ..., 1, 2, 3, .4, 5j, .55e-2, 0x101, 0b101, 'abc', r'def', b'ghi', u'jkl', rb'mno', 'string interpolation: (3, 1)', 'a: 3 b: 7', [], [1, 2, 1], (), (1, 2, 1), (1, ), {1, 2, 1}, {}, { 1: 2, 3: 4, '5': 6 } ]
def get_resource_manager(source_path: str, shortcuts: dict = None, injections: dict = None) -> ResourceManager: """ Read and parse a config. See ``resource_manager.read_config`` for details. Warnings -------- This function is deprecated. Use ``resource_manager.read_config`` instead: >>> from resource_manager import read_config >>> config = read_config('some_path.config') """ return read_config(source_path, shortcuts={**SHORTCUTS, **(shortcuts or {})}, injections=injections)
def render_config_resource(): parser = argparse.ArgumentParser( description= 'Run the given `config` by evaluating the passed `expression`.') parser.add_argument('config', help='Path to a config file.') parser.add_argument('expression', help='The entry point.') # ignoring unknown args as they might be used inside the config args = parser.parse_known_args()[0] value = read_config(args.config).eval(args.expression) if value is not None: print(value)
def build(): parser = argparse.ArgumentParser( 'Build an experiment layout from the provided config.', add_help=False) parser.add_argument('config') args = parser.parse_known_args()[0] layout = read_config(args.config).layout layout.build_parser(parser) parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS, help='Show this message and exit') args = parser.parse_args() layout.build(**vars(args))
def test_unpacking(): rm = read_config('statements/funcdef.config') assert rm.unpack([1, 2]) == 3 assert rm.nested_unpack([1, [2, 3]]) == (1, 2, 3) assert rm.deep_unpack([[[[[[1]]]]], 2]) == (1, 2) assert rm.single_unpack([[[1]]]) == [[1]] with pytest.raises(TypeError): rm.unpack(1) with pytest.raises(ValueError): rm.unpack([1]) with pytest.raises(ValueError): rm.unpack([1, 2, 3]) with pytest.raises(SyntaxError): read_string('a, b = 1, 2') with pytest.raises(SyntaxError): read_string('def f(x): a, *b = x; return a')
class TestMNIST(unittest.TestCase): # TODO: use a temp dir base_path = Path('~/tests/MNIST').expanduser() experiment_path = base_path / 'exp' config_path = 'dpipe/tests/mnist/setup.config' config = read_config(config_path) @classmethod def tearDownClass(cls): if cls.experiment_path.exists(): shutil.rmtree(cls.experiment_path) def test_pipeline(self): # build self.config.layout.build(self.config_path, self.experiment_path) names = {p.name for p in self.experiment_path.iterdir()} assert {'resources.config' } | {f'experiment_{i}' for i in range(len(names) - 1)} == names # split ids = set(self.config.ids) test_ids = [] for exp in self.experiment_path.iterdir(): if exp.is_dir(): train, val, test = [ set(load(exp / f'{name}_ids.json')) for name in ['train', 'val', 'test'] ] test_ids.append(test) assert not train & val assert not train & test assert not val & test assert ids == train | val | test for first, second in itertools.permutations(test_ids, 2): assert not first & second # training self.config.layout.run(self.experiment_path / 'resources.config', folds=[0]) assert load(self.experiment_path / 'experiment_0/test_metrics/accuracy.json') >= .95
def test_comprehensions(): rm = read_config('expressions/comprehensions.config') assert rm.everything == [ list(range(10)), [0, 6], [1, 2, 3], [1, 3], [1, 3] ] assert rm.set_comp == {i for i in range(10)} assert rm.dict_comp == {i: i + 1 for i in range(10)} assert list(rm.gen_comp) == list(range(10)) assert rm.even == list(range(0, 10, 2)) with pytest.raises(SemanticError): read_string('_ = [x for i in range(1)]') with pytest.raises(SemanticError): read_string('_ = [[x, i] for i in range(1)]') with pytest.raises(SemanticError): read_string('_ = [i for i in [[2]] if x != 2 for x in i]')
def test_slice(): rm = read_config('expressions/slices.config') assert rm.correct_slices == rm.slices
import pandas as pd from dpipe.io import save_json, load_numpy from dpipe.im.metrics import dice_score from sinogram_augmentation.metrics import surface_dice if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--exp_path', required=True, type=str) parser.add_argument('--n_exp', required=True, type=str) args = parser.parse_known_args()[0] exp_path = Path(args.exp_path) n = args.n_exp cfg = read_config(exp_path / 'resources.config') print(f'\n>>> Metric for `experiment_{n}`\n', flush=True) df = pd.read_csv(f'{os.path.realpath("..")}/cancer-500-paired-kernels.csv') test_ids1 = list(df['SeriesInstanceUID_1'].values) test_ids2 = list(df['SeriesInstanceUID_2'].values) dice_records, sdice05_records, sdice10_records, sdice20_records = {}, {}, {}, {} for id1, id2 in tqdm(zip(test_ids1, test_ids2)): predict1 = load_numpy( exp_path / f'experiment_{n}/test_predictions_cancer500/{id1}.npy', decompress=True) predict2 = load_numpy( exp_path / f'experiment_{n}/test_predictions_cancer500/{id2}.npy', decompress=True) if predict1.shape == predict2.shape: spacing = cfg.dataset_to_test.spacing(id1)
import os import argparse import functools from resource_manager import read_config if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('command') parser.add_argument('--config_path', required=True) args = parser.parse_known_args()[0] read_config(args.config_path).get_resource(args.command)
def test_upper_import(): rm = read_config('imports/folder/upper_import.config') assert 'just override os' == rm.os assert np == rm.numpy
def test_inheritance_order(): rm = read_config('imports/order1.config') assert rm.literals is not None rm = read_config('imports/order2.config') assert rm.literals is None
def test_cycle_import(): try: read_config('imports/cycle_import.config') except RecursionError: pytest.fail()
def test_import_partial_upper(): rm = read_config('imports/folder/upper_partial.config') assert rm.numpy is None assert np == rm.np rm = read_config('imports/folder/child/third.config') assert rm.a == [1, 2, 1]
def test_multiple_definitions(): rm = read_config('statements/multiple_definitions.config') assert '''a = b = c = 1\nd = a\n''' == rm.render_config() rm = read_config('statements/import_from_multiple.config') assert '''a = 2\nb = c = 1\nd = a\n''' == rm.render_config()
def test_import_partial(): rm = read_config('imports/import_from_config.config') assert rm.one == [1, 2] rm = read_config('imports/import_twice.config') assert rm.link_ == rm.link assert rm.func_ == rm.func
def test_cached(): rm = read_config('imports/cached/main.config') assert rm.x == 1
def test_build_config(): rm = read_config('imports/config_import.config', shortcuts={'expressions': 'expressions'}) with open('imports/built.config') as built: assert rm.render_config() == built.read()