MultiRegionFit#
- class niriss_tools.grism.MultiRegionFit(config_path, obj_id, obj_z, run_all=True)[source]#
Bases:
objectA multi-region version of
grizli.multifit.MultiBeam.- Parameters:
- config_path
PathLike The TOML-formatted configuration file containing the parameters to use for the fit.
- obj_id
int The object ID number to fit.
- obj_z
float 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. IfFalse, the configuration file will be parsed, but the individual fitting methods must be called manually.
- config_path
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:
- header
fits.Header The original header.
- header
- Returns:
fits.HeaderThe 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:
- z
float, 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_order
int, optional Fit a polynomial function to the spectrum, with a default order of
0.- n_samples
int, optional The number of samples to draw from the joint posterior distributions in each region, by default
3.- n_iters
int, optional The number of iterations to perform when fitting, by default
10.- bad_pa_threshold
float|None, optional The threshold for identifying bad PAs before fitting. By default
1.6, ifNoneall beams will be used.- spec_wavs
ArrayLike|None, optional The wavelength sampling to use when generating the template spectra from the
bagpipesposterior distributions. The default value ofNonesets this to the \(0.96 - 2.3\mu\rm{m}\) range in \(45\mathring{A}\) steps.- oversamp_factor
int, 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.- veldisp
float, 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_dir
PathLike|None, optional Where the output files will be written. If
None(default), files will be written toself.out_dir/multiregion.- temp_dir
PathLike|None, optional The temporary directory to use for memmapped files (if
memmap==True). IfNone(default), the current working directory will be used.- memmapbool, optional
Whether to use a memmap to store large files. By default
False. IfTrue, the large model array will be written to a temporary array on disk.- cpu_count
int, optional The number of CPUs to use for multiprocessing, by default -1.
- overwritebool, optional
If
True, overwrite any existing fit. By defaultFalse, and will attempt to load a previous fit.- use_lines
ArrayLike, 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 theCloudynomenclature (Ferland+17)."grizli": The name of the emission line ingrizli, 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
grizlioutput format. By defaultTrue.- 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.- pline
dict, optional Parameters for generating the drizzled emission line maps. Defaults to
DEFAULT_PLINE.- seed
int|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_method
str, 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_iters
intorArrayLike, 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_itersattempts will be re-fit withnnls_iters[0]iterations. This is only relevant ifnnls_method != "scipy".- nnls_tol
floatorArrayLike, 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 initialn_itersattempts.- n_shifted
intorNone, 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_samples
intorNone, 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.
- z
- Returns:
tupleExact 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_path
PathLike The location of the previously generated model grid.
- binned_data_path
PathLike The location of the binned photometric catalogue and segmentation map used as input for Bagpipes.
- binned_data_hdu
str|int|None, optional The identifier of the HDU within
binned_data_pathcontaining the photometric catalogue, by default"PHOT_CAT".- bagpipes_atlas_params
dict|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_paramswill be used.- load_fn
Callable|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_bagpipeswill be used.- overwrite_fitbool, optional
If
True, then any existing posterior distributions and output catalogues will be overwritten. By defaultFalse.- id_colname
str, optional The name of the column in the photometric catalogue containing the bin ID, by default
"bin_id".- n_cores
int, 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 bymultiprocessing.cpu_count. By default,4processes will be used.- obj_z
float|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_atlasfor more details.- z_range
float, optional As above.
- atlas_path
- 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_kwargs
dict 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_path
PathLike|None, optional The location of a
*beams.fitsfile to use for fitting. IfNone(default), this will be selected automatically based on theobj_idand directory structure specified in the configuration file.- img_cutout
int, 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_kwargs
dict, optional Any additional parameters to pass through to
gen_stacked_beams.- **multibeam_kwargs
dict, optional Any additional parameters to pass through to
grizli.multifit.MultiBeam.
- binning_kwargs
- Returns:
- new_beam_path
PathLike The location of the
*beams.fitsused for alignment. Ifuse_stacks==True, this will be a stacked version of the input file.- binned_data_path
PathLike The location of the binned data in FITS format. This contains both the segmentation map and the binned photometric catalogue.
- new_beam_path
- Raises:
IOErrorIf no
*beams.fitsfile 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_z
float|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. Ifz_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. IfNone(default), this will be set toself.obj_z.- z_range
float, 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 setz_range=0.0. Ifobj_zisArrayLike, this parameter is ignored.- num_age_bins
int, optional The number of age bins to fit, each of which will have a constant star formation rate following Leja+19. By default,
5bins are generated.- min_age_bin
float, 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_type
str, 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_samples
int|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 defaultFalse.- n_cores_atlas
int, 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 bymultiprocessing.cpu_count. By default,4processes will be used.
- obj_z
- Returns:
PathLikeThe location of the model atlas.