blocks-transformer/score_processing.py

103 lines
3.3 KiB
Python
Raw Permalink 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_score based on hierarchy ----
2025-03-12 16:16:45 +00:00
try:
reason_hierarchy = [
("hd_score_s1", "S1 Triggered", score_threshold, True),
("hd_score_s2", "S2 Triggered", score_threshold, True),
("hd_score_g1", "G1 Triggered", score_threshold, True),
("hd_score_s3", "S3 Triggered", score_threshold, True),
("hd_score_m1", "M1 Triggered", score_threshold, True),
("hd_score_iso_m2", "M2 Triggered", score_threshold2, False),
("hd_score_g2", "G2 Triggered", score_threshold2, False),
]
winning_score = None
winning_reason = "Pass"
winning_threshold = None
for key, reason, threshold, use_gt in reason_hierarchy:
score = data.get(key)
if score is None:
continue
passes = (score > threshold) if use_gt else (score >= threshold)
if passes:
winning_score = score
winning_reason = reason
winning_threshold = threshold
break
if winning_score is None:
winning_score = max([
data.get("hd_score_s1", 0),
data.get("hd_score_s2", 0),
data.get("hd_score_g1", 0),
data.get("hd_score_s3", 0),
data.get("hd_score_m1", 0),
data.get("hd_score_iso_m2", 0),
data.get("hd_score_g2", 0),
])
winning_reason = "Pass"
hd_score = winning_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 winning_reason != "Pass":
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 winning_reason != "Pass":
threshold_used = winning_threshold if winning_threshold is not None else score_threshold
description = (
f"HD Fraud Score is above the risk threshold {threshold_used}, "
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 ----
hd_action_reasoncode = winning_reason
logging.info(f"hd_action_reasoncode: {hd_action_reasoncode}")
2025-03-12 16:16:45 +00:00
# ---- 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,
}