def leiden_clustering(D, gamma=0.5): ''' Arguments: D (np.ndarray): square distance matrix shape (N, N) with 0s in diag gamma (float): resolution parameter for clustering Returns: membership (list): cluster assignment ''' A = NearestNeighbors(n_neighbors=5, metric='precomputed', n_jobs=16).fit(D).kneighbors_graph(D) G = ig.Graph.Adjacency(A.astype(int), mode='undirected') membership = la.find_partition(G, la.RBConfigurationVertexPartition, resolution_parameter=gamma).membership return membership