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, dict([ ('a', '3'), ('list_arg', [1, 2, 3]), ]))
def info(self, source=None, *, njobs=1): if source is None: source = self.index files = source.iterdir() else: source = self.marked / source files = source.glob("**/*/" + parser.PARAMS_FILE) yaml = configargparse.YAMLConfigFileParser() def get_dict(cfg): return yaml.parse(cfg.open("r")) def convert_column(col): if any(isinstance(v, str) for v in converter.convert_series(col)): return col else: return pd.Series(converter.convert_series(col), name=col.name, index=col.index) records = joblib.Parallel(n_jobs=njobs)( (joblib.delayed(get_dict)(c) for c in files)) try: df = (pd.DataFrame.from_records(records).apply( lambda s: convert_column(s)).sort_values("id").assign( root=lambda _: _.root.apply(self.root.__truediv__)). reset_index(drop=True)) cols = df.columns.tolist() cols.insert(0, cols.pop(cols.index("id"))) return df.reindex(columns=cols) except FileNotFoundError as e: raise KeyError(source.name) from e
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))
def testYAMLConfigFileParser_Basic(self): try: import yaml except: logging.warning("WARNING: PyYAML not installed. " "Couldn't test YAMLConfigFileParser") return p = configargparse.YAMLConfigFileParser() self.assertTrue(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, dict([('a', '3')]))
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)
def get_dict(cfg): return configargparse.YAMLConfigFileParser().parse(cfg.open('r'))
def get_parser_common(parser: configargparse.ArgumentParser, config_name: str): config_path = here / config_name assert config_path.exists(), "Config do not exist!" parser._config_file_parser = configargparse.YAMLConfigFileParser() parser._default_config_files = [str(config_path)] parser.add("--config", is_config_file=True, help="config file path") parser.add("--test", action="store_true", help="test mode") parser.add("--length", type=float, help="board length") parser.add("--length_unit", type=float, help="unit length") parser.add( "--bcs", type=yaml.safe_load, action="append", help="Dirichlet boundaries", ) parser.add("--data_dir", type=str, help="dir to store generated layout data") parser.add("--fem_degree", type=int, help="fem degree in fenics") parser.add("--u_D", type=int, help="value on Dirichlet boundary") parser.add("--nx", type=int, help="number of grid in x direction") # parser.add('--ny', type=int, help='number of grid in y direction') # parser.add('--nz', type=int, help='number of grid in z direction') parser.add("--sample_n", type=int, help="number of samples") parser.add( "--seed", type=int, default=np.random.randint(2**32), help="seed in np.random module", ) parser.add("--file_format", type=str, choices=["mat"], help="dataset file format") parser.add("--prefix", type=str, help="prefix of file") parser.add( "--method", type=str, choices=["fenics"], help="method to solve the equation", ) parser.add("--worker", type=int, default=os.cpu_count(), help="number of workers") parser.add("--ndim", type=int, choices=[2, 3], help="dimension") parser.add("--vtk", action="store_true", default=False, help="output vtk file") parser.add( "-V", "--version", action="version", version=f"layout-generator version: {__version__}", ) parser.add("--task", help="task", choices=["discrete", "continuous"], type=str) return parser
'mask': mask_proj_crop, 'image': rgb_cropped, 'transient': spad_single_relevant, } np.save(output_dir / f'{scene}', out) # Generate config file to evaluate this example: config = { 'scene': scene, 'min-depth': float(min_depth), 'max-depth': float(max_depth), 'full-height': int(rgb_cropped.shape[0]), 'full-width': int(rgb_cropped.shape[1]), 'refl-est': 'red', 'crop': [0, 0, 0, 0], 'radial': True, 'source-n-sid-bins': 600, 'source-alpha': float(min_depth), 'source-beta': float(max_depth), 'offset': 0., } config_writer = configargparse.YAMLConfigFileParser() serialized = config_writer.serialize(config) # set_trace() with open(output_dir / f'{scene}.yml', 'w') as f: f.write(serialized) # set_trace() # DEBUG # break