def stats(imagefile, regionfile): stats1 = cts.imstat(imagefile, stokes='I') peakflux = stats1['max'][0] stats2 = cts.imstat(imagefile, region=regionfile) rms = stats2['rms'][0] return rms, peakflux
def checkResidualMaskStats(imagename, outfile='test.csv'): ''' Purpose: go through each mask and residual image for each major cycle, calculate the peak residual, and imagename: root image name ''' f = open(outfile, 'w') imageList = glob.glob("*inputres*") f.write('cycle, imagename, mask, max\n') for image in imageList: cycle = int(re.search('inputres(.*)', image).group(1)) mask = image.replace('inputres', 'autothresh') stats = imstat(imagename=image, mask=mask) maxresid = max(stats['max'][0], abs(stats['min'])[0]) f.write("{:d}, {:s}, {:s}, {:f}\n".format(cycle, image, mask, maxresid)) f.close()
def calcResRMS(resname=''): istat = imstat(resname,axes=[0,1,2]) ### Make this calc within the PB. rres = istat['rms'] ### for all channels. For first, pick [0] print("RMS spectrum : "+str(rres)) return rres
def estimate_SNR(imagename, disk_mask, noise_mask, chans=None): """ Estimate peak SNR of source Args: imagename (string): Image name ending in '.image' disk_mask: in the CASA region format noise_mask (string): Annulus to measure image rms, in the CASA region format, e.g. 'annulus[[500pix, 500pix],["1arcsec", "2arcsec"]]' """ headerlist = casatasks.imhead(imagename, mode="list") beammajor = headerlist["beammajor"]["value"] beamminor = headerlist["beamminor"]["value"] beampa = headerlist["beampa"]["value"] print("# %s" % imagename) print("# Beam %.3f arcsec x %.3f arcsec (%.2f deg)" % (beammajor, beamminor, beampa)) if chans is not None: disk_stats = casatasks.imstat(imagename=imagename, region=disk_mask, chans=chans) else: disk_stats = casatasks.imstat(imagename=imagename, region=disk_mask) disk_flux = disk_stats["flux"][0] print("# Flux inside disk mask: %.3f mJy" % (disk_flux * 1000, )) peak_intensity = disk_stats["max"][0] print("# Peak intensity of source: %.3f mJy/beam" % (peak_intensity * 1000, )) # print("npoints", disk_stats["npts"]) # print("sum", disk_stats["sum"]) # print("mean", disk_stats["mean"]) # print("sigma", disk_stats["sigma"]) if chans is not None: rms = casatasks.imstat(imagename=imagename, region=noise_mask, chans=chans)["rms"][0] else: rms = casatasks.imstat(imagename=imagename, region=noise_mask)["rms"][0] print("# rms: %.3e mJy/beam" % (rms * 1000, )) SNR = peak_intensity / rms print("# Peak SNR: %.3f" % (SNR, )) print()
def calcDiffImage(baseImage, testImage, diffImage='test.diff'): ''' calculate the image difference in two different primary beam ranges. Date Programmer Changes ---------------------------------------------------------------------- 2/22/2020 A.A. Kepley Original Code ''' import analyzemsimage as ami if not os.path.exists(diffImage): # using statistics across cube rather than per channel stats = imstat(baseImage) sncut = 7.0 * stats[ 'medabsdevmed'] * 1.4286 # 7 sigma. could probably change to lower immath( imagename=[baseImage, testImage], mode='evalexpr', expr='100*(IM1-IM0)/IM0', outfile=diffImage, ## shold I flip this for consistency with sims? mask='\"' + baseImage + '\" >' + str(sncut[0]) + ' && \"' + testImage + '\" >' + str(sncut[0])) pbImage = baseImage.replace('.image', '.pb') if os.path.exists(pbImage): statspdiff = ami.runImstat(Image=diffImage, PB=pbImage, innerAnnulusLevel=1.0, level=0.5) outstatspdiff = ami.runImstat(Image=diffImage, PB=pbImage, innerAnnulusLevel=0.5, level=0.2) else: print("PB not found. Not calculating stats.") statspdiff = None outstatspdiff = None return statspdiff, outstatspdiff
def get_rms(dirty_image_path, mask_path): # Estimate the RMS of the un-masked pixels of the dirty image rms = casatasks.imstat(imagename=dirty_image_path, mask='"{}" < 1.0'.format(mask_path))["rms"][0] return rms
def display_image_interact(imprefix=''): ia.open(imprefix + '.image') shp = ia.shape() impix = ia.getchunk() ia.close() ia.open(imprefix + '.pb') shp = ia.shape() impb = ia.getchunk() ia.close() ploc = np.where(impix == impix.max()) imspec = impix[ploc[0][0], ploc[1][0], 0, :] pbspec = impb[ploc[0][0], ploc[1][0], 0, :] istat = imstat(imprefix + '.residual') print('Residual RMS : %3.7f' % (istat['rms'])) pow_slider = widgets.FloatSlider( value=1.0, min=0.1, max=2.0, step=0.05, description='Power Scale:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', ) ran_slider = widgets.FloatRangeSlider( value=[-0.1, 2.0], min=-0.1, max=1.0, step=0.01, description='Min-Max:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', ) yran_slider = widgets.FloatRangeSlider( value=[0.6, 1.1], min=0.0, max=1.5, step=0.01, description='yMin-yMax:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', ) interact(arrplot, iarr=fixed(impix[:, :, 0, 0].transpose()), pow=pow_slider, vran=ran_slider, pb=fixed(impb[:, :, 0, 0].transpose()), imspec=fixed(imspec), pbspec=fixed(pbspec), yran=yran_slider)
def displayImage(imname='try_ALMA_A_single.image', pbname='', resname=''): ia.open(imname) shp = ia.shape() csys = ia.coordsys() impix = ia.getchunk() ia.close() if pbname != '': ia.open(pbname) impb = ia.getchunk() ia.close() rad_to_deg = 180 / np.pi w = WCS(naxis=2) w.wcs.crpix = csys.referencepixel()['numeric'][0:2] w.wcs.cdelt = csys.increment()['numeric'][0:2] * rad_to_deg w.wcs.crval = csys.referencevalue()['numeric'][0:2] * rad_to_deg w.wcs.ctype = ['RA---SIN', 'DEC--SIN'] #w.wcs.ctype = ['RA','DEC'] pl.figure(figsize=(12, 5)) pl.clf() pl.subplot(121, projection=w) p1 = int(shp[0] * 0.25) p2 = int(shp[0] * 0.75) pl.imshow(impix[p1:p2, p1:p2, 0, 0].transpose(), origin='lower') if pbname != '': pl.contour(impb[p1:p2, p1:p2, 0, 0].transpose(), [0.5], colors=['magenta'], origin='lower') pl.title('Image from channel 0') pl.xlabel('Right Ascension') pl.ylabel('Declination') pk = 0.0 if shp[3] > 1: pl.subplot(122) ploc = np.where(impix == impix.max()) pl.plot(impix[ploc[0][0], ploc[1][0], 0, :], 'bo-', label='Im') if pbname != '': pl.plot(impb[ploc[0][0], ploc[1][0], 0, :], 'ro-', label='PB') pl.title('Spectrum at source peak') pl.xlabel('Channel') pl.ylim((0.4, 1.1)) pl.legend() pk = impix[ploc[0][0], ploc[1][0], 0, 0] print('Peak Intensity (chan0) : %3.7f' % (pk)) if pbname != '': pbk = impb[ploc[0][0], ploc[1][0], 0, 0] print('PB at location of Intensity peak (chan0) : %3.7f' % (pbk)) else: ploc = np.where(impix == impix.max()) print("Image Peak : %3.4f" % (impix[ploc[0][0], ploc[1][0], 0, 0])) if pbname != '': print("PB Value : %3.4f" % (impb[ploc[0][0], ploc[1][0], 0, 0])) pk = impix[ploc[0][0], ploc[1][0], 0, 0] if resname != '': istat = imstat(resname) ### Make this calc within the PB. rres = istat['rms'][0] print('Residual RMS : %3.7f' % (rres)) else: rres = None return pk, rres # Return peak intensity from channnel 0 and rms
def get_selfcal_args(vis, loop, nloops, nterms, deconvolver, discard_nloops, calmode, outlier_threshold, threshold, step): visbase = os.path.split( vis.rstrip('/ '))[1] # Get only vis name, not entire path #Assume first target will be in the visname later (relevant for writing outliers.txt at beginning of pipeline) if '.ms' in visbase: from casatools import msmetadata msmd = msmetadata() msmd.open(vis) basename = visbase.replace( '.ms', '.{0}'.format( msmd.namesforfields(msmd.fieldsforintent('TARGET'))[0])) msmd.done() else: basename = visbase.replace('.mms', '') imbase = basename + '_im_%d' # Images will be produced in $CWD imagename = imbase % loop outimage = imagename + '.image' pixmask = imagename + ".pixmask" maskfile = imagename + ".islmask" rmsfile = imagename + ".rms" caltable = basename + '.gcal%d' % loop prev_caltables = sorted(glob.glob('*.gcal?')) cfcache = basename + '.cf' thresh = 10 if nterms[loop] > 1 and deconvolver[loop] == 'mtmfs': outimage += '.tt0' if step not in ['tclean', 'sky'] and not os.path.exists(outimage): logger.error( "Image '{0}' doesn't exist, so self-calibration loop {1} failed. Will terminate selfcal process." .format(outimage, loop)) sys.exit(1) if step in ['tclean', 'predict']: pixmask = imbase % (loop - 1) + '.pixmask' rmsfile = imbase % (loop - 1) + '.rms' if step in ['tclean', 'predict', 'sky' ] and ((loop == 0 and not os.path.exists(pixmask)) or (0 < loop < nloops and calmode[loop] == '')): pixmask = '' #Check no missing caltables for i in range(0, loop): if calmode[i] != '' and not os.path.exists(basename + '.gcal%d' % i): logger.error( "Calibration table '{0}' doesn't exist, so self-calibration loop {1} failed. Will terminate selfcal process." .format(basename + '.gcal%d' % i, i)) sys.exit(1) for i in range(discard_nloops): prev_caltables.pop(0) if outlier_threshold != '' and outlier_threshold != 0: # and (loop > 0 or step in ['sky','bdsf'] and loop == 0): if step in ['tclean', 'predict', 'sky']: outlierfile = 'outliers_loop{0}.txt'.format(loop) else: outlierfile = 'outliers_loop{0}.txt'.format(loop + 1) else: outlierfile = '' if not (type(threshold[loop]) is str and 'Jy' in threshold[loop]) and threshold[loop] > 1: if step in ['tclean', 'predict']: if os.path.exists(rmsfile): from casatasks import imstat stats = imstat(imagename=rmsfile) threshold[loop] *= stats['min'][0] else: logger.error( "'{0}' doesn't exist. Can't do thresholding at S/N > {1}. Loop 0 must use an absolute threshold value. Check the logs to see why RMS map not created." .format(rmsfile, threshold[loop])) sys.exit(1) elif step == 'bdsf': thresh = threshold[loop] return imbase, imagename, outimage, pixmask, rmsfile, caltable, prev_caltables, threshold, outlierfile, cfcache, thresh, maskfile