-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSample.cpp
140 lines (125 loc) · 4.17 KB
/
Sample.cpp
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
#include "Sample.h"
#include <ctime>
#include <iostream>
#include "FeatureFactory.h"
#include "utils.h"
Sample::Sample(Eigen::MatrixXf *dataset, Eigen::VectorXi *labels,
Eigen::MatrixXi *indexMat, Eigen::MatrixXf *distMat,
int numClass, int numFeature,
Eigen::MatrixXf *cloud):
_dataset(dataset),
_labels(labels),
_indexMat(indexMat),
_distMat(distMat),
_numClass(numClass),
_numFeature(numFeature),
_cloud(cloud)
{
}
//Sample::Sample(Eigen::MatrixXf *dataset, Eigen::VectorXi *labels,
// Eigen::MatrixXi *indexMat, Eigen::MatrixXf *distMat,
// int numClass, int numFeature)
//{
// Sample(dataset, labels, indexMat, distMat, numClass, numFeature, dataset, nullptr);
//}
Sample::Sample(Sample* sample):
_dataset(sample->_dataset),
_labels(sample->_labels),
_indexMat(sample->_indexMat),
_distMat(sample->_distMat),
_numClass(sample->_numClass),
_numFeature(sample->_numFeature),
_numSelectedSamples(sample->getNumSelectedSamples()),
_selectedSamplesId(sample->getSelectedSamplesId()),
_cloud(sample->_cloud)
{
}
Sample::Sample(const Sample* sample, Eigen::VectorXi &samplesId) :
_dataset(sample->_dataset),
_labels(sample->_labels),
_indexMat(sample->_indexMat),
_distMat(sample->_distMat),
_numClass(sample->_numClass),
_numFeature(sample->_numFeature),
_selectedSamplesId(samplesId),
_numSelectedSamples(samplesId.rows()),
_cloud(sample->_cloud)
{
}
void Sample::randomSampleDataset(Eigen::VectorXi &selectedSamplesId, int numSelectedSamples)
{
int numTotalSamples = _dataset->rows();
_numSelectedSamples = numSelectedSamples;
// to generate a uniform distribution and sample with replacement
std::default_random_engine generator;
generator.seed(time(NULL));
// DEBUG to uncomment
// generator.seed(123456);
std::uniform_int_distribution<int> distribution(0, numTotalSamples - 1);
for (int i = 0; i < _numSelectedSamples; ++i)
{
selectedSamplesId[i] = distribution(generator);
//selectedSamplesId[i] = rand() % numTotalSamples;
}
_selectedSamplesId = selectedSamplesId;
}
void Sample::randomSampleFeatures()
{
// total population
int numPoints = _indexMat->cols();
for (int i = 0; i < _numFeature; ++i)
{
Features feature;
// number of voxels in this feature
feature._numVoxels = randomFrom124();
// each neighborhood is divided into 27 subvoxels
Random rd(27, feature._numVoxels);
feature._pointId = rd.sampleWithoutReplacement();
// for each voxel, randomly choose the number of points in each voxel which is
// in the range (0.1*numOfPointsInNeighborhood, 0.5*numOfPointsInNeighborhood)
/*for (int i = 0; i < feature._numVoxels; ++i)
{
Random rd(feature.maxSamplesPerVoxel - feature.minSamplesPerVoxel, 1);
int tmp = rd.sampleWithoutReplacement().at(0) + feature.minSamplesPerVoxel;
feature._voxelSize.push_back(tmp);
}*/
// randomly select a projection type from all possible projections
Random rd2(FeatureFactory::numOfPossibleProjections, 1);
feature._featType = rd2.sampleWithoutReplacement().at(0);
feature._thresh = 0;
_features.push_back(feature);
}
}
int Sample::randomFrom124()
{
int arr[] = { 1, 2, 4 };
Random rd(3, 1);
int inx = rd.sampleWithoutReplacement().at(0);
return arr[inx];
}
Eigen::MatrixXf Sample::buildNeighborhood(int pointId)
{
// number of points in the neighborhood
int k = _indexMat->cols();
// datapoint dimension
int d = _dataset->cols();
// the last dimension represents the dist to the central point
int d1 = d + 1;
/*std::cout << "pointID\n";
std::cout << pointId << std::endl;*/
Eigen::MatrixXf neighborhood(k, d);
Eigen::VectorXi candidatePointIndices;
candidatePointIndices = _indexMat->row(pointId);
// std::cout << "candidates\n";
// std::cout << candidatePointIndices << std::endl;
//neighborhood.row(0) = _dataset.row(pointId);
for (int i = 0; i < k; ++i)
//neighborhood.row(i) = _dataset->row(candidatePointIndices[i]);
neighborhood.row(i) = _cloud->row(candidatePointIndices[i]);
Eigen::VectorXf dists = _distMat->row(pointId);
Eigen::MatrixXf newdists = Eigen::Map <Eigen::Matrix<float, -1, 1>> (dists.data(), dists.size());
Eigen::MatrixXf neighWithDist(k, d1);
neighWithDist << neighborhood, newdists;
//std::cout << neighWithDist;
return neighWithDist;
}