Skip to content

Commit

Permalink
upgrade coverage and better handling of univariate and multivariate c…
Browse files Browse the repository at this point in the history
…ases.
  • Loading branch information
Sylvain Chevallier committed Jan 3, 2016
1 parent 60bca78 commit a849de7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ install:

# Run test
script:
- nosetests --with-coverage --cover-package mdla.py --logging-level=INFO
- nosetests --with-coverage --cover-package mdla.py --cover-package dict_metrics.py --logging-level=INFO


# Calculate coverage
after_success:
Expand Down
4 changes: 0 additions & 4 deletions dict_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,11 @@ def frobenius_based(A, B):
return norm(np.abs(A)-np.abs(B), 'fro')

def abs_euclidean(A, B):
if A.shape != B.shape:
raise ValueError('Atoms have different dim (', A.shape, ' and ', B.shape,'). Error raised in abs_euclidean(A, B)')
if (A.ndim != 1 and A.shape[1] != 1) or (B.ndim != 1 and B.shape[1] != 1):
raise ValueError('Atoms are not univariate (', A.shape, ' and ', B.shape,'). Error raised in abs_euclidean(A, B)')
return 2.*(1.-np.abs(A.T.dot(B)))

def euclidean (A, B):
if A.shape != B.shape:
raise ValueError('Atoms have different dim (', A.shape, ' and ', B.shape,'). Error raised in euclidean(A, B)')
if (A.ndim != 1 and A.shape[1] != 1) or (B.ndim != 1 and B.shape[1] != 1):
raise ValueError('Atoms are not univariate (', A.shape, ' and ', B.shape,'). Error raised in euclidean(A, B)')
return 2.*(1.-A.T.dot(B))
Expand Down
43 changes: 26 additions & 17 deletions test_dict_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
from dict_metrics import hausdorff, emd, _multivariate_correlation, \
detection_rate, precision_recall, precision_recall_points, beta_dist

# TODO: verif scale, dist (and non existant dist), n_kernels,
# univariate, multivariate

n_kernels, n_features, n_dims = 10, 5, 3
dm = [randn(n_features, n_dims) for i in range(n_kernels)]
for i in range(len(dm)):
dm[i] /= norm(dm[i], 'fro')
du = [randn(n_features,) for i in range(n_kernels)]
du = [randn(n_features, 1) for i in range(n_kernels)]
for i in range(len(du)):
du[i] /= norm(du[i])

Expand Down Expand Up @@ -59,18 +56,30 @@ def test_kernel_registration():
assert_almost_equal(0., m(dm3, dm, 'chordal'))

# max(dm3) > max(dm4), min(dm4) > min(dm3)
dm4 = []
for i in range(len(dm)):
k_l = dm[i].shape[0]
dm4.append(concatenate((zeros((i/2+1, 3)), dm[i]), axis=0))
dm5 = []
for i in range(len(dm)):
k_l = dm[i].shape[0]
dm5.append(concatenate((zeros((3, 3)), dm[i]), axis=0))
# dm4 = []
# for i in range(len(dm)):
# k_l = dm[i].shape[0]
# dm4.append(concatenate((zeros((i/2+1, 3)), dm[i]), axis=0))
# dm5 = []
# for i in range(len(dm)):
# k_l = dm[i].shape[0]
# dm5.append(concatenate((zeros((3, 3)), dm[i]), axis=0))
# for m in [hausdorff, emd]:
# assert_almost_equal(0., m(dm4, dm5, 'chordal'))
# assert_almost_equal(0., m(dm5, dm4, 'chordal'))

for m in [hausdorff, emd]:
assert_almost_equal(0., m(dm4, dm5, 'chordal'))
assert_almost_equal(0., m(dm5, dm4, 'chordal'))
du2 = [randn(n_features+i/2, 1) for i in range(n_kernels)]
for i in range(len(du2)):
du2[i] /= norm(du2[i])
du3 = []
for i in range(len(du)):
k_l = du[i].shape[0]
du3.append(concatenate((zeros((4, 1)), du[i]), axis=0))
for g, m in zip(gdu, [hausdorff, emd]):
assert_not_equal(0., m(du, du2, g))
assert_not_equal(0., m(du2, du, g))
assert_almost_equal(0., m(du, du3, g))
assert_almost_equal(0., m(du3, du, g))

def test_unknown_metric():
for m in [hausdorff, emd]:
Expand All @@ -93,8 +102,8 @@ def test_inhomogeneous_dims():
def test_univariate():
for m in [hausdorff, emd]:
for g in gdu:
assert_raises (ValueError, emd, dm, dm, g)

assert_raises (ValueError, m, dm, dm, g)
def test_correlation():
du2 = [randn(n_features,) for i in range(n_kernels)]
for i in range(len(du2)):
Expand Down

0 comments on commit a849de7

Please sign in to comment.