forked from errorcodexero/standing_predictor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutput.cpp
More file actions
708 lines (648 loc) · 17.1 KB
/
Copy pathoutput.cpp
File metadata and controls
708 lines (648 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#include "output.h"
#include "plot.h"
#include "ca.h"
#include "avatar.h"
#include "query.h"
#include "io.h"
#include "table.h"
using namespace std;
using Team_key=tba::Team_key;
PRINT_STRUCT(Team_points_used,TEAM_POINTS_USED)
PRINT_STRUCT(Output_tuple,OUTPUT_TUPLE)
ELEMENTWISE_RAND(Output_tuple,OUTPUT_TUPLE)
struct Script_namer{
size_t i=0;
string operator()(){
std::stringstream ss;
ss<<"n"<<i;
i++;
return ss.str();
}
};
char digit(auto i){
if(i<10) return '0'+i;
return 'a'+(i-10);
}
string color(double d){
//input range 0-1
//red->white->green
auto f=[](double v){
auto x=min(255,int(255*v));
return string()+digit(x>>4)+digit(x&0xf);
};
auto rgb=[=](double r,double g,double b){
return "#"+f(r)+f(g)+f(b);
};
if(d<.5){
return rgb(1,d*2,d*2);
}
auto a=2*(1-d);
return rgb(a,1,a);
}
auto colorize(double d){
return tag("td align=center bgcolor=\""+color(d)+"\"",
tag("font color=black",round3(d))
);
}
template<typename T>
T find_cutoff(map<T,Pr> const& cutoff_pr,double threshold){
auto t=sum(seconds(cutoff_pr));
assert(t>.99 && t<1.01);
double total=0;
for(auto [a,p]:cutoff_pr){
total+=p;
if(total>=threshold){
return a;
}
}
assert(0);
}
template<typename T>
tuple<T,T,T> summary(map<T,Pr> const& a){
return make_tuple(
find_cutoff(a,.05),
find_cutoff(a,.5),
find_cutoff(a,.95)
);
}
static std::map<tba::Team_key,std::string> find_charts(std::map<Team_key,Team_points_used> const& a){
std::vector<Plot_setup> setups;
for(auto [k,v]:a){
Plot_setup p;
p.title=::as_string(k);
p.data=mapf([](auto x){ return Plot_point2(x.first,x.second); },v.pre_dcmp_dist);
setups|=p;
}
auto plots=plot(setups);
return to_map(mapf(
[](auto const& x){
auto [in,out]=x;
return make_pair(in.first,out);
},
zip(a,plots)
));
}
static std::string show_skill(Skill_estimates const& in){
std::stringstream ss;
ss<<h2("Skill estimates");
ss<<as_table(in);
auto get_plot_setup=[&](std::string name,auto data_in){
std::vector<Plot_point3> data;
for(auto [pts_in,v]:data_in){
for(auto [pts_out,pr]:v){
data|=Plot_point3(pts_in,pts_out,pr);
}
}
return Plot_setup{data,name};
};
auto ps1=get_plot_setup("Second event",in.second_event);
auto ps2=get_plot_setup("At DCMP",in.at_dcmp);
auto out=plot(std::vector{ps1,ps2});
/*vector out{
plot(std::vector{ps1}),
plot(std::vector{ps2})
};*/
assert(out.size()==2);
ss<<h3("Probable second event points by first event points")<<out[0]<<"\n";
ss<<h3("Probable District Championship points given pre-dcmp points")<<out[1]<<"\n";
return ss.str();
}
static std::string color(Tournament_status const& a){
if(a==Qual_status_future()){
return "#ccccff";
}
if(a==Tournament_status_complete()){
return "#ffffff";
}
return "#ffffcc";
}
static std::string colorize(Tournament_status const& a){
return tag(
"td bgcolor=\""+color(a)+"\"",
tag("font color=black",show(a))
);
}
static std::string event_status(Annotated const& a){
std::stringstream o;
o<<h2("Events");
o<<"<table border>";
o<<tr(th("Type")+th("Name")+th("Date")+th("Status")+th("Extra"));
auto show_event=[&](auto const& x){
auto [event,extra]=x;
const auto name=[&]()->string{
if(event.short_name && event.short_name!="{}"){
return *event.short_name;
}
return ::as_string(event.key);
}();
o<<tr(
td(event.event_type)+
td(link(event.key,name))+
td([&]()->string{
assert(event.start_date);
assert(event.end_date);
return as_string(Interval<tba::Date>{*event.start_date,*event.end_date});
}())+
colorize(extra.status)+
td(
frc_events(event)+" "+
statbotics(event)+" "+
splat(event)
)
);
};
for(auto event:a.local){
show_event(event);
}
for(auto dcmp:a.dcmp){
show_event(dcmp.finals);
}
for(auto cmp:a.cmp){
show_event(cmp.finals);
}
o<<"</table>";
return o.str();
}
static std::string cutoff_table_long(auto data){
return "The cutoff values, along with how likely a team at that value is to miss advancing. For example: a line that said (50,.25) would correspond to the probability that team above 50 get in, teams below 50 do not, and 75% of teams ending up with exactly 50 would qualify for the district championship."+
tag("table border",
tr(th("Points")+th("Probability"))+
join(mapf(
[](auto a){
return tr(join(MAP(td,a)));
},
data
))
);
}
static std::string cutoff_table(Script_namer& get_script_name,string s,auto cutoff_pr,int slots,std::optional<std::string> note=""){
auto simple=simplify(cutoff_pr);
auto chart=plot([&](){
std::vector<std::pair<int,double>> r;
auto ks=keys(simple);
for(auto k:range_inclusive(min(ks),max(ks))){
r|=make_pair(k,simple[k]);
}
return r;
}());
auto name=get_script_name();
auto name2=get_script_name();
return h2(s+" cutoff value")+
table(tr(
td(
[=](){
std::stringstream ss;
if(slots){
ss<<"Slots: "<<slots<<"<br>\n";
ss<<note;
}
return ss.str();
}()+
h3("Summary")+
tag("table border",
tr(th("Probability")+th("Point total"))+
[=](){
auto q=summary(simple);
return
tr(th("5%")+td(std::get<0>(q)))+
tr(th("Median")+td(std::get<1>(q)))+
tr(th("95%")+td(std::get<2>(q)))
;
}()
)+
p(tag("a href='' onclick=\"toggle_viz('"+name+"');event.preventDefault();\"","Details"))
)+
td(chart)
))+
tag("table class='hidden' id=\""+name+"\"",
tr(td(
tag("table border",
tr(th("Points")+th("Probability"))+
join(mapf(
[](auto a){
return tr(join(MAP(td,a)));
},
simple
))
)+
tag("a href='' onclick=\"toggle_viz('"+name2+"');event.preventDefault();\"","More details")+
tag("table class=hidden id=\""+name2+"\"",
tr(td(cutoff_table_long(cutoff_pr)))
)
))
);
}
static const std::vector<std::pair<std::string,std::string>> COLUMNS{
{"Rank","Ranking of probability of advancement"},
{"P<sub>DCMP</sub>","Probability of making district championship"},
{"Team","Team number"},
{"Nickname","Team nickname"},
{"DCMP 5% pts","Extra points needed to have 5% chance of making district championship"},
{"DCMP 50% pts","Extra points needed to have 50% chance of making district championship"},
{"DCMP 95% pts","Extra points needed to have 95% chance of making district championship"},
{"P<sub>CMP</sub>","Probability of making championship"},
{"CMP 5% pts","Extra points needed to have a 5% chance of making championship"},
{"CMP 50% pts","Extra points needed to have a 50% chance of making championship"},
{"CMP 95% pts","Extra points needed to have a 95% chance of making championship"},
{"Rookie Points","Rookie bonus points awarded"},
{"Played","Results from events played so far"},
{"Remaining events","Number of counting events for which the team is scheduled"}
};
static std::string explain(Script_namer &get_script_name){
auto name=get_script_name();
// p(tag("a href='' onclick=\"toggle_viz('"+name+"');event.preventDefault();\"","Details"))
return tag("a href=\"#\" onclick=\"toggle_viz('"+name+"');event.preventDefault();\"","Column descriptions")+
tag("table border class=\"hidden\" id=\""+name+"\"",
tr(th("Column")+th("Description"))+
join(mapf(
[](auto a){
return tr(td(a.first)+td(a.second));
},
COLUMNS
))
);
}
static const string STYLE="\n\
:root{\n\
color-scheme: light dark;\n\
}\n\
table{\n\
border-spacing: 0px;\n\
}\n\
tr.rank:hover{\n\
background-color: #888888;\n\
}\n\
.hidden{\n\
display: none;\n\
}\n\
.tooltip{\n\
position:relative;\n\
display: inline-block;\n\
border-bottom: 1px dotted black;\n\
cursor: pointer;\n\
}\n\
.tooltip .tooltiptext{\n\
visibility: hidden;\n\
background-color: black;\n\
color: #ffffff;\n\
text-align: center;\n\
padding: 5px 0;\n\
border-radius: 6px;\n\
position: absolute;\n\
z-index: 1;\n\
bottom: 100%;\n\
left: 50%;\n\
margin-left: -65px;\n\
}\n\
.tooltip:hover .tooltiptext{\n\
visibility: visible;\n\
}\n";
static const auto SCRIPT="\n\
function toggle_viz(name){\n\
var content=document.getElementById(name);\n\
content.classList.toggle(\"hidden\");\n\
}\n";
std::string team_details(
Gen_html_input const& in,
Event_categories_annotated<
Rank_status<Tournament_status>,
Tournament_status,
Rank_status<District_status>
> const& limits,
std::map<tba::Team_key,std::string> &charts,
auto a
){
//auto x=in.points_used.at(a.team);
std::stringstream ss;
ss<<h3("Expected pre-dcmp points");
ss<<"<table>";
ss<<"<tr>";
ss<<"<td>";
//ss<<"Dist quartiles: "<<quartiles(in.points_used.at(a.team).pre_dcmp_dist);
auto c=[=](double target){
double total=0;
for(auto [k,v]:in.points_used.at(a.team).pre_dcmp_dist){
total+=v;
if(total>=target){
return td(k);
}
}
assert(0);
};
ss<<h3("Summary");
ss<<tag("table border",
tr(th("Probability")+th("Point total"))+
tr(th("5%")+c(.05))+
tr(th("Median")+c(.5))+
tr(th("95%")+c(.95))
);
ss<<"</td>";
ss<<td(charts[a.team]);
ss<<"</tr>";
ss<<"</table>";
/*ss<<h3("Points used");
ss<<as_table(x);*/
//ss<<as_table(a);
//ss<<"<p>"<<"Lock status:"<<in.lock.at(a.team)<<"\n";
ss<<h3("Schedule");
ss<<"<table border>";
ss<<tr(
th("Qualification")+
th("Event type")+
th("Event name")+
th("Date")+
th("Event Status")+
th("Unclaimed points")+
th("Team point range")+
th("Notes")
);
auto show_event=[&](string qual,auto p,auto y,std::optional<std::string> note=std::nullopt){
auto [event,event_data]=p;
const auto date=[=](){
assert(event.start_date);
assert(event.end_date);
return Interval<tba::Date>{*event.start_date,*event.end_date};
}();
ss<<"<tr>";
ss<<qual;
ss<<td(event.event_type);
const auto event_name=[=]()->string{
if(event.short_name && event.short_name!="{}"){
return *event.short_name;
}
return ::as_string(event.key);
}();
ss<<td(link(event.key,event_name));
ss<<td(date);
ss<<colorize(event_data.status);
//ss<<td(event);
//ss<<td(event_data);
ss<<td(event_data.unclaimed);
ss<<td(y);
if(note){
ss<<td(*note);
}
ss<<"</tr>";
};
for(auto p:limits.local){
auto [event,event_data]=p;
auto y=maybe_get(event_data.by_team,a.team);
if(!y) continue;
show_event(colorize(1),p,y);
}
auto dcmp=limits.dcmp.at(a.dcmp_home);
for(auto division:dcmp.divisions){
auto m=maybe_get(division.extra.by_team,a.team);
if(!m) continue;
//only show division when this team is in it
show_event(colorize(1),division,m);
}
show_event(
colorize(a.dcmp_make),
dcmp.finals,
maybe_get(dcmp.finals.extra.by_team,a.team),
"Lock status: "+in.lock.at(a.team)
);
for(auto cmp:limits.cmp){
for(auto division:cmp.divisions){
auto m=maybe_get(division.extra.by_team,a.team);
if(!m) continue;
show_event(colorize(1),division,m);
}
show_event(colorize(a.cmp_make),cmp.finals,"");
}
ss<<"</table>";
const auto fly=[&]()->string{
auto f=in.flight.find(a.team);
if(f==in.flight.end()){
return "";
}
auto [date,days,cost]=f->second;
std::stringstream ss;
ss<<"<table border>";
ss<<tr(tag("th colspan=3","Championship plane tickets"));
ss<<tr(
th("Date")+
td([&](){
std::stringstream ss;
ss<<date<<" ("<<days<<" before start)";
return ss.str();
}())+
td("When is it expected to be most favorable for making a purchase decision. Note that this date may move in either direction as more results are known.")
);
ss<<tr(
th("Expected cost")+
td(cost)+
td("Measured in multiple of standard flight cost. A combination of how much tickets tend to be at that time ahead and the level of certainty in qualification")
);
ss<<tr(th("Warning")+tag("td colspan=2","As I control neither the Illuminati nor the Strait of Hormuz no guarantee of accuracy can be made."));
ss<<"</table>";
return ss.str();
}();
ss<<p(fly);
ss<<"<br>"<<"More on this team:";
ss<<frc_events(in.year,a.team);
ss<<" "<<the_blue_alliance(a.team,in.year);
ss<<" "<<statbotics(a.team,in.year);
ss<<" "<<frc_locks(a.team);
ss<<" "<<splat(in.district_short,a.team);
/*try{
ss<<p(in.extra.at(a.team));
}catch(...){
ss<<"Failed to read extra data for "<<a.team<<"\n";
}*/
return ss.str();
}
void gen_html(
std::ostream& o,
Gen_html_input const& in,
Event_categories_annotated<
Rank_status<Tournament_status>,
Tournament_status,
Rank_status<District_status>
> const& limits
){
const auto by_team=[&](){
std::map<tba::Team_key,tba::Team> r;
for(auto x:in.team_info){
r.insert(make_pair(x.key,x));
}
return r;
}();
Script_namer get_script_name;
auto dcmp_string=[&](tba::Team_key t)->string{
auto f=by_team.find(t);
if(f==by_team.end()){
PRINT(in.district_short)
PRINT(in.year)
PRINT(t)
}
if(f==by_team.end()){
cout<<"Warning: Could not find home event for "<<t<<"\n";
return "";
}
assert(f!=by_team.end());
auto f1=f->second;
if(f1.state_prov!="California"){
return string();
}
return as_string(california_region(f1));
};
auto by_dcmp=group([&](auto const& x){ return dcmp_string(x.team); },in.result);
//PRINT(by_dcmp);
const map<tba::Team_key,std::string> team_str=[&](){
map<tba::Team_key,std::string> r;
for(auto &p:by_dcmp){
auto &l=p.second;
std::sort(
l.begin(),
l.end(),
[](auto a,auto b){
auto t=[](auto x){ return make_tuple(round3(x.dcmp_make),round3(x.cmp_make),x); };
return t(a)<t(b);
}
);
std::reverse(l.begin(),l.end());
auto m=mapf([](auto x){ return x.team; },l);
for(auto [i,team]:enumerate_from(1,m)){
std::stringstream ss;
auto d=dcmp_string(team);
ss<<d;
if(!d.empty()){
ss<<"("<<i<<" of "<<m.size()<<")";
}
r.insert(make_pair(team,ss.str()));
}
}
return r;
}();
auto get_team_str=[=](tba::Team_key t){
auto f=team_str.find(t);
assert(f!=team_str.end());
return f->second;
};
auto nickname=[&](auto k){
auto f=by_team.find(k);
if(f==by_team.end()){
cout<<"Warning: Unexpected team: "<<k<<"\n";
return ::as_string(k);
}
auto v=f->second.nickname;
assert(v);
return *v;
};
auto dcmp_name=[=](int i)->string{
if(in.district_short=="ca"){
switch(i){
case 0:
return "NORTH";
case 1:
return "SOUTH";
default:
assert(0);
}
}
return "";
};
//auto dcmp_names=(in.district_short=="ca")?2:1;
//auto cutoff_table1=cutoff_table("District Championship",dcmp_cutoff_pr);
const auto cutoff_table1=join(mapf(
[&](auto i){
return cutoff_table(
get_script_name,
"District Championship "+dcmp_name(i),
in.dcmp_cutoff_pr[i],
in.dcmp_slots[i]
);
},
range(in.dcmp_slots.size())
));
const auto cutoff_table_cmp=cutoff_table(
get_script_name,
"FRC Championship",
in.cmp_cutoff_pr,
in.worlds_slots,
"(Excluding by award)"
);
//double total_entropy=sum(::mapf(entropy,seconds(result)));
if(0){
double total_entropy=sum(::mapf([](auto x){ return entropy(x); },mapf([](auto x){ return x.dcmp_make; },in.result)));
PRINT(total_entropy);
}
auto charts=[&]()->std::map<Team_key,std::string>{
if(in.plot){
return find_charts(in.points_used);
}
return {};
}();
auto fancy=[&](auto a){
stringstream ss;
ss<<table(tr(td(avatar(a.team))+td(nickname(a.team))))<<"\n";
return ss.str();
};
o<<tag("html",
tag("head",
tag("title",in.title)+
tag("style",STYLE)+
tag("script",SCRIPT)
)+
tag("body",
tag("h1",in.title)+
frc_events(in.year,in.district_short)+"<br>"+
the_blue_alliance(in.year,in.district_short)+"<br>"+
frc_locks(in.district_short)+"<br>"+
splat(in.district_short)+"<br>"+
event_status(limits)+
cutoff_table1+
cutoff_table_cmp+
h2("Team Probabilities")+
p(explain(get_script_name))+
tag("table border",
tr(join(::mapf(
[](auto x){ return th1(x.first); },
COLUMNS
)))+
join(
::mapf(
[&](auto p){
auto [i,a]=p;
auto name=get_script_name();
const auto used=[=]()->Team_points_used{
//in.points_used.at(a.team);
auto f=in.points_used.find(a.team);
if(f!=in.points_used.end()){
return f->second;
}
return Team_points_used({},0,-99,{});
}();
return tag("tr class=\"rank\" onclick=\"toggle_viz('"+name+"');event.preventDefault();\"",
td(as_string(i)+" "+get_team_str(a.team))+
colorize(a.dcmp_make)+
td_right(make_link(a.team,in.year))+
td(fancy(a))+
td_right(a.dcmp_interesting[0])+
td_right(a.dcmp_interesting[1])+
td_right(a.dcmp_interesting[2])+
colorize(a.cmp_make)+
td_right(a.cmp_interesting[0])+
td_right(a.cmp_interesting[1])+
td_right(a.cmp_interesting[2])+
td_right(used.rookie_bonus)+
td(join(" ",used.event_points_earned))+
td_right(used.events_left)
)+tag("tr class=\"hidden\" id=\""+name+"\"",
tag("td colspan=\"100%\"",team_details(in,limits,charts,a))
);
},
enumerate_from(1,reversed(sorted(
in.result,
[](auto x){ return make_tuple(round3(x.dcmp_make),round3(x.cmp_make),x); }
)))
)
)
)+
h2("Extra data")+as_table(limits)+
show_skill(in.skill)
)
);
}