def _matching( master, cat, masteridskey=None, angular=False, radius=1.5, masked=False ): """ Function to match stars between frames. """ if masteridskey is None: masterids = np.arange(len(master)) master["masterindex"] = masterids idkey = "masterindex" else: idkey = masteridskey if angular: masterRaDec = np.empty((len(master), 2), dtype=np.float64) try: masterRaDec[:, 0] = master["RA"] masterRaDec[:, 1] = master["Dec"] except KeyError: masterRaDec[:, 0] = master["ra"] masterRaDec[:, 1] = master["dec"] imRaDec = np.empty((len(cat), 2), dtype=np.float64) try: imRaDec[:, 0] = cat["RA"] imRaDec[:, 1] = cat["Dec"] except KeyError: imRaDec[:, 0] = cat["ra"] imRaDec[:, 1] = cat["dec"] radius2 = radius / 3600.0 dist, ind = cx.crossmatch_angular( masterRaDec, imRaDec, max_distance=radius2 / 2.0 ) dist_, ind_ = cx.crossmatch_angular( imRaDec, masterRaDec, max_distance=radius2 / 2.0 ) else: masterXY = np.empty((len(master), 2), dtype=np.float64) masterXY[:, 0] = master["x"] masterXY[:, 1] = master["y"] imXY = np.empty((len(cat), 2), dtype=np.float64) imXY[:, 0] = cat["x"] imXY[:, 1] = cat["y"] dist, ind = cx.crossmatch(masterXY, imXY, max_distance=radius) dist_, ind_ = cx.crossmatch(imXY, masterXY, max_distance=radius) IDs = np.zeros_like(ind_) - 13133 for i in range(len(ind_)): if dist_[i] != np.inf: ind_o = ind_[i] if dist[ind_o] != np.inf: ind_s = ind[ind_o] if ind_s == i: IDs[i] = master[idkey][ind_o] if masked: mask = IDs > 0 return (IDs, mask) return IDs
def matching(master, cat, masteridskey=None, angular=False, radius=1.5, masked=False): """Function to match stars between frames. """ if masteridskey is None: masterids = np.arange(len(master)) master['masterindex'] = masterids idkey = 'masterindex' else: idkey = masteridskey if angular: masterRaDec = np.empty((len(master), 2), dtype=np.float64) try: masterRaDec[:, 0] = master['RA'] masterRaDec[:, 1] = master['Dec'] except KeyError: masterRaDec[:, 0] = master['ra'] masterRaDec[:, 1] = master['dec'] imRaDec = np.empty((len(cat), 2), dtype=np.float64) try: imRaDec[:, 0] = cat['RA'] imRaDec[:, 1] = cat['Dec'] except KeyError: imRaDec[:, 0] = cat['ra'] imRaDec[:, 1] = cat['dec'] radius2 = radius/3600. dist, ind = cx.crossmatch_angular(masterRaDec, imRaDec, max_distance=radius2/2.) dist_, ind_ = cx.crossmatch_angular(imRaDec, masterRaDec, max_distance=radius2/2.) else: masterXY = np.empty((len(master), 2), dtype=np.float64) masterXY[:, 0] = master['x'] masterXY[:, 1] = master['y'] imXY = np.empty((len(cat), 2), dtype=np.float64) imXY[:, 0] = cat['x'] imXY[:, 1] = cat['y'] dist, ind = cx.crossmatch(masterXY, imXY, max_distance=radius) dist_, ind_ = cx.crossmatch(imXY, masterXY, max_distance=radius) IDs = np.zeros_like(ind_) - 13133 for i in range(len(ind_)): if dist_[i] != np.inf: # dist_o = dist_[i] ind_o = ind_[i] if dist[ind_o] != np.inf: # dist_s = dist[ind_o] ind_s = ind[ind_o] if ind_s == i: IDs[i] = master[idkey][ind_o] # print((len(IDs), len(ind_), len(ind))) # print(("Matching result:: IDs > 0. => {}".format(sum(IDs > 0)))) if masked: mask = IDs > 0 return(IDs, mask) return(IDs)
def go_test(cat, image_data, thresh): threshold = thresh*bkg.globalrms #make the extraction sources = sep.extract(image_data, threshold) cat.to_pandas() sources = pd.DataFrame(sources) #crossmatch the cats S = np.array([cat['x'], cat['y']]).T O = np.array([sources['x'], sources['y']]).T #right distr, indr = cx.crossmatch(S, O, max_distance=0.3) matchsr = ~np.isinf(distr) #left distl, indl = cx.crossmatch(O, S, max_distance=0.3) matchsl = ~np.isinf(distl) ## objID = np.zeros_like(O[:,0]) -1 CSTARID = np.zeros_like(O[:,0]) -1 CSTARMAG = np.zeros_like(O[:,0]) -1 for i in range(len(O)): if distl[i] != np.inf: dist_o = distl[i] ind_o = indl[i] # now ind is a star number # lets see if that star has matched the same obj if distr[ind_o] != np.inf: dist_s = distr[ind_o] ind_s = indr[ind_o] if ind_s == i: objID[i] = ind_o CSTARID[i] = cat['cstarid'][ind_o] CSTARMAG[i] = cat['imag'][ind_o] sources['objID'] = objID sources['cstarid'] = CSTARID sources['threshold'] = np.repeat(thresh, len(sources)) sources['cstarmag'] = CSTARMAG #sources['was_a_hit'] = objID > 0. # report the hits as detections n_hits = sum(objID > 0.) print 'Number of hits ==> {}'.format(n_hits) # report the false detections n_false= sum(objID < 0.) print 'Number of falses ==> {}'.format(n_false) return sources
#---------------------------------------------------------------------- # cross-match # the imaging sample contains both standard and variable stars. We'll # perform a cross-match with the standard star catalog and choose objects # which are common to both. Xlocs = np.hstack((data_noisy['ra'][:, np.newaxis], data_noisy['dec'][:, np.newaxis])) Ylocs = np.hstack((data_stacked['RA'][:, np.newaxis], data_stacked['DEC'][:, np.newaxis])) print "number of noisy points: ", Xlocs.shape print "number of stacked points:", Ylocs.shape # find all points within 0.9 arcsec. This cutoff was selected # by plotting a histogram of the log(distances). dist, ind = crossmatch(Xlocs, Ylocs, max_distance=0.9 / 3600.) noisy_mask = (~np.isinf(dist)) stacked_mask = ind[noisy_mask] # select the data data_noisy = data_noisy[noisy_mask] X = X[noisy_mask] Xerr = Xerr[noisy_mask] data_stacked = data_stacked[stacked_mask] Y = Y[stacked_mask] Yerr = Yerr[stacked_mask] # double-check that our cross-match succeeded assert X.shape == Y.shape
#---------------------------------------------------------------------- # cross-match # the imaging sample contains both standard and variable stars. We'll # perform a cross-match with the standard star catalog and choose objects # which are common to both. Xlocs = np.hstack( (data_noisy['ra'][:, np.newaxis], data_noisy['dec'][:, np.newaxis])) Ylocs = np.hstack( (data_stacked['RA'][:, np.newaxis], data_stacked['DEC'][:, np.newaxis])) print("number of noisy points: ", Xlocs.shape) print("number of stacked points:", Ylocs.shape) # find all points within 0.9 arcsec. This cutoff was selected # by plotting a histogram of the log(distances). dist, ind = crossmatch(Xlocs, Ylocs, max_distance=0.9 / 3600) noisy_mask = (~np.isinf(dist)) stacked_mask = ind[noisy_mask] # select the data data_noisy = data_noisy[noisy_mask] X = X[noisy_mask] Xerr = Xerr[noisy_mask] data_stacked = data_stacked[stacked_mask] Y = Y[stacked_mask] Yerr = Yerr[stacked_mask] # double-check that our cross-match succeeded assert X.shape == Y.shape
# get imaging data image_data = fetch_imaging_sample() imX = np.empty((len(image_data), 2), dtype=np.float64) imX[:, 0] = image_data['ra'] imX[:, 1] = image_data['dec'] # get standard stars standards_data = fetch_sdss_S82standards() stX = np.empty((len(standards_data), 2), dtype=np.float64) stX[:, 0] = standards_data['RA'] stX[:, 1] = standards_data['DEC'] # crossmatch catalogs max_radius = 1. / 3600 # 1 arcsec dist, ind = crossmatch(imX, stX, max_radius) match = ~np.isinf(dist) dist_match = dist[match] dist_match *= 3600 ax = plt.axes() hist(dist_match, bins='knuth', ax=ax, histtype='stepfilled', ec='k', fc='#AAAAAA') ax.set_xlabel('radius of match (arcsec)') ax.set_ylabel('N(r, r+dr)') ax.text(0.95, 0.95, "Total objects: %i\nNumber with match: %i" % (imX.shape[0], np.sum(match)), ha='right', va='top', transform=ax.transAxes) ax.set_xlim(0, 0.2)
# get imaging data image_data = fetch_imaging_sample() imX = np.empty((len(image_data), 2), dtype=np.float64) imX[:, 0] = image_data['ra'] imX[:, 1] = image_data['dec'] # get standard stars standards_data = fetch_sdss_S82standards() stX = np.empty((len(standards_data), 2), dtype=np.float64) stX[:, 0] = standards_data['RA'] stX[:, 1] = standards_data['DEC'] # crossmatch catalogs max_radius = 1. / 3600 # 1 arcsec dist, ind = crossmatch(imX, stX, max_radius) match = ~np.isinf(dist) dist_match = dist[match] dist_match *= 3600 ax = plt.axes() hist(dist_match, bins='knuth', ax=ax, histtype='stepfilled', ec='k', fc='#AAAAAA') ax.set_xlabel('radius of match (arcsec)') ax.set_ylabel('N(r, r+dr)') ax.text(0.95,
def matching(detected, simulated, radius=1., masked=False, sep=False): """Function to match stars between frames. """ masterXY = np.empty((len(detected), 2), dtype=np.float64) master_idx = np.zeros(len(detected)) for i in range(len(detected)): if sep: masterXY[i, 0] = detected[i].x masterXY[i, 1] = detected[i].y else: masterXY[i, 0] = detected[i].X_IMAGE masterXY[i, 1] = detected[i].Y_IMAGE master_idx[i] = detected[i].id imXY = np.empty((len(simulated), 2), dtype=np.float64) for i in range(len(simulated)): imXY[i, 0] = simulated[i].x imXY[i, 1] = simulated[i].y try: dist, ind = cx.crossmatch(masterXY, imXY, max_distance=radius) dist_, ind_ = cx.crossmatch(imXY, masterXY, max_distance=radius) match = ~np.isinf(dist) match_ = ~np.isinf(dist_) except ValueError: print "masterXY.shape" print masterXY.shape print "\n\nimXY.shape" print imXY.shape print "\n" try: dist, ind = cx.crossmatch(masterXY, imXY, max_distance=radius*3) dist_, ind_ = cx.crossmatch(imXY, masterXY, max_distance=radius*3) match = ~np.isinf(dist) match_ = ~np.isinf(dist_) except: # import ipdb; ipdb.set_trace() print 'There were cross match exceptions' return np.zeros_like(imXY) - 13 IDs = np.zeros_like(ind_) - 13133 # IDs has length = len(imXY) = len(simulated) for i in xrange(len(ind_)): if dist_[i] != np.inf: dist_o = dist_[i] ind_o = ind_[i] if dist[ind_o] != np.inf: dist_s = dist[ind_o] ind_s = ind[ind_o] if ind_s == i: IDs[i] = master_idx[ind_o] # print master_idx[ind_o], ind_o print len(IDs), len(ind_), len(ind) if masked: mask = IDs > 0 return(IDs, mask) return(IDs)