-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot.cc
More file actions
2403 lines (2327 loc) · 98.5 KB
/
Copy pathplot.cc
File metadata and controls
2403 lines (2327 loc) · 98.5 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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "plot.h"
#include <algorithm>
#include <any>
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <ostream>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "app/app_io.pb.h"
#include "dataloader.h"
#include "gaussian_error.h"
#include "google/protobuf/map.h"
#include "google/protobuf/text_format.h"
#include "matplotlibcpp17/axes.h"
#include "matplotlibcpp17/figure.h"
#include "matplotlibcpp17/pyplot.h"
#include "pybind11/cast.h"
#include "pybind11/embed.h"
#include "pybind11/eval.h"
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
#include "pybind11/pytypes.h"
#include "src/google/protobuf/util/time_util.h"
#include "tools/plot/pivot_table.h"
namespace dyn_delta_approx {
namespace tools {
namespace plot {
namespace {
constexpr size_t numColors = 10;
constexpr size_t numLineStyles = 4;
constexpr size_t numMarkers = 10;
void save_textproto(const std::string& filename, app::app_io::Visualisation vis) {
app::app_io::VisualisationsFile file;
*file.add_visualisations() = vis;
std::ofstream file_output(filename);
std::string result;
google::protobuf::TextFormat::PrintToString(file, &result);
file_output << result;
}
constexpr absl::string_view kMinimizationProblem = "minimization_problem";
std::string prepare_title(const Visualisation& vis, const int capacity, const size_t count) {
std::string cap = "rnd";
if (capacity != -1) {
cap = std::to_string(capacity);
}
return absl::StrReplaceAll(vis.title(),
{{"$num", std::to_string(count)},
{"$capacity", cap},
{"$performance_characteristic", vis.performance_characteristic()}});
}
std::map<std::string, std::tuple<std::string, int, int>> result;
std::vector<bool> colors_used(numColors* numLineStyles, false);
std::map<std::string, std::tuple<std::string, int, int>> color_map(
const std::set<std::string>& names) {
if (names.size() > numColors * numLineStyles) {
std::cerr << "Too many in map" << std::endl;
exit(1);
}
std::hash<std::string> hasher;
for (auto c : names) {
if (result.contains(c)) {
continue;
}
if (result.contains(std::string(absl::StripAsciiWhitespace(c)))) {
result[c] = result[std::string(absl::StripAsciiWhitespace(c))];
continue;
}
std::string trimmed(absl::StripAsciiWhitespace(c));
int color = hasher(c) % (numColors);
while (colors_used[color]) {
color += 4;
color %= (numColors * numLineStyles);
}
colors_used[color] = true;
auto indx = color % numColors;
auto lIndx = color / numColors;
result[trimmed] =
std::make_tuple(std::string("C") + std::to_string(indx), lIndx, color % numMarkers);
result[c] = std::make_tuple(std::string("C") + std::to_string(indx), lIndx, color % numMarkers);
}
return result;
}
std::vector<Result> filter_exactness(std::vector<Result> data, Visualisation vis) {
auto bool_params = vis.bool_params();
auto string_params = vis.string_params();
bool exact_filter = bool_params["exact_filter"];
bool exact_filter_step1 = bool_params["exact_filter_step1"];
bool exact_filter_step_last1 = bool_params["exact_filter_step-1"];
bool exact_only = bool_params["exact_only"];
bool exact_only_step1 = bool_params["exact_only_step1"];
bool allow_list_instance = bool_params["allow_list_instances"];
if (allow_list_instance) {
std::set<std::string> filter_set;
for (int i = 0; i < 10; i++) {
if (auto pram = string_params[std::string("allow_instance") + std::to_string(i)];
pram != "") {
filter_set.insert(pram);
}
}
FilterBy<Result, std::string> filter(data, [](auto a) { return a.hypergraph().name(); });
data = filter.having([&](auto a) { return filter_set.contains(a); });
std::cout << "[INFO] Filtered allow list of size" << filter_set.size() << "." << std::endl;
}
if (exact_filter) {
std::cout << "[INFO] Filtering only at least one exact results." << std::endl;
FilterBy<Result, std::string> filter(data, [](auto a) { return a.hypergraph().name(); });
data = filter.at_least_one([](Result r) { return r.is_exact(); });
}
if (exact_filter_step1) {
std::cout << "[INFO] Filtering only at least one exact in step 1 results." << std::endl;
FilterBy<Result, std::string> filter(data, [](auto a) { return a.hypergraph().name(); });
data = filter.at_least_one([](Result r) {
return r.algorithm_run_informations_size() > 1 &&
r.algorithm_run_informations()[1].is_exact();
});
}
if (exact_filter_step_last1) {
std::cout << "[INFO] Filtering only at least one exact in last step 1 results." << std::endl;
FilterBy<Result, std::string> filter(data, [](auto a) { return a.hypergraph().name(); });
data = filter.at_least_one([](Result r) {
return r.algorithm_run_informations_size() > 0 &&
r.algorithm_run_informations()[r.algorithm_run_informations_size() - 1].is_exact();
});
}
if (exact_only) {
std::cout << "[INFO] Filtering only fully exact results." << std::endl;
FilterBy<Result, std::string> filter(data, [](auto a) { return a.hypergraph().name(); });
data = filter.all([](Result r) { return r.is_exact(); });
}
if (exact_only_step1) {
std::cout << "[INFO] Filtering only fully exact in step 1 results." << std::endl;
FilterBy<Result, std::string> filter(data, [](auto a) { return a.hypergraph().name(); });
data = filter.all([](Result r) {
return r.algorithm_run_informations_size() > 1 &&
r.algorithm_run_informations()[1].is_exact();
});
}
return data;
}
using RetTyp1 =
std::tuple<std::map<std::string, std::map<std::string, GaussianError<double>>>, int>;
// @returns Map of per algorithm per instance.
absl::StatusOr<RetTyp1> normalize_by_optimum(Visualisation vis, std::vector<Result>& data,
std::string sort = "all", int cap = 0) {
std::cout << "[INFO] Begin: Plotting " << data.size() << " results." << std::endl;
std::function<double(Result)> performance_char = [](Result r) -> double { return r.weight(); };
if (vis.performance_characteristic() == "size") {
performance_char = [](Result r) -> double { return r.size(); };
}
if (vis.performance_characteristic() == "quality") {
performance_char = [](Result r) -> double { return r.quality(); };
}
if (vis.performance_characteristic() == "memory") {
performance_char = [](Result r) -> double {
return r.run_information().max_allocated_memory_in_mb();
};
}
if (vis.performance_characteristic() == "last_edge_count") {
performance_char = [](Result r) -> double {
return r.algorithm_run_informations()[r.algorithm_run_informations_size() - 1].edge_count();
};
}
if (vis.performance_characteristic() == "last_node_count") {
performance_char = [](Result r) -> double {
return r.algorithm_run_informations()[r.algorithm_run_informations_size() - 1].node_count();
};
}
if (vis.performance_characteristic() == "last_runtime") {
performance_char = [](Result r) -> double {
return ::google::protobuf::util::TimeUtil::DurationToNanoseconds(
r.algorithm_run_informations()[r.algorithm_run_informations_size() - 1].algo_duration());
};
}
if (vis.performance_characteristic() == "runInformation_algoDuration" ||
vis.performance_characteristic() == "runtime") {
performance_char = [](Result r) -> double {
return ::google::protobuf::util::TimeUtil::DurationToNanoseconds(
r.run_information().algo_duration());
};
}
auto bool_params = vis.bool_params();
auto double_params = vis.double_params();
auto string_params = vis.string_params();
std::string normalize_by = string_params["normalize"];
data = filter_exactness(data, vis);
std::cout << "[INFO] Filtered: Plotting " << data.size() << " results." << std::endl;
using GE = GaussianError<double>;
std::function<GE(GE, GE)> optimal = [](auto a, auto b) { return std::max(a, b); };
std::function<GE(GE, GE)> not_optimal = [](auto a, auto b) { return std::min(a, b); };
if (bool_params[kMinimizationProblem]) {
optimal = [](GE a, GE b) { return std::min(a, b); };
not_optimal = [](GE a, GE b) { return std::max(a, b); };
}
// TODO maybe move to common preprocess step
std::map<std::string, std::vector<Result>> by_heuristic;
for (auto& r : data) {
by_heuristic[r.run_config().short_name()].push_back(r);
}
// make sure all heuristics are included, this step does not guarantee, check after compression
// first heuristic, second index instance
std::vector<std::pair<std::string, std::map<std::string, GaussianError<double>>>> double_grouped;
// TODO add specific values
for (auto& [heuristic, data_points] : by_heuristic) {
// group by instance
GroupBy<Result, std::string> grouped(data_points,
[](Result r) { return r.hypergraph().name(); });
double_grouped.push_back(std::make_pair(heuristic, grouped.avg(performance_char)));
}
std::map<std::string, std::map<std::string, GaussianError<double>>> ratios;
int idx = 0;
int i = 0;
if (double_grouped.size() == 0) {
return absl::DataLossError("no data left.");
}
for (auto& [heur, points] : double_grouped) {
if (double_grouped[idx].second.size() < points.size()) {
idx = i;
}
i++;
}
auto& [heur, points] = double_grouped[idx];
size_t count = 0;
for (auto& [instance, val] : points) {
// find min
// TODO Add maximization
GaussianError<double> opt_val = val;
GaussianError<double> non_opt_val = val;
bool not_found = false;
for (auto& [alg, data2] : double_grouped) {
if (data2.find(instance) == data2.end()) {
std::cerr << "[WARN] heuristic " << alg << " does not contain " << instance << std::endl;
not_found = true;
break;
}
if (data2[instance] < 0.0) {
std::cout << "[ERROR] negative value of " << data2[instance] << " at instance " << instance
<< std::endl;
}
if (normalize_by != "") {
if (alg == normalize_by) {
opt_val = data2[instance];
}
} else {
opt_val = optimal(data2[instance], opt_val);
non_opt_val = not_optimal(data2[instance], non_opt_val);
}
}
if (not_found) {
continue;
}
if (opt_val == 0.0) {
std::cout << "[WARN] ZERO opt: " << opt_val << " in instance: '" << instance
<< "'. will ignore this datapoint." << std::endl;
continue;
}
if (non_opt_val < double_params["min_max_threshold"] * 1e9) {
if (bool_params["verbose"]) {
std::cout << "[VERBOSE] non_opt_val " << instance << " " << non_opt_val << " < "
<< double_params["min_max_threshold"] << std::endl;
}
continue;
}
count++;
if (bool_params["verbose"]) {
std::cout << "[VERBOSE] opt_val " << instance << " " << opt_val << std::endl;
}
for (auto& [alg, data2] : double_grouped) {
if (bool_params["verbose"]) {
std::cout << "[INFO] " << alg << " " << instance << " " << data2[instance] / opt_val
<< std::endl;
}
if (bool_params["raw_value"]) {
ratios[alg][instance] = data2[instance];
} else {
ratios[alg][instance] = data2[instance] / opt_val;
}
}
}
return std::make_tuple(ratios, count);
}
} // namespace
VisualisationTool::VisualisationTool(const std::string& vis_file) {
std::ifstream f(vis_file);
if (!f) {
std::cerr << vis_file << " was not openable." << std::endl;
exit(1);
}
std::stringstream buffer;
buffer << f.rdbuf();
if (!google::protobuf::TextFormat::ParseFromString(buffer.str(), &file)) {
std::cerr << vis_file << " was not parsable." << std::endl;
exit(1);
}
mod = pybind11::module::import("matplotlib.pyplot");
seaborn = pybind11::module::import("seaborn");
pandas = pybind11::module::import("pandas");
skl = pybind11::module::import("sklearn.linear_model");
np = pybind11::module::import("numpy");
mod.attr("style").attr("use")(Args("bmh")); // bmh
constexpr int BIGGER_SIZE = 12;
constexpr int MEDIUM_SIZE = 10;
constexpr int SMALL_SIZE = 8;
mod.attr("rc")(*Args("font"), **Kwargs("size"_a = BIGGER_SIZE));
mod.attr("rc")(*Args("figure"), **Kwargs("titlesize"_a = BIGGER_SIZE));
mod.attr("rc")(*Args("legend"), **Kwargs("fontsize"_a = SMALL_SIZE));
mod.attr("rc")(*Args("xtick"), **Kwargs("labelsize"_a = SMALL_SIZE));
mod.attr("rc")(*Args("ytick"), **Kwargs("labelsize"_a = SMALL_SIZE));
mod.attr("rc")(*Args("legend"), **Kwargs("title_fontsize"_a = SMALL_SIZE));
mod.attr("rc")(*Args("axes"), **Kwargs("titlesize"_a = SMALL_SIZE, "labelsize"_a = SMALL_SIZE));
auto bool_params = file.bool_params();
if (bool_params["true_type"]) {
mod.attr("rc")(*Args("pdf"), **Kwargs("fonttype"_a = 42));
mod.attr("rc")(*Args("ps"), **Kwargs("fonttype"_a = 42));
}
plt = matplotlibcpp17::pyplot::PyPlot(mod);
// plt.plot()
}
void VisualisationTool::ensurePathsExists(std::string root_path, Visualisation& vis) {
std::filesystem::create_directories(root_path + "/vis/" + vis.folder_name());
vis.set_folder_name(root_path + "/vis/" + vis.folder_name());
}
#define lambda_bind(function_name) \
[&](auto a, auto b, auto c, auto d) { return this->function_name(a, b, c, d); }
Visualisation ensureDefaults(const Visualisation& vis,
const app::app_io::VisualisationsFile& file) {
auto default_string_params = file.string_params();
auto default_int64_params = file.int64_params();
auto default_double_params = file.double_params();
auto default_bool_params = file.bool_params();
auto default_rename_labels = file.rename_labels();
auto default_display_labels = file.display_labels();
for (auto [k, v] : vis.string_params()) {
default_string_params[k] = v;
}
for (auto [k, v] : vis.double_params()) {
default_double_params[k] = v;
}
for (auto [k, v] : vis.int64_params()) {
default_int64_params[k] = v;
}
for (auto [k, v] : vis.bool_params()) {
default_bool_params[k] = v;
}
for (auto [k, v] : vis.rename_labels()) {
default_rename_labels[k] = v;
}
for (auto [k, v] : vis.display_labels()) {
default_display_labels[k] = v;
}
Visualisation result = vis;
*result.mutable_bool_params() = default_bool_params;
*result.mutable_string_params() = default_string_params;
*result.mutable_double_params() = default_double_params;
*result.mutable_int64_params() = default_int64_params;
*result.mutable_rename_labels() = default_rename_labels;
*result.mutable_display_labels() = default_display_labels;
return result;
}
absl::Status VisualisationTool::plot(std::string root_path) {
for (auto vis : file.visualisations()) {
// ensure default parameters are inserted
vis = ensureDefaults(vis, file);
auto bool_params = vis.bool_params();
auto string_params = vis.string_params();
auto data_s = dyn_delta_approx::tools::plot::load_data(
root_path, vis.experiment_paths(),
std::map<std::string, std::string>(vis.rename_labels().begin(), vis.rename_labels().end()),
string_params["correct_path"], bool_params["prefix_names"], bool_params["print_params"]);
if (!data_s.ok()) {
return data_s.status();
}
auto data = data_s.value();
if (string_params["ignore_sort0"] != "") {
for (auto& v : data) {
v.filter({string_params["ignore_sort0"]});
}
}
ensurePathsExists(root_path, vis);
if (vis.type() == "performance_profile") {
if (auto st = plot_internal(data, vis, lambda_bind(plot_performance_profile_internal));
!st.ok()) {
return st;
}
} else if (vis.type() == "scatter_plot") {
if (auto st = plot_internal(data, vis, lambda_bind(scatter_plot_internal)); !st.ok()) {
return st;
}
} else if (vis.type() == "table") {
if (auto st = table(data, vis); !st.ok()) {
return st;
}
} else if (vis.type() == "stats_table") {
if (auto st = stats_table(data, vis); !st.ok()) {
return st;
}
} else if (vis.type() == "efficency") {
if (auto st = efficency(data, vis); !st.ok()) {
return st;
}
} else if (vis.type() == "stats") {
if (auto st = stats(data, vis); !st.ok()) {
return st;
}
} else if (vis.type() == "stats_debug") {
if (auto st = stats_debug(data, vis); !st.ok()) {
return st;
}
} else if (vis.type() == "categorial_bar_plot") {
if (auto st = categorial_bar_plot(data, vis); !st.ok()) {
return st;
}
} else if (vis.type() == "scatter_debug") {
if (auto st = scatter_debug_plot(data, vis); !st.ok()) {
return st;
}
} else if (vis.type() == "jointplot") {
if (auto st = jointplot_plot(data, vis); !st.ok()) {
return st;
}
} else if (vis.type() == "violin_plot") {
if (auto st = violin_plot(data, vis); !st.ok()) {
return st;
}
} else if (vis.type() == "write_csv") {
if (auto st = write_csv(data, vis); !st.ok()) {
return st;
}
} else {
return absl::UnimplementedError(" type '" + vis.type() + "' is not implemented (yet).");
}
}
return absl::OkStatus();
}
absl::Status VisualisationTool::plot_performance_profile_internal(Visualisation vis,
std::vector<Result>& data,
std::string sort, int cap) {
auto bool_params = vis.bool_params();
auto double_params = vis.double_params();
auto string_params = vis.string_params();
auto int64_params = vis.int64_params();
std::function<double(double, double)> optimal = [](double a, double b) { return std::max(a, b); };
std::function<double(double, double)> not_optimal = [](double a, double b) {
return std::min(a, b);
};
if (bool_params[kMinimizationProblem]) {
optimal = [](double a, double b) { return std::min(a, b); };
not_optimal = [](double a, double b) { return std::max(a, b); };
// comperator stays the same comparator = [](double a, double b) { return a < b; };
}
auto ratios_s = normalize_by_optimum(vis, data, sort, cap);
if (!ratios_s.ok()) {
return ratios_s.status();
}
auto [ratios_m, count] = std::move(ratios_s.value());
if (bool_params["verbose"]) {
std::cout << "[VERBOSE] count " << count << std::endl;
}
// ratios stores the number of entries at x-axis point x
std::map<std::string, std::map<double, size_t>> ratios;
for (const auto& [algo, data] : ratios_m) {
std::string min_instance = data.begin()->first;
auto min_val = data.begin()->second;
for (const auto& [instance, value] : data) {
ratios[algo][value.getValue()] += 1; // TODO pass on uncertainity
if (value < min_val) {
min_val = value;
min_instance = instance;
}
}
std::cout << algo << " Instance " << min_instance << " " << min_val << std::endl;
}
std::map<double, std::map<std::string, double>> displays;
double factor = 1.0 / ((double)ratios_m.begin()->second.size());
std::set<double> x_axis;
x_axis.insert(1.0);
for (auto& [alg, ratio] : ratios) {
if (ratio.size() == 0) {
std::cout << "[WARN] empty ratio. Something is wrong" << std::endl;
}
double i = 0;
for (auto [r, c] : ratio) {
x_axis.insert(r);
i += c;
displays[r][alg] = (i)*factor;
if (bool_params["verbose"]) {
std::cout << "[VERBOSE]" << alg << " " << r << " " << displays[r][alg] << std::endl;
}
}
// TODO (add avg output)
// std::iota(y_values.begin(), y_values.end(), 0);
// for (auto y : y_values) {
// y /= (double)y_values.size();
// }
// TODO optimum
}
if (!bool_params[kMinimizationProblem]) {
for (auto& [r, map] : displays) {
for (auto& [_, f] : map) {
f = 1.0 - f;
}
}
}
// map is ordered by <
std::map<std::string, std::vector<double>> y_axis;
std::vector<double> x_axis_vec;
// x_axis_vec.push_back(0.0);
for (auto x : x_axis) {
x_axis_vec.push_back(x);
}
std::map<std::string, double> next_lower_value;
if (!bool_params[kMinimizationProblem]) {
for (auto& [algo, _] : ratios) {
next_lower_value[algo] = 1.0;
}
}
for (auto& [x_value, map] : displays) {
for (auto [k, v] : ratios) {
if (map.find(k) == map.end()) {
map[k] = next_lower_value[k];
}
next_lower_value[k] = map[k];
}
for (auto [s, y_value] : map) {
y_axis[s].push_back(y_value);
}
}
auto least_optimal_value = x_axis_vec.front();
std::cout << "Least: " << least_optimal_value << std::endl;
for (const auto& x : x_axis_vec) {
least_optimal_value = not_optimal(least_optimal_value, x);
}
std::cout << "Least: " << least_optimal_value << std::endl;
if (!bool_params[kMinimizationProblem]) {
std::reverse(x_axis_vec.begin(), x_axis_vec.end());
}
x_axis_vec.push_back(least_optimal_value * (bool_params[kMinimizationProblem] ? 1.1 : 0.9));
for (auto& [k, v] : y_axis) {
if (!bool_params[kMinimizationProblem]) {
std::reverse(v.begin(), v.end());
}
v.push_back(1.0);
}
int n = 0;
std::vector<std::string> styles = {"-", "--", "-.", ":"};
std::vector<std::string> markers = {
".", "o", "v", "^", "<", ">", "8", "s", "p", "*",
};
auto fig =
plt.figure(Args(), Kwargs("figsize"_a = py::make_tuple(double_params["fig_width_inch"],
double_params["fig_height_inch"])));
auto ax = fig.add_axes(Args(std::vector<double>({0.125, 0.175, 0.80, 0.75})));
std::vector<std::pair<std::string, std::vector<double>>> y_axis2;
std::copy(y_axis.begin(), y_axis.end(), std::back_inserter(y_axis2));
if (bool_params["sort_legend_avg"]) {
std::map<std::string, double> val;
for (auto& [name, values] : y_axis2) {
val[name] = std::transform_reduce(
values.begin(), values.end(), x_axis_vec.begin(), 0.0,
[](auto a, auto b) { return a + b; }, [](auto a, auto b) { return a * b; }) /
((double)x_axis_vec.size());
}
std::sort(y_axis2.begin(), y_axis2.end(),
[&](auto& a, auto& b) { return val[a.first] > val[b.first]; });
}
std::set<std::string> names;
for (auto& [k, v] : y_axis2) {
names.insert(k);
}
auto cmap = color_map(names);
for (auto& [algo, y_a] : y_axis2) {
auto [color, line_style, mark] = cmap[algo];
ax.plot(Args(x_axis_vec, y_a),
Kwargs("label"_a = algo, "alpha"_a = 0.7,
"drawstyle"_a = (bool_params[kMinimizationProblem] ? "steps-post" : "steps-pre"),
"linestyle"_a = styles[line_style], "markersize"_a = 4, "markevery"_a = 0.1,
"marker"_a = markers[mark], "color"_a = color));
n++;
}
if (double_params["x_limit"] != 0) {
ax.set_xlim(Args(1.0, double_params["x_limit"]));
} else {
ax.set_xlim(Args(1.0, least_optimal_value * (bool_params[kMinimizationProblem] ? 1.1 : 0.9)));
}
if (bool_params["x_log_scale"]) {
ax.set_xscale(Args("log"));
}
ax.set_xlabel(Args("$\\tau$"));
ax.set_ylabel(Args("Fraction of Instances"));
if (!bool_params["legend_off"]) {
std::string legend_pos =
string_params["legend_pos"] == "" ? "best" : string_params["legend_pos"];
ax.legend(Args(), Kwargs("loc"_a = legend_pos));
}
if (bool_params["legend_seperate"]) {
int rows_count = 1;
if (int64_params["legend_rows"] != 0) {
rows_count = int64_params["legend_rows"];
}
auto [fig_legend, ax_legend] = plt.subplots();
auto legend = ax_legend.legend(
ax.unwrap().attr("get_legend_handles_labels")(),
Kwargs("ncol"_a = (int)std::ceil(((double)y_axis2.size()) / ((double)rows_count)),
"loc"_a = "center")); // ncol=2, loc='center'
auto renderer = fig_legend.unwrap().attr("canvas").attr("get_renderer")();
auto bbox =
legend.unwrap()
.attr("get_window_extent")(*Args(renderer))
.attr("transformed")(fig_legend.unwrap().attr("dpi_scale_trans").attr("inverted")());
ax_legend.unwrap().attr("axis")(*Args("off"));
fig_legend.savefig(Args(vis.folder_name() + "/" + vis.file_prefix() + "_performance_profile_" +
std::to_string(cap) + "_" + sort + ".legend.pdf"),
Kwargs("bbox_inches"_a = bbox));
}
// # Get the bounding box of the legend
// renderer = fig_legend.canvas.get_renderer()
// bbox = legend.get_window_extent(renderer).transformed(fig_legend.dpi_scale_trans.inverted())
// # Save the legend with the adjusted bounding box
// fig_legend.savefig('legend.pdf', bbox_inches=bbox)
// # Save the legend to a separate PDF
// fig_legend, ax_legend = plt.subplots()
// ax_legend.axis('off') # Hide axes
// ax_legend.legend(*ax.get_legend_handles_labels()) # Add legend to new figure
// fig_legend.savefig('legend.pdf', bbox_inches='tight')
// # Save the plot without the legend
// ax.legend().remove() # Remove legend from original plot
// fig.savefig('plot.pdf')
ax.set_title(Args(prepare_title(vis, cap, count)));
fig.savefig(Args(vis.folder_name() + "/" + vis.file_prefix() + "_performance_profile_" +
std::to_string(cap) + "_" + sort + ".png"));
fig.savefig(Args(vis.folder_name() + "/" + vis.file_prefix() + "_performance_profile_" +
std::to_string(cap) + "_" + sort + ".pdf"),
Kwargs("bbox_inches"_a = "tight"));
save_textproto(vis.folder_name() + "/" + vis.file_prefix() + "_performance_profile_" +
std::to_string(cap) + "_" + sort + ".textproto",
vis);
std::cout << "Plotted "
<< (vis.folder_name() + "/" + vis.file_prefix() + "_performance_profile_" +
std::to_string(cap) + "_" + sort + ".pdf")
<< std::endl
<< (vis.folder_name() + "/" + vis.file_prefix() + "_performance_profile_" +
std::to_string(cap) + "_" + sort + ".png")
<< std::endl;
plt.clf(Args(), Kwargs());
// TODO add min/max settings
return absl::OkStatus();
}
absl::Status VisualisationTool::plot_internal(
std::vector<ExperimentResult>& data, Visualisation vis,
std::function<absl::Status(Visualisation, std::vector<Result>&, std::string, int)> function) {
auto transformed = transform_by_sort(data, vis);
for (auto& [sort, data_points] : transformed) {
std::cout << "Plotting " << sort << std::endl;
if (vis.capacity_size() > 0) {
for (auto cap : vis.capacity()) {
decltype(data_points) filtered;
std::copy_if(data_points.begin(), data_points.end(), std::back_inserter(filtered),
[cap](Result e) { return e.run_config().capacity() == cap; });
auto plotted = function(vis, filtered, sort, cap);
if (!plotted.ok()) {
return plotted;
}
}
} else {
auto plotted = function(vis, data_points, sort, 0);
if (!plotted.ok()) {
return plotted;
}
}
}
return absl::OkStatus();
}
absl::Status VisualisationTool::table_internal(Visualisation vis, std::vector<Result>& data2,
std::vector<FailedExperiment>& failed,
const std::set<std::string> index,
const std::set<std::string> pivot, std::string sort,
int64_t cap) {
auto string_params = vis.string_params();
auto bool_params = vis.bool_params();
std::vector<Result> data;
if (bool_params["special_copy"]) {
std::set<std::string> in_data = {"min_degree", "max_degree", "connected_components", "b_edges",
"a_nodes"};
std::copy_if(data2.begin(), data2.end(), std::back_inserter(data),
[&](const auto& a) { return in_data.contains(a.run_config().short_name()); });
} else {
data = data2;
}
data = filter_exactness(data, vis);
GroupBy<Result, std::array<std::string, 2>> groupby_instance_run_config(
data, [](Result result) -> std::array<std::string, 2> {
return {result.hypergraph().name(), result.run_config().short_name()};
});
auto grouped_inst = groupby_instance_run_config.agg<Result>(
[](Result r) { return r; },
[](Result a, Result b) {
*b.mutable_run_information()->mutable_algo_duration() =
::google::protobuf::util::TimeUtil::NanosecondsToDuration(
::google::protobuf::util::TimeUtil::DurationToNanoseconds(
a.run_information().algo_duration()) +
::google::protobuf::util::TimeUtil::DurationToNanoseconds(
b.run_information().algo_duration()));
return b;
},
[](Result a, size_t s) {
*a.mutable_run_information()->mutable_algo_duration() =
::google::protobuf::util::TimeUtil::NanosecondsToDuration(
::google::protobuf::util::TimeUtil::DurationToNanoseconds(
a.run_information().algo_duration()) /
s);
return a;
},
Result{});
std::vector<Result> as_v;
std::transform(grouped_inst.begin(), grouped_inst.end(), std::back_inserter(as_v),
[](auto a) { return a.second; });
GroupBy<Result, std::array<std::map<std::string, Any>, 2>> grouped(
bool_params["group_per_instance"] ? as_v : data,
[&](Result result) -> std::array<std::map<std::string, Any>, 2> {
std::map<std::string, Any> res1, res2;
for (auto i : index) {
res1.insert(std::make_pair(i, performance_characteristics.at(i)(result)));
}
for (auto i : pivot) {
res2.insert(std::make_pair(i, performance_characteristics.at(i)(result)));
}
return {res1, res2};
});
GroupBy<FailedExperiment, std::array<std::map<std::string, Any>, 2>> failed_group(
failed, [&](FailedExperiment result) -> std::array<std::map<std::string, Any>, 2> {
std::map<std::string, Any> res1, res2;
for (auto i : index) {
res1.insert(std::make_pair(i, performance_characteristics_failed.at(i)(result)));
}
for (auto i : pivot) {
res2.insert(std::make_pair(i, performance_characteristics_failed.at(i)(result)));
}
return {res1, res2};
});
std::cout << "FAiled: " << failed_group.different_element_count() << std::endl;
PivotTable pivot_table(
string_params["mean"] == "max" ? grouped.max<double>([&](Result r) {
return (performance_characteristics.at(vis.performance_characteristic())(r).to_double());
})
: string_params["mean"] == "geometric"
? grouped.geo_mean_log<double>([&](Result r) -> double {
return (
performance_characteristics.at(vis.performance_characteristic())(r).to_double());
})
: grouped.agg<double>(
[&](Result r) -> double {
return (performance_characteristics.at(vis.performance_characteristic())(r)
.to_double());
},
[](auto a, auto b) { return a + b; },
[](auto a, size_t n) { return a / ((double)n); }),
failed_group.agg<app::app_io::FailReason>(
[](auto FR) { return FR.reason(); }, [](auto a, auto b) { return b; },
[](auto a, size_t s) { return a; }, app::app_io::FailReason::Unkown),
index, pivot, {/*"b_m_2_approx", "a_nodes_2_approx", "c_m/n"*/},
[&](const auto& entry, const auto& virtual_func) -> Any {
for (auto [k, v] : entry) {
std::cout << k << " : " << v.to_string() << std::endl;
}
if (virtual_func == "b_m_2_approx") {
for (const auto& d : data2) {
if (d.hypergraph().name() == entry.at("hypergraph_name").to_string() &&
d.run_config().short_name() == "2+dfs_m") {
return ((double)d.algorithm_run_informations()[0].edge_count()) /
((double)d.hypergraph().edge_count());
}
}
}
if (virtual_func == "a_nodes_2_approx") {
for (const auto& d : data2) {
if (d.hypergraph().name() == entry.at("hypergraph_name").to_string() &&
d.run_config().short_name() == "2+dfs_m") {
return ((double)d.algorithm_run_informations()[0].node_count()) /
((double)d.hypergraph().node_count());
}
}
}
if (virtual_func == "c_m/n") {
for (const auto& d : data2) {
if (d.hypergraph().name() == entry.at("hypergraph_name").to_string() &&
d.run_config().short_name() == "2+dfs_m") {
return ((double)d.hypergraph().edge_count()) / ((double)d.hypergraph().node_count());
}
}
}
return {};
});
std::ofstream output(vis.folder_name() + "/" + vis.file_prefix() + "_table_" +
std::to_string(cap) + "_" + sort + ".tex");
std::function<bool(Any, Any)> compare = [](Any a, Any b) { return a < b; };
if (!bool_params["minimization_problem"]) {
compare = [](Any a, Any b) { return a > b; };
}
output << pivot_table
.to_table("size", bool_params["normalize_rows_max"], true, vis,
[&](auto a) {
std::vector<std::set<std::string>> res;
Any min = 0.0;
Any max = 0.0;
bool first = true;
for (const auto& [name, v] : a) {
if ((string_params["ignore_min"] != "") &&
name.at("runConfig_shortName").to_string() >=
string_params["ignore_min"]) {
continue;
}
if (first && !v.isError()) {
first = false;
min = v;
max = v;
}
if (compare(v, min) && !v.isError()) {
min = v;
}
if (v > max && !v.isError()) {
max = v;
}
}
for (auto& [name, v] : a) {
if ((string_params["ignore_min"] != "") &&
name.at("runConfig_shortName").to_string() >=
string_params["ignore_min"]) {
res.push_back({});
continue;
}
if (v.isError()) {
res.push_back({"error"});
} else if (v >= max && bool_params["max_bf"]) {
res.push_back({"max"});
} else if (v == min && !bool_params["max_bf"]) {
res.push_back({"min"});
} else {
res.push_back({});
}
}
return res;
})
.to_latex("Caption", "table:def", pivot_table.colspec());
std::cout << "Wrote "
<< vis.folder_name() + "/" + vis.file_prefix() + "_table_" + std::to_string(cap) + "_" +
sort + ".tex"
<< std::endl;
return absl::OkStatus();
}
std::string rename_or_default(std::string s, auto map) {
auto& ref = map[s];
if (ref != "") {
return ref;
}
return s;
}
absl::Status VisualisationTool::stats_from_debug_table_internal(
Visualisation vis, std::vector<Result>& data2, std::vector<FailedExperiment>& failed,
std::string sort, int64_t cap) {
std::map<std::string, std::map<std::string, Any>> properties_by_hname;
auto rlabels = vis.rename_labels();
for (auto d : data2) {
auto h_name = d.hypergraph().name();
for (auto [k, v] : d.algorithm_run_informations()[d.algorithm_run_informations_size() - 1]
.debug_information()
.int64_info()) {
properties_by_hname[rename_or_default(k, rlabels)][h_name] = v;
}
for (auto [k, v] : d.algorithm_run_informations()[d.algorithm_run_informations_size() - 1]
.debug_information()
.double_info()) {
properties_by_hname[rename_or_default(k, rlabels)][h_name] = v;
}
for (auto [k, v] : d.algorithm_run_informations()[d.algorithm_run_informations_size() - 1]
.debug_information()
.bool_info()) {
properties_by_hname[rename_or_default(k, rlabels)][h_name] = v;
}
for (auto [k, v] : d.algorithm_run_informations()[d.algorithm_run_informations_size() - 1]
.debug_information()
.string_info()) {
properties_by_hname[rename_or_default(k, rlabels)][h_name] = v;
}
}
auto display_labels = vis.display_labels();
std::ofstream output(vis.folder_name() + "/" + vis.file_prefix() + "_debug_stats_table_" +
std::to_string(cap) + "_" + sort + ".tex");
for (auto [k, map] : properties_by_hname) {
output << " & " << rename_or_default(k, display_labels);
}
output << std::endl;
if (properties_by_hname.size() == 0) {
return absl::NotFoundError("no debug data found");
}
py::object lambda_func = py::eval("lambda x: x");
auto string_params = vis.string_params();
if (auto lsource = string_params["hypergraph_name_lambda"]; lsource != "") {
lambda_func = py::eval(lsource);
}
for (auto [h, v] : properties_by_hname.begin()->second) {
output << (lambda_func(h).cast<std::string>());
for (auto [k, map] : properties_by_hname) {
output << " & " << map[h].to_string();
}
output << "\\\\" << std::endl;
}
std::cout << "Written "
<< vis.folder_name() + "/" + vis.file_prefix() + "_debug_stats_table_" +
std::to_string(cap) + "_" + sort + ".tex"
<< std::endl;
return absl::OkStatus();
}
absl::Status VisualisationTool::stats_table_internal(Visualisation vis, std::vector<Result>& data2,
std::vector<FailedExperiment>& failed,
const std::set<std::string> index,
const std::set<std::string> pivot,
std::string sort, int64_t cap) {
auto string_params = vis.string_params();
auto bool_params = vis.bool_params();
std::vector<Result> data = data2;
data = filter_exactness(data, vis);
GroupBy<Result, std::map<std::string, Any>> grouped(
data, [&](Result result) -> std::map<std::string, Any> {
std::map<std::string, Any> res1;
for (auto i : index) {
res1.insert(std::make_pair(i, performance_characteristics.at(i)(result)));
}
return res1;
});
GroupBy<FailedExperiment, std::map<std::string, Any>> failed_group(
failed, [&](FailedExperiment result) -> std::map<std::string, Any> {
std::map<std::string, Any> res1;
for (auto i : index) {
res1.insert(std::make_pair(i, performance_characteristics_failed.at(i)(result)));
}
return {res1};
});
std::set<std::map<std::string, Any>> transformed_pivot;
for (auto p : pivot) {
transformed_pivot.insert({{"entry", p}});
}
std::map<std::map<std::string, Any>, GroupByEvaluator<Any>> evals;