File size: 5,327 Bytes
00639e5
 
 
7b37610
00639e5
 
 
 
 
 
 
 
 
 
 
 
ca1791d
 
 
00639e5
 
 
 
 
 
 
 
 
 
 
 
 
516fa71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00639e5
 
 
ca1791d
00639e5
516fa71
 
 
 
 
 
 
 
 
00639e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b37610
00639e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# src/query_builder.py

from typing import List, Dict
from langsmith import traceable


def build_queries_for_stone(stone: Dict) -> List[str]:
    """
    Tek bir taş için multiple search queries üret.
    Segmentation modeli varsa morphology ve density homogeneity de kullanılır.
    """
    size_mm = stone['size']['max_dimension_mm']
    size_category = stone['size']['size_category']
    density = stone['density']['category']

    queries = [
        # Klinik rapor bağlamı için anchor query
        f"kidney stone {size_category} {size_mm}mm clinical report findings interpretation EAU guidelines",

        # Boyut-bazlı tedavi
        f"treatment options for {size_mm}mm kidney stone clinical guidelines",

        # Boyut kategorisi yönetim
        f"management of {size_category} renal calculus recommendations",

        # Relative density karakterizasyonu (HU değil, göreli yoğunluk)
        f"{density} density kidney stone imaging characteristics composition",

        # Spontan pasaj ve MET
        f"spontaneous passage likelihood {size_mm}mm stone medical expulsive therapy",
    ]

    # Orta boy taşlar için özel sorgular (5-10mm)
    if size_category == 'medium':
        queries.append(
            f"ureteral stone {size_mm}mm spontaneous passage rate percentage probability"
        )
        queries.append(
            "kidney stone 5mm 10mm MET alpha blocker intervention threshold EAU guideline"
        )
        queries.append(
            f"kidney stone under 10mm {size_mm}mm conservative treatment observation MET option"
        )
        queries.append(
            "distal ureteral stone less than 10mm spontaneous expulsion rate alpha blocker"
        )

    # Büyük taşlar için cerrahi
    if size_category in ['large', 'very_large']:
        queries.append(
            f"surgical treatment selection {size_mm}mm renal stone EAU AUA guideline recommendation"
        )
        queries.append(
            f"PCNL indication kidney stone larger than 10mm 20mm first line EAU recommendation"
        )
        queries.append(
            f"SWL contraindication failure large renal stone size limit ESWL efficacy"
        )
        queries.append(
            "renal stone treatment algorithm 10 20mm PCNL RIRS SWL selection EAU figure"
        )

    # ── Morphology-based queries (yalnızca segmentation modeli çalıştıysa) ──
    morphology = stone.get('morphology')
    if morphology:
        circularity = morphology.get('circularity', 1.0)
        solidity = morphology.get('solidity', 1.0)

        # Düzensiz şekil → pasaj güçlüğü, obstrüksiyon riski
        if circularity < 0.65:
            queries.append(
                "irregular kidney stone morphology spontaneous passage difficulty ureteral obstruction"
            )

        # Düşük solidity → girintili-çıkıntılı yüzey
        if solidity < 0.85:
            queries.append(
                "complex spiculated kidney stone surface morphology treatment prognosis"
            )

    # ── Density homojenliği query ──
    homogeneity = stone['density'].get('homogeneity')
    if homogeneity == 'heterogeneous':
        queries.append(
            "heterogeneous kidney stone composition mixed density treatment response ESWL"
        )
    elif homogeneity == 'homogeneous':
        queries.append(
            "homogeneous kidney stone composition ESWL fragmentation efficacy"
        )

    return queries


@traceable(name="Query Builder", run_type="tool")
def build_all_queries(features: Dict) -> List[str]:
    """
    Feature extraction output'u için tüm queries.
    Unique query string listesi döndürür.
    """
    if not features.get('stone_detected', False):
        return []

    all_queries = []

    for stone in features.get('stones', []):
        all_queries.extend(build_queries_for_stone(stone))

    # Multiple stones
    if features.get('multiple_stones', False):
        all_queries.append(
            "management multiple kidney stones bilateral calculi complex treatment"
        )
        if features.get('total_stone_area_mm2'):
            all_queries.append(
                "total stone burden multiple renal calculi treatment planning"
            )

    # Duplicate'leri çıkar (case-insensitive)
    seen = set()
    unique_queries = []
    for q in all_queries:
        q_lower = q.lower()
        if q_lower not in seen:
            seen.add(q_lower)
            unique_queries.append(q)

    return unique_queries


if __name__ == "__main__":
    test_features = {
        "stone_detected": True,
        "count": 1,
        "stones": [{
            "stone_id": 1,
            "size": {
                "max_dimension_mm": 7.2,
                "size_category": "medium"
            },
            "density": {
                "category": "high",
                "homogeneity": "heterogeneous"
            },
            "shape": "irregular",
            "location": {"quadrant": "upper-left"},
            "morphology": {
                "circularity": 0.55,
                "solidity": 0.80
            }
        }],
        "multiple_stones": False
    }

    queries = build_all_queries(test_features)
    print(f"Generated {len(queries)} queries:")
    for i, q in enumerate(queries, 1):
        print(f"{i}. {q}")