
    .jJ                    `    d Z ddlmZ ddlZddlZdZdZdZ	dZ
dZd	Zd
Zd ZddZddZdddZy)ab  The analyst's workbook.

Four sheets, in the order someone actually reads them:

    Summary    what this run found, at a glance
    Estimates  every telecast, filterable, with the evidence behind each
    Markets    totals per market
    Method     the parameters that produced the numbers, and their provenance

The Method sheet matters as much as the estimates. Every figure in this
workbook comes out of a model whose exponents, curves and intervals were
measured rather than assumed, and a reader who cannot see those values has to
take the estimates on trust. It also states plainly what is NOT applied.
    )annotationsNz#0F1120z#4B3FD6z#E0243Fz#0F9B8Ez#D7DAE6z#7C8199z#F4F5F9c                &   i }| j                  dddt        dd      |d<   | j                  ddt        d	      |d
<   | j                  dddt        dt        d      |d<   | j                  ddt        ddd      |d<   | j                  dddt        dddd      |d<   | j                  dddt        dddd      |d<   | j                  dddt
        dddd      |d<   | j                  ddt        d	      |d<   | j                  ddddt        ddddt        d
      |d<   | j                  dddt        d       |d!<   | j                  ddddt        d"      |d#<   | j                  ddddt        d$      |d%<   | j                  dddddt        d&      |d'<   | j                  ddd(dt        d$      |d)<   | j                  ddd(ddt        d&      |d*<   | j                  dddt        ddd(d      |d+<   | j                  dddt        ddd(d      |d,<   | j                  dddt
        ddd(d      |d-<   | j                  ddd(dt        d$      |d.<   | j                  ddd/dt        d$      |d0<   | j                  ddt        dt        d1      |d2<   | j                  ddt
        dt        d1      |d3<   | j                  ddt        ddd4      |d5<   | j                  ddt        ddd4      |d6<   | j                  ddt
        dddd7      |d8<   |S )9NCalibri   Tvcenter)	font_name	font_sizebold
font_colorvaligntitleConsolas	   )r   r	   r   sub      )r   r	   r
   r   bottomborder_colorh2   lefttop)r   r	   r   alignr   kpi_lbl   z#,##0.0#)r   r	   r
   r   r   r   
num_formatkpi_val	kpi_val_t	kpi_val_ckpi_notez#FFFFFF   )
r   r	   r
   r   bg_colorr   r   	text_wrapborderr   hdr
   )r   r	   r$   r   cell)r   r	   r
   r$   r   cell_br   r	   r   r$   r   num)r   r	   r   r
   r$   r   num_bz#,##0numwnumwbkpi_valw
kpi_valw_t
kpi_valw_cintz0.#pct)r   r	   r   r$   r   monoflag)r   r	   r   r#   r   bodybody_m)r   r	   r   r#   r   r
   warn)
add_formatINKMUTEDINDIGOTEALCRIMSONRULE)wbfs     5/var/www/html/crystal/crystal2/engine/excel_report.py_formatsrB      s   
AYRQU.1Y H IAgJ}}:AUZ[\AeHmm)"d+.!SR SAdG==zY^+1U"D EAiL ==yrSW06S\0:"< =AiL ]]UY26S\2<$> ?AkN ]]UY29FV_2<$> ?AkN MM
Z_"`aAjM}}91d,53QW(1PQ.13 4AeH IBRS/35 6AfI--ibRV+,d!D EAhK}}:BV`()4A BAeHZbXb(,4 Q RAgJ
 JRW^)*DB CAfIZbX_(,4 Q RAgJMM	TX17&T]18#: ;AjM mm)"VZ37&T]3:%< =AlO mm)"VZ3:VW`3:%< =AlO }}:BV]()4A BAeH}}:BV[()4A BAeHJQV[)*DB CAfIJQV])*DB CAfIIBVY,0EC DAfI--ibX].2e!E FAhKIBV],0E4Q RAfIH    c                `    	 t        |       j                         S # t        t        f$ r Y yw xY w)NF)float
is_integer	TypeError
ValueError)vs    rA   _wholerJ   `   s1    Qx""$$z" s    --c                    dddd|   }t        |      r	dddd|   }||   }	| j                  ||||d	          | j                  |d
z   |||	       |r| j                  |dz   |||d          y y )Nr   r   r   )itcr.   r/   r0   )r   r   r   r   r!   r   r    )rJ   write)
wsr@   rowcollabelvaluenotetonekeyfmts
             rA   _kpirY   g   s    +
>t
DCe}$<(**-/
C&CHHS#ua	l+HHS1Wc5#&
q#tQz]3 rC   c                V   ddl }| j                         }dD ].  }||j                  v st        j                  ||   d      ||<   0 |j                  dt        j                  d|j                              j                  d      |d<   |d	   j                         }|j                  t        |      d
dd      }	t        |	      }
|	j                  d      }|j                  d       |j                  dd       |j                  dd       |j!                  ddd|
d          |j!                  dd| d| dt        j"                  j%                         d|
d          |j&                  |d	f   }|j&                  |df   }t)        |d   j+                  t              j                  j-                         dk7  j/                               }t1        |      rt3        |j/                               dz  nd}t5        ||
dddt1        |      d       t5        ||
ddd t)        |j/                               d!|j7                         z  d"d#d$       t5        ||
ddd%t9        |d      d&       t5        ||
dd'd(t1        |      r#t9        t3        |j;                               d      ndd)       t5        ||
d*dd+t1        |      r#t9        t3        |j7                               d      ndd,       t5        ||
d*dd-||rd.nd/|rd0nd$       |j!                  d1dd2|
d3          d4}|j!                  |dd5|
d6          |j!                  |dd|
d6          |dz  }|j=                  |dg d7|
d8          |j&                  |   j?                  d9      jA                  d:d;d<d=>      jC                  d?d
@      }tE        |jG                         |dz   A      D ]  \  }\  }}|j!                  |dt        |      |
dB          t3        |d?         }|jI                  |d||
tK        |      rdCndD          |jI                  |dt)        |dE         |
dF          t3        |dG         }|jI                  |dH||
tK        |      rdCndD          |jI                  |dt3        |dI         |
dJ           t1        |      r1|jM                  |dz   d|t1        |      z   ddKtN        dtN        dL       |t1        |      z   dz   }|j!                  |ddM|
d6          |j!                  |dd|
d6          |dz  }|j=                  |dg dN|
d8          |j&                  |   j?                  dO      jA                  d;d=P      jC                  dEd
@      }tE        |jG                         |dz   A      D ]  \  }\  }}|j!                  |dt        |      |
dQ          |jI                  |dt)        |dE         |
dF          |jI                  |dt3        |dE         tQ        t)        |j/                               d      z  |	jS                  dRdSdTdtT        dU             |jI                  |dHt3        |dI         |
dJ           |j!                  |t1        |      z   dz   ddV|
dW          g dX}|D cg c]  }|d   |j                  v s| }}|	j                  dY      }|j                  d       |jW                  dd       tE        |      D ]4  \  }\  }}}}|j                  |||       |j!                  d|||
d8          6 |jY                  ddZ       |jC                  d	d
d[\      }tE        |jG                         dA      D ]  \  }\  } }tE        |      D ]  \  }\  }!}"}#}$|j                  |!      }|:t[        |t2              rt]        j^                  |      rt        j`                  |      r|jc                  ||d|
dQ          p|$d]v r;|$}!|$d^v rtK        |      r	|$dDk(  rdCnd_}!|jI                  ||t3        |      |
|!          |j!                  ||t        |      |
|$            |je                  ddtQ        t1        |      d      t1        |      dz
         tg        d` tE        |      D        d      }%|%.t1        |      r#|jM                  d|%t1        |      |%dadbdcddde       tg        df tE        |      D        d      }&|&1t1        |      r&|jM                  d|&t1        |      |&dKtN        ddg       |	j                  dh      }'|'j                  d       |'j                  ddi       |'j                  djdk       |'j=                  ddg dl|
d8          |'jY                  ddZ       |'jW                  dd       |j?                  d9      jA                  d:d;dmd<d=ddn fo      jC                  d?d
@      }(tE        |(jG                         dA      D ]%  \  }\  }}|'j!                  |dt        |      |
dB          t3        |d?   xs d      }|'jI                  |d||
tK        |      rdCndD          |'jI                  |dt)        |dE         |
dF          |'jI                  |dt)        |dp         |
dF          t3        |dG   xs d      }|'jI                  |dH||
tK        |      rdCndD          |'jI                  |dt3        |dI   xs d      |
dJ          |'jI                  |dqt)        |dr         |
dF          ( t1        |(      r&|'jM                  ddt1        |(      ddKth        ddg       |	j                  ds      })|)j                  d       |)j                  dd       |)j                  dtdu       |)j                  dvdw       |)j                  dxdy       |)j!                  dddz|
d          |)j!                  ddd{|
d          tk        |d|i       xs i }*tk        |d}d~      }+|*j                  d      xs i },|*j                  d      xs i }-|*j                  d      xs i }.d}|)j!                  |dd|
d8          |)j!                  |dd|
d8          |)j!                  |dd|
d8          |)jY                  |d       d|+dfd|,j                  dd      ddfd|,j                  dd      ddfd|,j                  dd      ddfd|,j                  dd      dd|,j                  dd       ddfddg}/tE        |/|dz   A      D ]W  \  }\  }0}1}|)j!                  |d|0|
dB          |)j!                  |dt        |1      |
d          |)j!                  |d||
dW          Y |t1        |/      z   dz   }|)j!                  |dd|
d6          |)j!                  |dd|
d6          |)j!                  |dd|
d6          |dz  }dd|.r|.j                  dd      d!z  ddnddfd|.r|.j                  dd      d!z  ddnddfd|-r|-j                  dd      dnddfg}2tE        |2|A      D ]N  \  }\  }0}1}|)j!                  |d|0|
dB          |)j!                  |d|1|
d          |)j!                  |d||
dW          P |t1        |2      z   dz   }3|)j!                  |3dd|
d6          |)j!                  |3dd|
d6          |)j!                  |3dd|
d6          |)j!                  |3dz   dd|
dB          |)j!                  |3dz   dd|
d          |)j!                  |3dz   dd|
d          |)j!                  |3dz   dd|
dB          |)j!                  |3dz   dd|
d          |)j!                  |3dz   dd|
dW          |)j!                  |3dHz   dd|
dW          |	jm                          yc c}w )zDWrite the formatted workbook. `out` is the estimated schedule frame.r   N)estimated_ama_000
confidenceinterval80_lowinterval80_highevidence_rowscoerce)errorsr4    )indexr[   FT)constant_memorynan_inf_to_errorsSummaryr   zA:AzB:I   r!   zAudience estimatesr   u       ·    run u
       ·    z%d %b %Y  %H:%Mr   r\   g     @@g           zTELECASTS IN FILEz	rows read   	ESTIMATEDd   z.1fz	% of rowsrM   zTOTAL AMA (MILLIONS)zsummed across telecasts   zMEDIAN PER TELECASTzAMA 000sr   zMEAN CONFIDENCEz
out of 100zFLAGGED FOR REVIEWzneeds your eyeznothing to reviewrN      u   Total AMA is the sum of average-minute audiences across telecasts. It is not a count of unique people — a viewer watching three broadcasts is counted three times.r       zTop marketsr   )MarketTotal AMA (000s)	TelecastsMedianMean confidencer%   country_name)r[   sum)r[   size)r[   median)r\   mean)totalnmedconfry   )	ascending)startr(   r,   r*   rz   r1   r{      r|   r2   data_bar)type	bar_color	bar_solidbar_border_colorEvidence used)Strategyrq   Sharers   strategy)rz   r|   r'   r   r&   z0.0%r)   zEstimates are model output and have not passed final analyst sign-off. Every row carries the evidence tier and the 80% interval that produced it.r6   ))
event_nameEvent   r(   )rt   ro      r'   )channel_nameChannelr   r'   )telecast_typeTypern   r3   )	prog_dateDater   r'   )hourHourrl   r1   )sports_teamsTeams   r'   )match_levelRoundrn   r3   )r[   z
AMA (000s)rn   r+   )r]   z80% lowrm   r*   )r^   z80% highrm   r*   )r\   
Confidencerm   r2   )r_   zEvidence rowsr   r1   )r   r      r'   )source_channelzSource channel   r3   )r4   Flagr   r4   	Estimatesr   last)r}   na_position)r*   r+   r1   r2   )r*   r+   r-   c              3  8   K   | ]  \  }}|d    dk(  s|  yw)r   r\   N .0jrN   s      rA   	<genexpr>zwrite_report.<locals>.<genexpr>   s     ETQ!0DqE   3_color_scalez#F6C6CBz#FDF1D6z#CFE9E3)r   	min_color	mid_color	max_colorc              3  8   K   | ]  \  }}|d    dk(  s|  yw)r   r[   Nr   r   s      rA   r   zwrite_report.<locals>.<genexpr>   s      LTQ!8K0KqLr   )r   r   r   Marketsr   zB:G   )ro   rp   rq   	Estimatedrr   rs   Flagged)r[   countc                    t        | j                  t              j                  j                         dk7  j	                               S )Nrb   )r1   astypestrstripru   )ss    rA   <lambda>zwrite_report.<locals>.<lambda>  s0    c188C=+<+<+B+B+D+J*O*O*Q&R rC   )ry   rz   estr{   r|   flr      r   MethodzB:B"   zC:Cr   zD:D>   zHow these numbers were producedu`   case-based structured-analogy forecasting · multiplicative decomposition · empirical intervalscalibrationcalibration_sourcezassumed constants	exponentssigma_by_tiercross_market_backtest	ParameterValuezHow it was obtainedr   zCalibration sourcezdMeasured from the evidence base itself, or the fallback constants if no calibration file is present.u   Exponent · TV universetvuz.3fzkWeighted least squares on log audience with event fixed effects absorbed. Assumed 1.0 before it was fitted.u   Exponent · channel sharesharez	As above.u   Exponent · sport affinityaffinityz	Fitted onn_cells,z	 cells / n_eventsz eventsu{   Identification comes from how markets differ within the same event — the same comparison a cross-market projection makes.)zHour & weekday curvesmeasuredzuWithin-stratum ratio estimation: each hour's median as a ratio to its own group's median, across thousands of groups.)z80% intervalu   exp(±1.2816 · σ)uq   σ is the residual standard deviation of log error per tier, from hold-out backtests — not an assumed constant.r3   zMeasured accuracy)zMedian error, same-marketz67%z'Hold-one-row-out over 1,200 broadcasts.zMedian error, cross-market
median_APEz.0f%z89%zLeave-one-market-out: every row for a market removed, then estimated from the rest of the world. This is the harder and more honest test.zWithin a factor of 2hit_within_2xz27%z+Cross-market. Roughly one estimate in four.u   σ, cross-market4z.2fz2.45uE   An 80% interval of about ×/÷ 23. Wide because the evidence says so.zWhat is NOT appliedzRound / match stageznot appliedzA final is currently weighted the same as a group match. The round is captured by enrichment but the weight table is not yet read by the estimator.r7   u%   Event interest · broadcast intensityz5Both reference tables are available and not yet used.zQStated here because a number is only as good as the reader's ability to check it.)7
xlsxwritercopycolumnspd
to_numericgetSeriesrc   fillnanotnaWorkbookr   rB   add_worksheethide_gridlines
set_columnrO   	Timestampnowlocr1   r   r   ru   lenrE   rY   rx   roundrw   	write_rowgroupbyaggsort_values	enumerateiterrowswrite_numberrJ   conditional_formatr;   maxr8   r>   freeze_panesset_row
isinstancenpisfiniteisnawrite_blank
autofilternextr<   getattrclose)4outpathrun_idr   refr   drN   okr?   r@   rP   r   r|   flaggedtotal_mrmkrL   namerQ   rI   r2tierscolswer   _krS   width_fmtview_rW   _l_wrX   ciaiwmr   wmethcalsrcexpsigcmrowsabaccr3s4                                                       rA   write_reportr  s   su   
A 8		>==1h7AaD8 fbii!'':;BB2FAfI	
		%	%	'B			SYEX\(]	^BA 
		)	$BaMM%MM%HHQ'74HHQeWN6(*ll&&(9;<=eHF %%''
(C55\!"D1V9##C(,,224:??ABG+.s8eCGGI'GQ1)3q6;?Q1k3rvvx=	M#i	(#/Q1,eGQ.?	"$Q1+cRUhU53F-J\]	Q1'TuTYY[/A1)EXY	Q1*G$	*=gsSVXHHR 9:;J-I 	AHHQ=!D'*BHHQ2qw,GFALLA_5
%%)

N
+315R29O  Q;w%;0  $BKKMQ? <;D#
As4y!H+.
G
rq!QF1I&SX8YZ
1c#c(mQuX6
E

R__Q1a&)QV6WX
1eCK0!E(;< 2w
a!eQCGQ'1,0fN	O 
SWqBHHROQtW-rxxAr1T7/K!GBLLQMqQVxXUU2Yz*c18NcOk#k/ 
 $ENN$4BFC <;D#
As4y!F),
1c#c(mQuX6
1eCHoCM10EEJR5;qZ^'` a	b 	1eCK0!E(;< HHR#e*_q !Zx[*D  1!qtqyy0A1D1			+	&BaOOAq'0 (##Bud
aE"
Auah'( JJq"==,6=RD : /8As%.t_ 
	/!A!RSAyZ51"++a.RWWUVZq!T1V9566**vay$'5L&gC1eAh#7As1vqv.
	// MM!QCIq)3t9q=9	EYt_Et	LB	~#d)
aSY'6,5I,57	8 
LYt_Ld	SB	~#d)
aSY'1UYZ	\ 
		)	$BaMM%bmmE26LLA @AB5KJJq"rq!,99^$C26S28W+RS  U K5K1  $CLLN!< 8;D#
As4y!H+.
G!
"BOOAq!QPQvX]=^$_
1c#c(mQuX6
1c#e*oqx8
E
a
 "//!Q1vayVV[;\"]
1eCK$415qx@
1c#d)nah78 3x
aCHa'1SWX	Z X&E		UA 0 0 ;U=M=MeUW=X	UB	KK177D	KK1 LMNuXW #}b
)
/RC
#+-@
AC
''+

$"C
''/
"
(bC	(	)	/RB	A	KK1k1U8,ekk!Q5.R	KK1+QuX6a8L	s %\ 	]	"swwua'8&=
-	. 
%!)<S(AKP	%#''*a*@)ET	A.q1377:q;Q:RRYZ
<	=	8	3D$ "$a!e4 *9Aq!Aq!Qx[)5;;q!SVQvY+OAq!Qx[)* 
SY	B	KKA*AdG4	KKAr1T7#U[[QAdG%D!GBZ( .0VVL!-DS-H,MQ*OST #PRrvvoq'A#'Ec&J!$LX]9;C3773?3"7VSUVC "#R0 *9Aq!Aq!Qx[)5;;q!Q&	+JAq!Qx[)* 
c#h	B	KKA,ag6	KKAr1T7#U[[QAdG%D	KKQ0!H+>	KKQ=!F)4	KKQ 89:6D 
KKQBAhKP	KKQ=!F)4	KKQRTUV^T_`	KKQ 234X;@ HHJI 2s   ~&$~&)returnbool)rb   rL   )N)r   zpd.DataFramer   r   r   r   r  None)__doc__
__future__r   numpyr   pandasr   r9   r;   r=   r<   r>   r:   BANDrB   rJ   rY   r  r   rC   rA   <module>r$     sP    #   	
?D	4hrC   