Python configargparse.YAMLConfigFileParser() Examples

The following are 16 code examples of configargparse.YAMLConfigFileParser(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module configargparse , or try the search function .
Example #1
Source File: preprocess.py    From ITDD with MIT License 6 votes vote down vote up
def parse_args():
    parser = configargparse.ArgumentParser(
        description='preprocess.py',
        config_file_parser_class=configargparse.YAMLConfigFileParser,
        formatter_class=configargparse.ArgumentDefaultsHelpFormatter)

    opts.config_opts(parser)
    opts.add_md_help_argument(parser)
    opts.preprocess_opts(parser)

    opt = parser.parse_args()
    torch.manual_seed(opt.seed)

    check_existing_pt_files(opt)

    return opt 
Example #2
Source File: utils.py    From OpenKiwi with GNU Affero General Public License v3.0 6 votes vote down vote up
def save_args_to_file(file_name, **kwargs):
    """
    Saves `**kwargs` to a file.

    Args:
        file_name (str): The name of the file where the args should
            be saved in.

    """
    options_to_save = {
        k.replace('_', '-'): v for k, v in kwargs.items() if v is not None
    }

    content = configargparse.YAMLConfigFileParser().serialize(options_to_save)
    Path(file_name).write_text(content)
    logging.debug('Saved current options to config file: {}'.format(file_name)) 
Example #3
Source File: search.py    From OpenKiwi with GNU Affero General Public License v3.0 6 votes vote down vote up
def run(options, extra_options):
    config_parser = configargparse.YAMLConfigFileParser()
    config_options = config_parser.parse(Path(options.config).read_text())
    meta, fixed_options = split_options(config_options)

    # Run for each combination of arguments
    fixed_args = [options.model_name] + extra_options
    if options.experiment_name:
        fixed_args += parser.convert_item_to_command_line_arg(
            None, 'experiment-name', options.experiment_name
        )

    meta_keys = meta.keys()
    meta_values = meta.values()
    for values in itertools.product(*meta_values):
        assert len(meta_keys) == len(values)
        run_args = []
        for key, value in zip(meta_keys, values):
            action = get_action(key)
            run_args.extend(
                parser.convert_item_to_command_line_arg(action, key, str(value))
            )
        full_args = fixed_args + run_args + fixed_options
        train.main(full_args) 
Example #4
Source File: better_argparse.py    From OpenKiwi with GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(
        self, name, pipeline_parser, pipeline_config_key, options_fn=None
    ):
        self.name = name
        self._pipeline_parser = pipeline_parser
        self._pipeline_config_key = pipeline_config_key.replace('-', '_')

        self._parser = configargparse.get_argument_parser(
            self.name,
            prog='kiwi {}'.format(self.name),
            add_help=False,
            config_file_parser_class=configargparse.YAMLConfigFileParser,
            ignore_unknown_config_file_keys=False,
        )

        self._parser.add(
            '--config',
            required=False,
            is_config_file=True,
            type=PathType(exists=True),
            help='Load config file from path',
        )

        if options_fn is not None:
            options_fn(self._parser) 
Example #5
Source File: test_configargparse.py    From ConfigArgParse with MIT License 6 votes vote down vote up
def testYAMLConfigFileParser_Basic(self):
        try:
            import yaml
        except:
            logging.warning("WARNING: PyYAML not installed. "
                            "Couldn't test YAMLConfigFileParser")
            return

        p = configargparse.YAMLConfigFileParser()
        self.assertGreater(len(p.get_syntax_description()), 0)

        input_config_str = StringIO("""a: '3'\n""")
        parsed_obj = p.parse(input_config_str)
        output_config_str = p.serialize(dict(parsed_obj))

        self.assertEqual(input_config_str.getvalue(), output_config_str)

        self.assertDictEqual(parsed_obj, {'a': '3'}) 
Example #6
Source File: better_argparse.py    From OpenKiwi with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_parser(cls, name, **kwargs):
        if name in cls._parsers:
            return cls._parsers[name]
        parser = configargparse.get_argument_parser(
            name,
            prog='... {}'.format(name),
            config_file_parser_class=configargparse.YAMLConfigFileParser,
            ignore_unknown_config_file_keys=True,
            **kwargs,
        )
        cls._parsers[name] = parser
        return parser 
Example #7
Source File: config.py    From HyperGCN with Apache License 2.0 5 votes vote down vote up
def parse():
	"""
	adds and parses arguments / hyperparameters
	"""
	default = os.path.join(current(), data + ".yml")
	p = configargparse.ArgParser(config_file_parser_class = YAMLConfigFileParser, default_config_files=[default])
	p.add('-c', '--my-config', is_config_file=True, help='config file path')
	p.add('--data', type=str, default=data, help='data name (coauthorship/cocitation)')
	p.add('--dataset', type=str, default=dataset, help='dataset name (e.g.: cora/dblp/acm for coauthorship, cora/citeseer/pubmed for cocitation)')
	p.add('--mediators', type=bool, default=mediators, help='True for Laplacian with mediators, False for Laplacian without mediators')
	p.add('--fast', type=bool, default=fast, help='faster version of HyperGCN (True)')
	p.add('--split', type=int, default=split, help='train-test split used for the dataset')
	p.add('--depth', type=int, default=depth, help='number of hidden layers')
	p.add('--dropout', type=float, default=dropout, help='dropout probability for GCN hidden layer')
	p.add('--rate', type=float, default=rate, help='learning rate')
	p.add('--decay', type=float, default=decay, help='weight decay')
	p.add('--epochs', type=int, default=epochs, help='number of epochs to train')
	p.add('--gpu', type=int, default=gpu, help='gpu number to use')
	p.add('--cuda', type=bool, default=cuda, help='cuda for gpu')
	p.add('--seed', type=int, default=seed, help='seed for randomness')
	p.add('-f') # for jupyter default
	return p.parse_args() 
Example #8
Source File: argparse2rst.py    From espnet with Apache License 2.0 5 votes vote down vote up
def get_parser():
    parser = configargparse.ArgumentParser(
        description='generate RST from argparse options',
        config_file_parser_class=configargparse.YAMLConfigFileParser,
        formatter_class=configargparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('src', type=str, nargs='+',
                        help='source python files that contain get_parser() func')
    return parser


# parser 
Example #9
Source File: parse.py    From encoder-agnostic-adaptation with MIT License 5 votes vote down vote up
def __init__(
            self,
            config_file_parser_class=cfargparse.YAMLConfigFileParser,
            formatter_class=cfargparse.ArgumentDefaultsHelpFormatter,
            **kwargs):
        super(ArgumentParser, self).__init__(
            config_file_parser_class=config_file_parser_class,
            formatter_class=formatter_class,
            **kwargs) 
Example #10
Source File: server.py    From OpenNMT-py with MIT License 5 votes vote down vote up
def _get_parser():
    parser = configargparse.ArgumentParser(
        config_file_parser_class=configargparse.YAMLConfigFileParser,
        description="OpenNMT-py REST Server")
    parser.add_argument("--ip", type=str, default="0.0.0.0")
    parser.add_argument("--port", type=int, default="5000")
    parser.add_argument("--url_root", type=str, default="/translator")
    parser.add_argument("--debug", "-d", action="store_true")
    parser.add_argument("--config", "-c", type=str,
                        default="./available_models/conf.json")
    return parser 
Example #11
Source File: parse.py    From OpenNMT-py with MIT License 5 votes vote down vote up
def __init__(
            self,
            config_file_parser_class=cfargparse.YAMLConfigFileParser,
            formatter_class=cfargparse.ArgumentDefaultsHelpFormatter,
            **kwargs):
        super(ArgumentParser, self).__init__(
            config_file_parser_class=config_file_parser_class,
            formatter_class=formatter_class,
            **kwargs) 
Example #12
Source File: server.py    From OpenNMT-kpg-release with MIT License 5 votes vote down vote up
def _get_parser():
    parser = configargparse.ArgumentParser(
        config_file_parser_class=configargparse.YAMLConfigFileParser,
        description="OpenNMT-py REST Server")
    parser.add_argument("--ip", type=str, default="0.0.0.0")
    parser.add_argument("--port", type=int, default="5000")
    parser.add_argument("--url_root", type=str, default="/translator")
    parser.add_argument("--debug", "-d", action="store_true")
    parser.add_argument("--config", "-c", type=str,
                        default="./available_models/conf.json")
    return parser 
Example #13
Source File: parse.py    From OpenNMT-kpg-release with MIT License 5 votes vote down vote up
def __init__(
            self,
            config_file_parser_class=cfargparse.YAMLConfigFileParser,
            formatter_class=cfargparse.ArgumentDefaultsHelpFormatter,
            **kwargs):
        super(ArgumentParser, self).__init__(
            config_file_parser_class=config_file_parser_class,
            formatter_class=formatter_class,
            **kwargs) 
Example #14
Source File: test_configargparse.py    From ConfigArgParse with MIT License 5 votes vote down vote up
def testYAMLConfigFileParser_All(self):
        try:
            import yaml
        except:
            logging.warning("WARNING: PyYAML not installed. "
                            "Couldn't test YAMLConfigFileParser")
            return

        p = configargparse.YAMLConfigFileParser()

        # test the all syntax case
        config_lines = [
            "a: '3'",
            "list_arg:",
            "- 1",
            "- 2",
            "- 3",
        ]

        # test parse
        input_config_str = StringIO("\n".join(config_lines)+"\n")
        parsed_obj = p.parse(input_config_str)

        # test serialize
        output_config_str = p.serialize(parsed_obj)
        self.assertEqual(input_config_str.getvalue(), output_config_str)

        self.assertDictEqual(parsed_obj, {'a': '3', 'list_arg': [1,2,3]})



################################################################################
# since configargparse should work as a drop-in replacement for argparse
# in all situations, run argparse unittests on configargparse by modifying
# their source code to use configargparse.ArgumentParser 
Example #15
Source File: better_argparse.py    From OpenKiwi with GNU Affero General Public License v3.0 4 votes vote down vote up
def __init__(
        self,
        name,
        model_parsers,
        options_fn=None,
        add_io_options=True,
        add_general_options=True,
        add_logging_options=True,
        add_save_load_options=True,
    ):
        self.name = name

        # Give the option to create pipelines with no models
        if model_parsers is not None:
            self._models = {model.name: model for model in model_parsers}
        else:
            self._models = None

        if name in self._parsers:
            self._parser = self._parsers[name]
        else:
            self._parser = configargparse.get_argument_parser(
                self.name,
                add_help=False,
                prog='kiwi {}'.format(self.name),
                config_file_parser_class=configargparse.YAMLConfigFileParser,
                ignore_unknown_config_file_keys=True,
            )
            self._parsers[name] = self._parser
            self.add_config_option(self._parser)

            if add_io_options:
                opts.io_opts(self._parser)
            if add_general_options:
                opts.general_opts(self._parser)
            if add_logging_options:
                opts.logging_opts(self._parser)
            if add_save_load_options:
                opts.save_load_opts(self._parser)

            if options_fn is not None:
                options_fn(self._parser)

            if model_parsers is not None:
                group = self._parser.add_argument_group('models')
                group.add_argument(
                    '--model',
                    required=True,
                    choices=self._models.keys(),
                    help="Use 'kiwi {} --model <model> --help' for specific "
                    "model options.".format(self.name),
                )

        if 'config' in self._parsers:
            self._config_option_parser = self._parsers['config']
        else:
            self._config_option_parser = configargparse.get_argument_parser(
                'config', add_help=False
            )
            self._parsers['config'] = self._config_option_parser
            self.add_config_option(self._config_option_parser, read_file=False) 
Example #16
Source File: tts_decode.py    From adviser with GNU General Public License v3.0 4 votes vote down vote up
def get_parser():
    """Get parser of decoding arguments."""
    parser = configargparse.ArgumentParser(
        description='Synthesize speech from text using a TTS model on one CPU',
        config_file_parser_class=configargparse.YAMLConfigFileParser,
        formatter_class=configargparse.ArgumentDefaultsHelpFormatter)
    # general configuration
    parser.add('--config', is_config_file=True, help='config file path')
    parser.add('--config2', is_config_file=True,
               help='second config file path that overwrites the settings in `--config`.')
    parser.add('--config3', is_config_file=True,
               help='third config file path that overwrites the settings in `--config` and `--config2`.')

    parser.add_argument('--ngpu', default=0, type=int,
                        help='Number of GPUs')
    parser.add_argument('--backend', default='pytorch', type=str,
                        choices=['chainer', 'pytorch'],
                        help='Backend library')
    parser.add_argument('--debugmode', default=1, type=int,
                        help='Debugmode')
    parser.add_argument('--seed', default=1, type=int,
                        help='Random seed')
    parser.add_argument('--out', type=str, required=True,
                        help='Output filename')
    parser.add_argument('--verbose', '-V', default=0, type=int,
                        help='Verbose option')
    parser.add_argument('--preprocess-conf', type=str, default=None,
                        help='The configuration file for the pre-processing')
    # task related
    parser.add_argument('--json', type=str, required=True,
                        help='Filename of train label data (json)')
    parser.add_argument('--model', type=str, required=True,
                        help='Model file parameters to read')
    parser.add_argument('--model-conf', type=str, default=None,
                        help='Model config file')
    # decoding related
    parser.add_argument('--maxlenratio', type=float, default=5,
                        help='Maximum length ratio in decoding')
    parser.add_argument('--minlenratio', type=float, default=0,
                        help='Minimum length ratio in decoding')
    parser.add_argument('--threshold', type=float, default=0.5,
                        help='Threshold value in decoding')
    parser.add_argument('--use-att-constraint', type=strtobool, default=False,
                        help='Whether to use the attention constraint')
    parser.add_argument('--backward-window', type=int, default=1,
                        help='Backward window size in the attention constraint')
    parser.add_argument('--forward-window', type=int, default=3,
                        help='Forward window size in the attention constraint')
    # save related
    parser.add_argument('--save-durations', default=False, type=strtobool,
                        help='Whether to save durations converted from attentions')
    parser.add_argument('--save-focus-rates', default=False, type=strtobool,
                        help='Whether to save focus rates of attentions')
    return parser