casa
$Rev:20696$
|
00001 # 00002 # This file was generated using xslt from its XML file 00003 # 00004 # Copyright 2009, Associated Universities Inc., Washington DC 00005 # 00006 import sys 00007 import os 00008 from casac import * 00009 import string 00010 from taskinit import casalog 00011 #from taskmanager import tm 00012 import task_imfit 00013 def imfit(imagename='', box='', region='', chans='', stokes='', mask='', includepix=[], excludepix=[], residual='', model='', estimates='', logfile='', append=True, newestimates='', complist='', overwrite=False, dooff=False, offset=0.0, fixoffset=False, stretch=False): 00014 00015 """Fit one or more elliptical Gaussian components on an image region(s) 00016 PARAMETER SUMMARY 00017 imagename Name of the input image 00018 box One or more box regions to use for fitting, 00019 eg "100, 120, 200, 220, 300, 300, 400, 400" 00020 to use two boxes. If both box and region 00021 parameters are specified, box is used. 00022 region Region of interest. See help par.region for specification options. 00023 stokes Stokes parameter to fit. If blank, first polarization plane is used. 00024 mask Mask to use. See help par.mask. Default is none. 00025 includepix Range of pixel values to include for fitting. Array of two numeric 00026 values assumed to have same units as image pixel values. Only one 00027 of includepix or excludepix can be specified. 00028 excludepix Range of pixel values to exclude for fitting. Array of two numeric 00029 values assumed to have same units as image pixel values. Only one 00030 of includepix or excludepix can be specified. 00031 residual Name of the residual image to write. 00032 model Name of the model image to write. 00033 estimates Name of file containing initial estimates of component parameters 00034 (see below for formatting details). 00035 logfile Name of file to write fit results. 00036 append If logfile exists, append to it (True) or overwrite it (False). 00037 newestimates File to write fit results which can be used as initial estimates 00038 for next run. 00039 complist Name of output component list table. 00040 overwrite Overwrite component list table if it exists? 00041 dooff Simultaneously fit a zero-level offset? 00042 offset Initial estimate for the zero-level offset. Only used if dooff is True. 00043 fixoffset Hold zero-level offset constant during fit? Only used if dooff is True. 00044 stretch Stretch the input mask if necessary and possible. Only used if a mask is specified. 00045 See help par.stretch. 00046 00047 OVERVIEW 00048 imfit is used to fit one or more gaussians to sources in an image as 00049 well as an optional zero-level offset. Fitting is limited to a single polarization 00050 but can be performed over several contiguous spectral channels. 00051 If the image has a clean beam, the report will contain both the convolved 00052 and the deconvolved fit results. It is a simple wrapper around the ia.fitcomponents() 00053 tool method. 00054 00055 When dooff is False, the method returns a dictionary with two keys, 'converged' and 'results'. 00056 The value of 'converged' is a boolean array which indicates if the fit converged 00057 on a channel by channel basis. 00058 The value of 'results' is a dictionary representing a component list reflecting the 00059 fit results. In the case of a deconvolved image, the sizes and position angles are those 00060 of the source convolved with the restoring beam. For convenience, the 'results' dictionary can 00061 be read into a compoenent list tool (default tool is named cl) using the fromrecord() method 00062 for easier inspection using tool methods, eg 00063 00064 cl.fromrecord(res['results']) 00065 00066 If dooff is True, in addtion to the specified number of 00067 gaussians, a zero-level offset will also be fit. The initial estimate for this 00068 offset is specified using the offset parameter. Units are assumed to be the 00069 same as the image brightness units.The zero level offset can be held constant during 00070 the fit by specifying fixoffset=True. In the case of dooff=True, the returned 00071 dictionary contains two additional keys, 'zerooff' and 'zeroofferr', which are both 00072 arrays containing the the fitted zero level offset value and its error, respectively, 00073 for each channel. Both sets of values are in image brightness units. 00074 In cases where the fit did not converge, these values are set to NaN. 00075 00076 The region can either be specified by a box(es) or a region (there is no 00077 garauntee which will be used if both are specified, so be certain only one is). 00078 If specified using the box parameter, multiple boxes can be given using the format 00079 box="blcx1, blcy1, trcx1, trcy1, blcx2, blcy2, trcx2, trcy2, ... , blcxN, blcyN, trcxN, trcyN" 00080 where N is the number of boxes. In this case, the union of the specified boxes will be used. 00081 00082 Ranges of pixel values can be included or excluded from the fit via includepix and excludepix. 00083 00084 If specified, the task will write the residual and/or model images for 00085 successful fits. 00086 00087 If an estimates file is not specified, the task will attempt to estimate 00088 initial parameters and fit a single Gaussian. If a multiple Gaussian fit 00089 is desired, the user must specify initial estimates via a text file 00090 (see below for details). 00091 00092 The user has the option of writing the result of the fit to a log file, 00093 and has the option of either appending to or overwriting an existing file. 00094 00095 The user has the option of writing the (convolved) parameters of a successful 00096 fit to a file which can be fed back to imfit as the estimates file for a 00097 subsequent run. 00098 00099 FITTING OVER MULTIPLE CHANNELS 00100 00101 For fitting over multiple channels, the result 00102 of the previous successful fit is used as the estimate for the next channel. The number 00103 of gaussians fit cannot be varied on a channel by channel basis. Thus the variation 00104 of source structure should be reasonably smooth in frequency to produce reliable fit 00105 results. 00106 00107 MASK SPECIFICATION 00108 00109 Mask specification can be done using an LEL expression. For example 00110 00111 mask = '"myimage">5' will use only pixels with values greater than 5. 00112 00113 INCLUDING AND EXCLUDING PIXELS 00114 00115 Pixels can be included or excluded from the fit based on their values 00116 using includepix and excludepix. Note that specifying both is not permitted 00117 and will result in an error. If specified, both take an array of two numeric 00118 values. 00119 00120 ESTIMATES 00121 00122 Initial estimates of fit parameters may be specified via an estimates 00123 text file. Each line of this file should contain a set of parameters for 00124 a single gaussian. Optionally, some of these parameters can be fixed during 00125 the fit. The format of each line is 00126 00127 peak intensity, peak x-pixel value, peak y-pixel value, major axis, minor axis, position angle, fixed 00128 00129 The fixed parameter is optional. The peak intensity is assumed to be in the 00130 same units as the image pixel values (eg Jy/beam). The peak coordinates are specified 00131 by pixel values. The major and minor axes and the position angle are the convolved 00132 parameters if the image has been convolved with a clean beam and are specified as quantities. 00133 The fixed parameter is optional and is a string. It may contain any combination of the 00134 following characters 'f' (peak intensity), 'x' (peak x position), 'y' (peak y position), 00135 'a' (major axis), 'b' (minor axis), 'p' (position angle). 00136 00137 In addition, lines in the file starting with a "#" are considered comments. 00138 00139 An example of such a file is: 00140 00141 # peak intensity must be in map units 00142 120, 150, 110, 23.5arcsec, 18.9arcsec, 120deg 00143 90, 60, 200, 46arcsec, 23arcsec, 140deg, fxp 00144 00145 This is a file which specifies that two gaussians are to be simultaneously fit, 00146 and for the second gaussian the specified peak intensity, x position, and position angle 00147 are to be held fixed during the fit. 00148 00149 ERROR ESTIMATES 00150 00151 The reported estimated errors are the maxima from two methods. In the first method, errors are calculated 00152 according to the description given in AIPS++ Note 224: AIPS++ Least Squares Background available at 00153 http://www.astron.nl/casacore/trunk/casacore/doc/notes/224.html . In the second method (Condon et al 00154 1998AJ.115.1693; Richards et al. 1999MNRAS.306..954), the following algorithms are used to compute the 00155 various errors. 00156 00157 R = rms of residual image 00158 P = fitted peak intensity 00159 S = P/R (signal to noise) 00160 00161 longitude error = (FWHM beam width in longitude direction)/(2*S) 00162 latitude error = (FWHM beam width in latitude direction)/(2*S) 00163 FWHM of major axis error = (FWHM beam along major axis)/S 00164 FWHM of minor axis error = (FWHM beam along minor axis)/S 00165 position angle error is calculated via standard error propogation of the FWHM major and minor axes sizes and errors 00166 flux error is computed via standard error propogation of the peak intensity and its error and the FWHM major 00167 and minor axes sizes and their errors. 00168 00169 In the case where the image has no beam, the pixel width is used as the diameter of the resolution element. 00170 00171 In the vast majority of (and perhaps all) cases, the errors reported come from the second method. 00172 00173 The deconvolved size and position angle errors are computed by taking the maximum of the absolute values of the 00174 differences of the best fit deconvolved value of the given parameter and the deconvolved size of the eight 00175 possible combinations of (FWHM major axis +/- major axis error), (FWHM minor axis +/- minor axis error), 00176 and (position andle +/- position angle error). 00177 00178 CAUTIONARY NOTES ON ERRORS 00179 imfit makes the following implicit assumptions: 00180 00181 1. The noise associated with the pixels it is given to fit is truly random and gaussian 00182 2. There is no offset (constant (unless one is fit), gradient, or more complex function) in pixel values. 00183 3. The things that are trying to be fit are bona-fide gaussians. 00184 00185 If any of these assumptions is not true, the errors reported will in general be lower limits. These 00186 assumptions do not hold if for example, there is structure in the noise of the image (eg the residual map produced 00187 by deconvolution has structure), the area being fit lies in a negative bowl, in extended emission, or on an image 00188 artifact (eg ripple), or the source(s) is nongaussian (eg a disk or more complex function convolved with a gaussian; 00189 just because something looks gaussian to the human image analysis system does not necessarily mean it is). 00190 In addition, in the case of an image produced from interferometric observations, the errors given are appropriate 00191 for good uv plane coverage. They may be a factor of two or more higher for sparse arrays or snapshots. 00192 In short, imfit is not a silver bullet for determining parameters in flawed images and reported errors 00193 should be interpreted cautiously in the vast majority of all "real life" cases. The closer the source being fit is to 00194 fulfilling these assumptions the more realistic reported errors will be. 00195 00196 EXAMPLE: 00197 00198 Here is how one might fit two gaussians to multiple channels of a cube using the fit 00199 from the previous channel as the initial estimate for the next. It also illustrates 00200 how one can specify a region in the associated continuum image as the region to use 00201 as the fit for the channel. 00202 00203 default imfit 00204 imagename = "co_cube.im" 00205 # specify region using region from continuum 00206 region = "continuum.im:source.rgn" 00207 chans = "2~20" 00208 # only use pixels with positive values in the fit 00209 excludepix = [-1e10,0] 00210 # estimates file contains initial parameters for two Gaussians in channel 2 00211 estimates = "initial_estimates.txt" 00212 logfile = "co_fit.log" 00213 # append results to the log file for all the channels 00214 append = "True" 00215 imfit() 00216 00217 00218 """ 00219 if type(includepix)==int: includepix=[includepix] 00220 if type(excludepix)==int: excludepix=[excludepix] 00221 00222 # 00223 # The following is work around to avoid a bug with current python translation 00224 # 00225 mytmp = {} 00226 00227 mytmp['imagename'] = imagename 00228 mytmp['box'] = box 00229 mytmp['region'] = region 00230 mytmp['chans'] = chans 00231 mytmp['stokes'] = stokes 00232 mytmp['mask'] = mask 00233 mytmp['includepix'] = includepix 00234 mytmp['excludepix'] = excludepix 00235 mytmp['residual'] = residual 00236 mytmp['model'] = model 00237 mytmp['estimates'] = estimates 00238 mytmp['logfile'] = logfile 00239 mytmp['append'] = append 00240 mytmp['newestimates'] = newestimates 00241 mytmp['complist'] = complist 00242 mytmp['overwrite'] = overwrite 00243 mytmp['dooff'] = dooff 00244 mytmp['offset'] = offset 00245 mytmp['fixoffset'] = fixoffset 00246 mytmp['stretch'] = stretch 00247 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00248 trec = casac.utils().torecord(pathname+'imfit.xml') 00249 00250 casalog.origin('imfit') 00251 if trec.has_key('imfit') and casac.utils().verify(mytmp, trec['imfit']) : 00252 result = task_imfit.imfit(imagename, box, region, chans, stokes, mask, includepix, excludepix, residual, model, estimates, logfile, append, newestimates, complist, overwrite, dooff, offset, fixoffset, stretch) 00253 00254 else : 00255 result = False 00256 return result