File size: 16,316 Bytes
cecefdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
 
cecefdc
 
 
f6c65ef
cecefdc
f6c65ef
cecefdc
f6c65ef
 
cecefdc
 
 
f6c65ef
 
 
6a28f91
 
 
f6c65ef
 
 
 
 
 
cecefdc
f6c65ef
779c826
f6c65ef
 
 
 
 
 
 
 
 
 
 
779c826
f6c65ef
 
 
 
 
 
779c826
 
 
 
f6c65ef
 
779c826
 
 
 
 
 
 
 
f6c65ef
779c826
f6c65ef
 
 
 
 
 
 
cecefdc
779c826
 
 
 
f6c65ef
 
 
cecefdc
 
 
 
f6c65ef
 
 
 
 
 
 
 
 
 
 
 
6a28f91
 
 
f6c65ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cecefdc
f6c65ef
cecefdc
f6c65ef
 
cecefdc
 
 
f6c65ef
 
 
6a28f91
 
 
f6c65ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cecefdc
f6c65ef
cecefdc
f6c65ef
 
 
 
 
 
cecefdc
 
 
 
 
 
f6c65ef
cecefdc
 
f6c65ef
cecefdc
 
 
f6c65ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cecefdc
f6c65ef
 
 
 
 
 
6a28f91
 
 
f6c65ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
 
 
f6c65ef
 
 
 
6a28f91
 
 
f6c65ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
 
f6c65ef
 
 
 
 
 
 
 
 
cecefdc
 
 
 
 
 
f6c65ef
cecefdc
 
f6c65ef
cecefdc
 
 
 
 
f6c65ef
cecefdc
 
6a28f91
 
 
 
 
 
cecefdc
f6c65ef
cecefdc
 
 
 
6a28f91
 
 
f6c65ef
cecefdc
 
 
f6c65ef
cecefdc
 
 
 
f6c65ef
cecefdc
 
 
f6c65ef
 
 
6a28f91
 
 
f6c65ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cecefdc
f6c65ef
 
 
cecefdc
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
"""Data loading utilities for dashboard with caching.

This module provides cached data loading functions to avoid
reloading large datasets on every user interaction.
"""

from __future__ import annotations

from pathlib import Path
from typing import Any

import pandas as pd
import polars as pl
import streamlit as st

from src.data.case_generator import CaseGenerator
from src.data.param_loader import ParameterLoader


@st.cache_data(ttl=3600)
def load_param_loader(params_dir: str = None) -> dict[str, Any]:
    """Load EDA-derived parameters.

    Args:
        params_dir: Directory containing parameter files (if None, uses latest EDA output)

    Returns:
        Dictionary containing key parameter data
    """
    if params_dir is None:
        # Find latest EDA output directory
        figures_dir = Path("reports/figures")
        version_dirs = [
            d for d in figures_dir.iterdir() if d.is_dir() and d.name.startswith("v")
        ]
        if version_dirs:
            latest_dir = max(version_dirs, key=lambda p: p.stat().st_mtime)
            params_dir = str(latest_dir / "params")
        else:
            params_dir = "configs/parameters"  # Fallback

    loader = ParameterLoader(Path(params_dir))

    # Extract case types from case_type_summary DataFrame
    if hasattr(loader, "case_type_summary") and loader.case_type_summary is not None:
        # Try both column name variations
        if "CASE_TYPE" in loader.case_type_summary.columns:
            case_types = loader.case_type_summary["CASE_TYPE"].unique().tolist()
        elif "casetype" in loader.case_type_summary.columns:
            case_types = loader.case_type_summary["casetype"].unique().tolist()
        else:
            case_types = []
    else:
        case_types = []

    # Extract stages from transition_probs DataFrame
    stages = (
        loader.transition_probs["STAGE_FROM"].unique().tolist()
        if hasattr(loader, "transition_probs")
        else []
    )

    # Build stage graph from transition probabilities
    stage_graph = {}
    for stage in stages:
        transitions = loader.get_stage_transitions(stage)
        stage_graph[stage] = transitions.to_dict("records")

    # Build adjournment stats
    adjournment_stats = {}
    for stage in stages:
        adjournment_stats[stage] = {}
        for ct in case_types:
            try:
                prob = loader.get_adjournment_prob(stage, ct)
                adjournment_stats[stage][ct] = prob
            except (KeyError, ValueError):
                adjournment_stats[stage][ct] = 0.0

    # Include global courtroom capacity stats if available
    try:
        court_capacity = loader.court_capacity  # type: ignore[attr-defined]
    except Exception:
        court_capacity = None

    return {
        "case_types": case_types,
        "stages": stages,
        "stage_graph": stage_graph,
        "adjournment_stats": adjournment_stats,
        # Expected by Data & Insights → Simulation Defaults section
        # File source: reports/figures/<version>/params/court_capacity_global.json
        "court_capacity_global": court_capacity,
    }


@st.cache_data(ttl=3600)
def load_cleaned_hearings(data_path: str = None) -> pd.DataFrame:
    """Load cleaned hearings data.

    Args:
        data_path: Path to cleaned hearings file (if None, uses latest EDA output)

    Returns:
        Pandas DataFrame with hearings data
    """
    if data_path is None:
        # Find latest EDA output directory
        figures_dir = Path("reports/figures")
        version_dirs = [
            d for d in figures_dir.iterdir() if d.is_dir() and d.name.startswith("v")
        ]
        if version_dirs:
            latest_dir = max(version_dirs, key=lambda p: p.stat().st_mtime)
            # Try parquet first, then CSV
            parquet_path = latest_dir / "hearings_clean.parquet"
            csv_path = latest_dir / "hearings_clean.csv"
            if parquet_path.exists():
                path = parquet_path
            elif csv_path.exists():
                path = csv_path
            else:
                st.warning(f"No cleaned hearings data found in {latest_dir}")
                return pd.DataFrame()
        else:
            st.warning("No EDA output directories found. Run EDA pipeline first.")
            return pd.DataFrame()
    else:
        path = Path(data_path)

    if not path.exists():
        st.warning(f"Hearings file not found: {path}")
        return pd.DataFrame()

    # Load based on file extension
    if path.suffix == ".parquet":
        df = pl.read_parquet(path).to_pandas()
    else:
        df = pl.read_csv(path).to_pandas()
    return df


@st.cache_data(ttl=3600)
def load_cleaned_data(data_path: str = None) -> pd.DataFrame:
    """Load cleaned case data.

    Args:
        data_path: Path to cleaned data file (if None, uses latest EDA output)

    Returns:
        Pandas DataFrame with case data
    """
    if data_path is None:
        # Find latest EDA output directory
        figures_dir = Path("reports/figures")
        version_dirs = [
            d for d in figures_dir.iterdir() if d.is_dir() and d.name.startswith("v")
        ]
        if version_dirs:
            latest_dir = max(version_dirs, key=lambda p: p.stat().st_mtime)
            # Try parquet first, then CSV
            parquet_path = latest_dir / "cases_clean.parquet"
            csv_path = latest_dir / "cases_clean.csv"
            if parquet_path.exists():
                path = parquet_path
            elif csv_path.exists():
                path = csv_path
            else:
                st.warning(f"No cleaned data found in {latest_dir}")
                return pd.DataFrame()
        else:
            st.warning("No EDA output directories found. Run EDA pipeline first.")
            return pd.DataFrame()
    else:
        path = Path(data_path)

    if not path.exists():
        st.warning(f"Data file not found: {path}")
        return pd.DataFrame()

    # Load based on file extension
    if path.suffix == ".parquet":
        df = pl.read_parquet(path).to_pandas()
    else:
        df = pl.read_csv(path).to_pandas()
    return df


@st.cache_data(ttl=3600)
def load_generated_cases(cases_path: str = "data/generated/cases.csv") -> list:
    """Load generated test cases.

    Args:
        cases_path: Path to generated cases CSV

    Returns:
        List of Case objects
    """

    # Helper to detect project root (directory containing pyproject.toml or repo files)
    def _detect_project_root(start: Path | None = None) -> Path:
        try:
            cur = (start or Path(__file__).resolve()).resolve()
        except Exception:
            cur = Path.cwd()
        for parent in [cur] + list(cur.parents):
            try:
                if (parent / "pyproject.toml").exists():
                    return parent
                # Fallback heuristic: both top-level folders present
                if (parent / "scheduler").is_dir() and (parent / "cli").is_dir():
                    return parent
            except Exception:
                continue
        return Path.cwd()

    # Build a list of candidate paths to be resilient to working directory and case differences
    candidates: list[Path] = []
    seen: set[str] = set()

    def _add(path: Path) -> None:
        try:
            key = str(path.resolve())
        except Exception:
            key = str(path)
        if key not in seen:
            seen.add(key)
            candidates.append(path)

    p = Path(cases_path)

    # Bases to try: as-is (absolute or relative to CWD), project root, and file's directory
    project_root = _detect_project_root()
    file_base = (
        Path(__file__).resolve().parent.parent.parent.parent
    )  # approximate repo root from file
    bases: list[Path] = [Path.cwd(), project_root, file_base]

    # 1) As provided
    _add(p)

    # 2) If relative, try under each base
    if not p.is_absolute():
        for base in bases:
            _add(base / p)

    # 3) Try swapping the top-level directory between data/Data if applicable
    def swap_data_top(path: Path) -> Path | None:
        parts = path.parts
        if not parts:
            return None
        top = parts[0]
        if top.lower() == "data":
            alt_top = "Data" if top == "data" else "data"
            if len(parts) > 1:
                return Path(alt_top).joinpath(*parts[1:])
            return Path(alt_top)
        return None

    # Apply swap to original and to base-joined variants
    to_consider = list(candidates)
    for c in to_consider:
        alt = swap_data_top(c)
        if alt is not None:
            _add(alt)
            # If relative, also try under bases
            if not alt.is_absolute():
                for base in bases:
                    _add(base / alt)

    # 4) Explicitly try the known alternative under project root when default is used
    if str(cases_path).replace("\\", "/").endswith("data/generated/cases.csv"):
        _add(project_root / "Data/generated/cases.csv")

    # Pick the first existing path
    chosen = next((c for c in candidates if c.exists()), None)
    if chosen is None:
        tried = ", ".join(str(Path(str(c)).resolve()) for c in candidates)
        st.warning(
            "Cases file not found. Tried: "
            + tried
            + f" | CWD: {Path.cwd()} | Project root: {project_root}"
        )
        return []

    cases = CaseGenerator.from_csv(chosen)
    return cases


@st.cache_data(ttl=3600)
def load_generated_hearings(
    hearings_path: str = "data/generated/hearings.csv",
) -> pd.DataFrame:
    """Load generated hearings history as a flat DataFrame.

    Args:
        hearings_path: Path to generated hearings CSV

    Returns:
        Pandas DataFrame with columns [case_id, date, stage, purpose, was_heard, event]
    """

    # Reuse robust path detection from load_generated_cases
    def _detect_project_root(start: Path | None = None) -> Path:
        try:
            cur = (start or Path(__file__).resolve()).resolve()
        except Exception:
            cur = Path.cwd()
        for parent in [cur] + list(cur.parents):
            try:
                if (parent / "pyproject.toml").exists():
                    return parent
                if (parent / "scheduler").is_dir() and (parent / "cli").is_dir():
                    return parent
            except Exception:
                continue
        return Path.cwd()

    candidates: list[Path] = []
    seen: set[str] = set()

    def _add(path: Path) -> None:
        try:
            key = str(path.resolve())
        except Exception:
            key = str(path)
        if key not in seen:
            seen.add(key)
            candidates.append(path)

    p = Path(hearings_path)
    project_root = _detect_project_root()
    file_base = Path(__file__).resolve().parent.parent.parent.parent
    bases: list[Path] = [Path.cwd(), project_root, file_base]

    _add(p)
    if not p.is_absolute():
        for base in bases:
            _add(base / p)

    # swap Data/data top folder if needed
    def swap_data_top(path: Path) -> Path | None:
        parts = path.parts
        if not parts:
            return None
        top = parts[0]
        if top.lower() == "data":
            alt_top = "Data" if top == "data" else "data"
            if len(parts) > 1:
                return Path(alt_top).joinpath(*parts[1:])
            return Path(alt_top)
        return None

    to_consider = list(candidates)
    for c in to_consider:
        alt = swap_data_top(c)
        if alt is not None:
            _add(alt)
            if not alt.is_absolute():
                for base in bases:
                    _add(base / alt)

    # Explicit additional under project root
    if str(hearings_path).replace("\\", "/").endswith("data/generated/hearings.csv"):
        _add(project_root / "Data/generated/hearings.csv")

    chosen = next((c for c in candidates if c.exists()), None)
    if chosen is None:
        # Don't warn loudly; simply return empty frame for graceful fallback
        return pd.DataFrame(
            columns=["case_id", "date", "stage", "purpose", "was_heard", "event"]
        )

    try:
        df = pd.read_csv(chosen)
    except Exception:
        return pd.DataFrame(
            columns=["case_id", "date", "stage", "purpose", "was_heard", "event"]
        )

    # Normalize columns
    expected_cols = ["case_id", "date", "stage", "purpose", "was_heard", "event"]
    for col in expected_cols:
        if col not in df.columns:
            df[col] = None
    # Parse dates
    try:
        df["date"] = pd.to_datetime(df["date"]).dt.date
    except Exception:
        pass
    return df[expected_cols]


def attach_history_to_cases(cases: list, hearings_df: pd.DataFrame) -> list:
    """Attach hearing history rows to Case.history for in-memory objects.

    This does not persist anything; it only enriches the provided Case objects.
    """
    if hearings_df is None or hearings_df.empty:
        return cases

    # Build index by case_id for speed
    by_case: dict[str, list[dict]] = {}
    for row in hearings_df.to_dict("records"):
        by_case.setdefault(row["case_id"], []).append(
            {
                "date": row.get("date"),
                "event": row.get("event", "hearing"),
                "stage": row.get("stage"),
                "purpose": row.get("purpose"),
                "was_heard": bool(row.get("was_heard", 0)),
            }
        )

    for c in cases:
        hist = by_case.get(getattr(c, "case_id", None))
        if hist:
            # sort by date just in case
            hist_sorted = sorted(
                hist,
                key=lambda e: (e.get("date") or getattr(c, "filed_date", None) or 0),
            )
            c.history = hist_sorted
            # Update aggregates from history if missing
            c.hearing_count = sum(1 for e in hist_sorted if e.get("event") == "hearing")
            last = hist_sorted[-1]
            if last.get("date") is not None:
                c.last_hearing_date = last.get("date")
            if last.get("purpose"):
                c.last_hearing_purpose = last.get("purpose")
    return cases


@st.cache_data
def get_case_statistics(df: pd.DataFrame) -> dict[str, Any]:
    """Compute statistics from case DataFrame.

    Args:
        df: Case data DataFrame

    Returns:
        Dictionary of statistics
    """
    if df.empty:
        return {}

    stats = {
        "total_cases": len(df),
        "case_types": df["CaseType"].value_counts().to_dict()
        if "CaseType" in df
        else {},
        "stages": df["Remappedstages"].value_counts().to_dict()
        if "Remappedstages" in df
        else {},
    }

    # Adjournment rate if applicable
    if "Outcome" in df.columns:
        total_hearings = len(df)
        adjourned = len(df[df["Outcome"] == "ADJOURNED"])
        stats["adjournment_rate"] = (
            adjourned / total_hearings if total_hearings > 0 else 0
        )

    return stats


# RL training history loader removed as RL features are no longer supported


def get_data_status() -> dict[str, bool]:
    """Check availability of various data sources.

    Returns:
        Dictionary mapping data source to availability status
    """
    # Find latest EDA output directory
    figures_dir = Path("reports/figures")
    if figures_dir.exists():
        version_dirs = [
            d for d in figures_dir.iterdir() if d.is_dir() and d.name.startswith("v")
        ]
        if version_dirs:
            latest_dir = max(version_dirs, key=lambda p: p.stat().st_mtime)
            cleaned_data_exists = (latest_dir / "cases_clean.parquet").exists()
            params_exists = (latest_dir / "params").exists()
            # Check for HTML figures in the versioned directory
            eda_figures_exist = len(list(latest_dir.glob("*.html"))) > 0
        else:
            cleaned_data_exists = False
            params_exists = False
            eda_figures_exist = False
    else:
        cleaned_data_exists = False
        params_exists = False
        eda_figures_exist = False

    return {
        "cleaned_data": cleaned_data_exists,
        "parameters": params_exists,
        "eda_figures": eda_figures_exist,
    }