def star_plot(ax, targ_ra, targ_dec, data, iso, g_radius, nbhd): """Star bin plot""" filter = filters.star_filter(survey, data) iso_filter = simple_utils.cut_isochrone_path(data[mag_dered_1], data[mag_dered_2], data[mag_err_1], data[mag_err_2], iso, radius=0.1, return_all=False) # projection of image proj = ugali.utils.projector.Projector(targ_ra, targ_dec) x, y = proj.sphereToImage(data[filter & iso_filter][basis_1], data[filter & iso_filter][basis_2]) ax.scatter(x, y, edgecolor='none', s=3, c='black') ax.set_xlim(0.2, -0.2) ax.set_ylim(-0.2, 0.2) #ax.gca().set_aspect('equal') ax.set_xlabel(r'$\Delta \alpha$ (deg)') ax.set_ylabel(r'$\Delta \delta$ (deg)') ax.set_title('Stars')
def cm_plot(targ_ra, targ_dec, data, iso, g_radius, nbhd, type): """Color-magnitude plot""" angsep = ugali.utils.projector.angsep(targ_ra, targ_dec, data[basis_1], data[basis_2]) annulus = (angsep > g_radius) & (angsep < 1.) if type == 'stars': filter = filters.star_filter(survey, data) plt.title('Stellar Color-Magnitude') elif type == 'galaxies': filter = filters.galaxy_filter(survey, data) plt.title('Galactic Color-Magnitude') iso_filter = simple_utils.cut_isochrone_path(data[mag_dered_1], data[mag_dered_2], data[mag_err_1], data[mag_err_2], iso, radius=0.1, return_all=False) # Plot background objects plt.scatter(data[mag_dered_1][filter & annulus] - data[mag_dered_2][filter & annulus], data[mag_dered_1][filter & annulus], c='k', alpha=0.1, edgecolor='none', s=1) # Plot isochrone ugali.utils.plotting.drawIsochrone(iso, lw=2, label='{} Gyr, z = {}'.format( iso.age, iso.metallicity)) # Plot objects in nbhd plt.scatter(data[mag_dered_1][filter & nbhd] - data[mag_dered_2][filter & nbhd], data[mag_dered_2][filter & nbhd], c='g', s=5, label='r < {:.3f}$^\circ$'.format(g_radius)) # Plot objects in nbhd and near isochrone plt.scatter(data[mag_dered_1][filter & nbhd & iso_filter] - data[mag_dered_2][filter & nbhd & iso_filter], data[mag_dered_1][filter & nbhd & iso_filter], c='r', s=5, label='$\Delta$CM < 0.1') plt.axis([-0.5, 1, 16, mag_max]) plt.gca().invert_yaxis() plt.gca().set_aspect(1. / 4.) plt.legend(loc='upper left') plt.xlabel('{} - {} (mag)'.format(band_1.lower(), band_2.lower())) plt.ylabel('{} (mag)'.format(band_1.lower()))
def density_plot(targ_ra, targ_dec, data, iso, g_radius, nbhd, type): """Stellar density plot""" if type == 'stars': filter = filters.star_filter(survey, data) plt.title('Stellar Density') elif type == 'galaxies': filter = filters.galaxy_filter(survey, data) plt.title('Galactic Density') elif type == 'blue_stars': filter = filters.color_filter(survey, data) \ & filters.star_filter(survey, data) plt.title('Blue Stellar Density') iso_filter = simple_utils.cut_isochrone_path(data[mag_dered_1], data[mag_dered_2], data[mag_err_1], data[mag_err_2], iso, radius=0.1, return_all=False) # projection of image proj = ugali.utils.projector.Projector(targ_ra, targ_dec) x, y = proj.sphereToImage(data[filter & iso_filter][basis_1], data[filter & iso_filter][basis_2]) bound = 0.5 #1. steps = 100. bins = np.linspace(-bound, bound, steps) signal = np.histogram2d(x, y, bins=[bins, bins])[0] sigma = 0.01 * (0.25 * np.arctan(0.25 * g_radius * 60. - 1.5) + 1.3) convolution = scipy.ndimage.filters.gaussian_filter( signal, sigma / (bound / steps)) plt.pcolormesh(bins, bins, convolution.T, cmap='Greys') plt.xlim(bound, -bound) plt.ylim(-bound, bound) plt.gca().set_aspect('equal') plt.xlabel(r'$\Delta \alpha$ (deg)') plt.ylabel(r'$\Delta \delta$ (deg)') ax = plt.gca() divider = make_axes_locatable(ax) cax = divider.append_axes('right', size='5%', pad=0) plt.colorbar(cax=cax)
def analysis(targ_ra, targ_dec, mod, mc_source_id): """Analyze a candidate""" pix_nside_select = ugali.utils.healpix.angToPix(nside, targ_ra, targ_dec) ra_select, dec_select = ugali.utils.healpix.pixToAng( nside, pix_nside_select) pix_nside_neighbors = np.concatenate([[pix_nside_select], healpy.get_all_neighbours( nside, pix_nside_select)]) # Construct data #data = simple_utils.construct_modal_data(mode, pix_nside_neighbors) data = simple_utils.construct_real_data(pix_nside_neighbors) if (mode == 0): print('mode = 0: running only on real data') elif (mode == 1): print('mode = 1: running on real data and simulated data') # inject objects for simulated object of mc_source_id sim_data = simple_utils.construct_sim_data(pix_nside_neighbors, mc_source_id) data = simple_utils.inject_sim(data, sim_data, mc_source_id) else: print('No mode specified; running only on real data') print('Loading data...') data = simple_utils.construct_modal_data(mode, pix_nside_neighbors, mc_source_id) quality_cut = filters.quality_filter(survey, data) data = data[quality_cut] print('Found {} objects...').format(len(data)) data = filters.dered_mag(survey, data) # This should be generalized to also take the survey iso = isochrone_factory(name=isoname, survey=isosurvey, age=12, z=0.0001, distance_modulus=mod, band_1=band_1.lower(), band_2=band_2.lower()) # g_radius estimate filter = filters.star_filter(survey, data) iso_filter = simple_utils.cut_isochrone_path(data[mag_dered_1], data[mag_dered_2], data[mag_err_1], data[mag_err_2], iso, radius=0.1, return_all=False) angsep = ugali.utils.projector.angsep(targ_ra, targ_dec, data[basis_1], data[basis_2]) bins = np.linspace(0, 0.4, 21) # deg centers = 0.5 * (bins[1:] + bins[0:-1]) area = np.pi * (bins[1:]**2 - bins[0:-1]**2) * 60**2 hist = np.histogram(angsep[(angsep < 0.4) & filter & iso_filter], bins=bins)[0] # counts f_interp = interpolate.interp1d( np.linspace(centers[0], centers[-1], len(hist)), hist / area, 'cubic') f_range = np.linspace(centers[0], centers[-1], 1000) f_val = f_interp(f_range) pairs = zip(f_range, f_val) peak = max(pairs[:len(pairs) / 4], key=lambda x: x[1]) # find peak within first quarter def peak_index(pairs, peak): for i in range(len(pairs)): if pairs[i] == peak: return i osc = int( 0.04 / 0.4 * 1000) # +/- 0.04 (rounded down) deg oscillation about local extremum relmin = argrelextrema(f_val, np.less, order=osc)[0] try: if len(relmin) > 0: #half_point = f_range[relmin[0]] i = 0 while ((f_range[relmin[i]] <= f_range[peak_index(pairs, peak)]) & (i <= len(relmin) - 1)): i += 1 half_point = f_range[relmin[i]] elif len(relmin) == 0: half_peak = ( peak[1] + np.mean(f_val[len(f_val) / 4:]) ) / 2. # normalized to background (after first quarter) #half_peak = np.mean(f_val[len(f_val)/4:]) half_pairs = [] for i in pairs[peak_index(pairs, peak):len(pairs) / 2]: # start after peak, stay within first quarter if i != peak: half_pairs.append((i[0], abs(i[1] - half_peak))) half_point = min(half_pairs, key=lambda x: x[1])[0] # deg except: half_point = 0.1 # fixed value to catch errors g_min = 0.5 / 60. # deg g_max = 12. / 60. # deg if half_point < g_min: g_radius = g_min elif half_point > g_max: g_radius = g_max else: g_radius = half_point # deg angsep = ugali.utils.projector.angsep(targ_ra, targ_dec, data[basis_1], data[basis_2]) nbhd = (angsep < g_radius) return (data, iso, g_radius, nbhd)
def radial_plot(ax, targ_ra, targ_dec, data, iso, g_radius, nbhd, field_density=None): """Radial distribution plot""" ## Deprecated? #filter_s = filters.star_filter(survey, data) #filter_g = filters.galaxy_filter(survey, data) ax.title('Radial Distribution') angsep = ugali.utils.projector.angsep(targ_ra, targ_dec, data[basis_1], data[basis_2]) # Isochrone filtered/unfiltered iso_seln_f = simple_utils.cut_isochrone_path(data[mag_dered_1], data[mag_dered_2], data[mag_err_1], data[mag_err_2], iso, radius=0.1, return_all=False) iso_seln_u = ~iso_seln_f bins = np.linspace(0, 0.4, 21) # deg centers = 0.5 * (bins[1:] + bins[0:-1]) area = np.pi * (bins[1:]**2 - bins[0:-1]**2) * 60**2 def interp_values(type, seln): if type == 'stars': filter = filters.star_filter(survey, data) elif type == 'galaxies': filter = filters.galaxy_filter(survey, data) if seln == 'f': iso_filter = iso_seln_f elif seln == 'u': iso_filter = iso_seln_u hist = np.histogram(angsep[(angsep < 0.4) & filter & iso_filter], bins=bins)[0] # counts f_interp = interpolate.interp1d( np.linspace(centers[0], centers[-1], len(hist)), hist / area, 'cubic') f_range = np.linspace(centers[0], centers[-1], 1000) f_val = f_interp(f_range) return (f_range, f_val) def value_errors(type, seln): if type == 'stars': filter = filters.star_filter(survey, data) elif type == 'galaxies': filter = filters.galaxy_filter(survey, data) if seln == 'f': iso_filter = iso_seln_f elif seln == 'u': iso_filter = iso_seln_u hist = np.histogram(angsep[(angsep < 0.4) & filter & iso_filter], bins=bins)[0] # counts val = hist / area yerr = np.sqrt(hist) / area return (val, yerr) f_range, f_val = interp_values('stars', 'f') pairs = zip(f_range, f_val) peak = max(pairs[:len(pairs) / 4], key=lambda x: x[1]) # find peak within first quarter def peak_index(pairs, peak): for i in range(len(pairs)): if pairs[i] == peak: return i ax.axvline(x=f_range[peak_index(pairs, peak)], color='m', label='peak') ax.axvline(x=g_radius, color='r', label='g_radius') f_range, f_val = interp_values('galaxies', 'f') ax.plot(f_range, f_val, '-g', label='Filtered Galaxies') f_range, f_val = interp_values('stars', 'u') ax.plot(f_range, f_val, '-k', alpha=0.25, label='Unfiltered Stars') val, y_err = value_errors('stars', 'f') ax.plot(centers, val, '.b') ax.errorbar(centers, val, yerr=y_err, fmt='none', ecolor='b', elinewidth=1, capsize=5) f_range, f_val = interp_values('stars', 'f') ax.plot(f_range, f_val, '-b', label='Filtered Stars') if field_density: ax.axhline(y=field_density, color='blue', ls='--', label='Model Field') ymax = plt.ylim()[1] ax.annotate(r'$\approx %0.1f$' + str(round(g_radius, 3)) + '$^\circ$', (g_radius * 1.1, ymax / 50.), color='red', bbox=dict(boxstyle='round,pad=0.0', fc='white', alpha=0.75, ec='white', lw=0)) ax.legend(loc='upper right') #ax.set_xlim(bins[0], bins[-1]) #ax.set_ylim(0., ymax) ax.set_xscale('log') ax.set_yscale('log') ax.set_xlabel('Angular Separation (deg)') ax.set_ylabel('Denisty (arcmin$^{-2})$')