Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Norrrrrrr lyn WAInjectBench TPR FPR Calculation

From Leeroopedia
Knowledge Sources
Domains Evaluation, Security, Statistics
Last Updated 2026-02-14 16:00 GMT

Overview

Concrete inline computation for computing TPR and FPR from detector outputs, used across all WAInjectBench detection pipelines.

Description

The TPR/FPR calculation is an inline computation (not a standalone function) that appears identically in both main_text.py (L34-37) and main_image.py (L42-45). It divides the count of detected IDs by the total sample count, rounds to 4 decimal places, and labels the result as either "tpr" or "fpr" based on the is_malicious flag. A guard clause handles the edge case where total_num is 0.

Usage

This computation is embedded within process_file and process_folder functions. It is also replicated in both ensemble modules for recomputing rates after union aggregation.

Code Reference

Source Location

  • Repository: WAInjectBench
  • File: main_text.py (L34-37), main_image.py (L42-45)

Signature

# Text variant (main_text.py:L34-37)
if is_malicious:
    rate_key, rate_value = "tpr", round(len(detect_ids) / total_num, 4) if total_num > 0 else 0.0
else:
    rate_key, rate_value = "fpr", round(len(detect_ids) / total_num, 4) if total_num > 0 else 0.0

# Image variant (main_image.py:L42-45) — identical logic
if is_malicious:
    rate_key, rate_value = "tpr", round(len(detect_ids) / total_num, 4) if total_num > 0 else 0.0
else:
    rate_key, rate_value = "fpr", round(len(detect_ids) / total_num, 4) if total_num > 0 else 0.0

Import

# No import needed; this is inline arithmetic

I/O Contract

Inputs

Name Type Required Description
detect_ids List[int] Yes IDs flagged by the detector
total_num int Yes Total number of samples in the file/folder
is_malicious bool Yes Whether the data source is from the malicious/ directory

Outputs

Name Type Description
rate_key str Either "tpr" or "fpr"
rate_value float Detection rate rounded to 4 decimal places

Usage Examples

Computing Detection Rates

# Example: malicious file with 100 samples, 85 detected
detect_ids = list(range(1, 86))  # 85 detected IDs
total_num = 100
is_malicious = True

rate_key = "tpr"
rate_value = round(len(detect_ids) / total_num, 4)  # 0.85
print(f"{rate_key}: {rate_value}")  # tpr: 0.85

# Example: benign file with 200 samples, 3 false positives
detect_ids = [12, 45, 167]
total_num = 200
is_malicious = False

rate_key = "fpr"
rate_value = round(len(detect_ids) / total_num, 4)  # 0.015
print(f"{rate_key}: {rate_value}")  # fpr: 0.015

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment