Skip to content

Commit

Permalink
✨ handling inf cv
Browse files Browse the repository at this point in the history
  • Loading branch information
MaaniBeigy committed Dec 28, 2021
1 parent 0ec3fec commit 0e50273
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 41 deletions.
8 changes: 4 additions & 4 deletions .logs/complexity.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pycvcqv/dataframe.py
F 13:0 cv_dataframe - A (3)
F 37:0 cqv_dataframe - A (3)
F 43:0 cqv_dataframe - A (3)
pycvcqv/cqv.py
F 15:0 cqv - A (2)
pycvcqv/singlethread.py
F 13:0 singlethread_cv_processor - A (1)
F 38:0 singlethread_cqv_processor - A (1)
pycvcqv/formulas.py
F 11:0 _cv - A (2)
F 48:0 _cqv - A (2)
F 12:0 _cv - A (5)
F 55:0 _cqv - A (2)
pycvcqv/__init__.py
F 18:0 get_version - A (2)
pycvcqv/is_numeric.py
Expand All @@ -26,4 +26,4 @@ pycvcqv/cv.py
F 16:0 coefficient_of_variation - A (2)

16 blocks (classes, functions, methods) analyzed.
Average complexity: A (1.6875)
Average complexity: A (1.875)
12 changes: 6 additions & 6 deletions .logs/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
platform linux -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /data/aasaam/pycvcqv, configfile: pyproject.toml
plugins: html-3.1.1, metadata-1.11.0, cov-3.0.0
collected 20 items
collected 24 items

tests/test_cqv.py ........... [ 55%]
tests/test_cv.py ......... [100%]
tests/test_cqv.py ........... [ 45%]
tests/test_cv.py ............. [100%]

---------- coverage: platform linux, python 3.8.10-final-0 -----------
Name Stmts Miss Branch BrPart Cover Missing
Expand All @@ -14,16 +14,16 @@ pycvcqv/__init__.py 11 0 0 0 100%
pycvcqv/cqv.py 12 0 2 0 100%
pycvcqv/cv.py 12 0 2 0 100%
pycvcqv/dataframe.py 11 0 4 0 100%
pycvcqv/formulas.py 18 0 4 0 100%
pycvcqv/formulas.py 21 0 6 0 100%
pycvcqv/is_numeric.py 18 0 10 0 100%
pycvcqv/multithread.py 16 0 0 0 100%
pycvcqv/prepare_output.py 13 0 4 0 100%
pycvcqv/singlethread.py 16 0 0 0 100%
pycvcqv/types.py 13 0 0 0 100%
pycvcqv/userthread.py 14 0 0 0 100%
-----------------------------------------------------------------------
TOTAL 154 0 26 0 100%
TOTAL 157 0 28 0 100%

Required test coverage of 50.0% reached. Total coverage: 100.00%

============================== 20 passed in 1.73s ==============================
============================== 24 passed in 1.79s ==============================
2 changes: 1 addition & 1 deletion .logs/dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Installing dependencies from lock file

No dependencies to install or update

Installing the current project: pycvcqv (0.1.10)
Installing the current project: pycvcqv (0.1.11)
4 changes: 2 additions & 2 deletions .logs/maintainability.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pycvcqv/dataframe.py - A (77.93)
pycvcqv/dataframe.py - A (77.06)
pycvcqv/cqv.py - A (100.00)
pycvcqv/singlethread.py - A (100.00)
pycvcqv/formulas.py - A (79.80)
pycvcqv/formulas.py - A (77.15)
pycvcqv/__init__.py - A (88.71)
pycvcqv/types.py - A (100.00)
pycvcqv/is_numeric.py - A (89.86)
Expand Down
2 changes: 1 addition & 1 deletion assets/images/maintainability.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion cookiecutter-config-file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ default_context:
minimal_python_version: 3.7
github_name: "MaaniBeigy"
email: "manibeygi@gmail.com"
version: "0.1.10"
version: "0.1.11"
line_length: "88"
create_example_template: "none"
26 changes: 3 additions & 23 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion pycvcqv/cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,14 @@ def coefficient_of_variation(
)
# --------------------------------- non DataFrame --------------------------------
else:
result = float(_cv(data, ddof, skipna, ndigits, correction, multiplier))
result = float(
_cv(
data=data,
ddof=ddof,
skipna=skipna,
ndigits=ndigits,
correction=correction,
multiplier=multiplier,
)
)
return result
8 changes: 7 additions & 1 deletion pycvcqv/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ def cv_dataframe(
if v == num_threads
)
return operators[0](
data, num_threads, ddof, skipna, ndigits, correction, multiplier
data=data,
num_threads=num_threads,
ddof=ddof,
skipna=skipna,
ndigits=ndigits,
correction=correction,
multiplier=multiplier,
)


Expand Down
7 changes: 7 additions & 0 deletions pycvcqv/formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Optional

import pandas as pd
from numpy import Inf

from pycvcqv.types import NumArrayLike

Expand All @@ -19,6 +20,12 @@ def _cv(
"""Internal function to calculate cv."""
# ------------------- convert data to pandas.core.series.Series -------------------
prep_data: pd.Series = pd.Series(data)
# ------------------------- return Inf if mean close to zero ----------------------
if prep_data.mean(skipna=skipna) == 0 or (
prep_data.mean(skipna=skipna) < 0.000001
and prep_data.std(skipna=skipna, ddof=ddof) > prep_data.mean(skipna=skipna)
):
return float(Inf)
# ------------------ the basic coefficient of variation function ------------------
_cv = prep_data.std(skipna=skipna, ddof=ddof) / prep_data.mean(skipna=skipna)
length = len(prep_data)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "pycvcqv"
version = "0.1.10"
version = "0.1.11"
description = "Coefficient of Variation (CV) and Coefficient of Quartile Variation (CQV) with Confidence Intervals (CI) "
readme = "README.md"
authors = ["MaaniBeigy <manibeygi@gmail.com>"]
Expand Down
58 changes: 58 additions & 0 deletions tests/test_cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# --------------------------- Import libraries and functions --------------------------
import pandas as pd
import pytest
from numpy import inf
from pandas.testing import assert_frame_equal

from pycvcqv.cv import coefficient_of_variation
Expand Down Expand Up @@ -210,3 +211,60 @@ def test_cv_dataframe_multithread_default_3_cores():
}
),
)


def test_zero_division_cv_returns_none_with_kwargs():
"""Tests cv function for zero division with kwargs."""
assert coefficient_of_variation(data=[-2, -1, 0, 1, 2]) == float("inf")


def test_zero_division_cv_returns_none_without_kwargs():
"""Tests cv function for zero division without kwargs."""
assert coefficient_of_variation([-2, -1, 0, 1, 2]) == float("inf")


def test_zero_division_cv_returns_none_without_kwargs_dataframe():
"""Tests cv function for zero division for dataframe."""
data = pd.DataFrame(
{
"col-1": pd.Series([-2, -1, 0, 1, 2]),
"col-2": pd.Series([-2, -1, 0, 1, 2]),
}
)
result = coefficient_of_variation(data=data, num_threads=3)
assert_frame_equal(
result,
pd.DataFrame(
{
"columns": pd.Series(["col-1", "col-2"]),
"cv": pd.Series([float(inf), float(inf)]),
}
),
)


def test_zero_division_cv_returns_none_when_std_is_high():
"""Tests cv function for zero division when std is high."""
vector = [
-1.687949,
-1.556078,
-1.292336,
-1.160465,
-0.984637,
-0.764852,
-0.676938,
-0.589024,
-0.237368,
0.158245,
0.246159,
0.597815,
0.597815,
0.729686,
0.773643,
0.817600,
0.861557,
1.125299,
1.345084,
1.696740,
]
assert coefficient_of_variation(data=vector) == float("inf")

0 comments on commit 0e50273

Please sign in to comment.