def hist(region, bands, shapes): colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] filename = glob( './cat/mastercat_region{}*_photometered.dat'.format(region))[0] t = Table(Table.read(filename, format='ascii'), masked=True) t = filter_masked(t, shapes) # NOT GENERALIZED nu3, ppbeam3 = grabfileinfo(region, bands[0])[-2:] nu6, ppbeam6 = grabfileinfo(region, bands[1])[-2:] flux_array = np.ndarray( (len(bands), len(shapes), len(t)) ) # bands are across rows, shapes are across columns, sources are across depth for i in range(len(bands)): for j in range(len(shapes)): flux_array[i][j] = np.array(t['{}_flux_band{}'.format( shapes[j], bands[i])]) fig = plt.figure(figsize=(12, 6)) for l, shape in enumerate(shapes): ax = fig.add_subplot(1, 2, l + 1) print(nu3, nu6, flux_array[0][l][0] / flux_array[1][l][0], specindex(nu3, nu6, flux_array[0][l][0], flux_array[1][l][0])) n, bins, patches = ax.hist(specindex(nu3, nu6, flux_array[0][l], flux_array[1][l]), bins=np.linspace(1, 5, 10), color=colors[0]) ax.set_title('{} Apertures'.format(namedict[shapes[l]])) ax.set_xlabel('Alpha') ax.set_xlim([1, 5]) #ax.set_xscale('log') ax.set_ylabel('n') plt.suptitle('Alpha Parameter For Sources In Region {} In Bands {}'.format( region, bands)) plt.subplots_adjust(top=0.887, bottom=0.088, left=0.052, right=0.985, hspace=0.234, wspace=0.129)
def probe(region, band, min_values, min_deltas, min_npixs): infilename = grabfileinfo(region, band)[0] print("Min values: ", min_values) print("Min deltas: ", min_deltas) print("Min npix: ", min_npixs, '\n') print('Total task progress:') pb = ProgressBar(len(min_values) * len(min_npixs)) print() for i in range(len(min_values)): for j in range(len(min_npixs)): print("\nMin value: {:.8g} Min delta: {:.8g} Min npix: {}". format(min_values[i], min_deltas[i], min_npixs[j])) detect(infilename, region, band, min_value=min_values[i], min_delta=min_deltas[i], min_npix=min_npixs[j], verbose=True) print('Total task progress:') pb.update()
# Save the catalog with new columns for SNR catalog.add_column(Column(snr_vals), name='snr_band' + band) catalog.add_column(np.invert(catalog.mask['snr_band' + band]).astype(int), name='detected_band' + band) catalog.add_column(Column(rejects), name='rejected') catalog.write('./cat/cat_' + outfile + '_filtered.dat', format='ascii') if __name__ == '__main__': parser = argparse.ArgumentParser( description='Reject sources below some SNR threshold') parser.add_argument( 'region', metavar='region', type=str, help='name of the region as listed in "imgfileinfo.dat"') parser.add_argument( 'band', metavar='band', type=int, help='integer representing the ALMA band of observation') args = parser.parse_args() region = str(args.region) band = args.band imfile = grabfileinfo(region, band)[0] catfile = grabcatname(region, band) reject(imfile, catfile, 6.)
parser = argparse.ArgumentParser( description='Detect sources in an image file using dendrograms') parser.add_argument( 'region', metavar='region', type=str, help='name of the region as listed in "imgfileinfo.dat"') parser.add_argument( 'band', metavar='band', type=int, help='integer representing the ALMA band of observation') args = parser.parse_args() region = str(args.region) band = args.band infilename, _, _, min_value, delta_fraction, min_npix, _, _ = grabfileinfo( region, band) print("Min value: ", min_value) print("Min delta: {:.5g}".format(min_value * delta_fraction)) print("Min npix: ", min_npix, '\n') detect(infilename, region, band, min_value=min_value, min_delta=min_value * delta_fraction, min_npix=min_npix, verbose=True)
def flux(region): # import the catalog file, get names of bands filename = glob('./cat/mastercat_region{}*'.format(region))[0] catalog = Table(Table.read(filename, format='ascii'), masked=True) catalog.sort('_idx') bands = np.array(filename.split('bands_')[1].split('.dat')[0].split('_'), dtype=int) n_bands = len(bands) n_rows = len(catalog) ellipse_npix_col = MaskedColumn(length=len(catalog), name='ellipse_npix', mask=True) circ1_npix_col = MaskedColumn(length=len(catalog), name='circ1_npix', mask=True) circ2_npix_col = MaskedColumn(length=len(catalog), name='circ2_npix', mask=True) circ3_npix_col = MaskedColumn(length=len(catalog), name='circ3_npix', mask=True) n_rejected = 0 for i in range(n_bands): band = bands[i] # Load image file for this band print("\nLoading image file for region {} in band {} (Image {}/{})". format(region, band, i + 1, n_bands)) imfile = grabfileinfo(region, band)[0] contfile = fits.open(imfile) data = contfile[0].data.squeeze() # Set up wcs, beam, and pixel scale for this image mywcs = wcs.WCS(contfile[0].header).celestial beam = radio_beam.Beam.from_fits_header(contfile[0].header) pixel_scale = np.abs( mywcs.pixel_scale_matrix.diagonal().prod())**0.5 * u.deg ppbeam = (beam.sr / (pixel_scale**2)).decompose().value print('ppbeam: ', ppbeam) data = data / ppbeam # Set up columns for each aperture peak_flux_col = MaskedColumn(length=len(catalog), name='peak_flux_band{}'.format(band), mask=True) annulus_median_col = MaskedColumn( length=len(catalog), name='annulus_median_band{}'.format(band), mask=True) annulus_rms_col = MaskedColumn(length=len(catalog), name='annulus_rms_band{}'.format(band), mask=True) ellipse_flux_col = MaskedColumn( length=len(catalog), name='ellipse_flux_band{}'.format(band), mask=True) circ1_flux_col = MaskedColumn(length=len(catalog), name='circ1_flux_band{}'.format(band), mask=True) circ2_flux_col = MaskedColumn(length=len(catalog), name='circ2_flux_band{}'.format(band), mask=True) circ3_flux_col = MaskedColumn(length=len(catalog), name='circ3_flux_band{}'.format(band), mask=True) ellipse_rms_col = MaskedColumn(length=len(catalog), name='ellipse_rms_band{}'.format(band), mask=True) circ1_rms_col = MaskedColumn(length=len(catalog), name='circ1_rms_band{}'.format(band), mask=True) circ2_rms_col = MaskedColumn(length=len(catalog), name='circ2_rms_band{}'.format(band), mask=True) circ3_rms_col = MaskedColumn(length=len(catalog), name='circ3_rms_band{}'.format(band), mask=True) circ1_r, circ2_r, circ3_r = 5e-6 * u.deg, 1e-5 * u.deg, 1.5e-5 * u.deg print('Photometering sources') pb = ProgressBar(len(catalog[np.where(catalog['rejected'] == 0)])) masks = [] datacube = [] rejects = [] snr_vals = [] names = [] # Iterate over sources, extracting ellipse parameters for j in range(n_rows): if catalog['rejected'][j] == 1: continue source = catalog[j] x_cen = source['x_cen'] * u.deg y_cen = source['y_cen'] * u.deg major = source['major_fwhm'] * u.deg minor = source['minor_fwhm'] * u.deg pa = source['position_angle'] * u.deg annulus_width = 1e-5 * u.deg center_distance = 1e-5 * u.deg # Convert to pixel coordinates position = coordinates.SkyCoord(x_cen, y_cen, frame='icrs', unit=(u.deg, u.deg)) pix_position = np.array(position.to_pixel(mywcs)) pix_major = major / pixel_scale pix_minor = minor / pixel_scale # Create cutout size = np.max([ circ3_r.value, major.value + center_distance.value + annulus_width.value ]) * 2.2 * u.deg try: cutout = Cutout2D(data, position, size, mywcs, mode='partial') except NoOverlapError: catalog['rejected'][j] = 1 pb.update() continue cutout_center = regions.PixCoord(cutout.center_cutout[0], cutout.center_cutout[1]) datacube.append(cutout.data) # create all aperture shapes ellipse_reg = regions.EllipsePixelRegion(cutout_center, pix_major * 2., pix_minor * 2., angle=pa) circ1_reg = regions.CirclePixelRegion(cutout_center, circ1_r / pixel_scale) circ2_reg = regions.CirclePixelRegion(cutout_center, circ2_r / pixel_scale) circ3_reg = regions.CirclePixelRegion(cutout_center, circ3_r / pixel_scale) innerann_reg = regions.CirclePixelRegion( cutout_center, center_distance / pixel_scale + pix_major) outerann_reg = regions.CirclePixelRegion( cutout_center, center_distance / pixel_scale + pix_major + annulus_width / pixel_scale) annulus_mask = mask(outerann_reg, cutout) - mask( innerann_reg, cutout) # get flux information from regions ellipse_flux, ellipse_rms, peak_flux, ellipse_mask, ellipse_npix = apsum( ellipse_reg, cutout) circ1_flux, circ1_rms, _, circ1_mask, circ1_npix = apsum( circ1_reg, cutout) circ2_flux, circ2_rms, _, circ2_mask, circ2_npix = apsum( circ2_reg, cutout) circ3_flux, circ3_rms, _, circ3_mask, circ3_npix = apsum( circ3_reg, cutout) annulus_rms = rms(cutout.data[annulus_mask.astype('bool')]) annulus_median = np.median( cutout.data[annulus_mask.astype('bool')]) # Add grid plot mask to list masklist = [ ellipse_mask, annulus_mask, circ1_mask, circ2_mask, circ3_mask ] masks.append(masklist) # add fluxes to appropriate columns peak_flux_col[j] = peak_flux ellipse_flux_col[j], ellipse_rms_col[j] = ellipse_flux, ellipse_rms circ1_flux_col[j], circ1_rms_col[j] = circ1_flux, circ1_rms circ2_flux_col[j], circ2_rms_col[j] = circ2_flux, circ2_rms circ3_flux_col[j], circ3_rms_col[j] = circ3_flux, circ3_rms ellipse_npix_col[j] = ellipse_npix circ1_npix_col[j] = circ1_npix circ2_npix_col[j] = circ2_npix circ3_npix_col[j] = circ3_npix annulus_median_col[j] = annulus_median annulus_rms_col[j] = annulus_rms catalog['snr_band' + str(band)][j] = peak_flux / annulus_rms snr_vals.append(peak_flux / annulus_rms) names.append(catalog['_idx'][j]) # Secondary rejection rejected = 0 lowest_flux = np.min( [ellipse_flux, circ1_flux, circ2_flux, circ3_flux]) #if lowest_flux <= annulus_median*ellipse_npix or lowest_flux < 0: if lowest_flux < 0: catalog['rejected'][j] = 1 n_rejected += 1 rejected = 1 rejects.append(rejected) pb.update() # Plot the grid of sources plot_grid(datacube, masks, rejects, snr_vals, names) plt.suptitle('region={}, band={}'.format(region, band)) plt.show(block=False) # add columns to catalog catalog.add_columns([ peak_flux_col, ellipse_flux_col, ellipse_rms_col, circ1_flux_col, circ1_rms_col, circ2_flux_col, circ2_rms_col, circ3_flux_col, circ3_rms_col, ]) catalog.add_columns([ ellipse_npix_col, circ1_npix_col, circ2_npix_col, circ3_npix_col, annulus_median_col, annulus_rms_col ]) print("\n{} sources flagged for secondary rejection".format(n_rejected)) # save catalog catalog = catalog[sorted(catalog.colnames)] catalog.write(filename.split('.dat')[0] + '_photometered.dat', format='ascii') print("\nMaster catalog saved as '{}'".format( filename.split('.dat')[0] + '_photometered.dat'))
def ffplot(region, shapes, band1, band2, log=True, label=False, grid=True, peak=False): """Make a flux v. flux plot. Parameters ---------- region : str The region identifier for the images shapes : list (dtype=str) A list of aperture shapes to compare band1 : int The ALMA band of observation whose flux will be on the x-axis band2 : int The ALMA band of observation whose flux will be on the y-axis Keyword Arguments ----------- log : bool (default True) Display the plot on a log scale label : bool (default False) Draw ID labels for each source grid : bool (default True) Display a separate plot for each aperture peak : bool (default False) Use peak flux within ellipse instead of any aperture sums """ colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] filename = glob( './cat/mastercat_region{}*_photometered.dat'.format(region))[0] t = Table(Table.read(filename, format='ascii'), masked=True) # get nu1, nu2, ppbeam1, ppbeam2 from imgfileinfo.dat nu1, ppbeam1 = grabfileinfo(region, band1)[-2:] nu2, ppbeam2 = grabfileinfo(region, band2)[-2:] # Filter to rows where all values the user needs for plotting are unmasked t = filter_masked(t, shapes) # Grab the flux data flux_band1 = [] flux_band2 = [] npix = [] if peak: flux_band1.append(t['peak_flux_band' + str(band1)]) flux_band2.append(t['peak_flux_band' + str(band2)]) else: for shape in shapes: flux_band1.append(t[shape + '_flux_band' + str(band1)]) flux_band2.append(t[shape + '_flux_band' + str(band2)]) npix.append(t[shape + '_npix']) marker_labels = t['_idx'] specindex_xflux = np.linspace(np.min(flux_band1), np.max(flux_band1), 10) #specindex0_yflux = specindex(nu1, nu2, specindex_xflux, 0.5) specindex1_yflux = specindex(nu1, nu2, specindex_xflux, 1) specindex2_yflux = specindex(nu1, nu2, specindex_xflux, 2) specindex3_yflux = specindex(nu1, nu2, specindex_xflux, 3) # ------ PLOTTING ------ needs to be cleaned up if grid: n_images = len(shapes) xplots = int(np.around(np.sqrt(n_images))) yplots = xplots fig, axes = plt.subplots(ncols=yplots, nrows=xplots, figsize=(12, 12)) for i in range(len(shapes)): ax = np.ndarray.flatten(axes)[i] ax.errorbar(flux_band1[i], flux_band2[i], xerr=flux_band1[i] / np.sqrt(npix[i] / ppbeam1), yerr=flux_band2[i] / np.sqrt(npix[i] / ppbeam2), fmt='o', ms=2, alpha=0.75, elinewidth=0.5, color=colors[i], label='{} Aperture Sums'.format(namedict[shapes[i]])) #ax.plot(specindex_xflux, specindex0_yflux, '--', color='pink', label='Spectral Index = 0.5') ax.plot(specindex_xflux, specindex1_yflux, '--', color='m', label='Spectral Index = 1') ax.plot(specindex_xflux, specindex2_yflux, '--', color='purple', label='Spectral Index = 2') ax.plot(specindex_xflux, specindex3_yflux, '--', color='k', label='Spectral Index = 3') ax.set_xticks([]) ax.set_yticks([]) ax.set_xlim([.6 * np.min(flux_band1), 1.4 * np.max(flux_band1)]) ax.set_ylim([.1 * np.min(flux_band2), 1.9 * np.max(flux_band2)]) if label: for j, label in enumerate(marker_labels): ax.annotate(label, (flux_band1[i][j], flux_band2[i][j]), size=8) if log: ax.set_xlabel('Log Band {} Flux'.format(band1)) ax.set_ylabel('Log Band {} Flux'.format(band2)) ax.set_xscale('log') ax.set_yscale('log') else: ax.set_xlabel('Band {} Flux'.format(band1)) ax.set_ylabel('Band {} Flux'.format(band2)) ax.legend() else: plt.figure() if peak: plt.errorbar(flux_band1, flux_band2, fmt='o', ms=2, alpha=0.75, elinewidth=0.5, label='Peak Flux') else: for i in range(len(shapes)): plt.errorbar(flux_band1[i], flux_band2[i], xerr=flux_band1[i] / np.sqrt(npix[i] / ppbeam1), yerr=flux_band2[i] / np.sqrt(npix[i] / ppbeam2), fmt='o', ms=2, alpha=0.75, elinewidth=0.5, label='{} Aperture Sums'.format( namedict[shapes[i]])) plt.plot(specindex_xflux, specindex1_yflux, '--', color='m', label='Spectral Index = 1') plt.plot(specindex_xflux, specindex2_yflux, '--', color='purple', label='Spectral Index = 2') plt.plot(specindex_xflux, specindex3_yflux, '--', color='k', label='Spectral Index = 3') plt.xlim([.6 * np.min(flux_band1), 1.4 * np.max(flux_band1)]) plt.ylim([.1 * np.min(flux_band2), 1.9 * np.max(flux_band2)]) if label: for i in range(len(shapes)): for j, label in enumerate(marker_labels): plt.annotate(label, (flux_band1[i][j], flux_band2[i][j]), size=8) if peak: if log: plt.xlabel('Log Band {} Peak Flux'.format(band1)) plt.ylabel('Log Band {} Peak Flux'.format(band2)) plt.xscale('log') plt.yscale('log') else: plt.xlabel('Band {} Peak Flux'.format(band1)) plt.ylabel('Band {} Peak Flux'.format(band2)) else: if log: plt.xlabel('Log Band {} Flux'.format(band1)) plt.ylabel('Log Band {} Flux'.format(band2)) plt.xscale('log') plt.yscale('log') else: plt.xlabel('Band {} Flux'.format(band1)) plt.ylabel('Band {} Flux'.format(band2)) plt.legend() plt.suptitle('Flux v. Flux For Region {} In Bands {} and {}'.format( region, band1, band2))