Пример #1
0
    def test_source_code_compilation(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))

        new_source_code = pyparam_parser.compile_source_code(
            source_code=source_code, config=config)

        with open(self.source_code_tmp_path, "w") as file:
            file.write(new_source_code)
Пример #2
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)
Пример #3
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)
Пример #4
0
    def test_dict_dtype_pyparam(self):
        source_code = pyparam_parser.read_source_code(self.sample_path /
                                                      "template6.py")
        root = ast.parse(source=source_code)
        pyparam_nodes = pyparam_parser.find_pyparams_assignments_nodes(root)

        pyparam = NamedPyParam.from_ast_node(pyparam_nodes[0])

        exp_param = NamedPyParam(
            name="foo1_dict",
            param=PyParam(value={
                "a": 1,
                "b": 2
            },
                          dtype=dict,
                          scope="model",
                          desc="foo1"),
        )
        self.assertEqual(pyparam, exp_param)

        pyparam = NamedPyParam.from_ast_node(pyparam_nodes[1])
        exp_param = NamedPyParam(
            name="foo2_dict",
            param=PyParam(value={"a": [1, 1, 2]},
                          dtype=dict,
                          scope="model",
                          desc="foo2"),
        )

        self.assertEqual(pyparam, exp_param)

        pyparam = NamedPyParam.from_ast_node(pyparam_nodes[2])
        exp_param = NamedPyParam(
            name="foo3_dict",
            param=PyParam(
                value={
                    "a": {
                        "aa": 3,
                        "ab": [1, 3]
                    },
                    "b": [1, 2, 3],
                    "c": "test"
                },
                dtype=dict,
                scope="model",
                desc="foo2",
            ),
        )
        self.assertEqual(pyparam, exp_param)

        pyparam = NamedPyParam.from_ast_node(pyparam_nodes[3])

        exp_param = NamedPyParam(
            name="foo4_dict",
            param=PyParam(
                value=[
                    {
                        "a": {
                            "aa": 3,
                            "ab": [1, 3]
                        },
                        "b": [1, 2, 3],
                        "c": "test"
                    },
                    {
                        "A": {
                            "AA": 15.,
                            "AB": [1]
                        },
                        "B": [2, 3],
                        "C": "TEST"
                    },
                ],
                dtype=list,
                scope="model",
                desc="foo4 nested dict in list",
            ),
        )
        self.assertEqual(pyparam, exp_param)

        source_code = pyparam_parser.read_source_code(self.sample_path /
                                                      "template6.py")
        pyparam_parser.source_to_yaml_config(source_code, self.config_tmp_path)
        config = pyparam_parser.read_yaml_file(Path(self.config_tmp_path))

        pyparam_parser.compile_source_code(source_code,
                                           config,
                                           validate_version=False)
Пример #5
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)
Пример #6
0
 def test_to_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)