Docs
Loading...
Searching...
No Matches
Data.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
9#include <Eigen/Dense>
10#include <unordered_map>
11#include <vector>
12
13// compilation time verbosity level
14#ifndef VERBOSITY_LEVEL
15#define VERBOSITY_LEVEL 0
16#endif
17
26class Data {
27protected:
28 const utils_params &p;
29
30 Eigen::VectorXi allocations;
31 int K;
32
34 std::unordered_map<int, std::vector<int>> cluster_members;
35
40 void compact_cluster(int old_cluster);
41
48 void set_allocation_wo_compaction(int index, int cluster);
49
50public:
58 Data(const utils_params &p,
59 const Eigen::VectorXi &initial_allocations = Eigen::VectorXi());
60
65
72 double get_data(int i, int j = 0) const;
73
78 int get_n() const { return p.n; }
79
84 int get_K() const { return K; }
85
90 int get_p() const { return p.p; }
91
96 const Eigen::VectorXi &get_allocations() const { return allocations; }
97
103 int get_cluster_size(unsigned cluster_index) const {
104 if (cluster_index >= static_cast<unsigned>(K)) {
105 return 0;
106 }
107
108 const auto it = cluster_members.find(static_cast<int>(cluster_index));
109 return (it != cluster_members.end()) ? static_cast<int>(it->second.size())
110 : 0;
111 }
112
119 int get_cluster_assignment(int index) const {
120 if (index < 0 || index >= p.n) {
121 throw std::out_of_range("Index out of bounds in get_cluster_assignment");
122 }
123 return allocations(index);
124 }
125
132 Eigen::VectorXi get_cluster_assignments(int cluster) const;
133
140 Eigen::Map<const Eigen::VectorXi>
141 get_cluster_assignments_ref(int cluster) const;
142
147 std::unordered_map<int, std::vector<int>> get_cluster_map_copy() const {
148 return cluster_members;
149 }
150
152
157
164 virtual void set_allocation(int index, int cluster);
165
171 virtual void set_allocations(const Eigen::VectorXi &new_allocations);
172
183 virtual void
184 restore_state(Eigen::VectorXi &old_allocations,
185 std::unordered_map<int, std::vector<int>> &old_cluster_members,
186 int old_K);
187
192 const std::unordered_map<int, std::vector<int>> &get_cluster_map() const {
193 return cluster_members;
194 }
195
197
198 virtual ~Data() = default;
199};
virtual void set_allocation(int index, int cluster)
Setters.
Definition Data.cpp:200
int get_cluster_assignment(int index) const
Gets the cluster assignment of a specific point.
Definition Data.hpp:119
const std::unordered_map< int, std::vector< int > > & get_cluster_map() const
Gets the full mapping of clusters to their member points.
Definition Data.hpp:192
void compact_cluster(int old_cluster)
Removes an empty cluster and compacts cluster indices.
Definition Data.cpp:90
double get_data(int i, int j=0) const
Getters.
Definition Data.cpp:34
int get_n() const
Gets the total number of points.
Definition Data.hpp:78
int get_cluster_size(unsigned cluster_index) const
Gets the size of a specific cluster.
Definition Data.hpp:103
std::unordered_map< int, std::vector< int > > get_cluster_map_copy() const
Gets the full mapping of clusters to their member points.
Definition Data.hpp:147
virtual ~Data()=default
int get_p() const
Gets the number of columns in the data.
Definition Data.hpp:90
void set_allocation_wo_compaction(int index, int cluster)
Assigns a point to a cluster without compaction.
Definition Data.cpp:129
std::unordered_map< int, std::vector< int > > cluster_members
Maps cluster indices to vectors of point indices in that cluster.
Definition Data.hpp:34
const Eigen::VectorXi & get_allocations() const
Gets the cluster allocations vector.
Definition Data.hpp:96
virtual void restore_state(Eigen::VectorXi &old_allocations, std::unordered_map< int, std::vector< int > > &old_cluster_members, int old_K)
Restores allocations, cluster memberships, and cluster count from a saved state.
Definition Data.cpp:231
Eigen::VectorXi allocations
Cluster allocation for each point.
Definition Data.hpp:30
Data(const utils_params &p, const Eigen::VectorXi &initial_allocations=Eigen::VectorXi())
Constructs a Data object with a distance matrix.
Definition Data.cpp:10
Eigen::Map< const Eigen::VectorXi > get_cluster_assignments_ref(int cluster) const
Gets all point indices assigned to a specific cluster (map form).
Definition Data.cpp:67
virtual void set_allocations(const Eigen::VectorXi &new_allocations)
Sets all cluster allocations at once.
Definition Data.cpp:212
int get_K() const
Gets the current number of clusters.
Definition Data.hpp:84
int K
Current number of clusters.
Definition Data.hpp:31
Eigen::VectorXi get_cluster_assignments(int cluster) const
Gets all point indices assigned to a specific cluster.
Definition Data.cpp:44
const utils_params & p
Reference to model parameters.
Definition Data.hpp:28
Parameter struct for utility parameters and data structures.
Definition utils-params.hpp:17
Parameter management for useful parameters and data structures.