galfits.gsutils
GalfitS Utilities Module
This module provides utility functions and classes for multi-band galaxy fitting, including data preparation, parameter initialization, visualization, and result analysis.
Key Components
GSData: Multi-band imaging and spectroscopic data container
Parameter initialization: Create lmfit.Parameters with priors and constraints
Visualization: Plotting tools for data, models, and residuals
Unit conversions: Flux calibration and magnitude systems
PSF handling: PSF loading and manipulation
Result analysis: Extract and visualize fit results
The module interfaces with the main fitting classes (gsfitter, vdfitter, sedfitter) and provides high-level workflows for common analysis tasks.
- galfits.gsutils.ABmag_to_covf(mzp_AB, wave)[source]
Convert AB magnitude zeropoint to flux conversion factor.
- Parameters:
mzp_AB (float) – AB magnitude zeropoint.
wave (float) – Effective wavelength in Angstroms.
- Returns:
Conversion factor: counts = flux_Flambda × factor.
- Return type:
float
- galfits.gsutils.Fl_to_Fnu(Fl, lambd)[source]
Convert flux from F_λ (erg/s/cm²/Å) to F_ν (mJy).
- Parameters:
Fl (float or array_like) – Flux in erg/s/cm²/Å.
lambd (float or array_like) – Wavelength in Angstroms.
- Returns:
Flux density in milliJanskys.
- Return type:
float or array_like
- galfits.gsutils.Fnu_to_Fl(Fnu, lambd)[source]
Convert flux from F_ν (mJy) to F_λ (erg/s/cm²/Å).
- Parameters:
Fnu (float or array_like) – Flux density in milliJanskys.
lambd (float or array_like) – Wavelength in Angstroms.
- Returns:
Flux in erg/s/cm²/Å.
- Return type:
float or array_like
Notes
Conversion: \(F_\lambda = F_\nu \times \frac{c}{\lambda^2}\) where c = 2.998×10⁵ km/s.
- galfits.gsutils.MAB2Flam(MAB, Lambda)[source]
Convert AB magnitude to flux density in cgs units of erg/s/cm^3. M_{AB}=-2.5log_{10}f_{
u}-48.60
Parameters: MAB (float): AB magnitude Lambda (float): Wavelength in cm
Returns: float: Flux density in cgs units of erg/s/cm^3
- galfits.gsutils.add_Fourier_config(config, config_out, refer='b', target='c', galaxy='a', name='arm', m=2, separ=0.5, phiout=360, Re=None)[source]
- galfits.gsutils.covf_to_ABmag(covf, wave)[source]
Convert flux conversion factor to AB magnitude zeropoint.
- Parameters:
covf (float) – Flux conversion factor.
wave (float) – Effective wavelength in Angstroms.
- Returns:
AB magnitude zeropoint.
- Return type:
float
- galfits.gsutils.find_seq_fltr(Myfitter, fltr)[source]
find the sequence of fltr in Myfitter.GSdata.allbands
- Returns:
idx_fltr – 0-indexed sequence of fltr, you can index image like “Myfitter.GSdata.get_image(fltr)”
- Return type:
int
- galfits.gsutils.find_seq_gsmodel(Myfitter, gal_name)[source]
find the sequence of gal_name in Myfitter.model_list
- Returns:
gal_idx – 0-indexed sequence of gal_name, you can index gal_name like “Myfitter.model_list[gal_idx]”
- Return type:
int
- galfits.gsutils.free_sed_config(config, config_out, Myfitter, plabel, SEDmode=0, geovary=False)[source]
Free the SED parameters in the config file. Parameters: config (str): The name of the config file to read. config_out (str): The name of the output config file to write. Myfitter (object): The fitter object containing the model parameters. pname (str): The name of the profile to free the SED parameters for.
- galfits.gsutils.get_L_band(wave, flux, resp)[source]
Integrate flux through filter response curve.
Computes band-integrated flux by convolving SED with filter transmission.
- Parameters:
wave (array_like) – SED wavelength grid in Angstroms.
flux (array_like) – SED flux in F_λ units.
resp (array_like, shape (N, 2)) – Filter response: [:, 0] = wavelength, [:, 1] = transmission.
- Returns:
Band-integrated flux (same units as input flux).
- Return type:
float
Notes
Formula: \(\langle F \rangle = \frac{\int F(\lambda) R(\lambda) d\lambda}{\int R(\lambda) d\lambda}\)
- galfits.gsutils.get_effwave(resp)[source]
Compute effective wavelength of filter.
- Parameters:
resp (array_like, shape (N, 2)) – Filter response curve [:, 0] = wavelength, [:, 1] = transmission.
- Returns:
Effective wavelength in Angstroms.
- Return type:
float
- galfits.gsutils.load_gal_image(Myfitter, flt, gal_name)[source]
Extract full galaxy model image in a specified band, matching cal_model_image exactly.
- Parameters:
Myfitter (gsfitter) – The fitted model (after load_gs_model and cal_model_image).
flt (str) – Filter name (e.g. ‘nircam_f115w’).
gal_name (str) – Galaxy component name matching Myfitter.model_list[*].name.
- Returns:
gal_img (array) – Galaxy model image in the same units as model_image.
im (image object)
- galfits.gsutils.normimg(image, min=0, max=None, sky=0, frac=0.5)[source]
Normalize image with arcsinh stretch for visualization.
Applies arcsinh scaling for better dynamic range display. Handles both positive and negative pixel values (e.g., residuals). Sky-subtracted before scaling.
- Parameters:
image (array_like) – Input image array.
min (float, default=0) – Lower threshold for arcsinh stretch.
max (float, optional) – Upper threshold. If None, uses image maximum.
sky (float, default=0) – Sky background to subtract before scaling.
frac (float, default=0.5) – Fraction parameter controlling stretch behavior near zero.
- Returns:
Normalized image in range suitable for display.
- Return type:
array_like
Notes
Arcsinh stretch preserves faint features better than linear or log scaling. Commonly used for displaying astronomical images with large dynamic range.
- galfits.gsutils.parse_config_file(filename)[source]
Parse GalfitS configuration file into dictionary.
Reads configuration file with format “KEY) value # comment” and converts to Python dictionary. Used by read_config_file to load fitting parameters.
- Parameters:
filename (str) – Path to configuration file.
- Returns:
Configuration dictionary with keys like ‘R1)’, ‘I1)’, ‘M1)’, etc. Values auto-converted to Python types (lists, floats, strings).
- Return type:
dict
Notes
Config file format:
R1) target_name
R2) [ra, dec]
R3) redshift
I1) [image_file, ext]
M1) model_type
See also
read_config_fileMain config loader
write_data_configCreate config files
- galfits.gsutils.photometry_to_img(flux, flux_err, z, outputname, band=None, effectivewave=None, unit='mJy')[source]
converting a photometry point to a image with a given band first, convert the flux to a Luminosity, in unit of 1e38erg/s/A unit: mJy or flambda, or MagAB
- galfits.gsutils.read_config_file(config, workplace, priorpath=None, fixagnlinepro=False, dataonly=False, vectorize=True)[source]
Load GalfitS configuration file and initialize fitting setup.
Main entry point for setting up fit from configuration file. Creates galaxy models, loads images/spectra, initializes parameters with priors. Returns fully configured fitter object ready for optimization.
- Parameters:
config (str) – Path to configuration file.
workplace (str) – Working directory for data files.
priorpath (str, optional) – Path to prior configuration file. Default None (no priors).
fixagnlinepro (bool, default=False) – Whether to fix AGN line profiles during fit.
dataonly (bool, default=False) – If True, only load data without creating models/fitter.
vectorize (bool, default=True) – Use vectorized operations for speed.
- Returns:
Myfitter (gsfitter or specialized fitter) – Configured fitter object (mbifitter, imagefitter_phot, imagefitter_SED, vdfitter, or sedfitter depending on data type).
par_total (lmfit.Parameters) – Full parameter object with bounds and priors.
GS_data (galfitS_data) – Data container with loaded images/spectra.
Notes
Config file structure:
R1-R3: Region info (target, coordinates, redshift)
I sections: Image definitions
S sections: Spectrum definitions
A sections: Atlas groupings
M sections: Model definitions
P sections: Parameter values and bounds
Fitting modes auto-detected:
‘images - photometry’: Multi-band images, photometry-only
‘images - SED’: Multi-band with SED modeling
‘images + spectra - SED’: Combined imaging + spectroscopy
‘spectrum - SED’: Spectrum-only fitting
Examples
>>> # Basic fit >>> fitter, params, data = read_config_file('config.txt', './') >>> result = fitter.optimizer(num_steps=5000) >>> >>> # With priors >>> fitter, params, data = read_config_file( ... 'config.txt', './', priorpath='priors.txt' ... )
See also
parse_config_fileConfig file parser
write_data_configCreate config files
- galfits.gsutils.standard_display(Myfitter, workplace, targ, ndisage=False, addtional_scatters=[], wrange=[0.85, 1.15], saveimgs=False, sed_multiplot=False)[source]
- galfits.gsutils.write_config_models(confignamedata, confignamemodel, outputname)[source]
Write the model parameters to a config file. Parameters: configname (str): The name of the config file to write. Myfitter (object): The fitter object containing the model parameters. outputname (str): The name of the output file to write the parameters to.
- galfits.gsutils.write_data_config(outname, regioninfo, groupinfo, images, bands, cutsizes, convf, magzps=None, varyshift=True, varysky=False, useSED=True, skyrange=1.0, lenlabel=1)[source]
#im = [[image, image_ext, mask, mask_ext, sigimg, sigimg_ext, psf, psf_ext, psf_sampling, kerror]…] # groupinfo = [[‘hst’, [1,2]], [‘sdss’, [3,4,5,6]] …]