0

Currently I am using a similar code to the following to create arguments that are used in my code. The problem with that is different functions only demand specific arguments not all of them. For example, when data is handled only --dataname and --datapath are used. Unfortunately, I have to pass all arguments as args to every function and use only few of them. This makes the maintenance very difficult.

Questions:

  • Is there any way for me to separate them out as Architecture, Dataset, Initialization, and Optimization's arguments.
  • If so, how can I retrieve the ones that are asscociated with a different group.
  • If so, is there still a way to use all of them at once?
import argparse
parser = argparse.ArgumentParser(
                    description='Comprehensive image classification')
# ===== Architecture ===== #
parser.add_argument('--arch', '-a', metavar='ARCH', default='resnet50')
parser.add_argument('--pretrained', dest='pretrained', action='store_true',
                    help='use pre-trained model')
# ===== Dataset ===== #   
parser.add_argument('--dataname', default='imagenet', type=str,
                        help='mnist | cifar10 | cifar100| fashionmnist | imagenet')                
parser.add_argument('--datapath', default = '/datasets/imagenet/', 
                    metavar='DIR', help='path to dataset')
# ===== Initialization ===== #
parser.add_argument('--init-policy', default='kaimingn',
                    type=str, help='kaimingn | kaimingu | xaviern | xavieru')
parser.add_argument('--init-kaiming-mode', default='fan_in', type=str, help='fan_in | fan_out')
# ===== Optimization ======== #
parser.add_argument('--optimizer', '-o', default='SGD+M', type=str, help='SGD | SGD+M | Adam')
parser.add_argument('--epochs', default=90, type=int, metavar='N',
                    help='number of total epochs to run')
args = parser.parse_args()
Saeed
  • 598
  • 10
  • 19
  • Something with subparsers like in [Conditional command line arguments in Python using argparse](https://stackoverflow.com/q/9505898/15497888)? – Henry Ecker Oct 03 '21 at 01:31
  • There's no builtin trick in `argparse`. But in at least one previous SO, people have suggested tricks that might help. For example, set the `dest` as you do in one case, but to a string that indicates the group. Then after parsing rebuild the `args` namespace using those clues. Working with the `vars` dict might be easier. Or during setup create lists of `dest` groups. People have even suggested a custom `Namespace` class. – hpaulj Oct 03 '21 at 02:42
  • @Henry Ecker: I do not need conditional arguments, what I want is to partition all arguments that are in `args` so that I can only use a specific partition later not not all of them. – Saeed Oct 03 '21 at 04:05
  • @hpaulj: If I use `dest='arch'` for the first two ones, `dest='data'`, `dest='init'`, and `dest='optim'` respectively for each group. Would that work? Then given `args` how I can get `args` those that are associated with let us say `optim`? – Saeed Oct 03 '21 at 04:20
  • @@hpaulj: the problem is that when I use `args.data` I would get only one arguments not all of them. – Saeed Oct 03 '21 at 04:58
  • @hpaul: After reading a couple of articles about `argparse`, I think I should use `subparser` and `parent parser`, am I right? If so, would you help me to figure this out? – Saeed Oct 03 '21 at 05:31
  • The `parents` mechanism is just a convenience for adding the same argument to several subparsers. it doesn't add any functionality; it just saves a bit of typing. – hpaulj Oct 03 '21 at 06:30
  • @hpaulj: so it won't help. As I explained above `dest` does not work either. What would you suggest? – Saeed Oct 03 '21 at 06:38
  • I'm not suggesting the same `dest`, just something related. e.g. 'arch-arch', 'arch-pre', 'data-name', 'data-path', etc. – hpaulj Oct 03 '21 at 07:08
  • @hpaulj: I see. Then, how would you retrieve all arguments that are associated with `arch` at once? Let us say you have `arch-1`, `arch-2`, `arch-3`, `arch-4`, and `arch-5`, how would you get them from `args` and then pass them to a function of interest? – Saeed Oct 03 '21 at 07:17
  • I'm not suggesting anything magical. With the `vars` dict you can search the keys for ones that start with 'arch'. Just ordinary python dict, string and list processing. – hpaulj Oct 03 '21 at 07:22
  • @hpaulj: that's good. I am asking this question since I am not familiar with `args` in detail. Then, using `vars` can I create `args_arch` including all the ones associated with `arch` that has the same type as `args` has (`Namespace `) and use it throughout my code? – Saeed Oct 03 '21 at 07:39
  • `args` is a `argparse.Namespace` object. That's a simple class definition, I recommend adding a `print(args)` statement when debugging to see exactly what it contains. `vars(args)` creates a `dict` that may be easier to understand and use. – hpaulj Oct 03 '21 at 15:28

0 Answers0