|
1 | 1 | import itertools
|
| 2 | +import pickle |
2 | 3 |
|
3 | 4 | import numpy as np
|
4 | 5 | from numpy.testing import assert_array_almost_equal
|
|
9 | 10 | from nose import SkipTest
|
10 | 11 |
|
11 | 12 |
|
| 13 | +def dist_func(x1, x2, p): |
| 14 | + return np.sum((x1 - x2) ** p) ** (1. / p) |
| 15 | + |
| 16 | + |
12 | 17 | d
8000
ef cmp_version(version1, version2):
|
13 | 18 | version1 = tuple(map(int, version1.split('.')[:2]))
|
14 | 19 | version2 = tuple(map(int, version2.split('.')[:2]))
|
@@ -122,18 +127,24 @@ def haversine_slow(x1, x2):
|
122 | 127 |
|
123 | 128 |
|
124 | 129 | def test_pyfunc_metric():
|
125 |
| - def dist_func(x1, x2, p): |
126 |
| - return np.sum((x1 - x2) ** p) ** (1. / p) |
127 |
| - |
128 | 130 | X = np.random.random((10, 3))
|
129 | 131 |
|
130 | 132 | euclidean = DistanceMetric.get_metric("euclidean")
|
131 | 133 | pyfunc = DistanceMetric.get_metric("pyfunc", func=dist_func, p=2)
|
132 | 134 |
|
| 135 | + # Check if both callable metric and predefined metric initialized |
| 136 | + # DistanceMetric object is picklable |
| 137 | + euclidean_pkl = pickle.loads(pickle.dumps(euclidean)) |
| 138 | + pyfunc_pkl = pickle.loads(pickle.dumps(pyfunc)) |
| 139 | + |
133 | 140 | D1 = euclidean.pairwise(X)
|
134 | 141 | D2 = pyfunc.pairwise(X)
|
135 | 142 |
|
| 143 | + D1_pkl = euclidean_pkl.pairwise(X) |
| 144 | + D2_pkl = pyfunc_pkl.pairwise(X) |
| 145 | + |
136 | 146 | assert_array_almost_equal(D1, D2)
|
| 147 | + assert_array_almost_equal(D1_pkl, D2_pkl) |
137 | 148 |
|
138 | 149 |
|
139 | 150 | if __name__ == '__main__':
|
|
0 commit comments