Skip to content

Instantly share code, notes, and snippets.

@taldcroft
Last active September 27, 2015 06:38
Show Gist options
  • Save taldcroft/1227280 to your computer and use it in GitHub Desktop.
Save taldcroft/1227280 to your computer and use it in GitHub Desktop.
Find quasar pairs using KDTree
import scipy.spatial
from astropy.table import Table
dat = Table.read('sources_ra_dec_z.fits')
ok = ((dat['z'] > 0)
& (dat['ra'] > 150.0) & (dat['ra'] < 151.0)
& (dat['dec'] > 2.0) & (dat['dec'] < 3.0))
datok = dat[ok]
# hist(dat['z'][ok], bins=50)
x = np.array([datok['ra'], datok['dec']]).transpose()
kdt = scipy.spatial.KDTree(x)
print 'running query_pairs'
radec_pairs = kdt.query_pairs(dist)
pairs = []
zs = datok['z']
for i1, i2 in radec_pairs:
if np.abs(zs[i1] - zs[i2]) < 0.2:
pairs.append((i1, i2))
print len(pairs) # 104192
@embray
Copy link

embray commented Oct 8, 2014

Oh nice, I didn't even know about scipy.spatial.KDTree. Laughing now at all the horrible hacks I've seen various people write to do this kind of search (I knew there were better ways but I didn't know of any specific implementations that were convenient to use in Python).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment