Last active
December 26, 2021 09:57
-
-
Save avirambh/0b8f69e66485e761ed2d6423d74f330f to your computer and use it in GitHub Desktop.
override argparse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def override_args_from_yaml(args): | |
| """ | |
| :param args: | |
| :param yaml_settings: | |
| :return: | |
| """ | |
| if args.config_file: | |
| with open(args.config_file, 'r') as ymlfile: | |
| yaml_settings = yaml.load(ymlfile) | |
| # Go over yaml settings and add to args | |
| for k, v in yaml_settings.items(): | |
| if v is not None: | |
| if type(v) is str: | |
| exec('args.{}="{}"'.format(k, v)) | |
| else: | |
| exec('args.{}={}'.format(k, v)) | |
| return args | |
| def override_args_from_yaml_with_yaml_save(args, yaml_conf=None): | |
| """ | |
| if yaml_conf is given, use yaml_conf instead of args.config_file | |
| :param args: | |
| :param yaml_conf: | |
| :return: | |
| """ | |
| config_file = yaml_conf if yaml_conf else args.config_file | |
| cropper_config_file = args.cropper_config_file | |
| if config_file: | |
| if not os.path.exists(config_file): | |
| config_file = os.path.join(args.exp_name, config_file) | |
| with open(config_file, 'r') as ymlfile: | |
| yaml_settings = yaml.load(ymlfile) | |
| # Go over yaml settings and add to args | |
| for k, v in yaml_settings.items(): | |
| if v is not None: | |
| if type(v) is str: | |
| exec('args.{}="{}"'.format(k, v)) | |
| else: | |
| exec('args.{}={}'.format(k, v)) | |
| if(cropper_config_file): | |
| if not os.path.exists(cropper_config_file): | |
| cropper_config_file = os.path.join(args.exp_name, cropper_config_file) | |
| with open(cropper_config_file, 'r') as cropper_ymlfile: | |
| cropper_yaml_settings = yaml.load(cropper_ymlfile) | |
| for k, v in cropper_yaml_settings.items(): | |
| if v is not None: | |
| if type(v) is str: | |
| exec('args.{}="{}"'.format(k, v)) | |
| else: | |
| exec('args.{}={}'.format(k, v)) | |
| if args.save: | |
| config_filename = os.path.split(config_file)[-1] | |
| exp_dir = os.path.join('experiments', args.exp_name) | |
| yaml_path = os.path.join(exp_dir, config_filename) | |
| if not os.path.isdir(os.path.dirname(exp_dir)): | |
| os.mkdir(os.path.dirname(exp_dir)) | |
| if not os.path.isdir(exp_dir): | |
| os.mkdir(exp_dir) | |
| with open(yaml_path, 'w') as ymlfile: | |
| yaml.dump(args.__dict__, ymlfile) | |
| return args |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment