source: sasmodels/sasmodels/list_pars.py @ 72be531

core_shell_microgelscostrafo411magnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 72be531 was 72be531, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

use argparse for list_pars parameter listing

  • Property mode set to 100644
File size: 1.9 KB
Line 
1"""
2List all parameters used along with the models which use them.
3
4Usage:
5
6    python -m sasmodels.list_pars [-v]
7
8If '-v' is given, then list the models containing the parameter in
9addition to just the parameter name.
10"""
11from __future__ import print_function
12
13import sys
14import argparse
15
16from .core import load_model_info, list_models
17from .compare import columnize
18
19def find_pars(type=None):
20    """
21    Find all parameters in all models.
22
23    Returns the reference table *{parameter: [model, model, ...]}*
24    """
25    partable = {}
26    for name in list_models():
27        model_info = load_model_info(name)
28        for p in model_info.parameters.kernel_parameters:
29            if type is None or p.type == type:
30                partable.setdefault(p.name, [])
31                partable[p.name].append(name)
32    return partable
33
34def list_pars(names_only=True, type=None):
35    """
36    Print all parameters in all models.
37
38    If *names_only* then only print the parameter name, not the models it
39    occurs in.
40    """
41    partable = find_pars(type)
42    if names_only:
43        print(columnize(list(sorted(partable.keys()))))
44    else:
45        for k, v in sorted(partable.items()):
46            print("%s: %s"%(k, ", ".join(v)))
47
48def main():
49    """
50    Program to list the parameters used across all models.
51    """
52    parser = argparse.ArgumentParser(
53        description="Find all parameters in all models",
54        )
55    parser.add_argument(
56        '-v', '--verbose',
57        action='store_true',
58        help="list models which use this argument")
59    parser.add_argument(
60        'type', default="any", nargs='?',
61        metavar="volume|orientation|sld|none|any",
62        choices=['volume', 'orientation', 'sld', None, 'any'],
63        type=lambda v: None if v == 'any' else '' if v == 'none' else v,
64        help="only list arguments of the given type")
65    args = parser.parse_args()
66
67    list_pars(names_only=not args.verbose, type=args.type)
68
69if __name__ == "__main__":
70    main()
Note: See TracBrowser for help on using the repository browser.