[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 | |
---|
[72be531] | 13 | import argparse |
---|
[3330bb4] | 14 | |
---|
| 15 | from .core import load_model_info, list_models |
---|
| 16 | from .compare import columnize |
---|
| 17 | |
---|
[72be531] | 18 | def find_pars(type=None): |
---|
[3330bb4] | 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: |
---|
[72be531] | 28 | if type is None or p.type == type: |
---|
| 29 | partable.setdefault(p.name, []) |
---|
| 30 | partable[p.name].append(name) |
---|
[3330bb4] | 31 | return partable |
---|
| 32 | |
---|
[72be531] | 33 | def list_pars(names_only=True, type=None): |
---|
[3330bb4] | 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 | """ |
---|
[72be531] | 40 | partable = find_pars(type) |
---|
[3330bb4] | 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 | """ |
---|
[72be531] | 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 | 'type', 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 '' if v == 'none' else v, |
---|
| 63 | help="only list arguments of the given type") |
---|
| 64 | args = parser.parse_args() |
---|
| 65 | |
---|
| 66 | list_pars(names_only=not args.verbose, type=args.type) |
---|
[3330bb4] | 67 | |
---|
| 68 | if __name__ == "__main__": |
---|
| 69 | main() |
---|