1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
|
"""Unit tests for distance_transform.py functions.
"""
from random import randint, sample, seed
from unittest import TestCase
from numpy import array, ones, shape, sqrt
from numpy.testing import assert_allclose
from cogent3.maths.distance_transform import (
binary_dist_chisq,
binary_dist_chord,
binary_dist_euclidean,
binary_dist_hamming,
binary_dist_lennon,
binary_dist_ochiai,
binary_dist_otu_gain,
binary_dist_pearson,
binary_dist_sorensen_dice,
dist_abund_jaccard,
dist_bray_curtis,
dist_bray_curtis_faith,
dist_bray_curtis_magurran,
dist_canberra,
dist_chisq,
dist_chord,
dist_euclidean,
dist_gower,
dist_hellinger,
dist_kulczynski,
dist_manhattan,
dist_morisita_horn,
dist_pearson,
dist_soergel,
dist_spearman_approx,
dist_specprof,
jaccard,
trans_chisq,
trans_chord,
trans_hellinger,
trans_specprof,
zeros,
)
class functionTests(TestCase):
"""Tests of top-level functions."""
def setUp(self):
self.mat_test = array([[10, 10, 20], [10, 15, 10], [15, 5, 5]], "float")
self.emptyarray = array([], "d")
self.mtx1 = array([[1, 3], [0.0, 23.1]], "d")
self.dense1 = array([[1, 3], [5, 2], [0.1, 22]], "d")
self.zeromtx = array(
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], "d"
)
self.sparse1 = array(
[[0.0, 0.0, 5.33], [0.0, 0.0, 0.4], [1.0, 0.0, 0.0], [0.0, 0.0, 0.0]], "d"
)
self.input_binary_dist_otu_gain1 = array(
[[2, 1, 0, 0], [1, 0, 0, 1], [0, 0, 3, 0], [0, 0, 0, 1]]
)
def get_sym_mtx_from_uptri(self, mtx):
"""helper fn, only for square matrices"""
numrows, numcols = shape(mtx)
for i in range(numrows):
for j in range(i):
if i == j:
break
mtx[i, j] = mtx[j, i] # j < i, so row<col => upper triangle
return mtx
def test_dist_canberra(self):
"""tests dist_canberra
tests inputs of empty mtx, zeros, and results compared with calcs done
by hand"""
assert_allclose(dist_canberra(self.zeromtx), zeros((4, 4), "d"))
mtx1expected = array([[0.0, 46.2 / 52.2], [46.2 / 52.2, 0.0]], "d")
assert_allclose(dist_canberra(self.mtx1), mtx1expected)
sparse1exp = ones((self.sparse1.shape[0], self.sparse1.shape[0]))
# remove diagonal
sparse1exp[0, 0] = sparse1exp[1, 1] = sparse1exp[2, 2] = sparse1exp[3, 3] = 0.0
sparse1exp[0, 1] = sparse1exp[1, 0] = (5.33 - 0.4) / (5.33 + 0.4)
assert_allclose(dist_canberra(self.sparse1), sparse1exp)
def test_dist_canberra_bug(self):
i = array([[0, 0, 1], [0, 1, 1]])
d = (1.0 / 2.0) * sum(
[abs(0.0 - 1.0) / (0.0 + 1.0), abs(1.0 - 1.0) / (1.0 + 1.0)]
)
expected = array([[0.0, d], [d, 0.0]])
actual = dist_canberra(i)
assert_allclose(expected, actual)
def test_dist_euclidean(self):
"""tests dist_euclidean
tests inputs of empty mtx, zeros, and dense1 compared with calcs done
by hand"""
assert_allclose(dist_euclidean(self.zeromtx), zeros((4, 4), "d"))
dense1expected = array(
[
[0.0, sqrt(17.0), sqrt(0.9**2 + 19**2)],
[sqrt(17.0), 0.0, sqrt(4.9**2 + 20**2)],
[sqrt(0.9**2 + 19**2), sqrt(4.9**2 + 20**2), 0.0],
],
"d",
)
assert_allclose(dist_euclidean(self.dense1), dense1expected)
def test_dist_gower(self):
"""tests dist_gower
tests inputs of empty mtx, zeros, and results compared with calcs done
by hand"""
assert_allclose(dist_gower(self.zeromtx), zeros((4, 4), "d"))
mtx1expected = array([[0.0, 2.0], [2.0, 0.0]], "d")
assert_allclose(dist_gower(self.mtx1), mtx1expected)
sparse1expected = array(
[
[0.0, 4.93 / 5.33, 2, 1],
[4.93 / 5.33, 0.0, 1 + 0.4 / 5.33, 0.4 / 5.33],
[2, 1 + 0.4 / 5.33, 0, 1],
[1, 0.4 / 5.33, 1, 0.0],
],
"d",
)
assert_allclose(dist_gower(self.sparse1), sparse1expected)
def test_dist_manhattan(self):
"""tests dist_manhattan
tests inputs of empty mtx, zeros, and dense1 compared with calcs done
by hand"""
assert_allclose(dist_manhattan(self.zeromtx), zeros((4, 4), "d"))
dense1expected = array(
[[0.0, 5.0, 019.9], [5.0, 0.0, 24.9], [19.9, 24.90, 0.0]], "d"
)
assert_allclose(dist_manhattan(self.dense1), dense1expected)
def test_dist_abund_jaccard(self):
"""dist_abund_jaccard should compute distances for dense1 and mtx1"""
mtx1_expected = array([[0, 0.25], [0.25, 0]], "d")
assert_allclose(dist_abund_jaccard(self.mtx1), mtx1_expected)
dense1_expected = zeros((3, 3), "d")
assert_allclose(dist_abund_jaccard(self.dense1), dense1_expected)
sparse1_expected = array(
[
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 1.0, 0.0],
],
"d",
)
assert_allclose(dist_abund_jaccard(self.sparse1), sparse1_expected)
def test_dist_morisita_horn(self):
"""tests dist_morisita_horn
tests inputs of empty mtx, zeros, and dense1 compared with calcs done
by hand"""
assert_allclose(dist_morisita_horn(self.zeromtx), zeros((4, 4), "d"))
a = 1 - 2 * 69.3 / (26 / 16.0 * 23.1 * 4)
mtx1expected = array([[0, a], [a, 0]], "d")
assert_allclose(dist_morisita_horn(self.mtx1), mtx1expected)
def test_dist_bray_curtis(self):
"""tests dist_bray_curtis
tests inputs of empty mtx, zeros, and mtx1 compared with calcs done
by hand"""
assert_allclose(dist_manhattan(self.zeromtx), zeros((4, 4) * 1, "d"))
mtx1expected = array([[0, 21.1 / 27.1], [21.1 / 27.1, 0]], "d")
assert_allclose(dist_bray_curtis(self.mtx1), mtx1expected)
def test_dist_bray_curtis_faith(self):
"""tests dist_bray_curtis_faith
tests inputs of empty mtx, zeros, and mtx1 compared with calcs done
by hand"""
assert_allclose(dist_manhattan(self.zeromtx), zeros((4, 4) * 1, "d"))
mtx1expected = array([[0, 21.1 / 27.1], [21.1 / 27.1, 0]], "d")
assert_allclose(dist_bray_curtis_faith(self.mtx1), mtx1expected)
def test_dist_soergel(self):
"""tests dist_soergel
tests inputs of empty mtx, zeros, and dense1 compared with calcs done
by hand/manhattan dist"""
assert_allclose(dist_soergel(self.zeromtx), zeros((4, 4) * 1, "d"))
dense1expected = dist_manhattan(self.dense1)
dense1norm = array([[1, 8, 23], [8, 1, 27], [23, 27, 1]], "d")
dense1expected /= dense1norm
assert_allclose(dist_soergel(self.dense1), dense1expected)
def test_dist_kulczynski(self):
"""tests dist_kulczynski
tests inputs of empty mtx, zeros, and mtx1 compared with calcs done
by hand"""
assert_allclose(dist_kulczynski(self.zeromtx), zeros((4, 4) * 1, "d"))
mtx1expected = array(
[
[0, 1.0 - 1.0 / 2.0 * (3.0 / 4.0 + 3.0 / 23.1)],
[1.0 - 1.0 / 2.0 * (3.0 / 4.0 + 3.0 / 23.1), 0],
],
"d",
)
assert_allclose(dist_kulczynski(self.mtx1), mtx1expected)
def test_dist_pearson(self):
"""tests dist_pearson
tests inputs of empty mtx, zeros, mtx compared with calcs done
by hand, and an example from
http://davidmlane.com/hyperstat/A56626.html
"""
assert_allclose(dist_pearson(self.zeromtx), zeros((4, 4), "d"))
mtx1expected = array([[0, 0], [0, 0]], "d")
assert_allclose(dist_pearson(self.mtx1), mtx1expected)
# example 1 from http://davidmlane.com/hyperstat/A56626.html
ex1 = array([[1, 2, 3], [2, 5, 6]], "d")
ex1res = 1 - 4.0 / sqrt(2.0 * (8 + 2.0 / 3.0))
ex1expected = array([[0, ex1res], [ex1res, 0]], "d")
assert_allclose(dist_pearson(ex1), ex1expected)
def test_dist_spearman_approx(self):
"""tests dist_spearman_approx
tests inputs of empty mtx, zeros, and an example from wikipedia
"""
assert_allclose(dist_spearman_approx(self.zeromtx), zeros((4, 4) * 1, "d"))
# ex1 from wikipedia Spearman's_rank_correlation_coefficient 20jan2009
ex1 = array(
[
[106, 86, 100, 101, 99, 103, 97, 113, 112, 110],
[7, 0, 27, 50, 28, 29, 20, 12, 6, 17],
],
"d",
)
ex1res = 6.0 * 194.0 / (10.0 * 99.0)
ex1expected = array([[0, ex1res], [ex1res, 0]], "d")
assert_allclose(dist_spearman_approx(ex1), ex1expected)
# now binary fns
def test_binary_dist_otu_gain(self):
"""binary OTU gain functions as expected"""
actual = binary_dist_otu_gain(self.input_binary_dist_otu_gain1)
expected = array([[0, 1, 2, 2], [1, 0, 2, 1], [1, 1, 0, 1], [1, 0, 1, 0]])
assert_allclose(actual, expected)
def test_binary_dist_chisq(self):
"""tests binary_dist_chisq
tests inputs of empty mtx, zeros, and mtx1 compared with calcs done
by hand"""
assert_allclose(binary_dist_chisq(self.zeromtx), zeros((4, 4), "d"))
mtx1expected = array([[0, sqrt(9 / 8.0)], [sqrt(9 / 8.0), 0]], "d")
assert_allclose(binary_dist_chisq(self.mtx1), mtx1expected)
def test_binary_dist_chord(self):
"""tests binary_dist_chord
tests inputs of empty mtx, zeros, and results compared with calcs done
by hand"""
assert_allclose(binary_dist_chord(self.zeromtx), zeros((4, 4), "d"))
mtx1expected = array(
[
[0, sqrt(1 / 2.0 + (1.0 / sqrt(2.0) - 1.0) ** 2)],
[sqrt(1 / 2.0 + (1.0 / sqrt(2.0) - 1.0) ** 2), 0],
],
"d",
)
assert_allclose(binary_dist_chord(self.mtx1), mtx1expected)
def test_binary_dist_lennon(self):
"""tests binary_dist_lennon
tests inputs of empty mtx, zeros, and results compared with calcs done
by hand"""
assert_allclose(binary_dist_lennon(self.zeromtx), zeros((4, 4), "d"))
mtxa = array([[5.2, 9, 0.2], [0, 99, 1], [0, 0.0, 8233.1]], "d")
assert_allclose(binary_dist_lennon(mtxa), zeros((3, 3), "d"))
mtxb = array([[5.2, 0, 0.2, 9.2], [0, 0, 0, 1], [0, 3.2, 0, 8233.1]], "d")
mtxbexpected = array([[0, 0, 0.5], [0, 0, 0], [0.5, 0, 0]], "d")
assert_allclose(binary_dist_lennon(mtxb), mtxbexpected)
def test_binary_dist_pearson(self):
"""tests binary_dist_pearson
tests inputs of empty mtx, zeros, and dense1 compared with calcs done
by hand"""
assert_allclose(binary_dist_pearson(self.zeromtx), zeros((4, 4), "d"))
assert_allclose(binary_dist_pearson(self.dense1), zeros((3, 3)))
def test_jaccard_set(self):
"""tests jaccard_set"""
for klass in (set, frozenset):
a = klass([1, 2, 3])
b = klass([2, 3, 4])
c = klass([4, 5, 6])
empty = klass([])
assert_allclose(jaccard(a, b), 2 / 4)
assert_allclose(jaccard(a, a), 0)
assert_allclose(jaccard(a, c), 1)
assert_allclose(jaccard(a, empty), 1)
assert_allclose(jaccard(empty, a), 1)
assert_allclose(jaccard(empty, empty), 0)
# compare 2 large (500 element) sets
seed(0)
values = [randint(0, 1_000_000) for _ in range(1000)]
# choose 2 sets of 500 random items from the list
a = set(sample(values, 500))
b = set(sample(values, 500))
distance = jaccard(a, b)
assert_allclose(distance, 0.6715425)
def test_jaccard_ndarray(self):
"""tests jaccard_ndarray"""
a = array([1, 2, 3])
b = array([2, 3, 4])
c = array([4, 5, 6])
empty = array([])
assert_allclose(jaccard(a, b), 2 / 4)
assert_allclose(jaccard(a, a), 0)
assert_allclose(jaccard(a, c), 1)
assert_allclose(jaccard(a, empty), 1)
assert_allclose(jaccard(empty, a), 1)
assert_allclose(jaccard(empty, empty), 0)
# compare 2 large (500 element) sets
seed(0)
values = [randint(0, 1_000_000) for _ in range(1000)]
# choose 2 sets of 500 random items from the list
a = array([sample(values, 500)])
b = array([sample(values, 500)])
distance = jaccard(a, b)
assert_allclose(distance, 0.6715425)
def test_jaccard_other(self):
"""tests jaccard using types not covered with single dispatch"""
for a, b in ((1, 2), ([1], [2]), ("a", "b"), (1.0, 2.0)):
with self.assertRaises(NotImplementedError):
jaccard(a, b)
def test_binary_dist_ochiai(self):
"""tests binary_dist_ochiai
tests inputs of empty mtx, zeros, and mtx1 compared with calcs done
by hand"""
assert_allclose(binary_dist_ochiai(self.zeromtx), zeros((4, 4), "d"))
mtx1expected = array([[0, 1 - 1 / sqrt(2.0)], [1 - 1 / sqrt(2.0), 0]], "d")
assert_allclose(binary_dist_ochiai(self.mtx1), mtx1expected)
def test_binary_dist_hamming(self):
"""tests binary_dist_hamming
tests inputs of empty mtx, zeros, and mtx1 compared with calcs done
by hand"""
assert_allclose(binary_dist_hamming(self.zeromtx), zeros((4, 4), "d"))
mtx1expected = array([[0, 1], [1, 0]], "d")
assert_allclose(binary_dist_hamming(self.mtx1), mtx1expected)
def test_binary_dist_sorensen_dice(self):
"""tests binary_dist_sorensen_dice
tests inputs of empty mtx, zeros, and mtx1 compared with calcs done
by hand"""
assert_allclose(binary_dist_sorensen_dice(self.zeromtx), zeros((4, 4), "d"))
mtx1expected = array([[0, 1 / 3.0], [1 / 3.0, 0]], "d")
assert_allclose(binary_dist_sorensen_dice(self.mtx1), mtx1expected)
sparse1expected = array(
[[0, 0, 1.0, 1.0], [0, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 0]], "d"
)
assert_allclose(binary_dist_sorensen_dice(self.sparse1), sparse1expected)
def test_binary_dist_euclidean(self):
"""tests binary_dist_euclidean
tests two inputs compared with calculations by hand, and runs zeros
and an empty input"""
dense1expected = array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], "d")
sparse1expected = zeros((4, 4), "d")
sparse1expected[0, 2] = sqrt(2)
sparse1expected[0, 3] = 1.0
sparse1expected[1, 2] = sqrt(2)
sparse1expected[1, 3] = 1.0
sparse1expected[2, 3] = 1.0
sparse1expected = self.get_sym_mtx_from_uptri(sparse1expected)
assert_allclose(binary_dist_euclidean(self.dense1), dense1expected)
assert_allclose(binary_dist_euclidean(self.sparse1), sparse1expected)
assert_allclose(binary_dist_euclidean(self.zeromtx), zeros((4, 4), "d"))
# zj's stuff
def test_chord_transform(self):
"""trans_chord should return the exp result in the ref paper."""
exp = [
[0.40824829, 0.40824829, 0.81649658],
[0.48507125, 0.72760688, 0.48507125],
[0.90453403, 0.30151134, 0.30151134],
]
res = trans_chord(self.mat_test)
assert_allclose(res, exp)
def test_chord_dist(self):
"""dist_chord should return the exp result."""
assert_allclose(dist_chord(self.zeromtx), zeros((4, 4), "d"))
exp = [
[0.0, 0.46662021, 0.72311971],
[0.46662021, 0.0, 0.62546036],
[0.72311971, 0.62546036, 0.0],
]
dist = dist_chord(self.mat_test)
assert_allclose(dist, exp)
def test_chisq_transform(self):
"""trans_chisq should return the exp result in the ref paper."""
exp_m = [
[0.42257713, 0.45643546, 0.84515425],
[0.48294529, 0.7824608, 0.48294529],
[1.01418511, 0.36514837, 0.3380617],
]
res_m = trans_chisq(self.mat_test)
assert_allclose(res_m, exp_m)
def test_chisq_distance(self):
"""dist_chisq should return the exp result."""
assert_allclose(dist_chisq(self.zeromtx), zeros((4, 4), "d"))
exp_d = [
[0.0, 0.4910521, 0.78452291],
[0.4910521, 0.0, 0.69091002],
[0.78452291, 0.69091002, 0.0],
]
res_d = dist_chisq(self.mat_test)
assert_allclose(res_d, exp_d)
def test_hellinger_transform(self):
"""dist_hellinger should return the exp result in the ref paper."""
exp = [
[0.5, 0.5, 0.70710678],
[0.53452248, 0.65465367, 0.53452248],
[0.77459667, 0.4472136, 0.4472136],
]
res = trans_hellinger(self.mat_test)
assert_allclose(res, exp)
def test_hellinger_distance(self):
"""dist_hellinger should return the exp result."""
assert_allclose(dist_hellinger(self.zeromtx), zeros((4, 4), "d"))
exp = [
[0.0, 0.23429661, 0.38175149],
[0.23429661, 0.0, 0.32907422],
[0.38175149, 0.32907422, 0.0],
]
dist = dist_hellinger(self.mat_test)
assert_allclose(dist, exp)
def test_species_profile_transform(self):
"""trans_specprof should return the exp result."""
exp = [[0.25, 0.25, 0.5], [0.28571429, 0.42857143, 0.28571429], [0.6, 0.2, 0.2]]
res = trans_specprof(self.mat_test)
assert_allclose(res, exp)
def test_species_profile_distance(self):
"""dist_specprof should return the exp result."""
assert_allclose(dist_specprof(self.zeromtx), zeros((4, 4), "d"))
exp = [
[0.0, 0.28121457, 0.46368092],
[0.28121457, 0.0, 0.39795395],
[0.46368092, 0.39795395, 0.0],
]
dist = dist_specprof(self.mat_test)
assert_allclose(dist, exp)
def test_dist_bray_curtis_magurran1(self):
"""zero values should return zero dist, or 1 with nonzero samples"""
res = dist_bray_curtis_magurran(array([[0, 0, 0], [0, 0, 0], [1, 1, 1]]))
assert_allclose(res, array([[0, 0, 1], [0, 0, 1], [1, 1, 0]]))
def test_dist_bray_curtis_magurran2(self):
"""should match hand-calculated values"""
res = dist_bray_curtis_magurran(array([[1, 4, 3], [1, 3, 5], [0, 2, 0]]))
assert_allclose(
res,
array(
[
[0, 1 - 14 / 17, 1 - (0.4)],
[1 - 14 / 17, 0, 1 - 4 / 11],
[1 - 0.4, 1 - 4 / 11, 0],
]
),
)
|