CLUEstering
High-performance density-based weighted clustering library developed at CERN
Loading...
Searching...
No Matches
validation.hpp
1
2#pragma once
3
4#include <algorithm>
5#include <ranges>
6#include <span>
7#include <vector>
8
9namespace clue {
10
11 inline int compute_nclusters(std::span<const int> cluster_ids) {
12 return std::reduce(cluster_ids.begin(),
13 cluster_ids.end(),
14 std::numeric_limits<int>::lowest(),
15 [](int a, int b) { return std::max(a, b); }) +
16 1;
17 }
18
19 inline std::vector<std::vector<int>> compute_clusters_points(std::span<const int> cluster_ids) {
20 const auto nclusters = compute_nclusters(cluster_ids);
21 std::vector<std::vector<int>> clusters_points(nclusters);
22
23 std::for_each(cluster_ids.begin(), cluster_ids.end(), [&, i = 0](auto cluster_id) mutable {
24 if (cluster_id > -1)
25 clusters_points[cluster_id].push_back(i++);
26 });
27
28 return clusters_points;
29 }
30
31 inline std::vector<int> compute_clusters_size(std::span<const int> cluster_ids) {
32 const auto nclusters = compute_nclusters(cluster_ids);
33 const auto clusters_points = compute_clusters_points(cluster_ids);
34
35 std::vector<int> clusters(nclusters);
36 std::ranges::transform(
37 clusters_points, clusters.begin(), [&](const auto& cluster) { return cluster.size(); });
38 return clusters;
39 }
40
41 inline bool validate_results(std::span<const int> cluster_ids, std::span<const int> truth) {
42 auto result_clusters_sizes = compute_clusters_size(cluster_ids);
43 auto truth_clusters_sizes = compute_clusters_size(truth);
44 std::ranges::sort(result_clusters_sizes);
45 std::ranges::sort(truth_clusters_sizes);
46
47 bool compare_nclusters = compute_nclusters(cluster_ids) == compute_nclusters(truth);
48 bool compare_clusters_size = std::ranges::equal(result_clusters_sizes, truth_clusters_sizes);
49
50 return compare_nclusters && compare_clusters_size;
51 }
52
53} // namespace clue