def test_tversky(): shape = (4, 32, 32, 32, 1) y_pred = np.random.rand(*shape).astype(np.float64) y_true = np.random.randint(2, size=shape).astype(np.float64) # Test that tversky and dice are same when alpha = beta = 0.5 dice = metrics.dice(y_true, y_pred).numpy() tversky = metrics.tversky(y_true, y_pred, axis=(1, 2, 3), alpha=0.5, beta=0.5).numpy() assert_allclose(dice, tversky) # Test that tversky and jaccard are same when alpha = beta = 1.0 jaccard = metrics.jaccard(y_true, y_pred).numpy() tversky = metrics.tversky(y_true, y_pred, axis=(1, 2, 3), alpha=1., beta=1.).numpy() assert_allclose(jaccard, tversky) with pytest.raises(ValueError): metrics.tversky([0., 0., 1.], [1., 0., 1.], axis=0)
def test_jaccard(): x = np.zeros(4) y = np.zeros(4) out = metrics.jaccard(x, y, axis=None).numpy() assert_allclose(out, 1) x = np.ones(4) y = np.ones(4) out = metrics.jaccard(x, y, axis=None).numpy() assert_allclose(out, 1) x = [0., 0., 1., 1.] y = [1., 1., 1., 1.] out = metrics.jaccard(x, y, axis=None).numpy() ref = 1. - scipy.spatial.distance.jaccard(x, y) assert_allclose(out, ref) dice_out = metrics.dice(x, y, axis=None).numpy() assert_allclose(out, dice_out / (2. - dice_out)) x = [0., 0., 1., 1.] y = [1., 1., 0., 0.] out = metrics.jaccard(x, y, axis=None).numpy() ref = 1. - scipy.spatial.distance.jaccard(x, y) assert_allclose(out, ref, atol=1e-07) assert_allclose(out, 0, atol=1e-07) x = np.ones((4, 32, 32, 32, 1), dtype=np.float32) y = x.copy() x[:2, :10, 10:] = 0 y[:2, :3, 20:] = 0 y[3:, 10:] = 0 jaccards = np.empty(x.shape[0]) for i in range(x.shape[0]): jaccards[i] = 1. - scipy.spatial.distance.jaccard( x[i].flatten(), y[i].flatten()) assert_allclose(metrics.jaccard(x, y, axis=(1, 2, 3, 4)), jaccards)
def jaccard(y_true, y_pred, axis=(1, 2, 3, 4)): return 1.0 - metrics.jaccard(y_true=y_true, y_pred=y_pred, axis=axis)