Skip to content

Commit

Permalink
fix : .keys() calls removed
Browse files Browse the repository at this point in the history
  • Loading branch information
sepandhaghighi committed Dec 30, 2023
1 parent d492ebe commit f8d7bed
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 41 deletions.
4 changes: 2 additions & 2 deletions pycm/pycm_class_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ def basic_statistics(TP, TN, FP, FN):
:return: basic statistics as dict
"""
result = {}
for i in CLASS_PARAMS.keys():
for i in CLASS_PARAMS:
result[i] = {}
result["TP"] = TP
result["TN"] = TN
Expand Down Expand Up @@ -736,7 +736,7 @@ def class_statistics(TP, TN, FP, FN, classes, table):
:return: classes' statistics as dict
"""
result = basic_statistics(TP, TN, FP, FN)
for i in TP.keys():
for i in TP:
result["POP"][i] = TP[i] + TN[i] + FP[i] + FN[i]
result["P"][i] = TP[i] + FN[i]
result["N"][i] = TN[i] + FP[i]
Expand Down
14 changes: 7 additions & 7 deletions pycm/pycm_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ def __compare_class_handler__(compare, cm_dict):
class_weight_sum = sum(compare.class_weight.values())
class_benchmark_weight_sum = sum(compare.class_benchmark_weight.values())
for c in compare.classes:
for item in CLASS_BENCHMARK_SCORE_DICT.keys():
for item in CLASS_BENCHMARK_SCORE_DICT:
max_item_score = len(CLASS_BENCHMARK_SCORE_DICT[item]) - 1
all_class_score = [CLASS_BENCHMARK_SCORE_DICT[item][
cm.class_stat[item][c]] for cm in cm_dict.values()]
if all([isinstance(x, int) for x in all_class_score]):
for cm_name in cm_dict.keys():
for cm_name in cm_dict:
score = (compare.class_weight[c] / class_weight_sum) * (
CLASS_BENCHMARK_SCORE_DICT[item][cm_dict[cm_name].class_stat[item][c]] / max_item_score)
score = score * \
Expand All @@ -179,12 +179,12 @@ def __compare_overall_handler__(compare, cm_dict):
"""
overall_benchmark_weight_sum = sum(
compare.overall_benchmark_weight.values())
for item in OVERALL_BENCHMARK_SCORE_DICT.keys():
for item in OVERALL_BENCHMARK_SCORE_DICT:
max_item_score = len(OVERALL_BENCHMARK_SCORE_DICT[item]) - 1
all_overall_score = [OVERALL_BENCHMARK_SCORE_DICT[item][
cm.overall_stat[OVERALL_BENCHMARK_MAP[item]]] for cm in cm_dict.values()]
if all([isinstance(x, int) for x in all_overall_score]):
for cm_name in cm_dict.keys():
for cm_name in cm_dict:
score = OVERALL_BENCHMARK_SCORE_DICT[item][cm_dict[cm_name]
.overall_stat[OVERALL_BENCHMARK_MAP[item]]] / max_item_score
score = score * \
Expand All @@ -202,7 +202,7 @@ def __compare_rounder__(compare, cm_dict):
:type cm_dict: dict
:return: None
"""
for cm_name in cm_dict.keys():
for cm_name in cm_dict:
compare.scores[cm_name]["overall"] = numpy.around(
compare.scores[cm_name]["overall"], compare.digit)
compare.scores[cm_name]["class"] = numpy.around(
Expand Down Expand Up @@ -263,7 +263,7 @@ def __compare_weight_handler__(compare, weight, weight_type):
return None
if not isinstance(weight, dict):
raise pycmCompareError(error_dict[weight_type])
if set(weight.keys()) == set(valid_dict[weight_type]):
if set(weight) == set(valid_dict[weight_type]):
if all([isfloat(x) for x in weight.values()]
) and sum(weight.values()) != 0:
setattr(compare, weight_type, weight)
Expand Down Expand Up @@ -319,7 +319,7 @@ def __compare_assign_handler__(
compare.best_name = None
compare.sorted = None
compare.scores = {k: {"overall": 0, "class": 0}.copy()
for k in cm_dict.keys()}
for k in cm_dict}
__compare_weight_handler__(compare, class_weight, "class_weight")
__compare_weight_handler__(
compare,
Expand Down
4 changes: 2 additions & 2 deletions pycm/pycm_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __obj_file_handler__(cm, file):
loaded_transpose = False
cm.transpose = loaded_transpose
loaded_matrix = dict(obj_data["Matrix"])
for i in loaded_matrix.keys():
for i in loaded_matrix:
loaded_matrix[i] = dict(loaded_matrix[i])
matrix_param = matrix_params_from_table(loaded_matrix)
cm.digit = obj_data["Digit"]
Expand All @@ -160,7 +160,7 @@ def __obj_matrix_handler__(matrix, classes, transpose):
:return: matrix parameters as list
"""
if matrix_check(matrix):
if class_check(list(matrix.keys())) is False:
if class_check(list(matrix)) is False:
raise pycmMatrixError(MATRIX_CLASS_TYPE_ERROR)
matrix_param = matrix_params_from_table(matrix, classes, transpose)
else:
Expand Down
22 changes: 11 additions & 11 deletions pycm/pycm_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def __iter__(self):
:return: None
"""
for key in self.matrix.keys():
for key in self.matrix:
yield key, self.matrix[key]

def __contains__(self, class_name):
Expand Down Expand Up @@ -539,7 +539,7 @@ def F_beta(self, beta):
"""
try:
F_dict = {}
for i in self.TP.keys():
for i in self.TP:
F_dict[i] = F_calc(
TP=self.TP[i],
FP=self.FP[i],
Expand Down Expand Up @@ -656,13 +656,13 @@ def CI(
if isinstance(binom_method, str):
method = binom_method.lower()
if one_sided:
if alpha in ALPHA_ONE_SIDE_TABLE.keys():
if alpha in ALPHA_ONE_SIDE_TABLE:
CV = ALPHA_ONE_SIDE_TABLE[alpha]
else:
CV = ALPHA_ONE_SIDE_TABLE[0.05]
warn(CI_ALPHA_ONE_SIDE_WARNING, RuntimeWarning)
else:
if alpha in ALPHA_TWO_SIDE_TABLE.keys():
if alpha in ALPHA_TWO_SIDE_TABLE:
CV = ALPHA_TWO_SIDE_TABLE[alpha]
else:
CV = ALPHA_TWO_SIDE_TABLE[0.05]
Expand Down Expand Up @@ -744,7 +744,7 @@ def relabel(self, mapping, sort=False):
"""
if not isinstance(mapping, dict):
raise pycmMatrixError(MAPPING_FORMAT_ERROR)
if set(self.classes) != set(mapping.keys()):
if set(self.classes) != set(mapping):
raise pycmMatrixError(MAPPING_CLASS_NAME_ERROR)
if len(self.classes) != len(set(mapping.values())):
raise pycmMatrixError(MAPPING_CLASS_NAME_ERROR)
Expand All @@ -763,7 +763,7 @@ def relabel(self, mapping, sort=False):
self.normalized_table = normalized_table_temp
self.matrix = self.table
self.normalized_matrix = self.normalized_table
for param in self.class_stat.keys():
for param in self.class_stat:
temp_dict = {}
for classname in self.classes:
temp_dict[mapping[classname]
Expand Down Expand Up @@ -816,7 +816,7 @@ def weighted_average(self, param, weight=None, none_omit=False):
if weight is not None:
if not isinstance(weight, dict):
raise pycmAverageError(AVERAGE_WEIGHT_ERROR)
if set(weight.keys()) == set(self.classes) and all(
if set(weight) == set(self.classes) and all(
[isfloat(x) for x in weight.values()]):
selected_weight = weight.copy()
else:
Expand All @@ -828,7 +828,7 @@ def weighted_average(self, param, weight=None, none_omit=False):
try:
weight_list = []
param_list = []
for class_name in selected_param.keys():
for class_name in selected_param:
if selected_param[class_name] == "None" and none_omit:
continue
weight_list.append(selected_weight[class_name])
Expand All @@ -849,7 +849,7 @@ def weighted_kappa(self, weight=None):
if matrix_check(weight) is False:
warn(WEIGHTED_KAPPA_WARNING, RuntimeWarning)
return self.Kappa
if set(weight.keys()) != set(self.classes):
if set(weight) != set(self.classes):
warn(WEIGHTED_KAPPA_WARNING, RuntimeWarning)
return self.Kappa
return weighted_kappa_calc(
Expand All @@ -872,7 +872,7 @@ def weighted_alpha(self, weight=None):
if matrix_check(weight) is False:
warn(WEIGHTED_ALPHA_WARNING, RuntimeWarning)
return self.Alpha
if set(weight.keys()) != set(self.classes):
if set(weight) != set(self.classes):
warn(WEIGHTED_ALPHA_WARNING, RuntimeWarning)
return self.Alpha
return weighted_alpha_calc(
Expand Down Expand Up @@ -953,7 +953,7 @@ def position(self):
if self.predict_vector is None or self.actual_vector is None:
raise pycmVectorError(VECTOR_ONLY_ERROR)
if self.positions is None:
classes = list(self.label_map.keys())
classes = list(self.label_map)
positions = {
self.label_map[_class]: {
'TP': [],
Expand Down
8 changes: 4 additions & 4 deletions pycm/pycm_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def color_check(color):
return list(color)
if isinstance(color, str):
color_lower = color.lower()
if color_lower in TABLE_COLOR.keys():
if color_lower in TABLE_COLOR:
return TABLE_COLOR[color_lower]
return [0, 0, 0]

Expand Down Expand Up @@ -232,7 +232,7 @@ def html_class_stat(
str(i) + '</td>\n'
result += '<td>Description</td>\n'
result += '</tr>\n'
class_stat_keys = sorted(class_stat.keys())
class_stat_keys = sorted(class_stat)
if isinstance(class_param, list):
if set(class_param) <= set(class_stat_keys):
class_stat_keys = class_param
Expand Down Expand Up @@ -384,7 +384,7 @@ def csv_print(classes, class_stat, digit=5, class_param=None):
for item in classes:
result += ',"' + str(item) + '"'
result += "\n"
class_stat_keys = sorted(class_stat.keys())
class_stat_keys = sorted(class_stat)
if isinstance(class_param, list):
if set(class_param) <= set(class_stat_keys):
class_stat_keys = class_param
Expand Down Expand Up @@ -433,7 +433,7 @@ def stat_print(
for key in overall_stat_keys:
result += key + " " * (shift - len(key) + 7) + \
rounder(overall_stat[key], digit) + "\n"
class_stat_keys = sorted(class_stat.keys())
class_stat_keys = sorted(class_stat)
if isinstance(class_param, list):
if set(class_param) <= set(class_stat_keys):
class_stat_keys = sorted(class_param)
Expand Down
8 changes: 4 additions & 4 deletions pycm/pycm_overall_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ def entropy_calc(item, POP):
"""
try:
result = 0
for i in item.keys():
for i in item:
likelihood = item[i] / POP[i]
if likelihood != 0:
result += likelihood * math.log(likelihood, 2)
Expand Down Expand Up @@ -620,7 +620,7 @@ def cross_entropy_calc(TOP, P, POP):
"""
try:
result = 0
for i in TOP.keys():
for i in TOP:
reference_likelihood = P[i] / POP[i]
response_likelihood = TOP[i] / POP[i]
if response_likelihood != 0 and reference_likelihood != 0:
Expand Down Expand Up @@ -715,7 +715,7 @@ def kl_divergence_calc(P, TOP, POP):
"""
try:
result = 0
for i in TOP.keys():
for i in TOP:
reference_likelihood = P[i] / POP[i]
response_likelihood = TOP[i] / POP[i]
result += reference_likelihood * \
Expand Down Expand Up @@ -918,7 +918,7 @@ def PC_AC1_calc(P, TOP, POP):
"""
try:
result = 0
classes = list(P.keys())
classes = list(P)
for i in classes:
pi = ((P[i] + TOP[i]) / (2 * POP[i]))
result += pi * (1 - pi)
Expand Down
10 changes: 5 additions & 5 deletions pycm/pycm_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@
0.0005: 3.29}

CI_ALPHA_TWO_SIDE_WARNING = "The alpha value is invalid, automatically set to 0.05.\nSupported values (two-sided) : " + ",".join(
map(str, sorted(list(ALPHA_TWO_SIDE_TABLE.keys()))))
map(str, sorted(ALPHA_TWO_SIDE_TABLE)))

CI_ALPHA_ONE_SIDE_WARNING = "The alpha value is invalid, automatically set to 0.05.\nSupported values (one-sided) : " + ",".join(
map(str, sorted(list(ALPHA_ONE_SIDE_TABLE.keys()))))
map(str, sorted(ALPHA_ONE_SIDE_TABLE)))

CI_FORMAT_ERROR = "The input type is supposed to be string but it's not!"

Expand Down Expand Up @@ -496,7 +496,7 @@
"MCCI": SOA6_SCORE,
"QI": QI_SCORE}

CLASS_BENCHMARK_LIST = sorted(list(CLASS_BENCHMARK_SCORE_DICT.keys()))
CLASS_BENCHMARK_LIST = sorted(CLASS_BENCHMARK_SCORE_DICT)

OVERALL_BENCHMARK_SCORE_DICT = {
"SOA1": SOA1_SCORE,
Expand All @@ -510,7 +510,7 @@
"SOA9": SOA9_SCORE,
"SOA10": SOA10_SCORE}

OVERALL_BENCHMARK_LIST = sorted(list(OVERALL_BENCHMARK_SCORE_DICT.keys()))
OVERALL_BENCHMARK_LIST = sorted(OVERALL_BENCHMARK_SCORE_DICT)
KAPPA_BENCHMARK_LIST = ["SOA1", "SOA2", "SOA3", "SOA4"]

OVERALL_BENCHMARK_MAP = {
Expand Down Expand Up @@ -860,7 +860,7 @@
"None": "White"}
}

BENCHMARK_LIST = list(BENCHMARK_COLOR.keys())
BENCHMARK_LIST = list(BENCHMARK_COLOR)


TABLE_COLOR = {
Expand Down
11 changes: 5 additions & 6 deletions pycm/pycm_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ def matrix_check(table):
:return: bool
"""
try:
table_keys = table.keys()
if len(table_keys) == 0:
if len(table) == 0:
return False
for i in table_keys:
if set(table_keys) != set(table[i].keys()) or vector_check(
for i in table:
if set(table) != set(table[i]) or vector_check(
list(table[i].values())) is False:
return False
return True
Expand Down Expand Up @@ -296,11 +295,11 @@ def matrix_params_from_table(table, classes=None, transpose=False):
:return: [classes_list, table, TP, TN, FP, FN]
"""
if classes is None:
classes = sorted(table.keys())
classes = sorted(table)
classes_set = set(classes)
if len(classes_set) < 2:
raise pycmMatrixError(CLASS_NUMBER_ERROR)
if classes_set > set(table.keys()):
if classes_set > set(table):
raise pycmMatrixError(CLASSES_ERROR)
table_temp = table
map_dict = {k: 0 for k in classes}
Expand Down

0 comments on commit f8d7bed

Please sign in to comment.