Example #1
0
    def test_save_and_load_check_descriptions(self):

        source_code = pyparam_parser.read_source_code(self.sample_path /
                                                      "template7.py")
        pyparams = pyparam_parser.get_all_pyparams_from_source_code(
            source_code)
        pyparam_parser.source_to_yaml_config(source_code, self.config_tmp_path)

        config = pyparam_parser.read_yaml_file(self.config_tmp_path)
        loaded_pyparams = pyparam_parser.read_params_from_config(config)

        self.assertEqual(pyparams, loaded_pyparams)
        pyparam_parser.compile_source_code(source_code, config)
Example #2
0
    def test_get_param(self):
        config = pyparam_parser.read_yaml_file(self.sample_path /
                                               "template3_config.yml")
        params_list = pyparam_parser.read_params_from_config(config)
        selected_param = pyparam_parser.get_param(
            "feature_extractor/include_root", params_list)
        param = selected_param.param_replace(value=True)

        exp_param = NamedPyParam(
            name="include_root",
            param=PyParam(value=True,
                          dtype=bool,
                          scope="feature_extractor",
                          desc=""),
        )
        self.assertEqual(param, exp_param)
Example #3
0
    def test_replace_configs(self):
        config = pyparam_parser.read_yaml_file(self.sample_path /
                                               "template3_config.yml")
        config_params = pyparam_parser.read_params_from_config(config)
        param1 = pyparam_parser.get_param(
            "feature_extractor/include_root",
            config_params).param_replace(value=True)
        param2 = pyparam_parser.get_param(
            "feature_extractor/entry_flow_blocks",
            config_params).param_replace(value=(1, 1))
        param3 = NamedPyParam("some_var", PyParam("test", scope="test_scope"))
        params_to_replace = [param1, param2, param3]

        new_config_params = pyparam_parser.replace_params(
            params_to_replace,
            config_params=config_params,
            ignore_missing_keys=True)
        with self.assertRaises(ValueError):
            pyparam_parser.get_param("test_scope/some_var", new_config_params)
Example #4
0
    def test_params_wo_annotations_in_functions_def(self):

        source_code = pyparam_parser.read_source_code(self.sample_path /
                                                      "template9.py")
        pyparams = pyparam_parser.get_all_pyparams_from_source_code(
            source_code)
        pyparam_parser.source_to_yaml_config(source_code, self.config_tmp_path)
        config = pyparam_parser.read_yaml_file(self.config_tmp_path)
        loaded_pyparams = pyparam_parser.read_params_from_config(config)

        self.assertEqual(pyparams, loaded_pyparams)
        compiled_source = pyparam_parser.compile_source_code(
            source_code, config)

        self.assertTrue(
            "some_function(x, y, param2: int=2, param3: float=3, "
            "param4: int=4, param5=5, param6=6)" in compiled_source)
        self.assertTrue("self, arg1: float=1.1, arg2=2.2" in compiled_source)
        self.assertTrue("result = some_function(0, 1, param2=12, param3=13)" in
                        compiled_source)
        self.assertTrue("  param2 = 2" in compiled_source)
        self.assertTrue("  param3: int = 3" in compiled_source)
        self.assertTrue(
            "  def nested_function2(x, y, np2: int=2)" in compiled_source)
Example #5
0
    def test_replace_param(self):
        config = pyparam_parser.read_yaml_file(self.sample_path /
                                               "template3_config.yml")
        config_params = pyparam_parser.read_params_from_config(config)

        param1 = pyparam_parser.get_param(
            "feature_extractor/include_root",
            config_params).param_replace(value=True)

        param2 = pyparam_parser.get_param(
            "feature_extractor/entry_flow_blocks",
            config_params).param_replace(value=(1, 1))

        config_params = pyparam_parser.replace_param(
            param=param1, config_params=config_params)
        config_params = pyparam_parser.replace_param(
            param=param2, config_params=config_params)

        save_file_path = Path(f"{self.tmp_dir}/config.yaml")
        pyparam_parser.params_to_yaml_config(config_params=config_params,
                                             save_file_path=save_file_path)

        config = pyparam_parser.read_yaml_file(save_file_path)
        params = pyparam_parser.read_params_from_config(config)

        exp_params = [
            NamedPyParam(
                name="activation_fn_in_separable_conv",
                param=PyParam(value=False,
                              dtype=bool,
                              scope="feature_extractor",
                              desc=""),
            ),
            NamedPyParam(
                name="base_num_filters",
                param=PyParam(value=4,
                              dtype=int,
                              scope="feature_extractor",
                              desc=""),
            ),
            NamedPyParam(
                name="entry_flow_blocks",
                param=PyParam(
                    value=(1, 1),
                    dtype=tuple,
                    scope="feature_extractor",
                    desc="Number of units in each bock in the entry flow.",
                ),
            ),
            NamedPyParam(
                name="include_root",
                param=PyParam(value=True,
                              dtype=bool,
                              scope="feature_extractor",
                              desc=""),
            ),
            NamedPyParam(
                name="middle_flow_blocks",
                param=PyParam(
                    value=(1, ),
                    dtype=tuple,
                    scope="feature_extractor",
                    desc="Number of units in the middle flow.",
                ),
            ),
            NamedPyParam(
                name="regularize_depthwise",
                param=PyParam(value=False,
                              dtype=bool,
                              scope="feature_extractor",
                              desc=""),
            ),
            NamedPyParam(
                name="version",
                param=PyParam(value="1.0",
                              dtype=str,
                              scope="",
                              desc="model version"),
            ),
        ]

        self.assertEqual(params, exp_params)
Example #6
0
    def test_loading_yaml(self):
        source_code = pyparam_parser.read_source_code(self.sample_path /
                                                      "template3.py")
        pyparam_parser.source_to_yaml_config(source_code, self.config_tmp_path)

        config = pyparam_parser.read_yaml_file(Path(self.config_tmp_path))
        params = pyparam_parser.read_params_from_config(config)

        exp_params = [
            NamedPyParam(
                name="version",
                param=PyParam(value="1.0",
                              dtype=str,
                              scope="",
                              desc="model version"),
            ),
            NamedPyParam(
                name="base_num_filters",
                param=PyParam(value=4,
                              dtype=int,
                              scope="feature_extractor",
                              desc=""),
            ),
            NamedPyParam(
                name="include_root",
                param=PyParam(value=False,
                              dtype=bool,
                              scope="feature_extractor",
                              desc=""),
            ),
            NamedPyParam(
                name="regularize_depthwise",
                param=PyParam(value=False,
                              dtype=bool,
                              scope="feature_extractor",
                              desc=""),
            ),
            NamedPyParam(
                name="activation_fn_in_separable_conv",
                param=PyParam(value=False,
                              dtype=bool,
                              scope="feature_extractor",
                              desc=""),
            ),
            NamedPyParam(
                name="entry_flow_blocks",
                param=PyParam(
                    value=(1, 1, 1),
                    dtype=tuple,
                    scope="feature_extractor",
                    desc="Number of units in each bock in the entry flow.",
                ),
            ),
            NamedPyParam(
                name="middle_flow_blocks",
                param=PyParam(
                    value=(1, ),
                    dtype=tuple,
                    scope="feature_extractor",
                    desc="Number of units in the middle flow.",
                ),
            ),
        ]

        self.assertEqual(params, exp_params)