def test_invalid_metric(self): """Test whether computing RSA with an invalid metric raises error.""" with pytest.raises(ValueError, match='Invalid RSA metric'): next(rsa_gen(dsm_gen([dsm()]), dsm(), metric='foo')) # These metrics only work with multiple model DSMs with pytest.raises(ValueError, match='Need more than one model DSM'): next(rsa_gen(dsm_gen([dsm()]), dsm(), metric='partial')) with pytest.raises(ValueError, match='Need more than one model DSM'): next(rsa_gen(dsm_gen([dsm()]), dsm(), metric='partial-spearman'))
def test_regression(self): """Test computing RSA with regression.""" model_dsm1 = np.array([-1, 0, 1]) model_dsm2 = np.array([1, -2, 1]) data_dsm = dsm_gen([3 * model_dsm1 + 5 * model_dsm2]) rsa_val = next(rsa_gen(data_dsm, [model_dsm1, model_dsm2], metric='regression')) assert_allclose(rsa_val, [3, 5])
def test_partial_spearman(self): """Test computing RSA with partial spearman correlation.""" # Example verified with MATLAB's partialcorr function model_dsm1 = np.array([1, 2, 3, 4]) model_dsm2 = np.array([0, 0, 1, 1]) data_dsm = dsm_gen([[2, 4, 20, 15]]) rsa_val = next(rsa_gen(data_dsm, [model_dsm1, model_dsm2], metric='partial-spearman')) assert_allclose(rsa_val, [0, 2 / 3], atol=1E-15)
def test_partial(self): """Test computing RSA with partial correlation.""" # Example taken from https://en.wikipedia.org/wiki/Partial_correlation model_dsm1 = np.array([1, 2, 3, 4]) model_dsm2 = np.array([0, 0, 1, 1]) data_dsm = dsm_gen([[2, 4, 15, 20]]) rsa_val = next(rsa_gen(data_dsm, [model_dsm1, model_dsm2], metric='partial')) assert_allclose(rsa_val, [0.919145, 0.912871])
def test_kendall_tau_a(self): """Test computing RSA with Kendall's Tau Alpha""" data_dsm = dsm_gen([[1, 2, 3]]) model_dsm = np.array([1, 3, 3]) # This metric deals well with ties rsa_val = next(rsa_gen(data_dsm, model_dsm, metric='kendall-tau-a')) assert rsa_val == 2 / 3
def test_pearson(self): """Test computing RSA with Pearson correlation""" data_dsm = dsm_gen([[1, 2, 3]]) model_dsm = np.array([2, 3, 3.5]) assert next(rsa_gen(data_dsm, model_dsm, metric='pearson')) < 1.0
def test_spearman(self): """Test computing RSA with Spearman correlation""" data_dsm = dsm_gen([[1, 2, 3]]) model_dsm = np.array([2, 3, 3.5]) assert next(rsa_gen(data_dsm, model_dsm, metric='spearman')) == 1.0
def test_return_type(self): """Test return type of rsa_gen""" assert isinstance(rsa_gen(dsm_gen([dsm()]), dsm()), GeneratorType) assert next(rsa_gen(dsm_gen([dsm()]), dsm())).shape == tuple() assert next(rsa_gen(dsm_gen([dsm()]), [dsm()])).shape == (1,)