Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Separate config in the same CLI #32

Closed
mpariente opened this issue Dec 12, 2020 · 4 comments
Closed

Feature request: Separate config in the same CLI #32

mpariente opened this issue Dec 12, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@mpariente
Copy link

I forgot to mention in the other issues, but I've been looking at the source code a bit and playing with it and it seems great, so thanks! Making it similar to argparse was a great choice and will definitely weight in a lot for our adoption of jsonargparse.

One of the current possible way is to use --print_config, overwrite what we want and use --config to run the CLI.

In Asteroid (an audio source separation library), we support several datasets and several architectures. If we'd like to run the exact same architecture with the same trainer on all the datasets, could we think about composing config files so that a sub config file for an architecture could be reusable in several places?
Is this already possible? Maybe I didn't dig enough.

You might want to have a look at this PR in Asteroid which describes a bit what we'd use it for.

Example

It might look like that, where the --config is still necessary because the CLI would dump experiment_config.yml somewhere for reproducibility, and we could re-instantiate the run directly using --config.

import jsonargparse

class Model:
    def __init__(self, n: int):
        pass

class Data:
    def __init__(self, n: int = 8):
        pass

def get_args():
    parser = jsonargparse.ArgumentParser(parse_as_dict=True, description="Trial")
    parser.add_argument(
        "--config", action=jsonargparse.ActionConfigFile, help="Configuration file"
    )
    parser.add_argument(
        "--data.config", action=jsonargparse.ActionConfigFile, help="Configuration file"
    )
    parser.add_argument(
        "--model.config", action=jsonargparse.ActionConfigFile, help="Configuration file"
    )
    parser.add_class_arguments(Model, "model")
    parser.add_class_arguments(Data, "data")
    args = parser.parse_args(with_meta=False)
    return args

def main():
    args = get_args()
    print(args)

if __name__ == "__main__":
    main()

This is probably the last issue I'll raise tonight 😉

@mauvilsa
Copy link
Member

There is already this kind of support for some actions. namely: ActionJsonSchema, ActionJsonnet and ActionParser. I think it might already work by making data and model an ActionParser.

parser = jsonargparse.ArgumentParser()
parser_model = jsonargparse.ArgumentParser()
parser_data = jsonargparse.ArgumentParser()
parser_model.add_class_arguments(Model)
parser_data.add_class_arguments(Data)
parser.add_argument('--model`, action=ActionParser(parser=parser_model))
parser.add_argument('--data`, action=ActionParser(parser=parser_data))

There is no need to have an explicit config key for each. That is, from the command line it can be given as --data=data_config.yaml --model=model_config.yaml or --data.n=1 --model.n=2. Also adding ActionConfigFile in a config file could be as:

data: data_config.yaml
model: model_config.yaml

Then again, it wouldn't be a bad idea to add the support of sub config files for nested keys created by add_class_arguments avoiding the need to use ActionParser. This way it would work like in your example, but removing --data.config and --model.config since that would be unnecessary.

@mpariente
Copy link
Author

I'll have a look at the things you point to, thanks for your answer.

@mauvilsa mauvilsa added the enhancement New feature or request label Dec 15, 2020
mauvilsa added a commit that referenced this issue Dec 29, 2020
…d in signatures and jsonschema.

- Signature arguments can now be loaded from independent config files #32.
- parse_args now has a _skip_check option like the other parse methods.
- Added explicit 'from ex' in some reraises.
@mauvilsa
Copy link
Member

Then again, it wouldn't be a bad idea to add the support of sub config files for nested keys created by add_class_arguments avoiding the need to use ActionParser. This way it would work like in your example, but removing --data.config and --model.config since that would be unnecessary.

In master this is now implemented. By using parser.add_class_arguments(Model, "model") the parameters of model can be given in a subconfig file or with a command line argument as --model=model_config.yaml. See the unit tests signatures_tests.py#L427-L472.

@mauvilsa
Copy link
Member

This is now in release v3.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants