Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09118d9

Browse files
authored
MAINT utils._fast_dict uses types from utils._typedefs (#26025)
1 parent 00032b0 commit 09118d9

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

setup.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,7 @@ def check_package_status(package, min_version):
386386
"include_dirs": ["src"],
387387
"include_np": True,
388388
},
389-
{"sources": ["_fast_dict.pyx"], "language": "c++", "include_np": True},
390-
{"sources": ["_fast_dict.pyx"], "language": "c++", "include_np": True},
389+
{"sources": ["_fast_dict.pyx"], "language": "c++"},
391390
{"sources": ["_openmp_helpers.pyx"]},
392391
{"sources": ["_seq_dataset.pyx.tp", "_seq_dataset.pxd.tp"], "include_np": True},
393392
{

sklearn/utils/_fast_dict.pxd

+3-7
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ integers, and values float.
77

88
from libcpp.map cimport map as cpp_map
99

10-
# Import the C-level symbols of numpy
11-
cimport numpy as cnp
10+
from ._typedefs cimport float64_t, intp_t
1211

13-
ctypedef cnp.float64_t DTYPE_t
14-
15-
ctypedef cnp.intp_t ITYPE_t
1612

1713
###############################################################################
1814
# An object to be used in Python
1915

2016
cdef class IntFloatDict:
21-
cdef cpp_map[ITYPE_t, DTYPE_t] my_map
22-
cdef _to_arrays(self, ITYPE_t [:] keys, DTYPE_t [:] values)
17+
cdef cpp_map[intp_t, float64_t] my_map
18+
cdef _to_arrays(self, intp_t [:] keys, float64_t [:] values)

sklearn/utils/_fast_dict.pyx

+21-24
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ from libcpp.map cimport map as cpp_map
1212

1313
import numpy as np
1414

15-
#DTYPE = np.float64
16-
#ctypedef cnp.float64_t DTYPE_t
15+
from ._typedefs cimport float64_t, intp_t
1716

18-
#ITYPE = np.intp
19-
#ctypedef cnp.intp_t ITYPE_t
2017

2118
###############################################################################
2219
# An object to be used in Python
@@ -30,8 +27,8 @@ cdef class IntFloatDict:
3027

3128
def __init__(
3229
self,
33-
ITYPE_t[:] keys,
34-
DTYPE_t[:] values,
30+
intp_t[:] keys,
31+
float64_t[:] values,
3532
):
3633
cdef int i
3734
cdef int size = values.size
@@ -44,7 +41,7 @@ cdef class IntFloatDict:
4441
return self.my_map.size()
4542

4643
def __getitem__(self, int key):
47-
cdef cpp_map[ITYPE_t, DTYPE_t].iterator it = self.my_map.find(key)
44+
cdef cpp_map[intp_t, float64_t].iterator it = self.my_map.find(key)
4845
if it == self.my_map.end():
4946
# The key is not in the dict
5047
raise KeyError('%i' % key)
@@ -56,20 +53,20 @@ cdef class IntFloatDict:
5653
# Cython 0.20 generates buggy code below. Commenting this out for now
5754
# and relying on the to_arrays method
5855
#def __iter__(self):
59-
# cdef cpp_map[ITYPE_t, DTYPE_t].iterator it = self.my_map.begin()
60-
# cdef cpp_map[ITYPE_t, DTYPE_t].iterator end = self.my_map.end()
56+
# cdef cpp_map[intp_t, float64_t].iterator it = self.my_map.begin()
57+
# cdef cpp_map[intp_t, float64_t].iterator end = self.my_map.end()
6158
# while it != end:
6259
# yield deref(it).first, deref(it).second
6360
# inc(it)
6461

6562
def __iter__(self):
6663
cdef int size = self.my_map.size()
67-
cdef ITYPE_t [:] keys = np.empty(size, dtype=np.intp)
68-
cdef DTYPE_t [:] values = np.empty(size, dtype=np.float64)
64+
cdef intp_t [:] keys = np.empty(size, dtype=np.intp)
65+
cdef float64_t [:] values = np.empty(size, dtype=np.float64)
6966
self._to_arrays(keys, values)
7067
cdef int idx
71-
cdef ITYPE_t key
72-
cdef DTYPE_t value
68+
cdef intp_t key
69+
cdef float64_t value
7370
for idx in range(size):
7471
key = keys[idx]
7572
value = values[idx]
@@ -92,10 +89,10 @@ cdef class IntFloatDict:
9289
self._to_arrays(keys, values)
9390
return keys, values
9491

95-
cdef _to_arrays(self, ITYPE_t [:] keys, DTYPE_t [:] values):
92+
cdef _to_arrays(self, intp_t [:] keys, float64_t [:] values):
9693
# Internal version of to_arrays that takes already-initialized arrays
97-
cdef cpp_map[ITYPE_t, DTYPE_t].iterator it = self.my_map.begin()
98-
cdef cpp_map[ITYPE_t, DTYPE_t].iterator end = self.my_map.end()
94+
cdef cpp_map[intp_t, float64_t].iterator it = self.my_map.begin()
95+
cdef cpp_map[intp_t, float64_t].iterator end = self.my_map.end()
9996
cdef int index = 0
10097
while it != end:
10198
keys[index] = deref(it).first
@@ -104,8 +101,8 @@ cdef class IntFloatDict:
104101
index += 1
105102

106103
def update(self, IntFloatDict other):
107-
cdef cpp_map[ITYPE_t, DTYPE_t].iterator it = other.my_map.begin()
108-
cdef cpp_map[ITYPE_t, DTYPE_t].iterator end = other.my_map.end()
104+
cdef cpp_map[intp_t, float64_t].iterator it = other.my_map.begin()
105+
cdef cpp_map[intp_t, float64_t].iterator end = other.my_map.end()
109106
while it != end:
110107
self.my_map[deref(it).first] = deref(it).second
111108
inc(it)
@@ -116,9 +113,9 @@ cdef class IntFloatDict:
116113
out_obj.my_map = self.my_map
117114
return out_obj
118115

119-
def append(self, ITYPE_t key, DTYPE_t value):
116+
def append(self, intp_t key, float64_t value):
120117
# Construct our arguments
121-
cdef pair[ITYPE_t, DTYPE_t] args
118+
cdef pair[intp_t, float64_t] args
122119
args.first = key
123120
args.second = value
124121
self.my_map.insert(args)
@@ -128,10 +125,10 @@ cdef class IntFloatDict:
128125
# operation on dict
129126

130127
def argmin(IntFloatDict d):
131-
cdef cpp_map[ITYPE_t, DTYPE_t].iterator it = d.my_map.begin()
132-
cdef cpp_map[ITYPE_t, DTYPE_t].iterator end = d.my_map.end()
133-
cdef ITYPE_t min_key = -1
134-
cdef DTYPE_t min_value = np.inf
128+
cdef cpp_map[intp_t, float64_t].iterator it = d.my_map.begin()
129+
cdef cpp_map[intp_t, float64_t].iterator end = d.my_map.end()
130+
cdef intp_t min_key = -1
131+
cdef float64_t min_value = np.inf
135132
while it != end:
136133
if deref(it).second < min_value:
137134
min_value = deref(it).second

0 commit comments

Comments
 (0)