"""SOURCE render logic for METH-02 (REFERENCE ONLY). This is the exact function from our pipeline. It is DB-coupled — you cannot run it without our database. The query result it produces is materialized for you in data.csv, and the rendered output is figure.json (load that to display the chart). This file is provided so you can understand / re-theme the logic. """ def render_METH_02(reg): """How we label Originals vs Licensed - the classifier explained, with the real catalog split.""" chart_id = "METH-02"; output_dir = THIS_DIR / chart_id / ROLE conn = lib.connect_readonly(); t0 = time.time() # ONE COUNT PER TITLE: a title holding 2+ platform relationships is counted once, by its strongest # (the old DISTINCT-per-class version counted title-classification PAIRS, ~1,600 titles double-counted). df = lib.run_query(conn, """ WITH l AS (SELECT MAX(snapshot_date) AS d FROM categorization_title_x_platform), ranked AS ( SELECT imdb_id, composite_classification AS c, ROW_NUMBER() OVER (PARTITION BY imdb_id ORDER BY CASE composite_classification WHEN 'parent_original' THEN 1 WHEN 'parent_exclusive' THEN 2 WHEN 'cross_window_acquisition' THEN 3 WHEN 'broad_license' THEN 4 ELSE 5 END) AS rn FROM categorization_title_x_platform JOIN l ON snapshot_date=d WHERE composite_classification IS NOT NULL ) SELECT c, COUNT(*) AS n, (SELECT d FROM l) AS snap FROM ranked WHERE rn=1 GROUP BY c; """) multi = lib.run_query(conn, """ WITH l AS (SELECT MAX(snapshot_date) AS d FROM categorization_title_x_platform) SELECT COUNT(*) AS n FROM ( SELECT imdb_id FROM categorization_title_x_platform JOIN l ON snapshot_date=d WHERE composite_classification IS NOT NULL GROUP BY imdb_id HAVING COUNT(DISTINCT composite_classification) > 1) x; """) q_sec = time.time() - t0; conn.close() if df.empty: lib.needs_spec_review(output_dir, chart_id, reg["spec_file"], "PC-METH-02", "No classification data"); write_shim(output_dir, chart_id); return {"status": "needs_spec_review"} snap = str(df["snap"].iloc[0]) n_multi = int(multi["n"].iloc[0]) if not multi.empty else 0 PLAIN = {"parent_original": "True Original - made & owned by the platform", "parent_exclusive": "Exclusive - only here, but not made by them", "broad_license": "Licensed - available across many services", "cross_window_acquisition": "Acquired - picked up after release elsewhere", "sports": "Sports / live"} df["label"] = df["c"].map(PLAIN).fillna(df["c"]); df["n"] = df["n"].astype(int) df = df.sort_values("n", ascending=True) total = df["n"].sum() # visual weight tracks the headline's two subjects: True Original (green = the rare, owned slice) and # Licensed (navy); the supporting buckets stay muted. CMAP = {"parent_original": lib.GREEN, "broad_license": lib.NAVY, "parent_exclusive": lib.MUTED, "cross_window_acquisition": lib.MUTED, "sports": lib.MUTED} colors = [CMAP.get(c, lib.MUTED) for c in df["c"]] pct_str = ["<1%" if 0 < n / total < 0.005 else f"{n/total:.0%}" for n in df["n"]] bar_text = [f" {n:,} ({p})" for n, p in zip(df["n"], pct_str)] fig = go.Figure() fig.add_trace(go.Bar(x=df["n"], y=df["label"], orientation="h", marker=dict(color=colors), text=bar_text, textposition="outside", textfont=dict(size=10, color=lib.MUTED), cliponaxis=False, showlegend=False, hovertemplate="%{y}
%{x:,} titles")) fig.update_xaxes(range=lib.pad_range(df["n"], labels=bar_text, font_px=10, fig_width=1180)) orig = int(df[df["c"] == "parent_original"]["n"].sum()); lic = int(df[df["c"] == "broad_license"]["n"].sum()) title_lesson = (f"‘Original’ is rarer than it sounds: only {orig/total:.0%} of streaming titles are true originals, " f"while {lic/total:.0%} are simply licensed catalog") what = ("How our classifier labels every title's relationship to the platform showing it - from a true original (made and " "owned by that platform) down to licensed catalog available everywhere. Each title is counted once, by its strongest platform relationship.") why = ("‘Netflix Original’ and the like blur very different things. Separating made-here originals from mere exclusives and broad " "licenses is what lets every other chart compare platforms honestly - and shows how thin the genuinely-owned slice really is.") fig.update_layout(**lib.layout_v2(title_lesson, what, width=1180, height=680), xaxis=lib.standard_xaxis("", tickformat=",d"), yaxis=lib.standard_yaxis(showgrid=False)) lib.add_why_and_footer(fig, why, lib.assemble_footer("PC-METH-02", ["CL-010 originator classifier v2"], snap, ["categorization_title_x_platform"], f"composite_classification; titles holding 2+ relationships ({n_multi:,}) counted by the strongest")) lib.save_csv(df, output_dir); lib.export_chart(fig, output_dir, width=1180, height=680) lib.write_lesson_design(output_dir, chart_id, f"# {chart_id}\n\n{title_lesson}\n\n{what}\n") write_shim(output_dir, chart_id) lib.write_manifest(output_dir, chart_id, "ok", reg["spec_file"], pc_id="PC-METH-02", intended_lesson_from_spec="Originator classifier explained + catalog split", rendered_lesson={"reader_title": title_lesson, "reader_subtitle": what, "claim_strength": "strong", "rules_applied": "dominant composite_classification per title (one count per title)"}, row_count=len(df), freshness_date=snap, query_seconds=q_sec, render_seconds=time.time() - t0 - q_sec) return {"status": "ok", "rows": len(df)}