blocks-transformer/score_processing.py

116 lines
3.4 KiB
Python
Raw Normal View History

2025-03-12 16:16:45 +00:00
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s - %(message)s",
)
logger = logging.getLogger(__name__)
def processing(data: dict) -> dict:
score_threshold = 1200
score_threshold2 = 1225
2025-03-12 16:16:45 +00:00
# ---- Compute hd_score1, hd_score2, final hd_score ----
2025-03-12 16:16:45 +00:00
try:
hd_score1 = max([
data["hd_score_s1"],
data["hd_score_s2"],
data["hd_score_g1"],
data["hd_score_s3"],
data["hd_score_m1"],
])
hd_score2 = data["hd_score_iso_m2"]
if hd_score1 > score_threshold:
hd_score = hd_score1
elif hd_score2 >= score_threshold2:
hd_score = hd_score2
2025-03-12 16:16:45 +00:00
else:
hd_score = max(hd_score1, hd_score2)
# logging.info(f"Computed hd_score1={hd_score1}, hd_score2={hd_score2}, final hd_score={hd_score}")
2025-03-12 16:16:45 +00:00
except Exception as e:
logging.error(f"Error calculating hd_score values: {e}")
2025-03-12 16:16:45 +00:00
return {}
# ---- recommended_action ----
2025-03-12 16:16:45 +00:00
try:
if hd_score1 > score_threshold or hd_score2 >= score_threshold2:
recommended_action = "Decline Application"
else:
recommended_action = "Pass Application"
2025-03-12 16:16:45 +00:00
logging.info(f"recommended_action: {recommended_action}")
2025-03-12 16:16:45 +00:00
except Exception as e:
logging.error(f"Error assigning recommended_action: {e}")
return {}
# ---- description ----
2025-03-12 16:16:45 +00:00
try:
if hd_score1 > score_threshold:
description = (
f"HD Fraud Score is above the risk threshold {score_threshold}, "
f"Recommended action: Decline Application"
)
elif hd_score2 >= score_threshold2:
description = (
f"HD Fraud Score is above the risk threshold {score_threshold2}, "
f"Recommended action: Decline Application"
)
else:
description = (
"HD Fraud Score is below the risk threshold, Recommended action: Pass Application"
)
2025-03-12 16:16:45 +00:00
logging.info(f"description: {description}")
2025-03-12 16:16:45 +00:00
except Exception as e:
logging.error(f"Error creating description: {e}")
2025-03-12 16:16:45 +00:00
return {}
# ---- hd_action_reasoncode ----
2025-03-12 16:16:45 +00:00
try:
reason_hierarchy = [
("hd_score_s1", "S1 Triggered"),
("hd_score_s2", "S2 Triggered"),
("hd_score_g1", "G1 Triggered"),
("hd_score_s3", "S3 Triggered"),
("hd_score_m1", "M1 Triggered"),
("hd_score_iso_m2", "M2 Triggered"), # NEW
]
hd_action_reasoncode = "Pass"
for key, reason in reason_hierarchy:
score = data[key]
if key == "hd_score_iso_m2":
if score >= score_threshold2:
hd_action_reasoncode = reason
break
else:
if score > score_threshold:
hd_action_reasoncode = reason
break
logging.info(f"hd_action_reasoncode: {hd_action_reasoncode}")
2025-03-12 16:16:45 +00:00
except Exception as e:
logging.error(f"Error determining action_reasoncode: {e}")
2025-03-12 16:16:45 +00:00
return {}
# ---- Return only original 4 fields ----
2025-03-12 16:16:45 +00:00
return {
"hd_score": hd_score,
"recommended_action": recommended_action,
"description": description,
"hd_action_reasoncode": hd_action_reasoncode,
}