     1ÔåÆ"""
     2ÔåÆCLI command for refreshing pathway data from Snowflake.
     3ÔåÆ
     4ÔåÆThis command fetches activity data from Snowflake, processes it through the
     5ÔåÆpathway pipeline for all 6 date filter combinations, and stores the results
     6ÔåÆin the SQLite pathway_nodes table. Supports two chart types:
     7ÔåÆ- "directory": Trust ÔåÆ Directory ÔåÆ Drug ÔåÆ Pathway (default)
     8ÔåÆ- "indication": Trust ÔåÆ Search_Term ÔåÆ Drug ÔåÆ Pathway (requires GP diagnosis lookup)
     9ÔåÆ
    10ÔåÆUsage:
    11ÔåÆ    python -m cli.refresh_pathways
    12ÔåÆ    python -m cli.refresh_pathways --minimum-patients 10
    13ÔåÆ    python -m cli.refresh_pathways --provider-codes RGT,RM1
    14ÔåÆ    python -m cli.refresh_pathways --chart-type all
    15ÔåÆ    python -m cli.refresh_pathways --chart-type directory
    16ÔåÆ    python -m cli.refresh_pathways --dry-run
    17ÔåÆ
    18ÔåÆRun `python -m cli.refresh_pathways --help` for full options.
    19ÔåÆ"""
    20ÔåÆ
    21ÔåÆimport argparse
    22ÔåÆimport json
    23ÔåÆimport sqlite3
    24ÔåÆimport sys
    25ÔåÆimport time
    26ÔåÆimport uuid
    27ÔåÆfrom datetime import datetime
    28ÔåÆfrom pathlib import Path
    29ÔåÆfrom typing import Optional
    30ÔåÆ
    31ÔåÆfrom core import PathConfig, default_paths
    32ÔåÆfrom core.logging_config import get_logger, setup_logging
    33ÔåÆfrom data_processing.database import DatabaseManager, DatabaseConfig
    34ÔåÆfrom data_processing.schema import (
    35ÔåÆ    clear_pathway_nodes,
    36ÔåÆ    get_pathway_table_counts,
    37ÔåÆ    verify_pathway_tables_exist,
    38ÔåÆ    create_pathway_tables,
    39ÔåÆ)
    40ÔåÆfrom data_processing.pathway_pipeline import (
    41ÔåÆ    ChartType,
    42ÔåÆ    DATE_FILTER_CONFIGS,
    43ÔåÆ    fetch_and_transform_data,
    44ÔåÆ    process_all_date_filters,
    45ÔåÆ    process_pathway_for_date_filter,
    46ÔåÆ    process_indication_pathway_for_date_filter,
    47ÔåÆ    extract_denormalized_fields,
    48ÔåÆ    extract_indication_fields,
    49ÔåÆ    convert_to_records,
    50ÔåÆ)
    51ÔåÆfrom data_processing.diagnosis_lookup import (
    52ÔåÆ    assign_drug_indications,
    53ÔåÆ    get_patient_indication_groups,
    54ÔåÆ    load_drug_indication_mapping,
    55ÔåÆ)
    56ÔåÆ
    57ÔåÆlogger = get_logger(__name__)
    58ÔåÆ

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

"""
CLI command for refreshing pathway data from Snowflake.

This command fetches activity data from Snowflake, processes it through the
pathway pipeline for all 6 date filter combinations, and stores the results
in the SQLite pathway_nodes table. Supports two chart types:
- "directory": Trust ÔåÆ Directory ÔåÆ Drug ÔåÆ Pathway (default)
- "indication": Trust ÔåÆ Search_Term ÔåÆ Drug ÔåÆ Pathway (requires GP diagnosis lookup)

Usage:
    python -m cli.refresh_pathways
    python -m cli.refresh_pathways --minimum-patients 10
    python -m cli.refresh_pathways --provider-codes RGT,RM1
    python -m cli.refresh_pathways --chart-type all
    python -m cli.refresh_pathways --chart-type directory
    python -m cli.refresh_pathways --dry-run

Run `python -m cli.refresh_pathways --help` for full options.
"""

import argparse
import json
import sqlite3
import sys
import time
import uuid
from datetime import datetime
from pathlib import Path
from typing import Optional

from core import PathConfig, default_paths
from core.logging_config import get_logger, setup_logging
from data_processing.database import DatabaseManager, DatabaseConfig
from data_processing.schema import (
    clear_pathway_nodes,
    get_pathway_table_counts,
    verify_pathway_tables_exist,
    create_pathway_tables,
)
from data_processing.pathway_pipeline import (
    ChartType,
    DATE_FILTER_CONFIGS,
    fetch_and_transform_data,
    process_all_date_filters,
    process_pathway_for_date_filter,
    process_indication_pathway_for_date_filter,
    extract_denormalized_fields,
    extract_indication_fields,
    convert_to_records,
)
from data_processing.diagnosis_lookup import (
    assign_drug_indications,
    get_patient_indication_groups,
    load_drug_indication_mapping,
)

logger = get_logger(__name__)


Good, imports look clean. Now let me run the validation checks.


TodoWrite

Read current assign_drug_indications() and load_drug_indication_mapping() signatures

Reading function signatures from diagnosis_lookup.py

Update refresh_pathways.py indication block with drug-aware matching

Updating refresh_pathways.py indication processing

Validate: py_compile and import check

Update IMPLEMENTATION_PLAN.md and progress.txt, commit


Todos have been modified successfully. Ensure that you continue to use the todo list to track your progress. Please proceed with the current tasks if applicable

Read current assign_drug_indications() and load_drug_indication_mapping() signatures

Reading function signatures from diagnosis_lookup.py

Update refresh_pathways.py indication block with drug-aware matching

Updating refresh_pathways.py indication processing

Validate: py_compile and import check

Update IMPLEMENTATION_PLAN.md and progress.txt, commit

Validate: py_compile and import check


Bash

cd "C:\Users\charlwoodand\Ralph local\Tasks\UI Redesign" && python -m py_compile cli/refresh_pathways.py

Syntax check refresh_pathways.py


cd "C:\Users\charlwoodand\Ralph local\Tasks\UI Redesign" && python -c "from data_processing.diagnosis_lookup import assign_drug_indications, get_patient_indication_groups, load_drug_indication_mapping; print('Import OK')"

Verify imports from diagnosis_lookup


Import OK


cd "C:\Users\charlwoodand\Ralph local\Tasks\UI Redesign" && python -c "from cli.refresh_pathways import refresh_pathways; print('Full module import OK')"

Verify full module import of refresh_pathways


Full module import OK


All validation checks pass:
- `py_compile` ÔÇö no syntax errors
- Individual imports ÔÇö `assign_drug_indications`, `get_patient_indication_groups`, `load_drug_indication_mapping` all import successfully
- Full module import ÔÇö `cli.refresh_pathways` imports without error

Now let me update IMPLEMENTATION_PLAN.md to mark Task 3.1 subtasks as complete, then update progress.txt and commit.


   143ÔåÆ## Phase 3: Pipeline Integration
   144ÔåÆ
   145ÔåÆ### 3.1 Update `refresh_pathways.py` indication processing
   146ÔåÆ- [ ] In the `elif current_chart_type == "indication":` block:
   147ÔåÆ  1. Call `get_patient_indication_groups()` as before (but now returns ALL matches)
   148ÔåÆ  2. Load drug mapping: `drug_mapping = load_drug_indication_mapping()`
   149ÔåÆ  3. Call `assign_drug_indications(df, gp_matches_df, drug_mapping)`
   150ÔåÆ  4. Use modified_df (with indication-aware UPIDs) for pathway processing
   151ÔåÆ  5. Use indication_df for the indication mapping
   152ÔåÆ- [ ] Pass modified_df (not original df) to `process_indication_pathway_for_date_filter()`
   153ÔåÆ- [ ] Verify: Pipeline compiles, `python -m py_compile cli/refresh_pathways.py`
   154ÔåÆ
   155ÔåÆ### 3.2 Test with dry run
   156ÔåÆ- [ ] Run `python -m cli.refresh_pathways --chart-type indication --dry-run -v`
   157ÔåÆ- [ ] Verify:
   158ÔåÆ  - Modified UPIDs appear in pipeline log (e.g., `RMV12345|rheumatoid arthritis`)
   159ÔåÆ  - Patient counts are reasonable (will be higher than before since same patient can appear under multiple indications)
   160ÔåÆ  - Drug-indication matching is logged (match rate, fallback rate)
   161ÔåÆ  - Pathway hierarchy shows drug-specific grouping under correct indications
   162ÔåÆ

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

## Phase 3: Pipeline Integration

### 3.1 Update `refresh_pathways.py` indication processing
- [ ] In the `elif current_chart_type == "indication":` block:
  1. Call `get_patient_indication_groups()` as before (but now returns ALL matches)
  2. Load drug mapping: `drug_mapping = load_drug_indication_mapping()`
  3. Call `assign_drug_indications(df, gp_matches_df, drug_mapping)`
  4. Use modified_df (with indication-aware UPIDs) for pathway processing
  5. Use indication_df for the indication mapping
- [ ] Pass modified_df (not original df) to `process_indication_pathway_for_date_filter()`
- [ ] Verify: Pipeline compiles, `python -m py_compile cli/refresh_pathways.py`

### 3.2 Test with dry run
- [ ] Run `python -m cli.refresh_pathways --chart-type indication --dry-run -v`
- [ ] Verify:
  - Modified UPIDs appear in pipeline log (e.g., `RMV12345|rheumatoid arthritis`)
  - Patient counts are reasonable (will be higher than before since same patient can appear under multiple indications)
  - Drug-indication matching is logged (match rate, fallback rate)
  - Pathway hierarchy shows drug-specific grouping under correct indications


Edit

### 3.1 Update `refresh_pathways.py` indication processing
- [ ] In the `elif current_chart_type == "indication":` block:
  1. Call `get_patient_indication_groups()` as before (but now returns ALL matches)
  2. Load drug mapping: `drug_mapping = load_drug_indication_mapping()`
  3. Call `assign_drug_indications(df, gp_matches_df, drug_mapping)`
  4. Use modified_df (with indication-aware UPIDs) for pathway processing
  5. Use indication_df for the indication mapping
- [ ] Pass modified_df (not original df) to `process_indication_pathway_for_date_filter()`
- [ ] Verify: Pipeline compiles, `python -m py_compile cli/refresh_pathways.py`

### 3.1 Update `refresh_pathways.py` indication processing
- [x] In the `elif current_chart_type == "indication":` block:
  1. Call `get_patient_indication_groups()` as before (but now returns ALL matches)
  2. Load drug mapping: `drug_mapping = load_drug_indication_mapping()`
  3. Call `assign_drug_indications(df, gp_matches_df, drug_mapping)`
  4. Use modified_df (with indication-aware UPIDs) for pathway processing
  5. Use indication_df for the indication mapping
- [x] Pass modified_df (not original df) to `process_indication_pathway_for_date_filter()`
- [x] Verify: Pipeline compiles, `python -m py_compile cli/refresh_pathways.py`


The file C:\Users\charlwoodand\Ralph local\Tasks\UI Redesign\IMPLEMENTATION_PLAN.md has been updated successfully.

### 3.1 Update `refresh_pathways.py` indication processing
- [x] In the `elif current_chart_type == "indication":` block:
  1. Call `get_patient_indication_groups()` as before (but now returns ALL matches)
  2. Load drug mapping: `drug_mapping = load_drug_indication_mapping()`
  3. Call `assign_drug_indications(df, gp_matches_df, drug_mapping)`
  4. Use modified_df (with indication-aware UPIDs) for pathway processing
  5. Use indication_df for the indication mapping
- [x] Pass modified_df (not original df) to `process_indication_pathway_for_date_filter()`
- [x] Verify: Pipeline compiles, `python -m py_compile cli/refresh_pathways.py`

# Implementation Plan - Drug-Aware Indication Matching

## Project Overview

Update the indication-based pathway charts so that patient indications are matched **per drug**, not just per patient. Currently, each patient gets ONE indication (most recent GP diagnosis match). This ignores which drugs the patient is actually taking.

### The Problem

A patient on ADALIMUMAB + OMALIZUMAB currently gets assigned a single indication (e.g., "rheumatoid arthritis" ÔÇö the most recent GP match). But:
- ADALIMUMAB is used for rheumatoid arthritis, axial spondyloarthritis, crohn's disease, etc.
- OMALIZUMAB is used for asthma, allergic asthma, urticaria

These are different clinical pathways and should be treated as separate treatment journeys.

### The Solution

Match each drug to an indication by cross-referencing:
1. **GP diagnosis** ÔÇö which Search_Terms the patient has matching SNOMED codes for
2. **Drug mapping** ÔÇö which Search_Terms list each drug (from `DimSearchTerm.csv`)

Only assign a drug to an indication if BOTH conditions are met. If a patient's drugs map to different indications, they become separate pathways (via modified UPID).

### Key Design Decisions

| Aspect | Decision |
|--------|----------|
| Drug-indication source | `data/DimSearchTerm.csv` ÔÇö Search_Term ÔåÆ CleanedDrugName mapping |
| UPID modification | `{original_UPID}\|{search_term}` for drugs with matched indication |
| GP diagnosis matching | Return ALL matches per patient (not just most recent) |
| Drug matching | Substring match: HCD drug name contains DimSearchTerm fragment |
| Multiple indication matches per drug | Use highest GP code frequency as tiebreaker (COUNT of matching SNOMED codes per Search_Term) |
| GP code time range | Only codes from MIN(Intervention Date) onwards ÔÇö restricts to HCD data window |
| No indication match | Fallback to directory (same as current behavior) |
| Same patient, different indications | Separate pathways via different modified UPIDs |

### Examples

**Patient on ADALIMUMAB + GOLIMUMAB, GP dx: axial spondyloarthritis + asthma**
- axial spondyloarthritis drug list includes both ADALIMUMAB and GOLIMUMAB
- ÔåÆ Both drugs grouped under "axial spondyloarthritis", single pathway
- Modified UPID: `RMV12345|axial spondyloarthritis`

**Patient on ADALIMUMAB + OMALIZUMAB, GP dx: axial spondyloarthritis + asthma**
- axial spondyloarthritis lists ADALIMUMAB but not OMALIZUMAB
- asthma lists OMALIZUMAB but not ADALIMUMAB
- ÔåÆ Two separate pathways:
  - `RMV12345|axial spondyloarthritis` with ADALIMUMAB
  - `RMV12345|asthma` with OMALIZUMAB

**Patient on ADALIMUMAB, GP dx: rheumatoid arthritis (47 codes) + crohn's disease (2 codes)**
- Both Search_Terms list ADALIMUMAB AND patient has GP dx for both
- ÔåÆ Tiebreaker: highest code frequency ÔÇö rheumatoid arthritis has 47 matching SNOMED codes vs 2 for crohn's
- ÔåÆ Single pathway under rheumatoid arthritis (more clinical activity = more likely the treatment indication)

---

## Phase 1: Update Snowflake Query & Drug Mapping

### 1.1 Update `get_patient_indication_groups()` to return ALL matches with frequency
- [x] Modify the Snowflake query in `get_patient_indication_groups()` (diagnosis_lookup.py):
  - Remove `QUALIFY ROW_NUMBER() OVER (PARTITION BY ... ORDER BY EventDateTime DESC) = 1`
  - Return ALL matching Search_Terms per patient with code frequency:
    ```sql
    SELECT pc."PatientPseudonym" AS "PatientPseudonym",
           aic.Search_Term AS "Search_Term",
           COUNT(*) AS "code_frequency"
    FROM PrimaryCareClinicalCoding pc
    JOIN AllIndicationCodes aic ON pc."SNOMEDCode" = aic.SNOMEDCode
    WHERE pc."PatientPseudonym" IN (...)
      AND pc."EventDateTime" >= :earliest_hcd_date
    GROUP BY pc."PatientPseudonym", aic.Search_Term
    ```
  - `code_frequency` = number of matching SNOMED codes per Search_Term per patient
  - Higher frequency = more clinical activity = stronger signal for tiebreaker
  - `earliest_hcd_date` = `MIN(Intervention Date)` from the HCD DataFrame ÔÇö restricts GP codes to the HCD data window, reducing noise from old/irrelevant diagnoses
- [x] Accept `earliest_hcd_date` parameter in `get_patient_indication_groups()` and pass to query
- [x] Keep batch processing (500 patients per query)
- [x] Update return type: DataFrame now has multiple rows per patient (PatientPseudonym, Search_Term, code_frequency)
- [ ] Verify: Query returns more rows than before (patients with multiple matching diagnoses) *(requires live Snowflake ÔÇö will be verified in Phase 3/4)*

### 1.2 Merge related asthma Search_Terms in CLUSTER_MAPPING_SQL
- [x] In `CLUSTER_MAPPING_SQL` (diagnosis_lookup.py), merge these 3 Search_Terms into one `"asthma"` entry:
  - `allergic asthma` (Cluster: OMALIZUMAB only)
  - `asthma` (Cluster: BENRALIZUMAB, DUPILUMAB, INHALED, MEPOLIZUMAB, OMALIZUMAB, RESLIZUMAB)
  - `severe persistent allergic asthma` (Cluster: OMALIZUMAB only)
- [x] Map all 3 Cluster_IDs to `Search_Term = 'asthma'` in the CTE VALUES
- [x] `urticaria` (OMALIZUMAB, DERMATOLOGY) stays SEPARATE ÔÇö do NOT merge with asthma
- [x] Also update `load_drug_indication_mapping()` to apply the same merge when loading DimSearchTerm.csv:
  - Combine drug lists from all 3 entries under a single `"asthma"` key
  - Deduplicate drug fragments (OMALIZUMAB appears in all 3)
- [x] Verify: GP code lookup returns `"asthma"` (not `"allergic asthma"` or `"severe persistent allergic asthma"`)
- [x] Verify: Drug mapping for `"asthma"` includes full combined drug list: BENRALIZUMAB, DUPILUMAB, INHALED, MEPOLIZUMAB, OMALIZUMAB, RESLIZUMAB

### 1.3 Build drug-to-Search_Term lookup from DimSearchTerm.csv
- [x] Add function `load_drug_indication_mapping()` to `diagnosis_lookup.py`:
  - Loads `data/DimSearchTerm.csv`
  - Builds dict: `drug_fragment (uppercase) ÔåÆ list[Search_Term]`
  - Also builds reverse: `search_term ÔåÆ list[drug_fragments]`
  - CleanedDrugName is pipe-separated (e.g., "ADALIMUMAB|GOLIMUMAB|IXEKIZUMAB")
- [x] Add function `get_search_terms_for_drug(drug_name, search_term_to_fragments) -> list[str]`:
  - Returns all Search_Terms whose drug fragments are substrings of the drug name (case-insensitive)
  - More practical than per-term boolean check ÔÇö returns all matches at once for Phase 2 use
- [x] Verify: ADALIMUMAB matches "axial spondyloarthritis", OMALIZUMAB matches "asthma"

---

## Phase 2: Drug-Aware Indication Matching Logic

### 2.1 Create `assign_drug_indications()` function
- [x] Add to `diagnosis_lookup.py` or `pathway_pipeline.py`:
  ```
  def assign_drug_indications(
      df: pd.DataFrame,              # HCD data with UPID, Drug Name columns
      gp_matches_df: pd.DataFrame,   # PatientPseudonym ÔåÆ list of matched Search_Terms
      drug_mapping: dict,             # From load_drug_indication_mapping()
  ) -> tuple[pd.DataFrame, pd.DataFrame]:
      Returns: (modified_df, indication_df)
      - modified_df: HCD data with UPID replaced by {UPID}|{indication}
      - indication_df: mapping modified_UPID ÔåÆ Search_Term
  ```
- [x] Logic per UPID + Drug Name pair:
  1. Get patient's GP-matched Search_Terms with code_frequency (from gp_matches_df via PseudoNHSNoLinked)
  2. Get which Search_Terms include this drug (from drug_mapping)
  3. Intersection = valid indications for this drug-patient pair
  4. If 1 match: use it
  5. If multiple matches: use highest code_frequency as tiebreaker (most GP coding activity = most likely treatment indication)
  6. If 0 matches: use fallback directory
- [x] Modify UPID in df rows: `{original_UPID}|{matched_search_term}`
- [x] Build indication_df: `{modified_UPID}` ÔåÆ `Search_Term` (or fallback label)
- [x] Verify: Function compiles, handles edge cases (no GP match, no drug match)

### 2.2 Handle tiebreaker for multiple indication matches
- [x] When a drug matches multiple Search_Terms AND patient has GP dx for multiple:
  - Use `code_frequency` from the GP query (COUNT of matching SNOMED codes per Search_Term)
  - Higher code_frequency = more clinical activity for that condition = more likely treatment indication
  - E.g., patient with 47 RA codes and 2 crohn's codes ÔåÆ ADALIMUMAB assigned to RA
  - code_frequency is already returned by the updated query in Task 1.1
- [x] Verify: Tiebreaker logic correctly picks highest-frequency diagnosis
- [x] Verify: Tie on frequency (rare but possible) falls back to alphabetical Search_Term for determinism

---

## Phase 3: Pipeline Integration

### 3.1 Update `refresh_pathways.py` indication processing
- [ ] In the `elif current_chart_type == "indication":` block:
  1. Call `get_patient_indication_groups()` as before (but now returns ALL matches)
  2. Load drug mapping: `drug_mapping = load_drug_indication_mapping()`
  3. Call `assign_drug_indications(df, gp_matches_df, drug_mapping)`
  4. Use modified_df (with indication-aware UPIDs) for pathway processing
  5. Use indication_df for the indication mapping
- [ ] Pass modified_df (not original df) to `process_indication_pathway_for_date_filter()`
- [ ] Verify: Pipeline compiles, `python -m py_compile cli/refresh_pathways.py`

### 3.2 Test with dry run
- [ ] Run `python -m cli.refresh_pathways --chart-type indication --dry-run -v`
- [ ] Verify:
  - Modified UPIDs appear in pipeline log (e.g., `RMV12345|rheumatoid arthritis`)
  - Patient counts are reasonable (will be higher than before since same patient can appear under multiple indications)
  - Drug-indication matching is logged (match rate, fallback rate)
  - Pathway hierarchy shows drug-specific grouping under correct indications

---

## Phase 4: Full Refresh & Validation

### 4.1 Full refresh with both chart types
- [ ] Run `python -m cli.refresh_pathways --chart-type all`
- [ ] Verify:
  - Both chart types generate data
  - Directory charts unchanged (no modified UPIDs)
  - Indication charts reflect drug-aware matching

### 4.2 Validate indication chart correctness
- [ ] Check that drugs under an indication all appear in that Search_Term's drug list
- [ ] Verify that a patient on drugs for different indications creates separate pathway branches
- [ ] Verify that drugs sharing an indication are grouped in the same pathway
- [ ] Log: patient count comparison (old vs new approach)

### 4.3 Validate Reflex UI
- [ ] Run `python -m reflex compile` to verify app compiles
- [ ] Verify chart type toggle still works
- [ ] Verify indication chart shows correct hierarchy

---

## Completion Criteria

All tasks marked `[x]` AND:
- [ ] App compiles without errors (`reflex compile` succeeds)
- [ ] Both chart types generate pathway data
- [ ] Indication charts show drug-specific indication matching
- [ ] Drugs under the same indication for the same patient are in one pathway
- [ ] Drugs under different indications for the same patient create separate pathways
- [ ] Fallback works for drugs with no indication match
- [ ] Full refresh completes successfully
- [ ] Existing directory charts are unaffected

---

## Reference

### DimSearchTerm.csv Structure
```
Search_Term,CleanedDrugName,PrimaryDirectorate
rheumatoid arthritis,ABATACEPT|ADALIMUMAB|ANAKINRA|BARICITINIB|...,RHEUMATOLOGY
asthma,BENRALIZUMAB|DUPILUMAB|INHALED|MEPOLIZUMAB|OMALIZUMAB|RESLIZUMAB,THORACIC MEDICINE
```

### Modified UPID Format
```
Original:  RMV12345
Modified:  RMV12345|rheumatoid arthritis
Fallback:  RMV12345|RHEUMATOLOGY (no GP dx)
```

### Current vs New Indication Flow
```
CURRENT:
  Patient ÔåÆ GP dx (most recent) ÔåÆ single Search_Term ÔåÆ one pathway

NEW:
  Patient + Drug A ÔåÆ GP dx matching Drug A ÔåÆ Search_Term X
  Patient + Drug B ÔåÆ GP dx matching Drug B ÔåÆ Search_Term Y
  ÔåÆ If X == Y: one pathway under X
  ÔåÆ If X != Y: two pathways (modified UPIDs)
```

### Key Files

| File | Changes |
|------|---------|
| `data_processing/diagnosis_lookup.py` | Update query, add drug mapping functions |
| `data_processing/pathway_pipeline.py` | Possibly minor changes for modified UPIDs |
| `cli/refresh_pathways.py` | Integrate drug-aware matching into pipeline |
| `data/DimSearchTerm.csv` | Reference data (read-only) |
| `analysis/pathway_analyzer.py` | No changes expected (UPID changes are transparent) |
| `pathways_app/pathways_app.py` | No changes expected |

-- [ ] In the `elif current_chart_type == "indication":` block:

+- [x] In the `elif current_chart_type == "indication":` block:

-- [ ] Pass modified_df (not original df) to `process_indication_pathway_for_date_filter()`

-- [ ] Verify: Pipeline compiles, `python -m py_compile cli/refresh_pathways.py`

+- [x] Pass modified_df (not original df) to `process_indication_pathway_for_date_filter()`

+- [x] Verify: Pipeline compiles, `python -m py_compile cli/refresh_pathways.py`


Now update progress.txt with the iteration log.


   250ÔåÆ    6. Remove the old match_lookup/dict(zip) code and the manual indication_df building
   251ÔåÆ  - Import assign_drug_indications and load_drug_indication_mapping at top of file
   252ÔåÆ  - This replaces ~50 lines of the old approach with ~10 lines using the new function
   253ÔåÆ  - Can verify with py_compile; full Snowflake test via --dry-run
   254ÔåÆ### Blocked items:
   255ÔåÆ- None
   256ÔåÆ

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

    6. Remove the old match_lookup/dict(zip) code and the manual indication_df building
  - Import assign_drug_indications and load_drug_indication_mapping at top of file
  - This replaces ~50 lines of the old approach with ~10 lines using the new function
  - Can verify with py_compile; full Snowflake test via --dry-run
### Blocked items:
- None


  - Can verify with py_compile; full Snowflake test via --dry-run
### Blocked items:
- None

  - Can verify with py_compile; full Snowflake test via --dry-run
### Blocked items:
- None

## Iteration 5 ÔÇö 2026-02-05
### Task: 3.1 ÔÇö Update refresh_pathways.py indication processing to use assign_drug_indications()
### Why this task:
- All Phase 1 & 2 dependencies complete (query returns all matches, drug mapping loaded, assign_drug_indications() exists)
- Task 3.1 is the pipeline integration step ÔÇö wires the new drug-aware matching into the actual refresh pipeline
- Must be done before Task 3.2 (dry run test) which validates the integrated pipeline
### Status: COMPLETE
### What was done:
- Updated imports at top of `cli/refresh_pathways.py`:
  - Added `assign_drug_indications` and `load_drug_indication_mapping` from `data_processing.diagnosis_lookup`
- Replaced the entire indication processing block (old ~90 lines ÔåÆ new ~60 lines):
  - **Old approach**: `dict(zip(gp_matches_df['PatientPseudonym'], gp_matches_df['Search_Term']))` ÔÇö only kept LAST match per patient, no drug awareness
  - **New approach**:
    1. `load_drug_indication_mapping()` ÔåÆ `search_term_to_fragments`
    2. Compute `earliest_hcd_date` from `df['Intervention Date'].min()` as ISO string
    3. `get_patient_indication_groups(earliest_hcd_date=earliest_hcd_date_str)` ÔåÆ all GP matches with code_frequency
    4. `assign_drug_indications(df, gp_matches_df, search_term_to_fragments)` ÔåÆ `(modified_df, indication_df)`
    5. Pass `modified_df` (not original `df`) to `process_indication_pathway_for_date_filter()`
    6. `indication_df` already indexed by modified UPID with 'Directory' column ÔÇö directly compatible
- Removed: old `match_lookup`, `upid_lookup`, manual `indication_records` building, `indication_df_for_chart` renaming
- Kept: Snowflake availability check, PseudoNHSNoLinked column check, error handling, date filter loop
### Validation results:
- Tier 1 (Code): py_compile PASSED, individual imports PASSED, full module import PASSED
- Tier 2 (Data): N/A ÔÇö requires live Snowflake for dry run test (Task 3.2)
- Tier 3 (Functional): N/A ÔÇö no UI changes
### Files changed:
- cli/refresh_pathways.py (updated imports, replaced indication processing block)
- IMPLEMENTATION_PLAN.md (marked 3.1 subtasks [x])
### Committed: [pending]
### Patterns discovered:
- `assign_drug_indications()` returns `indication_df` already indexed by modified UPID with 'Directory' column ÔÇö no need for intermediate renaming/reindexing steps that the old code required
- `earliest_hcd_date` must be converted via `pd.Timestamp(...).strftime('%Y-%m-%d')` because `df['Intervention Date'].min()` may return a Timestamp or string depending on data source
- The old code had a "stats['diagnosis_coverage']" tracking block ÔÇö this is now handled internally by `assign_drug_indications()` logging. If stats tracking in the return dict is needed later, can add it back.
### Next iteration should:
- Work on Task 3.2: Run `python -m cli.refresh_pathways --chart-type indication --dry-run -v`
  - This requires a live Snowflake connection
  - Verify: modified UPIDs appear in logs, match rates logged, pathway nodes generated
  - If dry run passes, move to Phase 4 (full refresh + validation)
- Key things to check in dry run output:
  - "Drug-aware indication matching complete" log message with match/fallback counts
  - "Modified UPIDs" count should be HIGHER than unique patient count (patients with multiple drugs for different indications)
  - Pathway node counts for indication charts should be in same ballpark as before (~300 per date filter)
  - No errors in indication pathway processing
### Blocked items:
- None


The file C:\Users\charlwoodand\Ralph local\Tasks\UI Redesign\progress.txt has been updated successfully.

  - Can verify with py_compile; full Snowflake test via --dry-run
### Blocked items:
- None

## Iteration 5 ÔÇö 2026-02-05
### Task: 3.1 ÔÇö Update refresh_pathways.py indication processing to use assign_drug_indications()
### Why this task:
- All Phase 1 & 2 dependencies complete (query returns all matches, drug mapping loaded, assign_drug_indications() exists)
- Task 3.1 is the pipeline integration step ÔÇö wires the new drug-aware matching into the actual refresh pipeline
- Must be done before Task 3.2 (dry run test) which validates the integrated pipeline
### Status: COMPLETE
### What was done:
- Updated imports at top of `cli/refresh_pathways.py`:
  - Added `assign_drug_indications` and `load_drug_indication_mapping` from `data_processing.diagnosis_lookup`
- Replaced the entire indication processing block (old ~90 lines ÔåÆ new ~60 lines):
  - **Old approach**: `dict(zip(gp_matches_df['PatientPseudonym'], gp_matches_df['Search_Term']))` ÔÇö only kept LAST match per patient, no drug awareness
  - **New approach**:
    1. `load_drug_indication_mapping()` ÔåÆ `search_term_to_fragments`
    2. Compute `earliest_hcd_date` from `df['Intervention Date'].min()` as ISO string
    3. `get_patient_indication_groups(earliest_hcd_date=earliest_hcd_date_str)` ÔåÆ all GP matches with code_frequency
    4. `assign_drug_indications(df, gp_matches_df, search_term_to_fragments)` ÔåÆ `(modified_df, indication_df)`
    5. Pass `modified_df` (not original `df`) to `process_indication_pathway_for_date_filter()`
    6. `indication_df` already indexed by modified UPID with 'Directory' column ÔÇö directly compatible
- Removed: old `match_lookup`, `upid_lookup`, manual `indication_records` building, `indication_df_for_chart` renaming
- Kept: Snowflake availability check, PseudoNHSNoLinked column check, error handling, date filter loop
### Validation results:
- Tier 1 (Code): py_compile PASSED, individual imports PASSED, full module import PASSED
- Tier 2 (Data): N/A ÔÇö requires live Snowflake for dry run test (Task 3.2)
- Tier 3 (Functional): N/A ÔÇö no UI changes
### Files changed:
- cli/refresh_pathways.py (updated imports, replaced indication processing block)
- IMPLEMENTATION_PLAN.md (marked 3.1 subtasks [x])
### Committed: [pending]
### Patterns discovered:
- `assign_drug_indications()` returns `indication_df` already indexed by modified UPID with 'Directory' column ÔÇö no need for intermediate renaming/reindexing steps that the old code required
- `earliest_hcd_date` must be converted via `pd.Timestamp(...).strftime('%Y-%m-%d')` because `df['Intervention Date'].min()` may return a Timestamp or string depending on data source
- The old code had a "stats['diagnosis_coverage']" tracking block ÔÇö this is now handled internally by `assign_drug_indications()` logging. If stats tracking in the return dict is needed later, can add it back.
### Next iteration should:
- Work on Task 3.2: Run `python -m cli.refresh_pathways --chart-type indication --dry-run -v`
  - This requires a live Snowflake connection
  - Verify: modified UPIDs appear in logs, match rates logged, pathway nodes generated
  - If dry run passes, move to Phase 4 (full refresh + validation)
- Key things to check in dry run output:
  - "Drug-aware indication matching complete" log message with match/fallback counts
  - "Modified UPIDs" count should be HIGHER than unique patient count (patients with multiple drugs for different indications)
  - Pathway node counts for indication charts should be in same ballpark as before (~300 per date filter)
  - No errors in indication pathway processing
### Blocked items:
- None

# Progress Log - Drug-Aware Indication Matching

## Project Context

This project extends the indication-based pathway charts (Phase 1-5 complete) with drug-aware matching.

**Previous state**: Patients get ONE indication based on their most recent GP diagnosis match (SNOMED cluster codes). This ignores which drugs the patient is taking.

**New goal**: Match each drug to an indication by cross-referencing the patient's GP diagnoses AND the drug's Search_Term mapping from DimSearchTerm.csv.

## Key Data/Patterns

### DimSearchTerm.csv
- Located at `data/DimSearchTerm.csv`
- Columns: Search_Term, CleanedDrugName (pipe-separated), PrimaryDirectorate
- ~165 rows mapping clinical conditions to drug name fragments
- Drug fragments are substrings that match standardized drug names from HCD data
- Some entries have generic fragments: INHALED, CONTINUOUS, STANDARD-DOSE, PEGYLATED

### Current get_patient_indication_groups() in diagnosis_lookup.py
- Uses CLUSTER_MAPPING_SQL as CTE in Snowflake query
- Returns ONLY the most recent match per patient (QUALIFY ROW_NUMBER() = 1)
- Needs to return ALL matching Search_Terms per patient (remove QUALIFY)
- Batches 500 patients per query

### Modified UPID approach
- Current: UPID = Provider Code[:3] + PersonKey (e.g., "RMV12345")
- New: UPID = original + "|" + search_term (e.g., "RMV12345|rheumatoid arthritis")
- The pipe delimiter "|" is safe because existing UPIDs are alphanumeric
- generate_icicle_chart_indication() treats UPID as an opaque identifier ÔÇö modified UPIDs work transparently
- The " - " delimiter in pathway ids is used for hierarchy levels, not within UPIDs

### PseudoNHSNoLinked mapping
- HCD data has PseudoNHSNoLinked column that matches PatientPseudonym in GP records
- PersonKey is provider-specific local ID ÔÇö do NOT use for GP matching
- One PseudoNHSNoLinked can map to multiple UPIDs (multi-provider patients)
- GP match lookup: PseudoNHSNoLinked ÔåÆ list of matched Search_Terms

### Drug matching logic
- For each HCD row (UPID + Drug Name):
  1. Get patient's GP-matched Search_Terms with code_frequency (via PseudoNHSNoLinked)
  2. Get which Search_Terms list this drug (from DimSearchTerm.csv)
  3. Intersection = valid indications
  4. If 1: use it. If multiple: pick highest code_frequency (most GP coding = most likely indication). If 0: fallback to directory.
- Modified UPID groups drugs under same indication together naturally
- code_frequency = COUNT(*) of matching SNOMED codes per Search_Term per patient in GP records
- GP code time range: only count codes from MIN(Intervention Date) onwards (the HCD data window)
  - Reduces noise from old/irrelevant diagnoses, makes frequency more meaningful
  - Pass earliest_hcd_date as parameter to get_patient_indication_groups()
- Tiebreaker rationale: 47 RA codes vs 2 crohn's codes ÔåÆ RA is clearly the active condition

### Known edge cases
- Some DimSearchTerm drug fragments are generic (INHALED, ORAL, CONTINUOUS)
  - These could match broadly but are constrained by GP diagnosis requirement
- A patient visiting multiple providers has multiple UPIDs
  - Each UPID gets its own drug-indication matching independently
- Same Search_Term appears twice in DimSearchTerm.csv with different directorates
  - e.g., "diabetes" ÔåÆ DIABETIC MEDICINE and OPHTHALMOLOGY
  - For indication charts, we use Search_Term not directorate, so this is fine

## Iteration Log

## Iteration 1 ÔÇö 2026-02-05
### Task: 1.3 ÔÇö Build drug-to-Search_Term lookup from DimSearchTerm.csv
### Why this task:
- First iteration, chose Phase 1 foundations. Task 1.2 (CSV loading) is self-contained and testable locally without Snowflake.
- Task 1.1 (Snowflake query update) can't be verified without a live connection ÔÇö better to do 1.2 first.
- Both 1.1 and 1.2 are independent, so order doesn't matter for dependencies.
### Status: COMPLETE
### What was done:
- Added `load_drug_indication_mapping()` to `diagnosis_lookup.py`:
  - Loads `data/DimSearchTerm.csv`, builds two dicts:
    - `fragment_to_search_terms`: drug fragment (UPPER) ÔåÆ list of Search_Terms
    - `search_term_to_fragments`: search_term ÔåÆ list of drug fragments (UPPER)
  - Handles duplicate Search_Terms (e.g., "diabetes" rows combined)
  - Result: 164 Search_Terms, 346 drug fragments
- Added `get_search_terms_for_drug()` to `diagnosis_lookup.py`:
  - Returns all Search_Terms whose drug fragments are substrings of the drug name (case-insensitive)
  - Named differently from plan's `drug_matches_search_term()` ÔÇö returns all matches at once rather than single boolean, more practical for Phase 2
- Updated `__all__` exports
### Validation results:
- Tier 1 (Code): py_compile passed, import check passed
- Tier 2 (Data): ADALIMUMAB ÔåÆ 7 indications (including axial spondyloarthritis, rheumatoid arthritis), OMALIZUMAB ÔåÆ 4 indications (asthma, allergic asthma, etc.), PEGYLATED LIPOSOMAL DOXORUBICIN ÔåÆ 4 matches via substring, "ADALIMUMAB 40MG" matches correctly with dosage info, diabetes fragments combined from 2 CSV rows
- Tier 3 (Functional): N/A (no UI changes)
### Files changed:
- data_processing/diagnosis_lookup.py (added load_drug_indication_mapping, get_search_terms_for_drug)
- IMPLEMENTATION_PLAN.md (marked 1.2 subtasks [x])
### Committed: 0779df7 "feat: add drug-to-indication mapping from DimSearchTerm.csv (Task 1.3)"
### Patterns discovered:
- DimSearchTerm.csv has 164 unique Search_Terms (not 165 as noted) because diabetes appears twice with different directorates but same Search_Term
- Some drug fragments are very generic: INHALED, CONTINUOUS, ORAL, STANDARD-DOSE, INTRAVENOUS, PEGYLATED, ROUTINE, INDUCTION ÔÇö these will match broadly but are constrained by the GP diagnosis requirement in Phase 2
- Function signatures for Phase 2: `get_search_terms_for_drug(drug_name, search_term_to_fragments)` returns list[str] ÔÇö use this to get candidate indications per drug
### Next iteration should:
- Work on Task 1.2: Merge asthma Search_Terms in CLUSTER_MAPPING_SQL and load_drug_indication_mapping()
  - Merge "allergic asthma", "asthma", "severe persistent allergic asthma" ÔåÆ "asthma"
  - Keep "urticaria" separate
  - This is self-contained and testable locally
- OR work on Task 1.1: Update `get_patient_indication_groups()` to return ALL matches with code_frequency
  - The current query at line ~1352 of diagnosis_lookup.py uses `QUALIFY ROW_NUMBER() OVER (PARTITION BY pc."PatientPseudonym" ORDER BY pc."EventDateTime" DESC) = 1` ÔÇö this must be replaced with GROUP BY + COUNT(*)
  - Add `earliest_hcd_date` parameter to restrict GP codes to HCD data window
  - Return columns: PatientPseudonym, Search_Term, code_frequency (not EventDateTime)
- OR if Snowflake isn't available to test 1.1, skip to Task 2.1 (assign_drug_indications function) which can be built and tested with mock data
### Blocked items:
- None

## Iteration 2 ÔÇö 2026-02-05
### Task: 1.2 ÔÇö Merge related asthma Search_Terms in CLUSTER_MAPPING_SQL
### Why this task:
- Previous iteration recommended this as the next task (self-contained, testable locally)
- Both CLUSTER_MAPPING_SQL and load_drug_indication_mapping() need consistent Search_Term names
- Must be done before Task 1.1 (Snowflake query) to ensure GP lookups return "asthma" not "allergic asthma"
### Status: COMPLETE
### What was done:
- Updated CLUSTER_MAPPING_SQL: changed 'allergic asthma' ÔåÆ 'asthma' (AST_COD) and 'severe persistent allergic asthma' ÔåÆ 'asthma' (SEVAST_COD)
  - Now 3 rows for 'asthma': AST_COD, eFI2_Asthma, SEVAST_COD
  - urticaria (XSAL_COD) stays separate
- Added SEARCH_TERM_MERGE_MAP constant: {"allergic asthma": "asthma", "severe persistent allergic asthma": "asthma"}
- Updated load_drug_indication_mapping() to apply SEARCH_TERM_MERGE_MAP when loading CSV
  - Normalizes Search_Term before accumulating fragments
  - Drug fragments from all 3 original rows combined under "asthma" key
- Exported SEARCH_TERM_MERGE_MAP in __all__
### Validation results:
- Tier 1 (Code): py_compile passed, import check passed
- Tier 2 (Data):
  - "asthma" fragments: OMALIZUMAB, BENRALIZUMAB, DUPILUMAB, INHALED, MEPOLIZUMAB, RESLIZUMAB (complete combined list)
  - "allergic asthma" no longer exists as separate key
  - "severe persistent allergic asthma" no longer exists as separate key
  - "urticaria" ÔåÆ ['OMALIZUMAB'] ÔÇö correctly separate
  - OMALIZUMAB maps to: ['asthma', 'urticaria'] ÔÇö correct
  - Total Search_Terms: 162 (was 164, 3 asthma entries ÔåÆ 1)
  - Total fragments: 346 (unchanged)
- Tier 3 (Functional): N/A (no UI changes)
### Files changed:
- data_processing/diagnosis_lookup.py (CLUSTER_MAPPING_SQL, SEARCH_TERM_MERGE_MAP, load_drug_indication_mapping)
- IMPLEMENTATION_PLAN.md (marked 1.2 subtasks [x])
### Committed: b0a8a9d "feat: merge asthma Search_Term variants in CLUSTER_MAPPING_SQL and drug mapping (Task 1.2)"
### Patterns discovered:
- SEARCH_TERM_MERGE_MAP is reusable: any future module that receives Search_Terms from Snowflake can apply the same normalization
- The merge approach (normalize at load time) is cleaner than post-hoc deduplication
### Next iteration should:
- Work on Task 1.1: Update `get_patient_indication_groups()` to return ALL matches with code_frequency
  - The current query at ~line 1467 uses `QUALIFY ROW_NUMBER() OVER (PARTITION BY pc."PatientPseudonym" ORDER BY pc."EventDateTime" DESC) = 1`
  - Replace with GROUP BY + COUNT(*) for code_frequency
  - Add `earliest_hcd_date` parameter to restrict GP codes to HCD data window
  - Return columns: PatientPseudonym, Search_Term, code_frequency
  - Empty DataFrame columns should match new return type
  - This requires Snowflake connectivity to fully test, but code changes can be verified with py_compile and import checks
- OR work on Task 2.1: Create assign_drug_indications() ÔÇö can be built and tested with mock data
  - This is independent of Task 1.1 if you mock the gp_matches_df input
### Blocked items:
- None

## Iteration 3 ÔÇö 2026-02-05
### Task: 1.1 ÔÇö Update get_patient_indication_groups() to return ALL matches with code_frequency
### Why this task:
- Previous iteration recommended this as the next task
- This is the last remaining Phase 1 dependency ÔÇö Tasks 2.1/2.2 need the GP matches data in the right format (multiple rows per patient with code_frequency)
- Task 1.1 changes the query fundamentals; better to do this before building assign_drug_indications() which consumes the output
### Status: COMPLETE
### What was done:
- Modified `get_patient_indication_groups()` in `diagnosis_lookup.py`:
  1. **Removed** `QUALIFY ROW_NUMBER() OVER (PARTITION BY pc."PatientPseudonym" ORDER BY pc."EventDateTime" DESC) = 1`
  2. **Replaced** with `GROUP BY pc."PatientPseudonym", aic.Search_Term` + `COUNT(*) AS "code_frequency"`
  3. **Added** `earliest_hcd_date: Optional[str] = None` parameter
  4. **Added** optional `AND pc."EventDateTime" >= %s` when earliest_hcd_date is provided
  5. **Updated** return columns from `(PatientPseudonym, Search_Term, EventDateTime)` to `(PatientPseudonym, Search_Term, code_frequency)`
  6. **Updated** all empty DataFrame returns to use new column names
  7. **Updated** logging to show multiple-rows-per-patient stats (avg indications per patient)
  8. **Updated** docstring to describe new behavior and parameters
- Backward compatible: `earliest_hcd_date` defaults to `None`, existing callers still work
- Note: caller in `refresh_pathways.py` (line 424-428) does `dict(zip(...))` which will only keep last match per patient with new multi-row format ÔÇö this will be updated in Task 3.1
### Validation results:
- Tier 1 (Code): py_compile PASSED, import check PASSED, function signature verified
- Tier 2 (Data): Empty DataFrame returns correct columns ['PatientPseudonym', 'Search_Term', 'code_frequency']; live Snowflake test deferred to Phase 3/4
- Tier 3 (Functional): N/A (no UI changes)
### Files changed:
- data_processing/diagnosis_lookup.py (modified get_patient_indication_groups function)
- IMPLEMENTATION_PLAN.md (marked 1.1 subtasks [x])
### Committed: c93417f "feat: return ALL GP matches with code_frequency in get_patient_indication_groups (Task 1.1)"
### Patterns discovered:
- The `earliest_hcd_date` parameter is passed as a string in ISO format (YYYY-MM-DD) via Snowflake %s placeholder ÔÇö Snowflake handles string-to-timestamp comparison implicitly
- The GROUP BY approach naturally deduplicates SNOMED codes within the same Search_Term ÔÇö a patient with the same SNOMED code recorded 5 times gets code_frequency=5 (reflecting clinical activity intensity)
- params list is built dynamically: `batch_pseudonyms + [earliest_hcd_date]` only when date filter is active
### Next iteration should:
- Work on Task 2.1: Create `assign_drug_indications()` function
  - This is now unblocked since 1.1 is complete (return format is known)
  - Input: HCD df, gp_matches_df (PatientPseudonym, Search_Term, code_frequency), drug_mapping from load_drug_indication_mapping()
  - Output: (modified_df with UPID|search_term, indication_df mapping modified_UPID ÔåÆ Search_Term)
  - Can be built and tested with mock data (no Snowflake needed)
  - Key logic: for each UPID+Drug pair, intersect drug's Search_Terms with patient's GP matches, pick highest code_frequency as tiebreaker
  - The function needs PseudoNHSNoLinked to look up GP matches, so the df must have that column
  - Task 2.2 (tiebreaker logic) can be done within 2.1 or as a follow-up
- The final Phase 1 subtask (1.1 verify with live Snowflake) will be tested during Phase 3/4 integration
### Blocked items:
- Task 1.1 final subtask "Verify: Query returns more rows" requires live Snowflake ÔÇö deferred to Phase 3/4

## Iteration 4 ÔÇö 2026-02-05
### Task: 2.1 + 2.2 ÔÇö Create assign_drug_indications() with tiebreaker logic
### Why this task:
- All Phase 1 dependencies complete (1.1 query returns ALL matches, 1.2 asthma merged, 1.3 drug mapping loaded)
- Task 2.1 is the core matching function needed before Phase 3 pipeline integration
- Task 2.2 (tiebreaker) is naturally part of 2.1 ÔÇö implemented together
- Can be built and tested with mock data (no Snowflake needed)
### Status: COMPLETE
### What was done:
- Added `assign_drug_indications()` to `diagnosis_lookup.py`:
  - Input: HCD df (with UPID, Drug Name, PseudoNHSNoLinked, Directory), gp_matches_df, search_term_to_fragments
  - Output: (modified_df with UPID|search_term, indication_df mapping modified_UPID ÔåÆ Search_Term)
  - Builds GP lookup: PseudoNHSNoLinked ÔåÆ {Search_Term: code_frequency}
  - Caches drugÔåÆSearch_Term lookups to avoid recomputing per row
  - For each (UPID, Drug Name) pair:
    - Intersects drug's Search_Terms with patient's GP matches
    - Single match: use it
    - Multiple matches: highest code_frequency wins, alphabetical tiebreak
    - No match: fallback to "{Directory} (no GP dx)"
  - Applies modified UPIDs via df.apply() (vectorized lookup from cache)
  - Builds indication_df with unique modified UPID ÔåÆ Directory column
  - Comprehensive logging: match rate, tiebreaker count, fallback count, top 5 indications
- Updated __all__ exports
### Validation results:
- Tier 1 (Code): py_compile PASSED, import check PASSED
- Tier 2 (Data): Mock data tests ALL PASSED:
  - ADALIMUMAB + GP dx (RA + asthma) ÔåÆ matched to RA (drug mapping intersection)
  - OMALIZUMAB + GP dx (RA + asthma) ÔåÆ matched to asthma (drug mapping intersection)
  - ADALIMUMAB + GP dx (RA 3 freq + crohn's 2 freq) ÔåÆ tiebreaker picks RA
  - ADALIMUMAB + GP dx (psoriatic 5 freq + RA 5 freq) ÔåÆ alphabetical tiebreak picks psoriatic arthritis
  - Higher frequency (47 RA vs 3 psoriatic) ÔåÆ RA wins
  - No GP match ÔåÆ fallback to directory
  - Empty GP DataFrame ÔåÆ all fallback
  - Different drugs with different indications ÔåÆ different modified UPIDs
- Tier 3 (Functional): N/A (no UI changes yet)
### Files changed:
- data_processing/diagnosis_lookup.py (added assign_drug_indications, updated __all__)
- IMPLEMENTATION_PLAN.md (marked 2.1 and 2.2 subtasks [x])
### Committed: 408976e "feat: add assign_drug_indications() for drug-aware indication matching (Task 2.1 + 2.2)"
### Patterns discovered:
- Function signature takes `search_term_to_fragments` (the second element from load_drug_indication_mapping()) ÔÇö NOT the full tuple. Callers must destructure: `_, st_to_frags = load_drug_indication_mapping()`
- The function uses df.apply() to set modified UPIDs ÔÇö for large DataFrames (656K rows), this could be slow. If performance is an issue in Phase 3, could vectorize with merge operations instead. But apply with cached lookup dict should be OK.
- "crohn's disease" is NOT in ADALIMUMAB's DimSearchTerm mapping (ADALIMUMAB maps to: ankylosing spondylitis, axial spondyloarthritis, plaque psoriasis, psoriatic arthritis, rheumatoid arthritis, ulcerative colitis, uveitis). Initial test assumption was wrong.
- indication_df has 'Directory' column (not 'Search_Term') for compatibility with generate_icicle_chart_indication() which expects indication_df.loc[upid, 'Directory']
### Next iteration should:
- Work on Task 3.1: Update `refresh_pathways.py` indication processing to use assign_drug_indications()
  - The current code at lines 424-428 uses `dict(zip(...))` which only keeps LAST match per patient ÔÇö this must be replaced
  - Key changes in the `elif current_chart_type == "indication":` block:
    1. Load drug mapping: `_, st_to_frags = load_drug_indication_mapping()`
    2. Pass `earliest_hcd_date=df['Intervention Date'].min().strftime('%Y-%m-%d')` to get_patient_indication_groups()
    3. Call `assign_drug_indications(df, gp_matches_df, st_to_frags)` to get (modified_df, indication_df)
    4. Use modified_df (not original df) for pathway processing
    5. indication_df is already in the right format (indexed by modified UPID, 'Directory' column)
    6. Remove the old match_lookup/dict(zip) code and the manual indication_df building
  - Import assign_drug_indications and load_drug_indication_mapping at top of file
  - This replaces ~50 lines of the old approach with ~10 lines using the new function
  - Can verify with py_compile; full Snowflake test via --dry-run
### Blocked items:
- None

+### Task: 3.1 ÔÇö Update refresh_pathways.py indication processing to use assign_drug_indications()

+- All Phase 1 & 2 dependencies complete (query returns all matches, drug mapping loaded, assign_drug_indications() exists)

+- Task 3.1 is the pipeline integration step ÔÇö wires the new drug-aware matching into the actual refresh pipeline

+- Must be done before Task 3.2 (dry run test) which validates the integrated pipeline

+- Updated imports at top of `cli/refresh_pathways.py`:

+  - Added `assign_drug_indications` and `load_drug_indication_mapping` from `data_processing.diagnosis_lookup`

+- Replaced the entire indication processing block (old ~90 lines ÔåÆ new ~60 lines):

+  - **Old approach**: `dict(zip(gp_matches_df['PatientPseudonym'], gp_matches_df['Search_Term']))` ÔÇö only kept LAST match per patient, no drug awareness

+    1. `load_drug_indication_mapping()` ÔåÆ `search_term_to_fragments`

+    2. Compute `earliest_hcd_date` from `df['Intervention Date'].min()` as ISO string

+    3. `get_patient_indication_groups(earliest_hcd_date=earliest_hcd_date_str)` ÔåÆ all GP matches with code_frequency

+    4. `assign_drug_indications(df, gp_matches_df, search_term_to_fragments)` ÔåÆ `(modified_df, indication_df)`

+    5. Pass `modified_df` (not original `df`) to `process_indication_pathway_for_date_filter()`

+    6. `indication_df` already indexed by modified UPID with 'Directory' column ÔÇö directly compatible

+- Removed: old `match_lookup`, `upid_lookup`, manual `indication_records` building, `indication_df_for_chart` renaming

+- Kept: Snowflake availability check, PseudoNHSNoLinked column check, error handling, date filter loop

+- Tier 1 (Code): py_compile PASSED, individual imports PASSED, full module import PASSED

+- Tier 2 (Data): N/A ÔÇö requires live Snowflake for dry run test (Task 3.2)

+- Tier 3 (Functional): N/A ÔÇö no UI changes

+- cli/refresh_pathways.py (updated imports, replaced indication processing block)

+- IMPLEMENTATION_PLAN.md (marked 3.1 subtasks [x])

+- `assign_drug_indications()` returns `indication_df` already indexed by modified UPID with 'Directory' column ÔÇö no need for intermediate renaming/reindexing steps that the old code required

+- `earliest_hcd_date` must be converted via `pd.Timestamp(...).strftime('%Y-%m-%d')` because `df['Intervention Date'].min()` may return a Timestamp or string depending on data source

+- The old code had a "stats['diagnosis_coverage']" tracking block ÔÇö this is now handled internally by `assign_drug_indications()` logging. If stats tracking in the return dict is needed later, can add it back.

+- Work on Task 3.2: Run `python -m cli.refresh_pathways --chart-type indication --dry-run -v`

+  - This requires a live Snowflake connection

+  - Verify: modified UPIDs appear in logs, match rates logged, pathway nodes generated

+  - If dry run passes, move to Phase 4 (full refresh + validation)

+- Key things to check in dry run output:

+  - "Drug-aware indication matching complete" log message with match/fallback counts

+  - "Modified UPIDs" count should be HIGHER than unique patient count (patients with multiple drugs for different indications)

+  - Pathway node counts for indication charts should be in same ballpark as before (~300 per date filter)

+  - No errors in indication pathway processing


TodoWrite

Read current assign_drug_indications() and load_drug_indication_mapping() signatures

Reading function signatures from diagnosis_lookup.py

Update refresh_pathways.py indication block with drug-aware matching

Updating refresh_pathways.py indication processing

Validate: py_compile and import check

Update IMPLEMENTATION_PLAN.md and progress.txt, commit


Validate: py_compile and import check

Validate: py_compile and import check


Now let me commit the changes.


Bash

cd "C:\Users\charlwoodand\Ralph local\Tasks\UI Redesign" && git status

Check git status before committing


On branch feature/snomed-indication-mapping
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   .claude/settings.local.json
	modified:   DESIGN_SYSTEM.md
	modified:   IMPLEMENTATION_PLAN.md
	modified:   RALPH_PROMPT.md
	modified:   cli/refresh_pathways.py
	modified:   guardrails.md
	modified:   pathways_app/__init__.py
	deleted:    pathways_app/app_v2.py
	modified:   progress.txt
	modified:   ralph.ps1
	modified:   uv.lock

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	data/DimSearchTerm.csv
	data/LookupSearchTermCleanedDrugName.csv
	data/condition_directorate_mapping.csv
	data/drug_indication_clusters.csv
	data/drug_snomed_mapping_enriched.csv
	data/na_directory_rows.csv
	pathways_app/pathways_app_old.py.bak
	snomed_indication_mapping_query.sql

no changes added to commit (use "git add" and/or "git commit -a")


I'll only stage the files I actually changed in this iteration: `cli/refresh_pathways.py`, `IMPLEMENTATION_PLAN.md`, and `progress.txt`.


cd "C:\Users\charlwoodand\Ralph local\Tasks\UI Redesign" && git add cli/refresh_pathways.py IMPLEMENTATION_PLAN.md progress.txt

Stage changed files for commit


cd "C:\Users\charlwoodand\Ralph local\Tasks\UI Redesign" && git diff --cached --stat

Verify staged changes


 IMPLEMENTATION_PLAN.md  |   6 +--
 cli/refresh_pathways.py | 141 +++++++++++++++---------------------------------
 progress.txt            |  46 ++++++++++++++++
 3 files changed, 93 insertions(+), 100 deletions(-)


Good ÔÇö 3 files, net reduction of 7 lines in refresh_pathways.py (cleaner code), plus progress notes.


cd "C:\Users\charlwoodand\Ralph local\Tasks\UI Redesign" && git commit -m "$(cat <<'EOF'
feat: integrate drug-aware indication matching into refresh pipeline (Task 3.1)

Replace old per-patient indication matching in refresh_pathways.py with
drug-aware matching via assign_drug_indications(). Each drug is now
cross-referenced against both the patient's GP diagnoses AND the
DimSearchTerm.csv drug mapping. GP codes restricted to HCD data window
via earliest_hcd_date parameter.
EOF
)"

Commit Task 3.1 changes


[feature/snomed-indication-mapping 920570b] feat: integrate drug-aware indication matching into refresh pipeline (Task 3.1)
 3 files changed, 93 insertions(+), 100 deletions(-)


Now update the commit hash in progress.txt.


Edit

### Committed: 920570b "feat: integrate drug-aware indication matching into refresh pipeline (Task 3.1)"


# Progress Log - Drug-Aware Indication Matching

## Project Context

This project extends the indication-based pathway charts (Phase 1-5 complete) with drug-aware matching.

**Previous state**: Patients get ONE indication based on their most recent GP diagnosis match (SNOMED cluster codes). This ignores which drugs the patient is taking.

**New goal**: Match each drug to an indication by cross-referencing the patient's GP diagnoses AND the drug's Search_Term mapping from DimSearchTerm.csv.

## Key Data/Patterns

### DimSearchTerm.csv
- Located at `data/DimSearchTerm.csv`
- Columns: Search_Term, CleanedDrugName (pipe-separated), PrimaryDirectorate
- ~165 rows mapping clinical conditions to drug name fragments
- Drug fragments are substrings that match standardized drug names from HCD data
- Some entries have generic fragments: INHALED, CONTINUOUS, STANDARD-DOSE, PEGYLATED

### Current get_patient_indication_groups() in diagnosis_lookup.py
- Uses CLUSTER_MAPPING_SQL as CTE in Snowflake query
- Returns ONLY the most recent match per patient (QUALIFY ROW_NUMBER() = 1)
- Needs to return ALL matching Search_Terms per patient (remove QUALIFY)
- Batches 500 patients per query

### Modified UPID approach
- Current: UPID = Provider Code[:3] + PersonKey (e.g., "RMV12345")
- New: UPID = original + "|" + search_term (e.g., "RMV12345|rheumatoid arthritis")
- The pipe delimiter "|" is safe because existing UPIDs are alphanumeric
- generate_icicle_chart_indication() treats UPID as an opaque identifier ÔÇö modified UPIDs work transparently
- The " - " delimiter in pathway ids is used for hierarchy levels, not within UPIDs

### PseudoNHSNoLinked mapping
- HCD data has PseudoNHSNoLinked column that matches PatientPseudonym in GP records
- PersonKey is provider-specific local ID ÔÇö do NOT use for GP matching
- One PseudoNHSNoLinked can map to multiple UPIDs (multi-provider patients)
- GP match lookup: PseudoNHSNoLinked ÔåÆ list of matched Search_Terms

### Drug matching logic
- For each HCD row (UPID + Drug Name):
  1. Get patient's GP-matched Search_Terms with code_frequency (via PseudoNHSNoLinked)
  2. Get which Search_Terms list this drug (from DimSearchTerm.csv)
  3. Intersection = valid indications
  4. If 1: use it. If multiple: pick highest code_frequency (most GP coding = most likely indication). If 0: fallback to directory.
- Modified UPID groups drugs under same indication together naturally
- code_frequency = COUNT(*) of matching SNOMED codes per Search_Term per patient in GP records
- GP code time range: only count codes from MIN(Intervention Date) onwards (the HCD data window)
  - Reduces noise from old/irrelevant diagnoses, makes frequency more meaningful
  - Pass earliest_hcd_date as parameter to get_patient_indication_groups()
- Tiebreaker rationale: 47 RA codes vs 2 crohn's codes ÔåÆ RA is clearly the active condition

### Known edge cases
- Some DimSearchTerm drug fragments are generic (INHALED, ORAL, CONTINUOUS)
  - These could match broadly but are constrained by GP diagnosis requirement
- A patient visiting multiple providers has multiple UPIDs
  - Each UPID gets its own drug-indication matching independently
- Same Search_Term appears twice in DimSearchTerm.csv with different directorates
  - e.g., "diabetes" ÔåÆ DIABETIC MEDICINE and OPHTHALMOLOGY
  - For indication charts, we use Search_Term not directorate, so this is fine

## Iteration Log

## Iteration 1 ÔÇö 2026-02-05
### Task: 1.3 ÔÇö Build drug-to-Search_Term lookup from DimSearchTerm.csv
### Why this task:
- First iteration, chose Phase 1 foundations. Task 1.2 (CSV loading) is self-contained and testable locally without Snowflake.
- Task 1.1 (Snowflake query update) can't be verified without a live connection ÔÇö better to do 1.2 first.
- Both 1.1 and 1.2 are independent, so order doesn't matter for dependencies.
### Status: COMPLETE
### What was done:
- Added `load_drug_indication_mapping()` to `diagnosis_lookup.py`:
  - Loads `data/DimSearchTerm.csv`, builds two dicts:
    - `fragment_to_search_terms`: drug fragment (UPPER) ÔåÆ list of Search_Terms
    - `search_term_to_fragments`: search_term ÔåÆ list of drug fragments (UPPER)
  - Handles duplicate Search_Terms (e.g., "diabetes" rows combined)
  - Result: 164 Search_Terms, 346 drug fragments
- Added `get_search_terms_for_drug()` to `diagnosis_lookup.py`:
  - Returns all Search_Terms whose drug fragments are substrings of the drug name (case-insensitive)
  - Named differently from plan's `drug_matches_search_term()` ÔÇö returns all matches at once rather than single boolean, more practical for Phase 2
- Updated `__all__` exports
### Validation results:
- Tier 1 (Code): py_compile passed, import check passed
- Tier 2 (Data): ADALIMUMAB ÔåÆ 7 indications (including axial spondyloarthritis, rheumatoid arthritis), OMALIZUMAB ÔåÆ 4 indications (asthma, allergic asthma, etc.), PEGYLATED LIPOSOMAL DOXORUBICIN ÔåÆ 4 matches via substring, "ADALIMUMAB 40MG" matches correctly with dosage info, diabetes fragments combined from 2 CSV rows
- Tier 3 (Functional): N/A (no UI changes)
### Files changed:
- data_processing/diagnosis_lookup.py (added load_drug_indication_mapping, get_search_terms_for_drug)
- IMPLEMENTATION_PLAN.md (marked 1.2 subtasks [x])
### Committed: 0779df7 "feat: add drug-to-indication mapping from DimSearchTerm.csv (Task 1.3)"
### Patterns discovered:
- DimSearchTerm.csv has 164 unique Search_Terms (not 165 as noted) because diabetes appears twice with different directorates but same Search_Term
- Some drug fragments are very generic: INHALED, CONTINUOUS, ORAL, STANDARD-DOSE, INTRAVENOUS, PEGYLATED, ROUTINE, INDUCTION ÔÇö these will match broadly but are constrained by the GP diagnosis requirement in Phase 2
- Function signatures for Phase 2: `get_search_terms_for_drug(drug_name, search_term_to_fragments)` returns list[str] ÔÇö use this to get candidate indications per drug
### Next iteration should:
- Work on Task 1.2: Merge asthma Search_Terms in CLUSTER_MAPPING_SQL and load_drug_indication_mapping()
  - Merge "allergic asthma", "asthma", "severe persistent allergic asthma" ÔåÆ "asthma"
  - Keep "urticaria" separate
  - This is self-contained and testable locally
- OR work on Task 1.1: Update `get_patient_indication_groups()` to return ALL matches with code_frequency
  - The current query at line ~1352 of diagnosis_lookup.py uses `QUALIFY ROW_NUMBER() OVER (PARTITION BY pc."PatientPseudonym" ORDER BY pc."EventDateTime" DESC) = 1` ÔÇö this must be replaced with GROUP BY + COUNT(*)
  - Add `earliest_hcd_date` parameter to restrict GP codes to HCD data window
  - Return columns: PatientPseudonym, Search_Term, code_frequency (not EventDateTime)
- OR if Snowflake isn't available to test 1.1, skip to Task 2.1 (assign_drug_indications function) which can be built and tested with mock data
### Blocked items:
- None

## Iteration 2 ÔÇö 2026-02-05
### Task: 1.2 ÔÇö Merge related asthma Search_Terms in CLUSTER_MAPPING_SQL
### Why this task:
- Previous iteration recommended this as the next task (self-contained, testable locally)
- Both CLUSTER_MAPPING_SQL and load_drug_indication_mapping() need consistent Search_Term names
- Must be done before Task 1.1 (Snowflake query) to ensure GP lookups return "asthma" not "allergic asthma"
### Status: COMPLETE
### What was done:
- Updated CLUSTER_MAPPING_SQL: changed 'allergic asthma' ÔåÆ 'asthma' (AST_COD) and 'severe persistent allergic asthma' ÔåÆ 'asthma' (SEVAST_COD)
  - Now 3 rows for 'asthma': AST_COD, eFI2_Asthma, SEVAST_COD
  - urticaria (XSAL_COD) stays separate
- Added SEARCH_TERM_MERGE_MAP constant: {"allergic asthma": "asthma", "severe persistent allergic asthma": "asthma"}
- Updated load_drug_indication_mapping() to apply SEARCH_TERM_MERGE_MAP when loading CSV
  - Normalizes Search_Term before accumulating fragments
  - Drug fragments from all 3 original rows combined under "asthma" key
- Exported SEARCH_TERM_MERGE_MAP in __all__
### Validation results:
- Tier 1 (Code): py_compile passed, import check passed
- Tier 2 (Data):
  - "asthma" fragments: OMALIZUMAB, BENRALIZUMAB, DUPILUMAB, INHALED, MEPOLIZUMAB, RESLIZUMAB (complete combined list)
  - "allergic asthma" no longer exists as separate key
  - "severe persistent allergic asthma" no longer exists as separate key
  - "urticaria" ÔåÆ ['OMALIZUMAB'] ÔÇö correctly separate
  - OMALIZUMAB maps to: ['asthma', 'urticaria'] ÔÇö correct
  - Total Search_Terms: 162 (was 164, 3 asthma entries ÔåÆ 1)
  - Total fragments: 346 (unchanged)
- Tier 3 (Functional): N/A (no UI changes)
### Files changed:
- data_processing/diagnosis_lookup.py (CLUSTER_MAPPING_SQL, SEARCH_TERM_MERGE_MAP, load_drug_indication_mapping)
- IMPLEMENTATION_PLAN.md (marked 1.2 subtasks [x])
### Committed: b0a8a9d "feat: merge asthma Search_Term variants in CLUSTER_MAPPING_SQL and drug mapping (Task 1.2)"
### Patterns discovered:
- SEARCH_TERM_MERGE_MAP is reusable: any future module that receives Search_Terms from Snowflake can apply the same normalization
- The merge approach (normalize at load time) is cleaner than post-hoc deduplication
### Next iteration should:
- Work on Task 1.1: Update `get_patient_indication_groups()` to return ALL matches with code_frequency
  - The current query at ~line 1467 uses `QUALIFY ROW_NUMBER() OVER (PARTITION BY pc."PatientPseudonym" ORDER BY pc."EventDateTime" DESC) = 1`
  - Replace with GROUP BY + COUNT(*) for code_frequency
  - Add `earliest_hcd_date` parameter to restrict GP codes to HCD data window
  - Return columns: PatientPseudonym, Search_Term, code_frequency
  - Empty DataFrame columns should match new return type
  - This requires Snowflake connectivity to fully test, but code changes can be verified with py_compile and import checks
- OR work on Task 2.1: Create assign_drug_indications() ÔÇö can be built and tested with mock data
  - This is independent of Task 1.1 if you mock the gp_matches_df input
### Blocked items:
- None

## Iteration 3 ÔÇö 2026-02-05
### Task: 1.1 ÔÇö Update get_patient_indication_groups() to return ALL matches with code_frequency
### Why this task:
- Previous iteration recommended this as the next task
- This is the last remaining Phase 1 dependency ÔÇö Tasks 2.1/2.2 need the GP matches data in the right format (multiple rows per patient with code_frequency)
- Task 1.1 changes the query fundamentals; better to do this before building assign_drug_indications() which consumes the output
### Status: COMPLETE
### What was done:
- Modified `get_patient_indication_groups()` in `diagnosis_lookup.py`:
  1. **Removed** `QUALIFY ROW_NUMBER() OVER (PARTITION BY pc."PatientPseudonym" ORDER BY pc."EventDateTime" DESC) = 1`
  2. **Replaced** with `GROUP BY pc."PatientPseudonym", aic.Search_Term` + `COUNT(*) AS "code_frequency"`
  3. **Added** `earliest_hcd_date: Optional[str] = None` parameter
  4. **Added** optional `AND pc."EventDateTime" >= %s` when earliest_hcd_date is provided
  5. **Updated** return columns from `(PatientPseudonym, Search_Term, EventDateTime)` to `(PatientPseudonym, Search_Term, code_frequency)`
  6. **Updated** all empty DataFrame returns to use new column names
  7. **Updated** logging to show multiple-rows-per-patient stats (avg indications per patient)
  8. **Updated** docstring to describe new behavior and parameters
- Backward compatible: `earliest_hcd_date` defaults to `None`, existing callers still work
- Note: caller in `refresh_pathways.py` (line 424-428) does `dict(zip(...))` which will only keep last match per patient with new multi-row format ÔÇö this will be updated in Task 3.1
### Validation results:
- Tier 1 (Code): py_compile PASSED, import check PASSED, function signature verified
- Tier 2 (Data): Empty DataFrame returns correct columns ['PatientPseudonym', 'Search_Term', 'code_frequency']; live Snowflake test deferred to Phase 3/4
- Tier 3 (Functional): N/A (no UI changes)
### Files changed:
- data_processing/diagnosis_lookup.py (modified get_patient_indication_groups function)
- IMPLEMENTATION_PLAN.md (marked 1.1 subtasks [x])
### Committed: c93417f "feat: return ALL GP matches with code_frequency in get_patient_indication_groups (Task 1.1)"
### Patterns discovered:
- The `earliest_hcd_date` parameter is passed as a string in ISO format (YYYY-MM-DD) via Snowflake %s placeholder ÔÇö Snowflake handles string-to-timestamp comparison implicitly
- The GROUP BY approach naturally deduplicates SNOMED codes within the same Search_Term ÔÇö a patient with the same SNOMED code recorded 5 times gets code_frequency=5 (reflecting clinical activity intensity)
- params list is built dynamically: `batch_pseudonyms + [earliest_hcd_date]` only when date filter is active
### Next iteration should:
- Work on Task 2.1: Create `assign_drug_indications()` function
  - This is now unblocked since 1.1 is complete (return format is known)
  - Input: HCD df, gp_matches_df (PatientPseudonym, Search_Term, code_frequency), drug_mapping from load_drug_indication_mapping()
  - Output: (modified_df with UPID|search_term, indication_df mapping modified_UPID ÔåÆ Search_Term)
  - Can be built and tested with mock data (no Snowflake needed)
  - Key logic: for each UPID+Drug pair, intersect drug's Search_Terms with patient's GP matches, pick highest code_frequency as tiebreaker
  - The function needs PseudoNHSNoLinked to look up GP matches, so the df must have that column
  - Task 2.2 (tiebreaker logic) can be done within 2.1 or as a follow-up
- The final Phase 1 subtask (1.1 verify with live Snowflake) will be tested during Phase 3/4 integration
### Blocked items:
- Task 1.1 final subtask "Verify: Query returns more rows" requires live Snowflake ÔÇö deferred to Phase 3/4

## Iteration 4 ÔÇö 2026-02-05
### Task: 2.1 + 2.2 ÔÇö Create assign_drug_indications() with tiebreaker logic
### Why this task:
- All Phase 1 dependencies complete (1.1 query returns ALL matches, 1.2 asthma merged, 1.3 drug mapping loaded)
- Task 2.1 is the core matching function needed before Phase 3 pipeline integration
- Task 2.2 (tiebreaker) is naturally part of 2.1 ÔÇö implemented together
- Can be built and tested with mock data (no Snowflake needed)
### Status: COMPLETE
### What was done:
- Added `assign_drug_indications()` to `diagnosis_lookup.py`:
  - Input: HCD df (with UPID, Drug Name, PseudoNHSNoLinked, Directory), gp_matches_df, search_term_to_fragments
  - Output: (modified_df with UPID|search_term, indication_df mapping modified_UPID ÔåÆ Search_Term)
  - Builds GP lookup: PseudoNHSNoLinked ÔåÆ {Search_Term: code_frequency}
  - Caches drugÔåÆSearch_Term lookups to avoid recomputing per row
  - For each (UPID, Drug Name) pair:
    - Intersects drug's Search_Terms with patient's GP matches
    - Single match: use it
    - Multiple matches: highest code_frequency wins, alphabetical tiebreak
    - No match: fallback to "{Directory} (no GP dx)"
  - Applies modified UPIDs via df.apply() (vectorized lookup from cache)
  - Builds indication_df with unique modified UPID ÔåÆ Directory column
  - Comprehensive logging: match rate, tiebreaker count, fallback count, top 5 indications
- Updated __all__ exports
### Validation results:
- Tier 1 (Code): py_compile PASSED, import check PASSED
- Tier 2 (Data): Mock data tests ALL PASSED:
  - ADALIMUMAB + GP dx (RA + asthma) ÔåÆ matched to RA (drug mapping intersection)
  - OMALIZUMAB + GP dx (RA + asthma) ÔåÆ matched to asthma (drug mapping intersection)
  - ADALIMUMAB + GP dx (RA 3 freq + crohn's 2 freq) ÔåÆ tiebreaker picks RA
  - ADALIMUMAB + GP dx (psoriatic 5 freq + RA 5 freq) ÔåÆ alphabetical tiebreak picks psoriatic arthritis
  - Higher frequency (47 RA vs 3 psoriatic) ÔåÆ RA wins
  - No GP match ÔåÆ fallback to directory
  - Empty GP DataFrame ÔåÆ all fallback
  - Different drugs with different indications ÔåÆ different modified UPIDs
- Tier 3 (Functional): N/A (no UI changes yet)
### Files changed:
- data_processing/diagnosis_lookup.py (added assign_drug_indications, updated __all__)
- IMPLEMENTATION_PLAN.md (marked 2.1 and 2.2 subtasks [x])
### Committed: 408976e "feat: add assign_drug_indications() for drug-aware indication matching (Task 2.1 + 2.2)"
### Patterns discovered:
- Function signature takes `search_term_to_fragments` (the second element from load_drug_indication_mapping()) ÔÇö NOT the full tuple. Callers must destructure: `_, st_to_frags = load_drug_indication_mapping()`
- The function uses df.apply() to set modified UPIDs ÔÇö for large DataFrames (656K rows), this could be slow. If performance is an issue in Phase 3, could vectorize with merge operations instead. But apply with cached lookup dict should be OK.
- "crohn's disease" is NOT in ADALIMUMAB's DimSearchTerm mapping (ADALIMUMAB maps to: ankylosing spondylitis, axial spondyloarthritis, plaque psoriasis, psoriatic arthritis, rheumatoid arthritis, ulcerative colitis, uveitis). Initial test assumption was wrong.
- indication_df has 'Directory' column (not 'Search_Term') for compatibility with generate_icicle_chart_indication() which expects indication_df.loc[upid, 'Directory']
### Next iteration should:
- Work on Task 3.1: Update `refresh_pathways.py` indication processing to use assign_drug_indications()
  - The current code at lines 424-428 uses `dict(zip(...))` which only keeps LAST match per patient ÔÇö this must be replaced
  - Key changes in the `elif current_chart_type == "indication":` block:
    1. Load drug mapping: `_, st_to_frags = load_drug_indication_mapping()`
    2. Pass `earliest_hcd_date=df['Intervention Date'].min().strftime('%Y-%m-%d')` to get_patient_indication_groups()
    3. Call `assign_drug_indications(df, gp_matches_df, st_to_frags)` to get (modified_df, indication_df)
    4. Use modified_df (not original df) for pathway processing
    5. indication_df is already in the right format (indexed by modified UPID, 'Directory' column)
    6. Remove the old match_lookup/dict(zip) code and the manual indication_df building
  - Import assign_drug_indications and load_drug_indication_mapping at top of file
  - This replaces ~50 lines of the old approach with ~10 lines using the new function
  - Can verify with py_compile; full Snowflake test via --dry-run
### Blocked items:
- None

## Iteration 5 ÔÇö 2026-02-05
### Task: 3.1 ÔÇö Update refresh_pathways.py indication processing to use assign_drug_indications()
### Why this task:
- All Phase 1 & 2 dependencies complete (query returns all matches, drug mapping loaded, assign_drug_indications() exists)
- Task 3.1 is the pipeline integration step ÔÇö wires the new drug-aware matching into the actual refresh pipeline
- Must be done before Task 3.2 (dry run test) which validates the integrated pipeline
### Status: COMPLETE
### What was done:
- Updated imports at top of `cli/refresh_pathways.py`:
  - Added `assign_drug_indications` and `load_drug_indication_mapping` from `data_processing.diagnosis_lookup`
- Replaced the entire indication processing block (old ~90 lines ÔåÆ new ~60 lines):
  - **Old approach**: `dict(zip(gp_matches_df['PatientPseudonym'], gp_matches_df['Search_Term']))` ÔÇö only kept LAST match per patient, no drug awareness
  - **New approach**:
    1. `load_drug_indication_mapping()` ÔåÆ `search_term_to_fragments`
    2. Compute `earliest_hcd_date` from `df['Intervention Date'].min()` as ISO string
    3. `get_patient_indication_groups(earliest_hcd_date=earliest_hcd_date_str)` ÔåÆ all GP matches with code_frequency
    4. `assign_drug_indications(df, gp_matches_df, search_term_to_fragments)` ÔåÆ `(modified_df, indication_df)`
    5. Pass `modified_df` (not original `df`) to `process_indication_pathway_for_date_filter()`
    6. `indication_df` already indexed by modified UPID with 'Directory' column ÔÇö directly compatible
- Removed: old `match_lookup`, `upid_lookup`, manual `indication_records` building, `indication_df_for_chart` renaming
- Kept: Snowflake availability check, PseudoNHSNoLinked column check, error handling, date filter loop
### Validation results:
- Tier 1 (Code): py_compile PASSED, individual imports PASSED, full module import PASSED
- Tier 2 (Data): N/A ÔÇö requires live Snowflake for dry run test (Task 3.2)
- Tier 3 (Functional): N/A ÔÇö no UI changes
### Files changed:
- cli/refresh_pathways.py (updated imports, replaced indication processing block)
- IMPLEMENTATION_PLAN.md (marked 3.1 subtasks [x])
### Committed: [pending]
### Patterns discovered:
- `assign_drug_indications()` returns `indication_df` already indexed by modified UPID with 'Directory' column ÔÇö no need for intermediate renaming/reindexing steps that the old code required
- `earliest_hcd_date` must be converted via `pd.Timestamp(...).strftime('%Y-%m-%d')` because `df['Intervention Date'].min()` may return a Timestamp or string depending on data source
- The old code had a "stats['diagnosis_coverage']" tracking block ÔÇö this is now handled internally by `assign_drug_indications()` logging. If stats tracking in the return dict is needed later, can add it back.
### Next iteration should:
- Work on Task 3.2: Run `python -m cli.refresh_pathways --chart-type indication --dry-run -v`
  - This requires a live Snowflake connection
  - Verify: modified UPIDs appear in logs, match rates logged, pathway nodes generated
  - If dry run passes, move to Phase 4 (full refresh + validation)
- Key things to check in dry run output:
  - "Drug-aware indication matching complete" log message with match/fallback counts
  - "Modified UPIDs" count should be HIGHER than unique patient count (patients with multiple drugs for different indications)
  - Pathway node counts for indication charts should be in same ballpark as before (~300 per date filter)
  - No errors in indication pathway processing
### Blocked items:
- None

+### Committed: 920570b "feat: integrate drug-aware indication matching into refresh pipeline (Task 3.1)"


Bash

cd "C:\Users\charlwoodand\Ralph local\Tasks\UI Redesign" && git add progress.txt && git commit -m "docs: update progress.txt with Iteration 5 results (Task 3.1)"

Commit progress update


[feature/snomed-indication-mapping 73088b0] docs: update progress.txt with Iteration 5 results (Task 3.1)
 1 file changed, 1 insertion(+), 1 deletion(-)


TodoWrite

Read current assign_drug_indications() and load_drug_indication_mapping() signatures

Reading function signatures from diagnosis_lookup.py

Update refresh_pathways.py indication block with drug-aware matching

Updating refresh_pathways.py indication processing

Validate: py_compile and import check

Update IMPLEMENTATION_PLAN.md and progress.txt, commit


**Task 3.1 complete.** Here's what was done:

**Changes to `cli/refresh_pathways.py`:**
- Added imports for `assign_drug_indications` and `load_drug_indication_mapping`
- Replaced the old indication processing block (~90 lines ÔåÆ ~60 lines) with drug-aware matching:
  1. Loads drug mapping from DimSearchTerm.csv
  2. Computes `earliest_hcd_date` from HCD data to restrict GP code window
  3. Gets ALL GP matches per patient (with code_frequency) via updated query
  4. Calls `assign_drug_indications()` to cross-reference drugs against GP diagnoses
  5. Passes `modified_df` (with indication-aware UPIDs like `RMV12345|rheumatoid arthritis`) to pathway processing
  6. Uses `indication_df` directly ÔÇö already in the correct format (indexed by modified UPID, `Directory` column)

**Validation:** `py_compile` passed, all imports verified, full module import OK.

**Commits:** `920570b` (code) + `73088b0` (progress docs)

**Next task:** 3.2 ÔÇö Run `python -m cli.refresh_pathways --chart-type indication --dry-run -v` to test the integrated pipeline with live Snowflake data.