-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_batch_context.py
More file actions
83 lines (68 loc) · 3.21 KB
/
Copy pathagent_batch_context.py
File metadata and controls
83 lines (68 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Lightweight batch-level consistency hints for DeepLISA Agent."""
from __future__ import annotations
from typing import Dict, List
import numpy as np
class BatchContext:
"""Track stable batch-level grid and region properties."""
def __init__(self):
self.records: List[Dict] = []
def observe(self, metrics: Dict) -> Dict:
assessment = self.assess(metrics)
if metrics.get("local_roi_mode"):
return assessment
merged = dict(metrics)
merged.update(assessment)
self.records.append(merged)
return assessment
def assess(self, metrics: Dict) -> Dict:
warnings: List[str] = []
if metrics.get("local_roi_mode"):
return {
"batch_consistency_score": 1.0,
"batch_warnings": warnings,
"batch_reference_count": len(self.records),
}
if len(self.records) < 2:
return {
"batch_consistency_score": 1.0,
"batch_warnings": warnings,
"batch_reference_count": len(self.records),
}
score = 1.0
pitch_values = [float(r["grid_pitch_px"]) for r in self.records if r.get("grid_pitch_px")]
angle_values = [float(r["grid_angle_deg"]) for r in self.records if r.get("grid_angle_deg") is not None]
layouts = [r.get("grid_layout") for r in self.records if r.get("grid_layout")]
region_counts = [r.get("region_count") for r in self.records if r.get("region_count")]
backgrounds = [bool(r.get("high_background", False)) for r in self.records]
if metrics.get("grid_mode") and pitch_values and metrics.get("grid_pitch_px"):
pitch_ref = float(np.median(pitch_values))
pitch = float(metrics.get("grid_pitch_px"))
if abs(pitch - pitch_ref) > max(2.0, 0.10 * pitch_ref):
warnings.append("grid_pitch_outlier")
score -= 0.25
if metrics.get("grid_mode") and angle_values and metrics.get("grid_angle_deg") is not None:
angle_ref = float(np.median(angle_values))
angle = float(metrics.get("grid_angle_deg"))
if abs(angle - angle_ref) > 3.0:
warnings.append("grid_angle_outlier")
score -= 0.20
if layouts and metrics.get("grid_layout"):
layout_ref = max(set(layouts), key=layouts.count)
if metrics.get("grid_layout") != layout_ref:
warnings.append("grid_layout_mismatch")
score -= 0.20
if region_counts and metrics.get("region_count"):
region_ref = max(set(region_counts), key=region_counts.count)
if metrics.get("region_count") != region_ref:
warnings.append("region_count_mismatch")
score -= 0.15
if backgrounds and bool(metrics.get("high_background", False)) != (sum(backgrounds) >= len(backgrounds) / 2):
warnings.append("background_state_differs")
score -= 0.10
return {
"batch_consistency_score": float(np.clip(score, 0.0, 1.0)),
"batch_warnings": warnings,
"batch_reference_count": len(self.records),
}