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