torchmd-net icon indicating copy to clipboard operation
torchmd-net copied to clipboard

Argument parsing depends on input argument order

Open RaulPPelaez opened this issue 9 months ago • 0 comments

The argument parsing mechanism assumes that arguments (CLI or in the yaml file) will be processed in always the same order, but Argparse offers no such guarantee.

    parser.add_argument('--load-model', action=LoadFromCheckpoint, help='Restart training using a model checkpoint')  # keep first
    parser.add_argument('--conf', '-c', type=open, action=LoadFromFile, help='Configuration yaml file')  # keep second
    parser.add_argument('--num-epochs', default=300, type=int, help='number of epochs')

This assumes --load-model will always be handled before the rest, but it seems like the actual order is the one given in the CLI. Argparse offers no way to change this behavior, so instead we could make these options store a string and then manually process them in the order we need, so something like:

    parser.add_argument('--load-model', type=str, help='Restart training using a model checkpoint')  # keep first
    parser.add_argument('--conf', '-c', type=str, help='Configuration yaml file')  # keep second
    parser.add_argument('--num-epochs', default=300, type=int, help='number of epochs')
...
   input_args = parser.parse_args()
   args = {}
   if input_args.load_model:
       args = LoadFromCheckpoint(input_args.load_model)
       del input_args.load_model
   if input_args.conf:
      args.update(LoadFromFile(input_args.conf))
      del input_args.conf
   for k,v in input_args.dict():
       args[k] = v

RaulPPelaez avatar May 23 '24 10:05 RaulPPelaez