From 46a11e37743d5ae884f887a619d2c3938f055219 Mon Sep 17 00:00:00 2001 From: Luis Pedro Coelho Date: Mon, 16 Sep 2024 00:22:12 +1000 Subject: [PATCH] RFCT Simplify norm_abundance function --- SemiBin/utils.py | 9 ++------- test/test_utils.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/SemiBin/utils.py b/SemiBin/utils.py index 84bef04..a18ce9c 100644 --- a/SemiBin/utils.py +++ b/SemiBin/utils.py @@ -659,12 +659,7 @@ def compute_min_length(min_length, fafile, ratio): def norm_abundance(data): import numpy as np n = data.shape[1] - 136 - flag = False if n >= 20: - flag = True - else: - if n >= 5: - if np.mean(np.sum(data[:, 136:], axis=1)) > 2: - flag = True - return flag + return True + return (n >= 5 and np.mean(np.sum(data[:, 136:], axis=1)) > 2) diff --git a/test/test_utils.py b/test/test_utils.py index d9cd559..2031ee7 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,4 +1,4 @@ -from SemiBin.utils import get_must_link_threshold, get_marker, split_data, n50_l50, extract_bams +from SemiBin.utils import get_must_link_threshold, get_marker, split_data, n50_l50, extract_bams, norm_abundance from hypothesis import given, strategies as st from io import StringIO import numpy as np @@ -135,3 +135,16 @@ def test_extract_bams(tmpdir): tmpdir) assert rs is None +def test_norm_abundance(): + assert not norm_abundance(np.random.randn(10, 136)) + assert not norm_abundance(np.random.randn(12, 137)) + assert not norm_abundance(np.random.randn(12, 140)) + assert not norm_abundance(np.random.randn(12, 148)) + assert not norm_abundance(np.abs(np.random.randn(12, 138))*2) + assert not norm_abundance(np.abs(np.random.randn(12, 138))*4) + + assert norm_abundance(np.abs(np.random.randn(12, 148))) + assert norm_abundance( np.random.randn(12, 156) ) + assert norm_abundance( np.random.randn(12, 164) ) + assert norm_abundance(np.abs(np.random.randn(12, 164))) +