コード例 #1
0
    def test_clean_arg_true(self):
        """
        Test parses dict correctly with clean set to true.
        """
        conf = {'_foo': '2.0',
                '_bar': '1.0'}
        args = process_args(conf)

        conf = {'test': 'foo + bar'}
        args = process_args(conf, clean=True)

        expected = {'test': 'foobar'}

        self.assertDictEqual(args, expected)
コード例 #2
0
    def create_function_dict(self, conf):
        """
        Create a dictionary of all functions from a dictionary
        (or configparser).

        Args:
            conf (dict):
                The configuration to parse into a list of functions.
                e.g. {'f1.type': 'constant', 'f1.mean': '2e-5', 'f1.sd': ...,
                      'f2.type': ..., ...}

        Returns:
            dict: A dictionary with key as the names from files and val as a
                complete function.
        """
        all_funcs = process_args(conf, factory=self, str_keys=['type', 'path'])

        funcs_dict = {}
        for k, v in all_funcs.items():
            if isinstance(v, dict):
                f_type = v.pop('type')
                funcs_dict[k.lower()] = self.create_function(f_type, **v)
            else:
                funcs_dict[k.lower()] = v

        return funcs_dict
コード例 #3
0
    def test_dict_with_interim_values(self):
        """
        Test parses dict correctly with interim values.
        """
        conf = {'test': 'foo + 1',
                '_foo': '2.0'}

        expected = {'test': 3.0}

        args = process_args(conf)
        self.assertDictEqual(args, expected)
コード例 #4
0
    def test_dict_with_only_values(self):
        """
        Test parses dict correctly with only values.
        """
        conf = {'test': '3.0',
                'foo': 'false'}

        expected = {'test': 3.0,
                    'foo': False}

        args = process_args(conf)
        self.assertDictEqual(args, expected)
コード例 #5
0
    def test_str_keys_arg(self):
        """
        Test parses dict correctly with some entries defined as strings.
        """

        conf = {'test': 'foo + 1',
                'test2': '2.0'}

        expected = {'test': 'foo + 1',
                    'test2': 2.0}

        args = process_args(conf, str_keys=['test'])
        self.assertDictEqual(args, expected)
コード例 #6
0
    def test_dict_with_interim_functions(self):
        """
        Test parses dict correctly with interim functions.
        """
        conf = {'test': 'foo + 2.0',
                '_foo.type': 'gaussian',
                '_foo.scale': '10',
                '_foo.mean': '0.5',
                '_foo.sd': '0.5'}

        args = process_args(conf, self.factory)
        self.assertListEqual(list(args.keys()), ['test'])
        self.assertAlmostEqual(args['test']([0.5, 0.5, 0.5]).item(), 12.0)
コード例 #7
0
    def test_dict_with_many_interim_values(self):
        """
        Test parses dict correctly with many interim values.
        """

        conf = {'test': 'foo + 1',
                '_foo': 'bar + baz',
                '_bar': '17.0',
                '_baz': 'bar * 2'}

        expected = {'test': 17.0 + (17.0 * 2) + 1}

        args = process_args(conf)
        self.assertDictEqual(args, expected)
コード例 #8
0
    def test_nested_values(self):
        """
        Test parses dict correctly with nested values.
        """
        conf = {'test.a': '3.0',
                'test.b': '4.1',
                'foo.b': 'false'}

        expected = {'test': {'a': 3.0,
                             'b': 4.1},
                    'foo': {'b': False}}

        args = process_args(conf)
        self.assertDictEqual(args, expected)
コード例 #9
0
    def parse(self, conf):
        """
        Parse the SOLVER section of the config into the required attributes.

        Args:
            conf (configparser section or dict):
                The full SOLVER section from the config.
        """
        known_vars = list(vars(self))
        all_inps = process_args(conf,
                                factory=None,
                                str_keys=['file_path', 'method'])
        self.file_path = all_inps['file_path']
        self.method = all_inps['method']
        self.params = {k: v for k, v in all_inps.items()
                       if k not in known_vars}
コード例 #10
0
    def parse(self, conf):
        """
        Parse the BOUNDARIES section of the config into the bcs list.

        Args:
            conf (configparser section or dict):
                The full BOUNDARIES section from the config.
        """
        boundaries = process_args(conf,
                                  factory=self.factory,
                                  str_keys=['type', 'boundary_type'])

        for b in boundaries.values():
            for k, v in b.items():
                if isinstance(v, dict) and 'type' in v:
                    f_type = v.pop('type')
                    func = self.factory.create_function(f_type, **v)
                    b[k] = func

        self.bcs = list(boundaries.values())
コード例 #11
0
    def test_dict_with_functions(self):
        """
        Test parses dict correctly with functions.
        """
        conf = {'test.type': 'constant',
                'test.value': '4.1',
                'foo.type': 'gaussian',
                'foo.scale': '10',
                'foo.mean': '0.5',
                'foo.sd': '0.5'}

        expected = {'test': {'type': 'constant',
                             'value': 4.1},
                    'foo': {'type': 'gaussian',
                            'scale': 10,
                            'mean': 0.5,
                            'sd': 0.5}}

        args = process_args(conf)
        self.assertDictEqual(args, expected)