找到数据中每个星系之间的最近距离,并根据它们之间的最近距离创建对

Find the closest distance between every galaxy in the data and create pairs based on closest distance between them

我的任务是将大量星系中距离最近的星系配对。我有每个的 RA、DEC 和 Z,还有一个公式可以根据给定的数据计算出每个之间的距离。但是,我无法找到一种有效的方法来遍历整个列表以找到列表中每个星系与每个其他星系之间的距离,然后将每个星系与其最近的邻居进行匹配。

已通过以下方式导入数据:

    hdulist = fits.open("documents/RADECMASSmatch.fits")
    CATAID = data['CATAID_1']
    Xpos_DEIMOS_1 = data['Xpos_DEIMOS_1']
    z = data['Z_1']
    RA = data['RA']
    DEC = data['DEC']

我试过类似的方法:

    radiff = []
    for i in range(0,n):
        for j in range(i+1,n):
            radiff.append(abs(RA[i]-RA[j]))

初步计算出每个星系之间赤经和赤纬的差异,这确实有效,但我觉得必须有更好的方法。

一位朋友提出了类似的建议:

    galaxy_coords = (data['RA'],data['DEC'],data['Z])
    separation_matrix = np.zeros((len(galaxy_coords),len(galaxy_coords))

    done = []
    for i, coords1 in enumerate(galaxy_coords):
          for j, coords2 in enumerate(galaxy_coords):
                if (j,i) in done:
                    separation_matrix[i,j] += separation matrix[j,i]
                    continue
                    separation = your_formula(coords1, coords2)
                    separation_matrix[i,j] += separation
                    done.append((i,j))

但我不是很理解这个所以不能轻易应用它。我试过了,但没有任何用处。

如有任何帮助,我们将不胜感激,谢谢

您朋友的代码似乎生成了每对之间距离的二维数组,并利用了对称性 (distance(x,y) = distance(y,x))。如果它使用 itertools 生成组合,并在同一迭代中将 your_formula(coords1, coords2) 分配给 separation_matrix[i,j]separation_matrix[j,i],而不是对 i、j 和 j 分别进行迭代,那会稍微好一些,一.

更好的可能是这个使用基于树的算法的包:https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.KDTree.html。它似乎专注于直线坐标,但在线性时间内应该是可寻址的。