Skip to content

Commit

Permalink
black reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonMolinsky committed Feb 25, 2024
1 parent 7fb1659 commit e8fdede
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 53 deletions.
33 changes: 13 additions & 20 deletions src/abul_naga_yalcin_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ class AbulNagaYalcinIndex(BaseIndex):
beta: float
name: str = "Abul Naga & Yalcin Index"

@field_validator('alpha', 'beta')
@field_validator("alpha", "beta")
@classmethod
def greater_or_equal_one(cls, v: float) -> float:
if v < 1:
raise ValueError('Parameters "alpha" and "beta" must be greater'
'or equal to 1')
raise ValueError(
'Parameters "alpha" and "beta" must be greater' "or equal to 1"
)
return v


def any_index(categories: ArrayLike,
responses: ArrayLike,
alpha=1.,
beta=1.) -> AbulNagaYalcinIndex:
def any_index(
categories: ArrayLike, responses: ArrayLike, alpha=1.0, beta=1.0
) -> AbulNagaYalcinIndex:
"""
Function for computation Abul Naga & Yalcin index.
Expand Down Expand Up @@ -109,31 +109,24 @@ def any_index(categories: ArrayLike,
exp_alpha = 0.5**alpha
exp_beta = 0.5**beta

k_a_b = (m - 1) * exp_alpha - (
1 - (n_categories - m) * exp_beta
)
k_a_b = (m - 1) * exp_alpha - (1 - (n_categories - m) * exp_beta)

ds = info(
ds=responses, indicators=categories
)
ds = info(ds=responses, indicators=categories)

ds = ds['cumulative']
ds = ds["cumulative"]
p_alpha = ds[ds.index < m] / 100
p_beta = ds[ds.index >= m] / 100

# below median
p_alpha_alpha = p_alpha ** alpha
p_alpha_alpha = p_alpha**alpha
p_a = np.sum(p_alpha_alpha)

# above and equal to median
p_beta_beta = p_beta ** beta
p_beta_beta = p_beta**beta
p_b = np.sum(p_beta_beta)

index = (p_a - p_b + c) / (k_a_b + c)

return AbulNagaYalcinIndex(
index=index,
alpha=alpha,
beta=beta,
n_classes=n_categories
index=index, alpha=alpha, beta=beta, n_classes=n_categories
)
3 changes: 1 addition & 2 deletions src/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def info(ds: Sequence, indicators: ArrayLike) -> pd.DataFrame:
freq_counts_df["ratio"] = counts_to_total * 100

# Add all indicators
summary_df = pd.DataFrame(index=indicators,
columns=["frequency", "ratio"])
summary_df = pd.DataFrame(index=indicators, columns=["frequency", "ratio"])
summary_df.index.name = "indicator"

# Merge the frequency and percent DataFrames
Expand Down
35 changes: 30 additions & 5 deletions unit_tests/test_02_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,42 @@


def test_on_example():
dataset = [2, 1, 4, 3, 1, 5, 3, 3, 4, 2, 1,
1, 3, 3, 4, 5, 5, 4, 3, 2, 2, 1, 2, 1]
dataset = [
2,
1,
4,
3,
1,
5,
3,
3,
4,
2,
1,
1,
3,
3,
4,
5,
5,
4,
3,
2,
2,
1,
2,
1,
]
summary_stats = summary(dataset)
column_name = 'summary'
column_name = "summary"

assert isinstance(summary_stats, pd.DataFrame)
assert summary_stats.loc["count", column_name] == len(dataset)
assert summary_stats.loc["min", column_name] == 1
assert summary_stats.loc["max", column_name] == 5
assert round(summary_stats.loc["mean", column_name]) \
== round(sum(dataset) / len(dataset))
assert round(summary_stats.loc["mean", column_name]) == round(
sum(dataset) / len(dataset)
)
assert round(summary_stats.loc["std", column_name], 2) == 1.37
assert round(summary_stats.loc["25%", column_name], 2) == 1.75
assert round(summary_stats.loc["50%", column_name], 2) == 3.00
Expand Down
14 changes: 7 additions & 7 deletions unit_tests/test_02_ordinal_info_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from src.core import info


INDICATORS = ['not satisfied', 'ok', 'excellent', 'test']
INDICATORS = ["not satisfied", "ok", "excellent", "test"]

DS = []
DS.extend(3 * ['not satisfied'])
DS.extend(10 * ['ok'])
DS.extend(35 * ['excellent'])
DS.extend(3 * ["not satisfied"])
DS.extend(10 * ["ok"])
DS.extend(35 * ["excellent"])
RESULT = info(DS, INDICATORS)


Expand All @@ -23,19 +23,19 @@ def test_index_ordering():


def test_columns():
expected_columns = {'frequency', 'ratio', 'cumulative'}
expected_columns = {"frequency", "ratio", "cumulative"}
assert expected_columns == set(RESULT.columns)


def test_ratio():
ratios = RESULT['ratio'].values
ratios = RESULT["ratio"].values
print(RESULT)
assert np.alltrue(ratios >= 0)
assert np.alltrue(ratios <= 100)


def test_cumulative():
cumsum = RESULT['cumulative'].values
cumsum = RESULT["cumulative"].values
previous_val = 0
for x in cumsum:
assert x >= 0
Expand Down
26 changes: 7 additions & 19 deletions unit_tests/test_abul_naga_yalcin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
def test_random():
categories = list(range(1, 10))
example_data = random.choices(categories, k=500)
result = any_index(
categories=categories,
responses=example_data
)
result = any_index(categories=categories, responses=example_data)

assert isinstance(result, AbulNagaYalcinIndex)
assert isinstance(result.index, float)
Expand All @@ -33,20 +30,11 @@ def test_lowest_vs_highest():
middle = [1, 1, 1, 2, 2, 2, 3, 3, 3, 3]
highest = [1, 1, 1, 1, 1, 3, 3, 3, 3, 3]

res_low = any_index(
categories=categories,
responses=lowest
)
res_low = any_index(categories=categories, responses=lowest)

res_mid = any_index(
categories=categories,
responses=middle
)
res_mid = any_index(categories=categories, responses=middle)

res_high = any_index(
categories=categories,
responses=highest
)
res_high = any_index(categories=categories, responses=highest)

assert res_low.index < res_mid.index
assert res_mid.index < res_high.index
Expand All @@ -73,14 +61,14 @@ def test_weighted_alpha_and_beta():
categories=categories,
responses=responses,
alpha=low_param,
beta=high_param
beta=high_param,
)

res_above_more_important = any_index(
categories=categories,
responses=responses,
alpha=high_param,
beta=low_param
beta=low_param,
)

assert res_below_more_important.index != res_above_more_important.index
Expand All @@ -98,7 +86,7 @@ def test_not_greater_than_1():
categories=categories,
responses=example_data,
alpha=param_a,
beta=param_b
beta=param_b,
)

assert result.index <= 1

0 comments on commit e8fdede

Please sign in to comment.