Skip to content

Commit 2646b11

Browse files
jjerphanogrisel
andcommitted
Wrap v and mat arrays in ReadonlyWrapper to support readonly
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
1 parent 8a7670f commit 2646b11

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

sklearn/metrics/_dist_metrics.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ cdef DTYPE_t INF = np.inf
2929

3030
from ..utils._typedefs cimport DTYPE_t, ITYPE_t, DITYPE_t, DTYPECODE
3131
from ..utils._typedefs import DTYPE, ITYPE
32-
32+
from ..utils._readonly_array_wrapper import ReadonlyArrayWrapper
3333

3434
######################################################################
3535
# newObj function
@@ -191,8 +191,8 @@ cdef class DistanceMetric:
191191
"""
192192
def __cinit__(self):
193193
self.p = 2
194-
self.vec = np.zeros(1, dtype=DTYPE, order='c')
195-
self.mat = np.zeros((1, 1), dtype=DTYPE, order='c')
194+
self.vec = ReadonlyArrayWrapper(np.zeros(1, dtype=DTYPE, order='c'))
195+
self.mat = ReadonlyArrayWrapper(np.zeros((1, 1), dtype=DTYPE, order='c'))
196196
self.size = 1
197197

198198
def __reduce__(self):
@@ -214,8 +214,8 @@ cdef class DistanceMetric:
214214
set state for pickling
215215
"""
216216
self.p = state[0]
217-
self.vec = state[1]
218-
self.mat = state[2]
217+
self.vec = ReadonlyArrayWrapper(state[1])
218+
self.mat = ReadonlyArrayWrapper(state[2])
219219
if self.__class__.__name__ == "PyFuncDistance":
220220
self.func = state[3]
221221
self.kwargs = state[4]
@@ -444,7 +444,7 @@ cdef class SEuclideanDistance(DistanceMetric):
444444
D(x, y) = \sqrt{ \sum_i \frac{ (x_i - y_i) ^ 2}{V_i} }
445445
"""
446446
def __init__(self, V):
447-
self.vec = np.asarray(V, dtype=DTYPE)
447+
self.vec = ReadonlyArrayWrapper(np.asarray(V, dtype=DTYPE))
448448
self.size = self.vec.shape[0]
449449
self.p = 2
450450

0 commit comments

Comments
 (0)