|
16 | 16 |
|
17 | 17 | import numpy as np # type: ignore |
18 | 18 |
|
| 19 | +try: |
| 20 | + # Optional import. This allows the doc builder and tests to run even |
| 21 | + # when bumps is not on the path. |
| 22 | + from bumps.parameter import Parameter as BumpsParameter # type: ignore |
| 23 | + from bumps.parameter import Reference |
| 24 | +except ImportError: |
| 25 | + class BumpsParameter: |
| 26 | + """See parameter.Parameter in the bumps documentation.""" |
| 27 | + class Reference: |
| 28 | + """See parameter.Reference in the bumps documentation.""" |
| 29 | + |
19 | 30 | from .data import plot_theory |
20 | 31 | from .direct_model import DataMixin |
21 | 32 |
|
@@ -61,22 +72,34 @@ def create_parameters(model_info, **kwargs): |
61 | 72 | parameters for each model parameter, and a dictionary of |
62 | 73 | *{name: str}* containing the polydispersity distribution types. |
63 | 74 | """ |
64 | | - pars = {} # type: dict[str, BumpsParameter] |
65 | | - pd_types = {} # type: dict[str, str] |
| 75 | + def addpar(name: str, default: float, limits: tuple[float]) -> None: |
| 76 | + value = kwargs.pop(name, default) |
| 77 | + pars[name] = BumpsParameter.default(value, name=name, limits=limits) |
| 78 | + |
| 79 | + pars: dict[str, BumpsParameter] = {} |
| 80 | + pd_types: dict[str, str] = {} |
66 | 81 | for p in model_info.parameters.call_parameters: |
67 | | - value = kwargs.pop(p.name, p.default) |
68 | | - pars[p.name] = BumpsParameter.default(value, name=p.name, limits=p.limits) |
| 82 | + # To check the limits on parameters in sasmodels, the following |
| 83 | + # regex captures most (all?) parameter definition lines: |
| 84 | + # grep "^\(parameters *= *\[\)\? *\[['\"]" sasmodels/models/*.py |
| 85 | + # Skip those which have inf, 360 or 180 in the definition: |
| 86 | + # ... | grep -v inf | grep -v 360 | grep -v 180 |
| 87 | + # Looking at the remaining values they all represent hard limits on |
| 88 | + # the model parameter that should not be exceeded by the fitter. |
| 89 | + |
| 90 | + # Bumps respects hard limits, so make sure they are big enough. We probably don't |
| 91 | + # need to do this since the values in the models should be set right. |
| 92 | + # angular = (p.type == "orientation") or (p.type == "magnetic" and p.units == "degrees") |
| 93 | + # limits = p.limits if not angular else (-180, 180) if "theta" in p.name else (-360, 360) |
| 94 | + addpar(p.name, p.default, p.limits) |
69 | 95 | if p.polydisperse: |
70 | | - pd_limits = (-360.0, 360.0) if p.type == "orientation" else (0., 1.) |
71 | | - for part, default, limits in [ |
72 | | - ('_pd', 0., pd_limits), |
73 | | - ('_pd_n', 35., (0, 1000)), |
74 | | - ('_pd_nsigma', 3., (0, 10)), |
75 | | - ]: |
76 | | - name = p.name + part |
77 | | - value = kwargs.pop(name, default) |
78 | | - pars[name] = BumpsParameter.default(value, name=name, limits=limits) |
| 96 | + addpar(f"{p.name}_pd", 0., limits=(0.0, np.inf)) |
| 97 | + addpar(f"{p.name}_pd_n", 35, limits=(0, 1000)) |
| 98 | + addpar(f"{p.name}_pd_nsigma", 3., limits=(0.0, 10.0)) |
79 | 99 | name = p.name + '_pd_type' |
| 100 | + # If angular we should be defaulting to a cyclic gaussian, but this is not |
| 101 | + # yet in sasmodels. There is an implementation in the example/weights directory. |
| 102 | + # When we do add it be sure to update orientation.rst describing how to use it. |
80 | 103 | pd_types[name] = str(kwargs.pop(name, 'gaussian')) |
81 | 104 |
|
82 | 105 | if kwargs: # args not corresponding to parameters |
|
0 commit comments