From 08c81b8892060353bb7fb15555875f03bbdcb46e Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 19 Nov 2021 10:56:35 +0300 Subject: Avoid rounding: compare floats approximately Notes: https://github.com/genenetwork/genenetwork3/pull/56#issuecomment-973798918 * As mentioned in the notes, rather than rounding to an arbitrary number of decimal places, it is a much better practice to use approximate comparisons of floats for the tests. --- gn3/computations/partial_correlations.py | 2 +- tests/unit/computations/test_partial_correlations.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/gn3/computations/partial_correlations.py b/gn3/computations/partial_correlations.py index ee47290..4f45159 100644 --- a/gn3/computations/partial_correlations.py +++ b/gn3/computations/partial_correlations.py @@ -156,7 +156,7 @@ def tissue_correlation( "Method must be one of: {}".format(",".join(method_fns.keys()))) corr, pvalue = method_fns[method](primary_trait_values, target_trait_values) - return (round(corr, 10), round(pvalue, 10)) + return (corr, pvalue) def batch_computed_tissue_correlation( primary_trait_values: Tuple[float, ...], target_traits_dict: dict, diff --git a/tests/unit/computations/test_partial_correlations.py b/tests/unit/computations/test_partial_correlations.py index f77a066..3e1b6e1 100644 --- a/tests/unit/computations/test_partial_correlations.py +++ b/tests/unit/computations/test_partial_correlations.py @@ -3,6 +3,7 @@ from unittest import TestCase import pandas +from numpy.testing import assert_allclose from gn3.computations.partial_correlations import ( fix_samples, @@ -250,7 +251,7 @@ class TestPartialCorrelations(TestCase): with self.assertRaises(error, msg=error_msg): tissue_correlation(primary, target, method) - def test_tissue_correlation(self): + def test_tissue_correlation(self): # pylint: disable=R0201 """ Test that the correct correlation values are computed for the given: - primary trait @@ -259,11 +260,11 @@ class TestPartialCorrelations(TestCase): """ for primary, target, method, expected in ( ((12.34, 18.36, 42.51), (37.25, 46.25, 46.56), "pearson", - (0.6761779253, 0.5272701134)), + (0.6761779252651052, 0.5272701133657985)), ((1, 2, 3, 4, 5), (5, 6, 7, 8, 7), "spearman", - (0.8207826817, 0.0885870053))): + (0.8207826816681233, 0.08858700531354381))): with self.subTest(primary=primary, target=target, method=method): - self.assertEqual( + assert_allclose( tissue_correlation(primary, target, method), expected) def test_good_dataset_samples_indexes(self): -- cgit v1.2.3