|
4 | 4 |
|
5 | 5 | import numpy as np |
6 | 6 |
|
7 | | -from ..modeling.core import Fittable1DModel, Fittable2DModel |
8 | | - |
| 7 | +from ..modeling.core import (Fittable1DModel, Fittable2DModel, |
| 8 | + custom_model, _ModelMeta) |
9 | 9 |
|
10 | 10 | __all__ = ['discretize_model'] |
11 | 11 |
|
@@ -74,12 +74,15 @@ def add_kernel_arrays_2D(array_1, array_2): |
74 | 74 |
|
75 | 75 | def discretize_model(model, x_range, y_range=None, mode='center', factor=10): |
76 | 76 | """ |
77 | | - Function to evaluate analytical models on a grid. |
| 77 | + Function to evaluate analytical model functions on a grid. |
| 78 | +
|
| 79 | + So far the function can only deal with pixel coordinates. |
78 | 80 |
|
79 | 81 | Parameters |
80 | 82 | ---------- |
81 | | - model : `~astropy.modeling.FittableModel` |
82 | | - Model to be evaluated. |
| 83 | + model : `~astropy.modeling.FittableModel` or callable |
| 84 | + Model function to be evaluated. Can also be a callable |
| 85 | + function. See `astropy.modeling.custom_model` for details. |
83 | 86 | x_range : tuple |
84 | 87 | x range in which the model is evaluated. |
85 | 88 | y_range : tuple, optional |
@@ -135,27 +138,37 @@ def discretize_model(model, x_range, y_range=None, mode='center', factor=10): |
135 | 138 |
|
136 | 139 |
|
137 | 140 | """ |
138 | | - if isinstance(model, Fittable2DModel) and y_range is None: |
139 | | - raise Exception("Please specify y range.") |
| 141 | + if not callable(model): |
| 142 | + raise Exception('Model must be callable.') |
| 143 | + if not isinstance(model, (Fittable1DModel, Fittable2DModel, _ModelMeta)): |
| 144 | + model = custom_model(model) |
| 145 | + ndim = model.n_inputs |
| 146 | + if ndim > 2: |
| 147 | + raise Exception('discretize_model only supports 1-d and 2-d models.') |
| 148 | + |
| 149 | + if ndim == 2 and y_range is None: |
| 150 | + raise Exception("y range not specified, but model is 2-d") |
| 151 | + if ndim == 1 and y_range is not None: |
| 152 | + raise Exception("y range specified, but model is only 1-d.") |
140 | 153 | if mode == "center": |
141 | | - if isinstance(model, Fittable1DModel): |
| 154 | + if ndim == 1: |
142 | 155 | return discretize_center_1D(model, x_range) |
143 | | - if isinstance(model, Fittable2DModel): |
| 156 | + elif ndim == 2: |
144 | 157 | return discretize_center_2D(model, x_range, y_range) |
145 | 158 | elif mode == "linear_interp": |
146 | | - if isinstance(model, Fittable1DModel): |
| 159 | + if ndim == 1: |
147 | 160 | return discretize_linear_1D(model, x_range) |
148 | | - if isinstance(model, Fittable2DModel): |
| 161 | + if ndim == 2: |
149 | 162 | return discretize_bilinear_2D(model, x_range, y_range) |
150 | 163 | elif mode == "oversample": |
151 | | - if isinstance(model, Fittable1DModel): |
| 164 | + if ndim == 1: |
152 | 165 | return discretize_oversample_1D(model, x_range, factor) |
153 | | - if isinstance(model, Fittable2DModel): |
| 166 | + if ndim == 2: |
154 | 167 | return discretize_oversample_2D(model, x_range, y_range, factor) |
155 | 168 | elif mode == "integrate": |
156 | | - if isinstance(model, Fittable1DModel): |
| 169 | + if ndim == 1: |
157 | 170 | return discretize_integrate_1D(model, x_range) |
158 | | - if isinstance(model, Fittable2DModel): |
| 171 | + if ndim == 2: |
159 | 172 | return discretize_integrate_2D(model, x_range, y_range) |
160 | 173 | else: |
161 | 174 | raise DiscretizationError('Invalid mode.') |
|
0 commit comments