Skip to content

Add interactive plot editor with live preview and data protection#2

Closed
brycewang-stanford wants to merge 1 commit into
mainfrom
feature/interactive-plot-editor
Closed

Add interactive plot editor with live preview and data protection#2
brycewang-stanford wants to merge 1 commit into
mainfrom
feature/interactive-plot-editor

Conversation

@brycewang-stanford

Copy link
Copy Markdown
Owner

Summary

  • Add sp.interactive(fig) — a Stata-inspired plot editor with live preview in Jupyter (Image widget re-renders on every edit)
  • Data protection: DATA/FIT/CI artists are position-locked; axis limits auto-expand with 2% margin to prevent hiding data points
  • 30+ themes: unified sp.set_theme() supporting StatsPAI (4), matplotlib (10), and seaborn (16) styles
  • 10 font presets: AER, APA, Nature, IEEE, Elsevier, Springer, 中文学位论文, 中文期刊, 中文PPT, Beamer — plus manual font family/name selection
  • Full style controls (color, linewidth, linestyle, alpha, marker, scatter), undo/reset with complete state restoration, code generation, save/export

Test plan

  • Run python3 -c "from statspai.plots.interactive import FigureEditor; print('OK')" to verify imports
  • Run demo notebook test-notebooks/demo_graph.ipynb in Jupyter to test live preview
  • Verify data protection: editor.set_xlim(-2, 2) on data spanning [-5, 5] should warn and auto-expand
  • Verify undo/reset restores all properties including line colors, widths, alphas
  • Verify sp.set_theme('ggplot') and sp.set_theme('seaborn-whitegrid') work
  • Verify editor.apply_font_preset('AER / Econometrica') sets correct font and sizes

🤖 Generated with Claude Code

Introduce sp.interactive(fig) — a Stata-inspired plot editor that allows
cosmetic editing of matplotlib figures while locking statistical data elements.

Key features:
- Live preview: Jupyter Image widget re-renders on every edit via on_refresh callbacks
- Data protection: DATA/FIT/CI artists are position-locked; axis limits auto-expand
  with 2% margin to prevent hiding data points
- Artist classification: label-based keyword matching with word boundaries (regex)
- Full style controls: color, linewidth, linestyle, alpha, marker, scatter color
- Font system: 10 academic presets (AER, Nature, IEEE, 中文学位论文, etc.) + manual
  font family/name selection with Chinese font support
- 30+ themes: StatsPAI (4) + matplotlib (10) + seaborn (16) via unified set_theme()
- Code generation: deduped, clean float formatting, auto-adds imports
- Undo/Reset: full state snapshot including line/collection styles
- Save/Export: PNG/PDF with configurable DPI
- Safe replay: method dispatch instead of exec() for undo operations
- Memory-safe: editor stored on fig._statspai_editor (no global dict leak)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
brycewang-stanford added a commit that referenced this pull request Apr 20, 2026
…in check_identification

Closes trade-offs #1 and #2 from v0.9.3+1 retrospective.

New capabilities on sp.check_identification():

1. DAG-based bad-control detection (trade-off #2)
   - New ``dag=`` parameter accepting an sp.DAG.
   - When supplied, runs dag.bad_controls(treatment, outcome)
     (Cinelli-Forney-Pearl 2022) and flags mediators, descendants,
     colliders, and M-bias structural problems as BLOCKERS.
   - Also verifies the covariate set satisfies one of the DAG's
     adjustment sets; suggests the shortest valid adjustment set
     when the user's covariate list fails.
   - Catches the M-bias case where the correlation heuristic is
     silent (collider with low treatment correlation).

2. Strict mode
   - New ``strict=True`` parameter.  When the report's verdict is
     'BLOCKERS', raises IdentificationError with the full report
     attached on ``err.report`` so downstream code can still
     inspect findings without re-running.
   - IdentificationError exposed at sp.IdentificationError.
   - Default strict=False preserves existing return-a-report
     behaviour.

3. IV first-stage F check
   - When design='iv' with an instrument column, computes the
     first-stage F (OLS of d on z) and flags:
       F < 5   -> blocker (weak identification fails)
       F < 10  -> warning (Staiger-Stock 1997)
       F < 30  -> info    (moderate strength)
       F >= 30 -> silent  (comfortable)

6 new tests (4 DAG + strict mode + IV-strength).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant