make_model_image#
- photutils.datasets.make_model_image(shape, model, params_table, *, model_shape=None, bbox_factor=None, x_name='x_0', y_name='y_0', params_map=None, discretize_method='center', discretize_oversample=10, progress_bar=False)[source]#
Make a 2D image containing sources generated from a user-specified astropy 2D model.
The model parameters for each source are taken from the input
params_tabletable. By default, the table is searched for column names that match model parameter names and the values specified byx_nameandy_name. However, the user can specify a different mapping between model parameter names and column names using theparams_mapkeyword.- Parameters:
- shape2-tuple of int or int
The shape of the output image. If a single integer is provided, it will be used for both dimensions.
- model2D
astropy.modeling.Model The 2D model to be used to render the sources. The model must be two-dimensional, meaning that it accepts 2 inputs (i.e., (x, y)) and has 1 output. The model must have parameters for the x and y positions of the sources. Typically, these parameters are named ‘x_0’ and ‘y_0’, but the parameter names can be specified using the
x_nameandy_namekeywords.- params_table
Table A table containing the model parameters for each source. Each row of the table corresponds to a source whose model parameters are defined by the column names, which must match the model parameter names. The table must contain columns for the x and y positions of the sources. The column names for the x and y positions can be specified using the
x_nameandy_namekeywords. Model parameters not defined in the table orparams_mapkeep the values of the inputmodel. To attach units to model parameters,params_tablemust be input as aQTable. Rows that contain any nonfinite model parameters or a nonfinite'local_bkg'value will be skipped.If the table contains a column named
'model_shape', then the values in that column will be used to override themodel_shapekeyword and modelbounding_boxfor each source. This can be used to render each source with a different shape.If the table contains a column named
'local_bkg', then the per-pixel local background values in that column will be added to each model source over the region defined by itsmodel_shape. The'local_bkg'column must have the same flux units as the output image (e.g., if the inputmodelhas ‘amplitude’ or ‘flux’ parameters with units). Including'local_bkg'should be used with care, especially in crowded fields where themodel_shapeof sources overlap (see Notes below).Except for the
'model_shape'and'local_bkg'columns, columns whose names do not match a model parameter name are ignored unless they are mapped to a model parameter byparams_map.- model_shape2-tuple of int, int, or
None, optional The shape around the (x, y) center of each source that will be used to evaluate the
model. Ifmodel_shapeis a scalar integer, then a square shape of sizemodel_shapewill be used. IfNone, then the bounding box of the model will be used (which can optionally be scaled using thebbox_factorkeyword if the model supports it). If the model does not have abounding_boxattribute, then either this keyword or a'model_shape'column inparams_tablemust be specified. If specified, this keyword overrides the modelbounding_boxattribute. To use a different shape for each source, include a column named'model_shape'in theparams_table. For that case, this keyword is ignored.- bbox_factor
Noneor float, optional The multiplicative factor to pass to the model
bounding_boxmethod to determine the model shape. If the modelbounding_boxmethod does not accept afactorkeyword, then this keyword is ignored. IfNone, the default model bounding box will be used. This keyword is ignored ifmodel_shapeis specified or if theparams_tablecontains a'model_shape'column. Note that some Photutils PSF models have abbox_factorkeyword that is used to define the model bounding box. In that case, this keyword is ignored.- x_namestr, optional
The name of the
modelparameter that corresponds to the x position of the sources. This value must also be a column name inparams_tableunlessparams_mapmaps this parameter to a different column.- y_namestr, optional
The name of the
modelparameter that corresponds to the y position of the sources. This value must also be a column name inparams_tableunlessparams_mapmaps this parameter to a different column.- params_mapdict or None, optional
A dictionary mapping the model parameter names to the column names in the input
params_table. The dictionary keys are the model parameter names and the values are the column names in the inputparams_table. This can be used to map column names to model parameter names that are different. For example, if the input column name is ‘flux_f200w’ and the model parameter name is ‘flux’, then useparams_map={'flux': 'flux_f200w'}. This dictionary may also be used to map the model x and y parameters to columns with names other thanx_nameandy_name. In that case, the dictionary keys for the positions must be thex_nameandy_nameparameter names. If those keys are not in the dictionary, thex_nameandy_namecolumns are used.- discretize_method{‘center’, ‘interp’, ‘oversample’, ‘integrate’}, optional
One of the following methods for discretizing the model on the pixel grid:
'center'(default) Discretize model by taking the value at the center of the pixel bins. This method should be used for ePSF/PRF single or gridded models, which are already integrated over the pixels.'interp'Discretize model by bilinearly interpolating between the values at the corners of the pixel bins.'oversample'Discretize model by taking the average of model values in the pixel bins on an oversampled grid. Use thediscretize_oversamplekeyword to set the integer oversampling factor.'integrate'Discretize model by integrating the model over the pixel bins usingscipy.integrate.dblquad. This mode conserves the model integral on a subpixel scale, but it is extremely slow.
The
'oversample'or'integrate'method should be used for a model that is not integrated over the pixels, such as thephotutils.psfanalytic models whose names end inPSF. Neither should be used for an ePSF/PRF model, which would then be integrated over the pixels twice.- discretize_oversampleint, optional
The integer oversampling factor used when
discretize_method='oversample'. This keyword is ignored otherwise.- progress_barbool, optional
Whether to display a progress bar while adding the sources to the image. The progress bar requires that the tqdm optional dependency be installed.
- Returns:
- Raises:
- TypeError
If
modelis not anastropy.modeling.Modelinstance or ifparams_tableis not anTable.- ValueError
If
modeldoes not have 2 inputs and 1 output, ifdiscretize_methodis invalid, if aparams_mapkey is not a model parameter name, if a required column is not inparams_table, if the model has nobounding_boxattribute and no model shape is specified, if the'local_bkg'column does not have the same flux units as the output image, or if aparams_tablecolumn has units andparams_tableis not aQTable.
Notes
The local background value around each source is optionally included using the
'local_bkg'column in the inputparams_table. This local background is added to each source over itsmodel_shaperegion. In regions where themodel_shapeof sources overlap, the local background will be added multiple times. This is not an issue if the sources are well-separated, but for crowded fields, this option should be used with care.Examples
import matplotlib.pyplot as plt from astropy.modeling.models import Moffat2D from photutils.datasets import (make_model_image, make_random_models_table) model = Moffat2D() n_sources = 25 shape = (100, 100) param_ranges = {'amplitude': [100, 200], 'x_0': [0, shape[1]], 'y_0': [0, shape[0]], 'gamma': [1, 2], 'alpha': [1, 2]} params = make_random_models_table(n_sources, param_ranges, seed=0) model_shape = (15, 15) data = make_model_image(shape, model, params, model_shape=model_shape) fig, ax = plt.subplots() ax.imshow(data, origin='lower') fig.tight_layout()
(
Source code,png,hires.png,pdf,svg)
import matplotlib.pyplot as plt import numpy as np from astropy.modeling.models import Gaussian2D from photutils.datasets import make_model_image, make_model_params model = Gaussian2D() shape = (500, 500) n_sources = 100 params = make_model_params(shape, n_sources, x_name='x_mean', y_name='y_mean', min_separation=25, amplitude=(100, 500), x_stddev=(1, 3), y_stddev=(1, 3), theta=(0, np.pi)) model_shape = (25, 25) data = make_model_image(shape, model, params, model_shape=model_shape, x_name='x_mean', y_name='y_mean') fig, ax = plt.subplots() ax.imshow(data, origin='lower') fig.tight_layout()
(
Source code,png,hires.png,pdf,svg)