Imfit: 2D Multi-component Fitting of Galaxy Images

Imfit: Fast, Flexible Multi-component Fitting of Galaxy Images

About      Downloads      Example of Use      Tutorial/Quickstart

Imfit is a program for fitting astronomical images -- especially images of galaxies, though it can be used for fitting other sources. The user can specify a set of one or more 2D image functions (e.g., elliptical exponential, elliptical Sérsic, circular Gaussian) which are added together to generate a model image. This model image is then matched to the input image by adjusting the 2D function parameters via nonlinear minimization of the total χ2. Imfit can be compiled to use multi-threading (via OpenMP), taking advantage of multiple cores in modern CPUs for extra speed.

Individual 2D functions can be grouped into arbitrary sets sharing a common (x,y) position on the image plane; this allows fitting galaxies with off-center components, or multiple galaxies in the same image. Parameters for individual functions can be held fixed or restricted to user-specified ranges. The model image can be convolved with a Point Spread Function (PSF) image to better match the input image; the PSF image can be any square, centered image the user supplies. One or more subsections of the image can also be specified for convolution with oversampled PSF images. (A companion program can be used to generate PSF images based on, e.g., 2D Gaussian or Moffat functions -- or any combination of the functions built into Imfit.)

The χ2 minimization can be done using one of three algorithms:
  1. Levenberg-Marquardt (the default);
  2. Nelder-Mead simplex (slower, but less likely to be trapped in local minima in the χ2 landscape);
  3. Differential Evolution (much slower, but requires no initial guess and is even less likely to be trapped in local minima).

A companion program can do Markov chain Monte Carlo (MCMC) analysis of the model + data combination as well, using the same parameter files, statistical approaches, and image functions as the main program.

The per-pixel dispersions used in the χ2 calculation can be estimated from the data or from the model, or they can come from user-supplied dispersion or variance maps. You can also use Poisson-based maximum likelihood statistics (such as the Cash statistic; Cash 1979) instead of χ2 for the minimization, which is more appropriate for images with low counts and low or zero read noise.

A key characteristic of Imfit is a modular, object-oriented design that makes it easy to add new, user-designed 2D image functions. This is accomplished by writing C++ code for a new image-function class (e.g., by copying and modifying the header and implementation files for one of the existing image functions), modifying one additional file to include references to the new function, and re-compiling the program. Some notes and examples for doing this are included in the documentation.

Available image components (usually with elliptical isophotes) include:

  • Exponential and Sérsic profiles (with optional boxy/disky isophote shapes)
  • Gaussian, Moffat, and King profiles
  • Interpolated, scaled PSF images as point sources
  • Broken exponential profile, allowing (for example) disk truncations and antitruncations
  • Core-Sérsic profile (Graham et al. 2003; Trujillo et al. 2004)
  • Elliptical rings with Gaussian profiles
  • Analytic edge-on disk (van der Kruit & Searle 1981), including the generalized sech function from van der Kruit (1988) for the vertical profile
  • 3D density models of exponential disks, elliptical rings, and Ferrers bars seen at arbitrary inclinations, with pixel values calculated by integration along the line of sight through the object

Imfit is an open-source project; the source code is freely available under the GNU Public License (GPL).


What's New in Version 1.9:

Version 1.9 of Imfit -- released 27 November 2022 -- included the following new feature:
  • A new image function (PointSourceRot) which reproduces a scaled version of the PSF, rotated by an arbitrary angle.
  • The PointSource (and PointSourceRot) image functions now include the option of Lanczos2 instead of the default bicubic interpolation.
  • It is now possible to write image functions which, like the updated versions of PointSource and PointSourceRot, take one or more optional parameters to specialize the function, in the form of key-value pairs, which are applied to the function at startup. How they values are used is up to the individual function (e.g., they can be converted to floating-point values, used to select one of several internal algorithms, interpreted as the filename of an extra configuration file with options and/or data for the function, etc.).
  • Imfit will by default set the number of threads = the number of physical CPU cores; this can result in faster PSF convolutions.
  • Canceling a fit via Ctrl-C now saves a summary file of the current state of the fit.

See the Changelog for more details and previous updates.


These are compressed tar files containing executables for imfit, imfit-mcmc, and makeimage, the documentation PDF (also available separately, below), and a small subdirectory containing example files. (The executables contain statically linked versions of the CFITSIO, FFTW, NLopt, and GSL libraries -- see below for links to the respective sources -- and so you do not need to have these already installed.)

Note that the precompiled Linux binaries were compiled against GNU C library (glibc) version 2.23, so if your system has an older version, you may have problems running them (in which case compiling the source code yourself should work just fine).

Documentation by itself: imfit_howto.pdf.
See also the Tutorial.

Source Code: imfit-1.9.0-source.tar.gz.

(Previous versions: imfit-1.8.0-source.tar.gz; imfit-1.7.1-source.tar.gz;imfit-1.6.1-source.tar.gz;imfit-1.5.0-source.tar.gz;imfit-1.4.0-source.tar.gz;imfit-1.3.1-source.tar.gz;imfit-1.2.1-source.tar.gz; imfit-1.1-source.tar.gz; imfit-1.0.3-source.tar.gz.)

See the documentation (included in the source-code distribution in the docs/ subdirectory; also linked to directly above) for notes on how to compile the source code.

To compile Imfit from source, you will need, in addition to a C++ compiler, SCons (it's easy to install and use); you will also need the following external libraries (two required, two recommended but optional):

  • CFITSIO -- for reading and writing FITS files
  • FFTW (version 3) -- for Fast Fourier Transforms in PSF convolution
  • GNU Scientific Library (This should be version 2.0 or higher.)
  • [optional but recommended] NLopt -- needed for the Nelder-Mead simplex minimization algorithm; you can compile Imfit without it.

Imfit has been tested with GCC versions 5 through 9 (on Mac OS X) and 4.8, 4.9, and 7.1 (on Ubuntu Linux); it should compile with versions of GCC later 4.8.1 or later. It can also be compiled with recent versions of LLVM/Clang (see the documentation for more).



There is a preliminary/beta version of a Python wrapper for Imfit now available! This can be installed using "pip".

Imfit at GitHub:

You can also access the current (bleeding-edge, not guaranteed to work perfectly) source code at Imfit's GitHub page. (This includes extra tests and auxiliary/experimental code, Doxygen files for building API documentation, and other miscellanea.)

For most purposes, if you want to hack around with Imfit, it may be simpler to use the standard source-code release (above). But if you're comfortable cloning from or forking on GitHub, now you can!

Paper; Mailing List for Updates

A paper describing Imfit (Erwin 2015) has been published in The Astrophysical Journal; a copy can be found here, or via the arXiv.

If you'd like to be informed about updates to Imfit, send me an email [erwin at or erwin at] and I'll add your name to my informal mailing list.

Acknowledging and Citing Imfit

If Imfit is useful for you in your research, the simplest way to cite it is with the paper (Erwin 2015) noted above. There's also an Astrophysics Source Code Library reference and a Zenodo DOI: and I'm always happy if people include a link to this page.

An Example: Modeling the Central Regions of the Edge-on S0 Galaxy NGC 4762

NGC 4762 is an edge-on S0 galaxy in the Virgo Cluster; see the classic analysis of photographic-plate photometry by Wakamtsu & Hamabe (1984) for a detailed discussion of this galaxy's large-scale structure.

SDSS gri image (David W. Hogg, Michael R. Blanton, and the Sloan Digital Sky Survey Collaboration)

Here, I show a 2D decomposition of the inner 3 x 3 kpc of the galaxy (red box), using Imfit to model the Hubble Space Telescope F475W ACS/WFC image from the ACS Virgo Cluster Survey. The exquisite resolution of the HST image reveals evidence for a compact, edge-on nuclear ring, in addition to an elliptical bulge and two separate ``disk'' components (the latter two matching what Wakamatsu & Hamabe suggested were a bar and its surrounding lens). New components for modeling the edge-on ring and the complex edge-on disk components were written, compiled, and then used as part of the overall modeling process.

The final fitting was done using this configuration file and the following command (this actually fits the inner 3 x 6 kpc of the galaxy; note that fitting just a subset of the image can be specified right on the command line):

imfit n4762g_horiz_adu.fits[2263:3063,1858:3458] --config config_imfit_n4762.txt --mask n4762g_mask.fits[2263:3063,1858:3458] --psf acs_psf.fits

These are the four main components used in the modeling process (log-scaled isophote contour plots; an additional circular Gaussian representing an unresolved nuclear source was also used, but is not shown here):





(elliptical Sérsic component)

Stellar nuclear ring

(two-sided, edge-on Gaussian ring component)

Thin disk/bar

(broken-exponential edge-on disk component)

Thick disk/lens

(broken-exponential edge-on disk component)



Final PSF-convolved model image Hubble Space Telescope ACS/WFC F475W image