#!/usr/bin/env python3 """Local cohort coverage review. No employee score, ranking, network or inference.""" import argparse import csv import json from datetime import date, timedelta def review(rows, as_of, checkpoint_days): if checkpoint_days < 1: raise ValueError('checkpoint_days must be positive') seen = set() totals = dict(total=0, pending=0, mature=0, retained=0, early_exit=0, retention_unknown=0, goal_observed=0, goal_met=0, goal_not_met=0, manager_observed=0, both_observed=0) flags = [] for row in rows: ident = row['episode_id'].strip() if not ident or ident in seen: raise ValueError('empty or duplicate episode_id') seen.add(ident) dates = {key: date.fromisoformat(row[key]) if row[key] else None for key in ('start_on', 'known_employed_through', 'exit_on', 'goal_window_end', 'goal_reviewed_on', 'manager_window_end', 'manager_reviewed_on')} start = dates['start_on'] if start is None or start > as_of: raise ValueError('every episode must have actually started by as_of') if any(value and (value < start or value > as_of) for value in dates.values()): raise ValueError('dates must fall between start and as_of') known, exited = dates['known_employed_through'], dates['exit_on'] if known and exited: raise ValueError('use either continuous employment confirmation or known exit, not both') checkpoint = start + timedelta(days=checkpoint_days) mature = checkpoint <= as_of state = ('pending' if not mature else 'early_exit' if exited and exited <= checkpoint else 'retained' if (exited and exited > checkpoint) or (known and known >= checkpoint) else 'retention_unknown') goal = row['goal_result'] if goal not in ('', 'met', 'not_met'): raise ValueError('goal_result must be empty, met or not_met') goal_present = bool(goal) if goal_present != bool(dates['goal_window_end']) or goal_present != bool(dates['goal_reviewed_on']): raise ValueError('goal result, window end and review date must be present together') manager = bool(dates['manager_reviewed_on']) if manager != bool(dates['manager_window_end']): raise ValueError('manager window and review date must be present together') for component, present in (('goal', goal_present), ('manager', manager)): if present and (state != 'retained' or dates[component + '_window_end'] != checkpoint or dates[component + '_reviewed_on'] < checkpoint): raise ValueError('full-window observations require confirmed retention and the exact checkpoint window') totals['total'] += 1 totals[state] += 1 if mature: totals['mature'] += 1 if state == 'retained': totals['goal_observed'] += goal_present totals['goal_met'] += goal == 'met' totals['goal_not_met'] += goal == 'not_met' totals['manager_observed'] += manager totals['both_observed'] += goal_present and manager flags.append(dict(episode_id=ident, checkpoint_on=checkpoint.isoformat(), observation_state=state, goal_record_present=goal_present, manager_record_present=manager)) def fraction(n, d): return dict(numerator=n, denominator=d, percent=round(n / d * 100, 2) if d else None) mature, retained = totals['mature'], totals['retained'] known_count = retained + totals['early_exit'] return dict(as_of=as_of.isoformat(), checkpoint_days=checkpoint_days, counts=totals, retention_coverage=fraction(known_count, mature), retained_among_known=fraction(retained, known_count), retained_cohort_lower_bound=fraction(retained, mature), retained_cohort_upper_bound=fraction(retained + totals['retention_unknown'], mature), goal_coverage_among_confirmed_full_window=fraction(totals['goal_observed'], retained), manager_coverage_among_confirmed_full_window=fraction(totals['manager_observed'], retained), both_coverage_among_confirmed_full_window=fraction(totals['both_observed'], retained), combined_quality_score=None, episode_checks=flags) if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('csv_path') parser.add_argument('--as-of', required=True, type=date.fromisoformat) parser.add_argument('--checkpoint-days', required=True, type=int) args = parser.parse_args() with open(args.csv_path, newline='', encoding='utf-8') as source: result = review(list(csv.DictReader(source)), args.as_of, args.checkpoint_days) print(json.dumps(result, indent=2))