Skip to content

Commit

Permalink
using info() from core.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Reckony committed Oct 30, 2023
1 parent 44a6cfa commit f9570bd
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions src/apouey_index.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import typing
from typing import Sequence
from src.core import info, IND_TYPES

ALPHA_LOOKUP_TABLE = {
3: round(np.log(2) / np.log(3), 2),
Expand All @@ -13,41 +14,37 @@
}


def get_apouey_index(categories: int, responses: typing.List[int]) -> float:
if categories < 3 or categories > 10:
raise AttributeError(
def get_apouey_index(categories: IND_TYPES, responses: Sequence) -> float:
number_of_categories = len(categories)
if number_of_categories < 3 or number_of_categories > 10:
raise ValueError(
"Category out of range. Please use category within range 3-10."
)
resp_cumulative_proportions = _get_cumulative_proportions(
_get_proportions(categories, responses)
)

alpha = ALPHA_LOOKUP_TABLE[number_of_categories]
proportions_info = info(responses, categories)

sum_fc_alpha = 0
for category in range(1, categories - 1):
fc = resp_cumulative_proportions[category]
sum_fc_alpha += abs(fc - 0.5) ** ALPHA_LOOKUP_TABLE[categories]

result = 1 - (
(2 ** ALPHA_LOOKUP_TABLE[categories]) / (categories - 1)
) * sum_fc_alpha
return round(result, 2)


def _get_proportions(categories: int, responses: typing.List[int]) -> dict:
proportions_dict = {}
responses_num = len(responses)
for category in range(1, categories + 1):
proportions_dict[category] = round(
responses.count(category) / responses_num, 2)

return proportions_dict


def _get_cumulative_proportions(proportions_dict: dict) -> dict:
cumulative_proportions_dict = {}
proportions_list = list(proportions_dict.values())
for key, value in proportions_dict.items():
if key == 3:
cumulative_proportions_dict[key] = value
cumulative_proportions_dict[key] = round(
sum(proportions_list[:key]), 2)
return cumulative_proportions_dict

for category in range(1, len(categories) - 1):
fc = proportions_info['cumulative'][category]/100
sum_fc_alpha += abs(fc - 0.5) ** alpha

apouey_index = 1 - (2 ** alpha)/(len(categories) - 1) * sum_fc_alpha
return apouey_index


if __name__ == "__main__":
INDICATORS = [1, 2, 3, 4, 5]

DS = []
DS.extend(10 * [1])
DS.extend(10 * [2])
DS.extend(10 * [3])
DS.extend(5 * [4])
DS.extend(15 * [5])
RESULT = info(DS, INDICATORS)

print(RESULT)

print(get_apouey_index(INDICATORS, DS))

0 comments on commit f9570bd

Please sign in to comment.