Docs
Loading...
Searching...
No Matches
Data.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include <Eigen/Dense>
9#include <Rcpp.h>
10#include <RcppEigen.h>
11#include <unordered_map>
12#include "Params.hpp"
13
14// compilation time verbosity level
15#ifndef VERBOSITY_LEVEL
16#define VERBOSITY_LEVEL 0
17#endif
18
27class Data {
28protected:
29 const Params &params;
30
31 Eigen::VectorXi allocations;
32 int K;
33
35 std::unordered_map<int, std::vector<int>> cluster_members;
36
41 void compact_cluster(int old_cluster);
42
49 void set_allocation_wo_compaction(int index, int cluster);
50
51public:
59 Data(const Params &p, const Eigen::VectorXi &initial_allocations = Eigen::VectorXi());
60
65
72 double get_distance(int i, int j) const;
73
78 int get_n() const { return params.n; }
79
84 int get_K() const { return K; }
85
90 const Eigen::VectorXi &get_allocations() const { return allocations; }
91
97 int get_cluster_size(unsigned cluster_index) const {
98 return (cluster_index < K) ? cluster_members.at(cluster_index).size() : 0;
99 }
100
107 int get_cluster_assignment(int index) const {
108 if (index < 0 || index >= params.n) {
109 throw std::out_of_range("Index out of bounds in get_cluster_assignment");
110 }
111 return allocations(index);
112 }
113
120 Eigen::VectorXi get_cluster_assignments(int cluster) const;
121
128 Eigen::Map<const Eigen::VectorXi> get_cluster_assignments_ref(int cluster) const;
129
134 std::unordered_map<int, std::vector<int>> get_cluster_map_copy() const { return cluster_members; }
135
137
142
149 virtual void set_allocation(int index, int cluster);
150
156 virtual void set_allocations(const Eigen::VectorXi &new_allocations);
157
165 virtual void restore_state(Eigen::VectorXi &old_allocations,
166 std::unordered_map<int, std::vector<int>> &old_cluster_members, int old_K);
167
172 const std::unordered_map<int, std::vector<int>> &get_cluster_map() const { return cluster_members; }
173
175
176 virtual ~Data() = default;
177};
Parameter management for Bayesian nonparametric MCMC models.
virtual void set_allocation(int index, int cluster)
Setters.
Definition Data.cpp:182
int get_cluster_assignment(int index) const
Gets the cluster assignment of a specific point.
Definition Data.hpp:107
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:172
void compact_cluster(int old_cluster)
Removes an empty cluster and compacts cluster indices.
Definition Data.cpp:76
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:97
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:134
virtual ~Data()=default
const Params & params
Reference to model parameters.
Definition Data.hpp:29
void set_allocation_wo_compaction(int index, int cluster)
Assigns a point to a cluster without compaction.
Definition Data.cpp:113
std::unordered_map< int, std::vector< int > > cluster_members
Maps cluster indices to vectors of point indices in that cluster.
Definition Data.hpp:35
Data(const Params &p, const Eigen::VectorXi &initial_allocations=Eigen::VectorXi())
Constructs a Data object with a distance matrix.
Definition Data.cpp:10
const Eigen::VectorXi & get_allocations() const
Gets the cluster allocations vector.
Definition Data.hpp:90
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:213
Eigen::VectorXi allocations
Cluster allocation for each point.
Definition Data.hpp:31
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:59
virtual void set_allocations(const Eigen::VectorXi &new_allocations)
Sets all cluster allocations at once.
Definition Data.cpp:195
double get_distance(int i, int j) const
Getters.
Definition Data.cpp:33
int get_K() const
Gets the current number of clusters.
Definition Data.hpp:84
int K
Current number of clusters.
Definition Data.hpp:32
Eigen::VectorXi get_cluster_assignments(int cluster) const
Gets all point indices assigned to a specific cluster.
Definition Data.cpp:43
Structure containing all parameters needed for the NGGP (Normalized Generalized Gamma Process) and DP...
Definition Params.hpp:35