Skip to content

Commit 5b54e04

Browse files
felixmaximilianrth
authored andcommitted
Use of fused memoryviews without the gil in cython implementation of fast hadamard transform (fht).
1 parent be47964 commit 5b54e04

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

examples/kernel_approximation/plot_kernel_approximation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
# plot the results:
144144
plt.figure(figsize=(8, 8))
145145
accuracy = plt.subplot(211)
146-
# second y axis for timeings
146+
# second y axis for timings
147147
timescale = plt.subplot(212)
148148

149149
accuracy.plot(sample_sizes, nystroem_scores, label="Nystroem approx. kernel")
@@ -174,7 +174,8 @@
174174

175175
# legends and labels
176176
accuracy.set_title("Classification accuracy")
177-
timescale.set_title("Training times")
177+
timescale.set_title("Training times for dataset size of " + str(n_samples) + " with dimensionality of "
178+
+ str(np.size(data, 1)))
178179
accuracy.set_xlim(sample_sizes[0], sample_sizes[-1])
179180
accuracy.set_xticks(())
180181
accuracy.set_ylim(np.min(fourier_scores), 1)

sklearn/utils/cyfht.pyx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ cimport numpy as np
1818
cimport cython
1919
from libc.math cimport log2
2020

21-
22-
ctypedef np.double_t DTYPE_t
23-
24-
2521
def is_power_of_two(input_integer):
2622
""" Test if an integer is a power of two. """
2723
if input_integer == 1:
@@ -42,7 +38,7 @@ def pure_python_fht(array_):
4238
array_[j] = temp - array_[j]
4339

4440

45-
def fht(np.ndarray[DTYPE_t] array_):
41+
def fht(cython.floating[::1] array_):
4642
""" Single dimensional FHT. """
4743
if not is_power_of_two(array_.shape[0]):
4844
raise ValueError('Length of input for fht must be a power of two')
@@ -51,9 +47,9 @@ def fht(np.ndarray[DTYPE_t] array_):
5147

5248

5349
@cython.boundscheck(False)
54-
cdef _fht(np.ndarray[DTYPE_t, ndim=1] array_):
50+
cdef void _fht(cython.floating[::1] array_) nogil:
5551
cdef unsigned int bit, length, _, i, j
56-
cdef double temp
52+
cdef cython.floating temp
5753
bit = length = array_.shape[0]
5854
for _ in xrange(<unsigned int>(log2(length))):
5955
bit >>= 1
@@ -64,8 +60,7 @@ cdef _fht(np.ndarray[DTYPE_t, ndim=1] array_):
6460
array_[i] += array_[j]
6561
array_[j] = temp - array_[j]
6662

67-
68-
def fht2(np.ndarray[DTYPE_t, ndim=2] array_):
63+
def fht2(cython.floating[:, ::1] array_):
6964
""" Two dimensional row-wise FHT. """
7065
if not is_power_of_two(array_.shape[1]):
7166
raise ValueError('Length of rows for fht2 must be a power of two')
@@ -74,9 +69,8 @@ def fht2(np.ndarray[DTYPE_t, ndim=2] array_):
7469

7570

7671
@cython.boundscheck(False)
77-
cdef _fht2(np.ndarray[DTYPE_t, ndim=2] array_):
78-
cdef unsigned int bit, length, _, i, j, n
79-
cdef double temp
72+
cdef void _fht2(cython.floating[:, ::1] array_) nogil:
73+
cdef unsigned int n
8074
n = array_.shape[0]
8175
for x in xrange(n):
8276
# TODO: This call still shows up as yellow in cython -a presumably due

0 commit comments

Comments
 (0)