[3330bb4] | 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 sys |
---|
[72be531] | 14 | import argparse |
---|
[3330bb4] | 15 | |
---|
| 16 | from .core import load_model_info, list_models |
---|
| 17 | from .compare import columnize |
---|
| 18 | |
---|
[72be531] | 19 | def find_pars(type=None): |
---|
[3330bb4] | 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: |
---|
[72be531] | 29 | if type is None or p.type == type: |
---|
| 30 | partable.setdefault(p.name, []) |
---|
| 31 | partable[p.name].append(name) |
---|
[3330bb4] | 32 | return partable |
---|
| 33 | |
---|
[72be531] | 34 | def list_pars(names_only=True, type=None): |
---|
[3330bb4] | 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 | """ |
---|
[72be531] | 41 | partable = find_pars(type) |
---|
[3330bb4] | 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 | |
---|
| 48 | def main(): |
---|
| 49 | """ |
---|
| 50 | Program to list the parameters used across all models. |
---|
| 51 | """ |
---|
[72be531] | 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) |
---|
[3330bb4] | 68 | |
---|
| 69 | if __name__ == "__main__": |
---|
| 70 | main() |
---|