File size: 12,907 Bytes
cecefdc
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
 
 
f6c65ef
 
 
 
cecefdc
 
 
 
f6c65ef
cecefdc
 
 
f6c65ef
cecefdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6c65ef
 
 
 
 
 
 
cecefdc
 
 
 
 
 
 
 
 
 
 
 
f6c65ef
 
 
 
 
 
 
 
 
cecefdc
7a39bba
 
 
cecefdc
 
 
f6c65ef
cecefdc
f6c65ef
cecefdc
 
 
f6c65ef
cecefdc
 
 
f6c65ef
cecefdc
 
 
f6c65ef
cecefdc
f6c65ef
cecefdc
 
f6c65ef
cecefdc
 
 
f6c65ef
cecefdc
f6c65ef
 
cecefdc
f6c65ef
 
cecefdc
 
f6c65ef
 
cecefdc
 
f6c65ef
 
cecefdc
f6c65ef
 
 
cecefdc
f6c65ef
cecefdc
 
f6c65ef
cecefdc
 
 
7a39bba
 
 
 
cecefdc
 
f6c65ef
 
 
 
 
 
 
 
 
 
 
 
cecefdc
 
 
 
f6c65ef
 
 
 
 
cecefdc
f6c65ef
cecefdc
 
 
f6c65ef
 
 
 
cecefdc
f6c65ef
 
 
7a39bba
 
 
 
 
 
f6c65ef
cecefdc
 
 
f6c65ef
 
 
 
 
 
 
cecefdc
 
f6c65ef
cecefdc
 
 
 
f6c65ef
07fb80f
 
 
 
 
 
 
 
 
cecefdc
 
7a39bba
cecefdc
07fb80f
cecefdc
 
f6c65ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cecefdc
f6c65ef
 
 
cecefdc
07fb80f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cecefdc
 
f6c65ef
 
 
 
 
cecefdc
 
 
 
f6c65ef
 
 
 
 
cecefdc
f6c65ef
 
 
cecefdc
 
f6c65ef
 
cecefdc
f6c65ef
 
cecefdc
f6c65ef
 
 
 
 
 
 
 
 
cecefdc
f6c65ef
cecefdc
 
 
f6c65ef
cecefdc
 
f6c65ef
cecefdc
 
7a39bba
 
 
f6c65ef
cecefdc
 
7a39bba
 
 
 
 
f6c65ef
cecefdc
 
7a39bba
 
 
 
 
f6c65ef
cecefdc
 
 
 
 
 
7a39bba
 
 
 
 
cecefdc
 
f6c65ef
cecefdc
 
 
 
 
7a39bba
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"""Ripeness Classifier page - Interactive explainability and threshold tuning.

This page provides full transparency into how cases are classified as RIPE/UNRIPE/UNKNOWN,
allows interactive threshold tuning, and provides case-level explainability.
"""

from __future__ import annotations

from datetime import date, timedelta

import pandas as pd
import plotly.express as px
import streamlit as st

from src.core.case import Case, CaseStatus
from src.core.ripeness import RipenessClassifier, RipenessStatus
from src.dashboard.utils.data_loader import (
    attach_history_to_cases,
    load_generated_cases,
    load_generated_hearings,
)

# Page configuration
st.set_page_config(
    page_title="Ripeness Classifier",
    page_icon="target",
    layout="wide",
)

st.title("Ripeness Classifier - Explainability Dashboard")
st.markdown("Understand and tune the case readiness algorithm")

# Initialize session state for thresholds
if "min_service_hearings" not in st.session_state:
    st.session_state.min_service_hearings = 2
if "min_stage_days" not in st.session_state:
    st.session_state.min_stage_days = 30
if "min_case_age_days" not in st.session_state:
    st.session_state.min_case_age_days = 90

# Sidebar: Threshold controls
st.sidebar.header("Threshold Configuration")

st.sidebar.markdown("### Adjust Ripeness Thresholds")

min_service_hearings = st.sidebar.slider(
    "Min Service Hearings",
    min_value=0,
    max_value=10,
    value=st.session_state.min_service_hearings,
    step=1,
    help="Minimum number of service hearings before a case is considered RIPE",
)

min_stage_days = st.sidebar.slider(
    "Min Stage Days",
    min_value=0,
    max_value=180,
    value=st.session_state.min_stage_days,
    step=5,
    help="Minimum days in current stage",
)

min_case_age_days = st.sidebar.slider(
    "Min Case Age (days)",
    min_value=0,
    max_value=730,
    value=st.session_state.min_case_age_days,
    step=30,
    help="Minimum case age before considered RIPE",
)

# Detailed history toggle
use_history = st.sidebar.toggle(
    "Use detailed hearing history (if available)",
    value=True,
    help="When enabled, the classifier will use per-hearing history from hearings.csv if present.",
)

# Reset button
if st.sidebar.button("Reset to Defaults"):
    st.session_state.min_service_hearings = 2
    st.session_state.min_stage_days = 30
    st.session_state.min_case_age_days = 90
    st.rerun()

# Update session state
st.session_state.min_service_hearings = min_service_hearings
st.session_state.min_stage_days = min_stage_days
st.session_state.min_case_age_days = min_case_age_days

# Wire sidebar thresholds to the core classifier
RipenessClassifier.set_thresholds(
    {
        "MIN_SERVICE_HEARINGS": min_service_hearings,
        "MIN_STAGE_DAYS": min_stage_days,
        "MIN_CASE_AGE_DAYS": min_case_age_days,
    }
)

# Main content
tab1, tab2, tab3 = st.tabs(
    ["Current Configuration", "Interactive Testing", "Batch Classification"]
)

with tab1:
    st.markdown("### Current Classifier Configuration")

    col1, col2, col3 = st.columns(3)

    with col1:
        st.metric("Min Service Hearings", min_service_hearings)
        st.caption("Cases need at least this many service hearings")

    with col2:
        st.metric("Min Stage Days", min_stage_days)
        st.caption("Days in current stage threshold")

    with col3:
        st.metric("Min Case Age", f"{min_case_age_days} days")
        st.caption("Minimum case age requirement")

    st.markdown("---")

    # Classification logic flowchart
    st.markdown("### Classification Logic")

    with st.expander("View Decision Tree Logic"):
        st.markdown("""
        The ripeness classifier uses the following decision logic:

        **1. Service Hearings Check**
        - If `service_hearings < MIN_SERVICE_HEARINGS` -> **UNRIPE**

        **2. Case Age Check**
        - If `case_age < MIN_CASE_AGE_DAYS` -> **UNRIPE**

        **3. Stage-Specific Checks**
        - Each stage has minimum days requirement
        - If `days_in_stage < stage_requirement` -> **UNRIPE**

        **4. Keyword Analysis**
        - Certain keywords indicate ripeness (e.g., "reply filed", "arguments complete")
        - If keywords found -> **RIPE**

        **5. Final Classification**
        - If all criteria met -> **RIPE**
        - If some criteria failed but not critical -> **UNKNOWN**
        - Otherwise -> **UNRIPE**
        """)

    # Show stage-specific rules
    st.markdown("### Stage-Specific Rules")

    stage_rules = {
        "PRE-TRIAL": {"min_days": 60, "keywords": ["affidavit filed", "reply filed"]},
        "TRIAL": {"min_days": 45, "keywords": ["evidence complete", "cross complete"]},
        "POST-TRIAL": {
            "min_days": 30,
            "keywords": ["arguments complete", "written note"],
        },
        "FINAL DISPOSAL": {"min_days": 15, "keywords": ["disposed", "judgment"]},
    }

    df_rules = pd.DataFrame(
        [
            {
                "Stage": stage,
                "Min Days": rules["min_days"],
                "Keywords": ", ".join(rules["keywords"]),
            }
            for stage, rules in stage_rules.items()
        ]
    )

    st.dataframe(df_rules, use_container_width=True, hide_index=True)

with tab2:
    st.markdown("### Interactive Case Classification Testing")

    st.markdown(
        "Create a synthetic case and see how it would be classified with current thresholds"
    )

    col1, col2 = st.columns(2)

    with col1:
        case_id = st.text_input("Case ID", value="TEST-001")
        case_type = st.selectbox("Case Type", ["CIVIL", "CRIMINAL", "WRIT", "PIL"])
        case_stage = st.selectbox(
            "Current Stage", ["PRE-TRIAL", "TRIAL", "POST-TRIAL", "FINAL DISPOSAL"]
        )

    with col2:
        service_hearings_count = st.number_input(
            "Service Hearings", min_value=0, max_value=20, value=3
        )
        days_in_stage = st.number_input(
            "Days in Stage", min_value=0, max_value=365, value=45
        )
        case_age = st.number_input(
            "Case Age (days)", min_value=0, max_value=3650, value=120
        )

    # Keywords
    has_keywords = st.multiselect(
        "Keywords Found",
        options=[
            "reply filed",
            "affidavit filed",
            "arguments complete",
            "evidence complete",
            "written note",
        ],
        default=[],
    )

    if st.button("Classify Case"):
        # Create synthetic case
        today = date.today()
        filed_date = today - timedelta(days=case_age)

        # Map UI-friendly stage labels to classifier's internal stage names
        stage_map = {
            "PRE-TRIAL": "ADMISSION",  # early-stage administrative
            "TRIAL": "EVIDENCE",  # substantive stage
            "POST-TRIAL": "ORDERS / JUDGMENT",  # arguments/orders phase
            "FINAL DISPOSAL": "FINAL DISPOSAL",
        }
        classifier_stage = stage_map.get(case_stage, case_stage)

        test_case = Case(
            case_id=case_id,
            case_type=case_type,
            filed_date=filed_date,
            current_stage=classifier_stage,
            status=CaseStatus.PENDING,
        )

        # Populate aggregates and optional purpose based on selected keywords
        test_case.hearing_count = service_hearings_count
        test_case.days_in_stage = int(days_in_stage)
        test_case.age_days = int(case_age)
        test_case.last_hearing_purpose = has_keywords[0] if has_keywords else None

        # Use the real classifier
        status = RipenessClassifier.classify(test_case)
        reason = RipenessClassifier.get_ripeness_reason(status)

        color = (
            "green"
            if status == RipenessStatus.RIPE
            else ("red" if status.is_unripe() else "orange")
        )
        st.markdown("### Classification Result")
        st.markdown(f":{color}[**{status.value}**]")
        st.caption(reason)

        # Debug details to explain classification
        with st.expander("Why this classification? (debug)"):
            thresholds = RipenessClassifier.get_current_thresholds()
            service_ok = service_hearings_count >= thresholds[
                "MIN_SERVICE_HEARINGS"
            ] or bool(test_case.last_hearing_purpose)
            compliance_ok = (
                classifier_stage not in RipenessClassifier.UNRIPE_STAGES
                or days_in_stage >= thresholds["MIN_STAGE_DAYS"]
            )
            age_ok = case_age >= thresholds["MIN_CASE_AGE_DAYS"]

            st.write(
                {
                    "ui_stage": case_stage,
                    "classifier_stage": classifier_stage,
                    "hearing_count": service_hearings_count,
                    "days_in_stage": int(days_in_stage),
                    "age_days": int(case_age),
                    "last_hearing_purpose": test_case.last_hearing_purpose,
                    "evidence": {
                        "service_ok": service_ok,
                        "compliance_ok": compliance_ok,
                        "age_ok": age_ok,
                        "all_ok": service_ok and compliance_ok and age_ok,
                    },
                    "thresholds": thresholds,
                }
            )

with tab3:
    st.markdown("### Batch Classification Analysis")

    st.markdown(
        "Load generated test cases and classify them with current thresholds (core classifier)"
    )

    if st.button("Load & Classify Test Cases"):
        with st.spinner("Loading cases..."):
            try:
                cases = load_generated_cases()

                if use_history:
                    hearings_df = load_generated_hearings()
                    cases = attach_history_to_cases(cases, hearings_df)

                if not cases:
                    st.warning(
                        "No test cases found. Generate cases first: `uv run court-scheduler generate`"
                    )
                else:
                    st.success(f"Loaded {len(cases)} test cases")

                    # Classify all cases using the core classifier
                    classifications = {"RIPE": 0, "UNRIPE": 0, "UNKNOWN": 0}

                    today = date.today()
                    for case in cases:
                        # Ensure aggregates are available
                        case.age_days = (today - case.filed_date).days
                        if getattr(case, "stage_start_date", None):
                            case.days_in_stage = (today - case.stage_start_date).days
                        else:
                            case.days_in_stage = case.age_days

                        status = RipenessClassifier.classify(case)
                        if status == RipenessStatus.RIPE:
                            classifications["RIPE"] += 1
                        elif status == RipenessStatus.UNKNOWN:
                            classifications["UNKNOWN"] += 1
                        else:
                            classifications["UNRIPE"] += 1

                    # Display results
                    col1, col2, col3 = st.columns(3)

                    with col1:
                        pct = classifications["RIPE"] / len(cases) * 100
                        st.metric(
                            "RIPE Cases", f"{classifications['RIPE']:,}", f"{pct:.1f}%"
                        )

                    with col2:
                        pct = classifications["UNKNOWN"] / len(cases) * 100
                        st.metric(
                            "UNKNOWN Cases",
                            f"{classifications['UNKNOWN']:,}",
                            f"{pct:.1f}%",
                        )

                    with col3:
                        pct = classifications["UNRIPE"] / len(cases) * 100
                        st.metric(
                            "UNRIPE Cases",
                            f"{classifications['UNRIPE']:,}",
                            f"{pct:.1f}%",
                        )

                    # Pie chart
                    fig = px.pie(
                        values=list(classifications.values()),
                        names=list(classifications.keys()),
                        title="Classification Distribution",
                        color=list(classifications.keys()),
                        color_discrete_map={
                            "RIPE": "green",
                            "UNKNOWN": "orange",
                            "UNRIPE": "red",
                        },
                    )
                    st.plotly_chart(fig, use_container_width=True)

            except Exception as e:
                st.error(f"Error loading cases: {e}")

# Footer
st.markdown("---")
st.markdown(
    "*Adjust thresholds in the sidebar to see real-time impact on classification*"
)