From 50ff3695db2507042869381d854674041384aea3 Mon Sep 17 00:00:00 2001 From: Sunny Sun <38218185+sunnyosun@users.noreply.github.com> Date: Thu, 21 Mar 2024 20:14:05 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20ZeroDivisionError=20in=20`?= =?UTF-8?q?inspect`=20(#75)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ๐Ÿ› Fix zero division eror in inspect * ๐Ÿ‘ท Run 3.10 * ๐Ÿงช Add tests --- .github/workflows/build.yml | 4 ++-- lamin_utils/_inspect.py | 9 +++++++++ tests/test_inspect.py | 10 ++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0f69188..ab3eec5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,7 +12,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.9"] + python-version: ["3.10"] steps: - uses: actions/checkout@v3 @@ -34,6 +34,6 @@ jobs: - run: nox -s lint - run: nox -s build - uses: codecov/codecov-action@v2 - if: matrix.python-version == '3.9' + if: matrix.python-version == '3.10' with: token: ${{ secrets.CODECOV_TOKEN }} diff --git a/lamin_utils/_inspect.py b/lamin_utils/_inspect.py index da1bae7..b01ea05 100644 --- a/lamin_utils/_inspect.py +++ b/lamin_utils/_inspect.py @@ -54,6 +54,15 @@ def _validate_stats(identifiers: Iterable, matches: "np.ndarray"): nonval = _unique_rm_empty(df_val.index[~df_val["__validated__"]]).tolist() n_unique = len(val) + len(nonval) + if n_unique == 0: + return InspectResult( + validated_df=df_val, + validated=val, + nonvalidated=nonval, + frac_validated=0, + n_empty=0, + n_unique=0, + ) n_empty = df_val.shape[0] - n_unique frac_nonval = round(len(nonval) / n_unique * 100, 1) frac_val = 100 - frac_nonval diff --git a/tests/test_inspect.py b/tests/test_inspect.py index e304364..6642ad8 100644 --- a/tests/test_inspect.py +++ b/tests/test_inspect.py @@ -129,6 +129,16 @@ def test_inspect_empty_dup_input(genes): assert result.non_validated == [] +def test_inspect_zero_identifiers(): + result = inspect( + df=pd.DataFrame(), + identifiers=pd.Series([]), + field="symbol", + ) + assert result.validated == [] + assert result.non_validated == [] + + def test_inspect_empty_df(): import numpy as np import pandas as pd