Skip to content

Commit

Permalink
Solve dtype mismatch on Windows platforms
Browse files Browse the repository at this point in the history
Issue #5
  • Loading branch information
mariaderrico committed Jun 28, 2021
1 parent 0d04540 commit f109766
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
36 changes: 23 additions & 13 deletions src/Pipeline/_DPA.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# !python
# cython: boundscheck=False
# cython: cdivision=False
# cython: nonecheck=False
# cython: wraparound=False
# cython: profile=False
# cython: annotation_typing=False
# cython: infer_types=False

# file '_DPA.pyx'.
# Non-parametric Density Peak clustering:
# Automatic topography of high-dimensional data sets
Expand All @@ -6,12 +15,13 @@
#
# Licence: BSD 3 clause

from libc.stdint cimport int32_t, int64_t
import numpy as np
cimport numpy as c_np
import copy


def get_centers(N, indices, k_hat, g):
cpdef list get_centers(int N, int64_t[:,:] indices, list k_hat, list g):
cdef int i, j, c
cdef list centers = []
# Criterion 1 from Heuristic 1
Expand All @@ -31,15 +41,15 @@ def get_centers(N, indices, k_hat, g):
break
return centers

def initial_assignment(g, N, indices, centers):
cdef long c, i, el, k
cdef c_np.ndarray[long, ndim=1] ig_sort = np.argsort([-x for x in g])
cpdef c_np.ndarray[int32_t, ndim=1] initial_assignment(list g, int N, int64_t[:,:] indices, list centers):
cdef int64_t c, i, el, k
cdef c_np.ndarray[int64_t, ndim=1] ig_sort = np.argsort([-x for x in g])

# Assign points to clusters
#--------------------------
# Assign all the points that are not centers to the same cluster as the nearest point with higher g.
# This assignation is performed in order of decreasing g
cdef c_np.ndarray[long, ndim=1] clu_labels = np.zeros(N,dtype=int)-1
cdef c_np.ndarray[int32_t, ndim=1] clu_labels = np.zeros(N,dtype='int32')-1
for c in centers:
clu_labels[c] = centers.index(c)
for i in range(0,N):
Expand All @@ -51,7 +61,7 @@ def initial_assignment(g, N, indices, centers):
return clu_labels


def get_borders( N, k_hat, indices, clu_labels, Nclus, g, densities, err_densities, Z, centers):
cpdef tuple get_borders(int N, list k_hat, int64_t[:,:] indices, c_np.ndarray[int32_t, ndim=1] clu_labels, int Nclus, list g, list densities, list err_densities, int Z, list centers):
cdef dict border_dict = {}
#cdef dict border_kmax = {}
cdef dict g_saddle = {}
Expand Down Expand Up @@ -102,8 +112,8 @@ def get_borders( N, k_hat, indices, clu_labels, Nclus, g, densities, err_densiti
pass

# Fill in the border density matrix
cdef c_np.ndarray[double, ndim=2] Rho_bord = np.zeros((Nclus,Nclus),dtype=float)
cdef c_np.ndarray[double, ndim=2] Rho_bord_err = np.zeros((Nclus,Nclus),dtype=float)
cdef double[:,:] Rho_bord = np.zeros((Nclus,Nclus),dtype=np.double)
cdef double[:,:] Rho_bord_err = np.zeros((Nclus,Nclus),dtype=np.double)
for c,cp in g_saddle.keys():
i = g_saddle[(c,cp)][0]
Rho_bord[c][cp] = densities[i]
Expand Down Expand Up @@ -206,9 +216,9 @@ def get_borders( N, k_hat, indices, clu_labels, Nclus, g, densities, err_densiti


# Update topography
cdef c_np.ndarray[double, ndim=1] min_rho_bord = np.zeros(Nclus_m)
cdef c_np.ndarray[double, ndim=2] Rho_bord_m = np.zeros((Nclus_m,Nclus_m),dtype=float)
cdef c_np.ndarray[double, ndim=2] Rho_bord_err_m = np.zeros((Nclus_m,Nclus_m),dtype=float)
cdef double[:] min_rho_bord = np.zeros(Nclus_m)
cdef double[:,:] Rho_bord_m = np.zeros((Nclus_m,Nclus_m),dtype=np.double)
cdef double[:,:] Rho_bord_err_m = np.zeros((Nclus_m,Nclus_m),dtype=np.double)
for c,cp in g_saddle.keys():
i = g_saddle[(c,cp)][0]
c = D[c]
Expand All @@ -230,12 +240,12 @@ def get_borders( N, k_hat, indices, clu_labels, Nclus, g, densities, err_densiti
Rho_bord_err_m[c][c] = 0

# Halos
cdef c_np.ndarray[long, ndim=1] clu_halos = copy.deepcopy(clu_labels)
cdef c_np.ndarray[int32_t, ndim=1] clu_halos = copy.deepcopy(clu_labels)
clu_halos = find_halos(min_rho_bord, clu_halos, densities)

return Rho_bord_m, Rho_bord_err_m, clu_labels, clu_halos, Nclus_m, centers_m

def find_halos(min_rho_bord, clu_halos, densities):
cdef c_np.ndarray[int32_t, ndim=1] find_halos(double[:] min_rho_bord, c_np.ndarray[int32_t, ndim=1] clu_halos, list densities):
cdef int i
for i in range(len(densities)):
if densities[i]<min_rho_bord[clu_halos[i]] and min_rho_bord[clu_halos[i]]>0:
Expand Down
5 changes: 3 additions & 2 deletions src/Pipeline/_PAk.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# Licence: BSD 3 clause

from libc.math cimport log, sqrt, exp, lgamma, pi, pow
from libc.stdint cimport int32_t, int64_t
import sys
import numpy as np
cimport cython
Expand All @@ -29,7 +30,7 @@ cdef double get_volume(double V1, double dist, int dim):

cdef tuple ratio_test(int i, int N, double V1, dict V_dic, int dim,
double[:,:] distances, int k_max,
double D_thr, long[:,:] indices):
double D_thr, int64_t[:,:] indices):
# Compute the volume of the dim-sphere with unitary radius
cdef float Dk, vi, vj
cdef int k, j
Expand Down Expand Up @@ -81,7 +82,7 @@ cdef tuple ratio_test(int i, int N, double V1, dict V_dic, int dim,


cpdef tuple get_densities(int dim, double[:,:] distances,
int k_max, double D_thr, long[:,:] indices):
int k_max, double D_thr, int64_t[:,:] indices):
"""Main function implementing the Pointwise Adaptive k-NN density estimator.
Parameters
Expand Down

0 comments on commit f109766

Please sign in to comment.