def normalize_density(color, mag, density, n=5000): normDensity = np.zeros(shape=color.shape[0]) for IDx, (c, m, d) in tqdm(enumerate(zip(color, mag, density)), total=len(density)): distances = cdist(np.array([[c, m]]), np.array([color, mag]).T)[0] closestIDX= np.argpartition(distances, n) closestDensity = density[closestIDX[:n]] meanNearDensity = np.mean(closestDensity) normalizedDensity = d/meanNearDensity normDensity[IDx] = normalizedDensity return normDensity def normalize_density_magBin(color, mag, density, binSize=0.1): normDensity = np.zeros(shape=color.shape[0]) for IDx, (c, m, d) in tqdm(enumerate(zip(color, mag, density)), total=len(density)): cut = (mag > m-binSize/2) & (mag <= m+binSize/2) binDensity = density[cut] meanBinDensity = np.mean(binDensity) normalizedDensity = d/meanBinDensity normDensity[IDx] = normalizedDensity return normDensity