MultiRegionFit#

class niriss_tools.grism.MultiRegionFit(config_path, obj_id, obj_z, run_all=True)[source]#

Bases: object

A multi-region version of grizli.multifit.MultiBeam.

Parameters:
config_pathPathLike

The TOML-formatted configuration file containing the parameters to use for the fit.

obj_idint

The object ID number to fit.

obj_zfloat

The redshift at which the object should be fitted.

run_allbool, optional

If True (default), the fit will proceed automatically based on the setup described in the configuration file. If False, the configuration file will be parsed, but the individual fitting methods must be called manually.

Methods Summary

add_pipes_info(header)

Update a header with information about the 2D SED fitting.

fit_at_z([z, fit_background, poly_order, ...])

Fit the object at a specified redshift.

fit_atlas(atlas_path, binned_data_path[, ...])

Perform a 2D SED fit to the binned photometric data.

gen_aligned_photometry(binning_kwargs[, ...])

Align photometric data to the direct image in an extracted beam.

gen_atlas([obj_z, z_range, num_age_bins, ...])

Generate the fit parameters and model grid.

import_config(config_path)

Parse a configuration file and set class attributes accordingly.

run_all()

Run the full fit based on the supplied configuration file.

Methods Documentation

add_pipes_info(header)[source]#

Update a header with information about the 2D SED fitting.

Parameters:
headerfits.Header

The original header.

Returns:
fits.Header

The updated header.

fit_at_z(z=0.0, fit_background=True, poly_order=0, n_samples=3, n_iters=10, bad_pa_threshold=1.6, spec_wavs=None, oversamp_factor=1, veldisp=500, direct_images=None, out_dir=None, temp_dir=None, memmap=False, cpu_count=-1, overwrite=False, use_lines=[{'cloudy': ['H  1  1.87510m'], 'grizli': 'PaA', 'wave': 18756.3}, {'cloudy': ['H  1  1.28181m'], 'grizli': 'PaB', 'wave': 12821.7}, {'cloudy': ['H  1  1.09381m'], 'grizli': 'PaG', 'wave': 10941.2}, {'cloudy': ['H  1  1.00494m'], 'grizli': 'PaD', 'wave': 10052.2}, {'cloudy': ['H  1  6562.80A', 'N  2  6583.45A', 'N  2  6548.05A'], 'grizli': 'Ha', 'wave': 6564.697}, {'cloudy': ['H  1  4861.32A'], 'grizli': 'Hb', 'wave': 4862.738}, {'cloudy': ['O  3  5006.84A'], 'grizli': 'OIII-5007', 'wave': 5008.24}, {'cloudy': ['O  3  4958.91A'], 'grizli': 'OIII-4959', 'wave': 4960.295}, {'cloudy': ['O  3  4363.21A'], 'grizli': 'OIII-4363', 'wave': 4364.436}, {'cloudy': ['O  2  3726.03A', 'O  2  3728.81A'], 'grizli': 'OII', 'wave': 3728.48}, {'cloudy': ['S  3  9530.62A'], 'grizli': 'SIII-9530', 'wave': 9530.62}, {'cloudy': ['S  3  9068.62A'], 'grizli': 'SIII-9068', 'wave': 9068.62}, {'cloudy': ['S  2  6730.82A', 'S  2  6716.44A'], 'grizli': 'SII', 'wave': 6725.48}, {'cloudy': ['S  3  6312.06A'], 'grizli': 'SIII-6314', 'wave': 6313.81}, {'cloudy': ['Blnd  1.08302m'], 'grizli': 'HeI-1083', 'wave': 10830.3}, {'cloudy': ['Blnd  5875.66A'], 'grizli': 'HeI-5877', 'wave': 5877.249}, {'cloudy': ['He 1  3888.64A'], 'grizli': 'HeI-3889', 'wave': 3889.75}, {'cloudy': ['Ne 3  3868.76A'], 'grizli': 'NeIII-3867', 'wave': 3869.87}], save_lines=True, save_stacks=True, pline={'kernel': 'lanczos3', 'pixfrac': 1.0, 'pixscale': 0.06, 'size': 5}, seed=2744, nnls_method='scipy', nnls_iters=100, nnls_tol=1e-05, n_shifted=2, n_shifted_samples=1, cache_spec=True)[source]#

Fit the object at a specified redshift.

Parameters:
zfloat, optional

The redshift at which the object will be fitted, by default 0.

fit_backgroundbool, optional

Fit a constant background level, by default True.

poly_orderint, optional

Fit a polynomial function to the spectrum, with a default order of 0.

n_samplesint, optional

The number of samples to draw from the joint posterior distributions in each region, by default 3.

n_itersint, optional

The number of iterations to perform when fitting, by default 10.

bad_pa_thresholdfloat | None, optional

The threshold for identifying bad PAs before fitting. By default 1.6, if None all beams will be used.

spec_wavsArrayLike | None, optional

The wavelength sampling to use when generating the template spectra from the bagpipes posterior distributions. The default value of None sets this to the \(0.96 - 2.3\mu\rm{m}\) range in \(45\mathring{A}\) steps.

oversamp_factorint, optional

The factor by which the region segmentation map is oversampled before reprojecting to the beam coordinate system. This significantly slows down the model generation, but is essential to ensure that the template spectra correspond to the correct pixels in the NIRISS detector frame, and defaults to a factor of 1.

veldispfloat, optional

The velocity dispersion of the template spectra in km/s, by default 500.

direct_images_type_, optional

WIP, may allow for changing beam direct images at some point. By default None.

out_dirPathLike | None, optional

Where the output files will be written. If None (default), files will be written to self.out_dir/multiregion.

temp_dirPathLike | None, optional

The temporary directory to use for memmapped files (if memmap==True). If None (default), the current working directory will be used.

memmapbool, optional

Whether to use a memmap to store large files. By default False. If True, the large model array will be written to a temporary array on disk.

cpu_countint, optional

The number of CPUs to use for multiprocessing, by default -1.

overwritebool, optional

If True, overwrite any existing fit. By default False, and will attempt to load a previous fit.

use_linesArrayLike, optional

A list of lines, for which a 2D map will be generated based on the multi-region fit. Each item in the list should be a dict, containing the following keys:

  • "cloudy" : The name of one or more lines following the Cloudy nomenclature (Ferland+17).

  • "grizli" : The name of the emission line in grizli, or any other desired name.

  • "wave" : The rest-frame vacuum wavelength of the line.

save_linesbool, optional

Save the drizzled emission line maps, matching the grizli output format. By default True.

save_stacksbool, optional

Save the stacked beams, full models, and continuum models. The output format differs from grizli in that these stacks are not drizzled to account for the subpixel shifts. By default True.

plinedict, optional

Parameters for generating the drizzled emission line maps. Defaults to DEFAULT_PLINE.

seedint | None, optional

The seed for the random sampling, by default 2744. If None, then a new seed will be generated each time this method is called.

nnls_methodstr, optional

The method to use for finding the best-fit coefficients for the set of templates. Must be one of “scipy”, “numba”, or “adelie” (ordered in increasing speed). By default, “scipy” will be used.

nnls_itersint or ArrayLike, optional

The maximum number of iterations to attempt if the NNLS solution has not converged to the specified tolerance. If more than one value is passed, a two-stage fit will be run, whereby the best-fit solution from the first n_iters attempts will be re-fit with nnls_iters[0] iterations. This is only relevant if nnls_method != "scipy".

nnls_tolfloat or ArrayLike, optional

The desired tolerance for the NNLS solver. As with nnls_iters, the second value can be used to perform a more precise fit after the initial n_iters attempts.

n_shiftedint or None, optional

This allows for drawing additional posterior samples from other regions of the object. Regions will be selected randomly, with no preference as to spatial or spectral coherence (they are selected by shifting each segmentation map id when generating the models, hence the name). This reduces the chance of template mismatch based on the SED fit. By default, 2 additional regions will be used.

n_shifted_samplesint or None, optional

This determines the number of samples drawn from each of the additional regions (i.e. the total number of samples is given by n_samples + n_shifted * n_shifted_samples). By default, only 1 sample is drawn from each extra region.

cache_specbool, optional

Pre-generate the spectra before forward modelling. Can give a large speedup if running for many iterations, at the cost of additional disk space.

Returns:
tuple

Exact form of return still WIP.

fit_atlas(atlas_path, binned_data_path, binned_data_hdu='PHOT_CAT', bagpipes_atlas_params=None, load_fn=None, overwrite_fit=False, id_colname='bin_id', n_cores=4, obj_z=None, z_range=0.005)[source]#

Perform a 2D SED fit to the binned photometric data.

Parameters:
atlas_pathPathLike

The location of the previously generated model grid.

binned_data_pathPathLike

The location of the binned photometric catalogue and segmentation map used as input for Bagpipes.

binned_data_hdustr | int | None, optional

The identifier of the HDU within binned_data_path containing the photometric catalogue, by default "PHOT_CAT".

bagpipes_atlas_paramsdict | None, optional

A dictionary containing instructions on the kind of model which should be fitted to the data. This should match the previously generated model grid. If None (default), self.bagpipes_atlas_params will be used.

load_fnCallable | None, optional

A function which takes the ID as an argument and returns the model photometry. This should be in the form of an array with a column of fluxes in microjanskys and a column of flux errors in the same units. If None (default), load_photom_bagpipes will be used.

overwrite_fitbool, optional

If True, then any existing posterior distributions and output catalogues will be overwritten. By default False.

id_colnamestr, optional

The name of the column in the photometric catalogue containing the bin ID, by default "bin_id".

n_coresint, optional

The number of processes to use when fitting the catalogue. If set to 0, the code will run on a single process. If set to an integer less than 0, this will run on the number of cores returned by multiprocessing.cpu_count. By default, 4 processes will be used.

obj_zfloat | ArrayLike | None, optional

This can be used to override the redshift used for fitting, in case of a mismatch between the model atlas and the object of interest. See gen_atlas for more details.

z_rangefloat, optional

As above.

gen_aligned_photometry(binning_kwargs, use_stacks=True, beams_path=None, img_cutout=500, stack_beam_kwargs={}, **multibeam_kwargs)[source]#

Align photometric data to the direct image in an extracted beam.

Parameters:
binning_kwargsdict

Any arguments to pass to bin_and_save.

use_stacksbool, optional

Whether to fit to individual beams, or beams stacked by filter and grism. By default True.

beams_pathPathLike | None, optional

The location of a *beams.fits file to use for fitting. If None (default), this will be selected automatically based on the obj_id and directory structure specified in the configuration file.

img_cutoutint, optional

Make a slice of the original image with size in pixels [-cutout,+cutout] around the centre of the object, before alignment. By default, cutout=500.

stack_beam_kwargsdict, optional

Any additional parameters to pass through to gen_stacked_beams.

**multibeam_kwargsdict, optional

Any additional parameters to pass through to grizli.multifit.MultiBeam.

Returns:
new_beam_pathPathLike

The location of the *beams.fits used for alignment. If use_stacks==True, this will be a stacked version of the input file.

binned_data_pathPathLike

The location of the binned data in FITS format. This contains both the segmentation map and the binned photometric catalogue.

Raises:
IOError

If no *beams.fits file can be found, this method will raise an error.

gen_atlas(obj_z=None, z_range=0.005, num_age_bins=5, min_age_bin=20, sfh_type='continuity', n_samples=1000000.0, remake_atlas=False, n_cores_atlas=4)[source]#

Generate the fit parameters and model grid.

This method generates a dictionary of fit parameters for Bagpipes, as well as a large model grid to speed up the SED fitting.

Parameters:
obj_zfloat | ArrayLike | None, optional

The redshift of the object to fit. If a scalar value is passed, and z_range==0.0, the object will be fit to a single redshift value. If z_range!=0.0, this will be the centre of the redshift window. If an array is passed, this explicity sets the redshift range to use for fitting. If None (default), this will be set to self.obj_z.

z_rangefloat, optional

The maximum redshift range to search over, by default 0.005. To fit to a single redshift, pass a single value for obj_z, and set z_range=0.0. If obj_z is ArrayLike, this parameter is ignored.

num_age_binsint, optional

The number of age bins to fit, each of which will have a constant star formation rate following Leja+19. By default, 5 bins are generated.

min_age_binfloat, optional

The minimum age to use for the continuity SFH in Myr, i.e. the first bin will range from (0,min_age_bin). By default 20.

sfh_typestr, optional

The type of SFH prior to generate. Currently supports "continuity" (Leja+19, fixed age bins), "continuity_varied_z" (Leja+19, only the youngest age bin is fixed), and "dblplaw".

n_samplesint | float, optional

The number of samples to generate. By default 1e6. A useful number will typically be >10^5.

remake_atlasbool, optional

If True, any existing model atlas with the same name will be recreated and overwritten. By default False.

n_cores_atlasint, optional

The number of processes to use when generating the model grid. If set to 0, the code will run on a single process. If set to an integer less than 0, this will run on the number of cores returned by multiprocessing.cpu_count. By default, 4 processes will be used.

Returns:
PathLike

The location of the model atlas.

import_config(config_path)[source]#

Parse a configuration file and set class attributes accordingly.

Parameters:
config_pathPathLike

The TOML-formatted configuration file containing the parameters to use for the fit.

run_all()[source]#

Run the full fit based on the supplied configuration file.