Beispiel #1
0
def parse_arguments(args_to_parse):
    """
    Parse the command line arguments.

    Params
    ------
    args_to_parse : list of str
            Arguments to parse (split on whitespaces).
    """
    description = "AstraZeneca Coding Exercise"
    default_config = get_config_section([CONFIG_FILE], "Preset")
    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=FormatterNoDuplicate)

    # API options
    api = parser.add_argument_group('API specific options')
    api.add_argument('-u',
                     '--base-url',
                     help="Base url for API query",
                     default=default_config['base_url'])
    api.add_argument('-m',
                     '--manufacturer',
                     help='Drug manufacturer',
                     default=default_config['manufacturer'])

    # Plotting options
    plots = parser.add_argument_group('Plot specific options')
    plots.add_argument('-p',
                       '--plot-type',
                       default=default_config['plot_type'],
                       choices=PLOTS,
                       help='Type of plot to produce')
    plots.add_argument('-s',
                       '--save-fig',
                       help='Whether to save the figure',
                       type=bool,
                       default=default_config['save_fig'])

    # Analysis options
    analysis_options = parser.add_argument_group('Anaylsis options')
    analysis_options.add_argument('-a',
                                  '--analysis-type',
                                  type=str,
                                  choices=ANALYSIS_TYPE,
                                  default=default_config['analysis_type'],
                                  help="The type of analysis to undertake")

    args = parser.parse_args(args_to_parse)

    return args
Beispiel #2
0
def parse_arguments(args_to_parse):
    """Parse the command line arguments.

    Parameters
    ----------
    args_to_parse: list of str
        Arguments to parse (splitted on whitespaces).
    """
    default_config = get_config_section([CONFIG_FILE], "Custom")

    description = "PyTorch implementation and evaluation of disentangled Variational AutoEncoders and metrics."
    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=FormatterNoDuplicate)

    # General options
    general = parser.add_argument_group('General options')
    general.add_argument(
        'name',
        type=str,
        help="Name of the model for storing and loading purposes.")
    general.add_argument('-L',
                         '--log-level',
                         help="Logging levels.",
                         default=default_config['log_level'],
                         choices=LOG_LEVELS)
    general.add_argument('--no-progress-bar',
                         action='store_true',
                         default=default_config['no_progress_bar'],
                         help='Disables progress bar.')
    general.add_argument('--no-cuda',
                         action='store_true',
                         default=default_config['no_cuda'],
                         help='Disables CUDA training, even when have one.')
    general.add_argument(
        '-s',
        '--seed',
        type=int,
        default=default_config['seed'],
        help='Random seed. Can be `None` for stochastic behavior.')

    # Learning options
    training = parser.add_argument_group('Training specific options')
    training.add_argument(
        '--checkpoint-every',
        type=int,
        default=default_config['checkpoint_every'],
        help='Save a checkpoint of the trained model every n epoch.')
    training.add_argument('-d',
                          '--dataset',
                          help="Path to training data.",
                          default=default_config['dataset'],
                          choices=DATASETS)
    training.add_argument(
        '-x',
        '--experiment',
        default=default_config['experiment'],
        choices=EXPERIMENTS,
        help=
        'Predefined experiments to run. If not `custom` this will overwrite some other arguments.'
    )
    training.add_argument('-e',
                          '--epochs',
                          type=int,
                          default=default_config['epochs'],
                          help='Maximum number of epochs to run for.')
    training.add_argument('-b',
                          '--batch-size',
                          type=int,
                          default=default_config['batch_size'],
                          help='Batch size for training.')
    training.add_argument('--lr',
                          type=float,
                          default=default_config['lr'],
                          help='Learning rate.')

    # Model Options
    model = parser.add_argument_group('Model specfic options')
    model.add_argument('-m',
                       '--model-type',
                       default=default_config['model'],
                       choices=MODELS,
                       help='Type of encoder and decoder to use.')
    model.add_argument('-z',
                       '--latent-dim',
                       type=int,
                       default=default_config['latent_dim'],
                       help='Dimension of the latent variable.')
    model.add_argument('-l',
                       '--loss',
                       default=default_config['loss'],
                       choices=LOSSES,
                       help="Type of VAE loss function to use.")
    model.add_argument('-r',
                       '--rec-dist',
                       default=default_config['rec_dist'],
                       choices=RECON_DIST,
                       help="Form of the likelihood ot use for each pixel.")
    model.add_argument(
        '-a',
        '--reg-anneal',
        type=float,
        default=default_config['reg_anneal'],
        help=
        "Number of annealing steps where gradually adding the regularisation. What is annealed is specific to each loss."
    )

    # Loss Specific Options
    betaH = parser.add_argument_group('BetaH specific parameters')
    betaH.add_argument('--betaH-B',
                       type=float,
                       default=default_config['betaH_B'],
                       help="Weight of the KL (beta in the paper).")

    betaB = parser.add_argument_group('BetaB specific parameters')
    betaB.add_argument('--betaB-initC',
                       type=float,
                       default=default_config['betaB_initC'],
                       help="Starting annealed capacity.")
    betaB.add_argument('--betaB-finC',
                       type=float,
                       default=default_config['betaB_finC'],
                       help="Final annealed capacity.")
    betaB.add_argument(
        '--betaB-G',
        type=float,
        default=default_config['betaB_G'],
        help="Weight of the KL divergence term (gamma in the paper).")

    factor = parser.add_argument_group('factor VAE specific parameters')
    factor.add_argument('--factor-G',
                        type=float,
                        default=default_config['factor_G'],
                        help="Weight of the TC term (gamma in the paper).")
    factor.add_argument('--lr-disc',
                        type=float,
                        default=default_config['lr_disc'],
                        help='Learning rate of the discriminator.')

    btcvae = parser.add_argument_group('beta-tcvae specific parameters')
    btcvae.add_argument('--btcvae-A',
                        type=float,
                        default=default_config['btcvae_A'],
                        help="Weight of the MI term (alpha in the paper).")
    btcvae.add_argument(
        '--btcvae-G',
        type=float,
        default=default_config['btcvae_G'],
        help="Weight of the dim-wise KL term (gamma in the paper).")
    btcvae.add_argument('--btcvae-B',
                        type=float,
                        default=default_config['btcvae_B'],
                        help="Weight of the TC term (beta in the paper).")

    # Learning options
    evaluation = parser.add_argument_group('Evaluation specific options')
    evaluation.add_argument(
        '--is-eval-only',
        action='store_true',
        default=default_config['is_eval_only'],
        help='Whether to only evaluate using precomputed model `name`.')
    evaluation.add_argument(
        '--is-metrics',
        action='store_true',
        default=default_config['is_metrics'],
        help=
        "Whether to compute the disentangled metrcics. Currently only possible with `dsprites` as it is the only dataset with known true factors of variations."
    )
    evaluation.add_argument('--no-test',
                            action='store_true',
                            default=default_config['no_test'],
                            help="Whether not to compute the test losses.`")
    evaluation.add_argument('--eval-batchsize',
                            type=int,
                            default=default_config['eval_batchsize'],
                            help='Batch size for evaluation.')

    args = parser.parse_args(args_to_parse)
    if args.experiment != 'custom':
        if args.experiment not in ADDITIONAL_EXP:
            # update all common sections first
            model, dataset = args.experiment.split("_")
            common_data = get_config_section([CONFIG_FILE],
                                             "Common_{}".format(dataset))
            update_namespace_(args, common_data)
            common_model = get_config_section([CONFIG_FILE],
                                              "Common_{}".format(model))
            update_namespace_(args, common_model)

        try:
            experiments_config = get_config_section([CONFIG_FILE],
                                                    args.experiment)
            update_namespace_(args, experiments_config)
        except KeyError as e:
            if args.experiment in ADDITIONAL_EXP:
                raise e  # only reraise if didn't use common section

    return args