-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.hpp
More file actions
112 lines (104 loc) · 3.36 KB
/
Copy pathpoint.hpp
File metadata and controls
112 lines (104 loc) · 3.36 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
/*
* Copyright 2024 IntelliStream team (https://github.com/intellistream)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PDSC_POINT_HPP
#define PDSC_POINT_HPP
#include "common.hpp"
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
struct Point {
std::vector<f64> features;
u64 timestamp, true_clu_id;
Point(int dim = 0) : features(dim, 0.0), timestamp(0) {}
Point(std::vector<double> coords, double timestamp = 0.0)
: features(coords), timestamp(timestamp) {}
Point &operator/=(double scalar) {
for (int i = 0; i < features.size(); i++) {
features[i] /= scalar;
}
return *this;
}
double l2_dist(const Point &other) const {
double dist = 0.0;
for (int i = 0; i < features.size(); i++) {
dist +=
(features[i] - other.features[i]) * (features[i] - other.features[i]);
}
return sqrt(dist);
}
};
struct Dataset {
std::string name;
u64 num_points = 0;
u32 dim = 0, num_true_clusters = 0;
std::vector<Point> points;
void gen(u64 num_points, u32 dim) {
this->num_points = num_points;
this->dim = dim;
this->num_true_clusters = num_points / 100 + 1;
name = "random";
points.resize(num_points, Point(dim));
srand(static_cast<unsigned>(time(0)));
for (u64 i = 0; i < num_points; ++i) {
for (u32 j = 0; j < dim; ++j) {
points[i].features[j] = static_cast<double>(rand()) / RAND_MAX * 100;
}
points[i].timestamp = i; // Example timestamp, could be any sequence
points[i].true_clu_id = i % num_true_clusters + 1;
}
}
void load(const std::string &filename) {
std::ifstream file(filename);
if (file.is_open()) {
{
// read header line: "# dataset_name num_points dim num_true_clusters"
std::string header;
std::getline(file, header);
std::stringstream ss(header);
std::string token;
ss >> token >> name >> num_points >> dim >> num_true_clusters;
}
for (int i = 0; i < num_points; ++i) {
Point point(dim);
std::string line;
std::getline(file, line);
std::stringstream ss(line);
std::string token;
for (int j = 0; j < dim; ++j) {
std::getline(ss, token, ',');
point.features[j] = std::stod(token);
}
std::getline(ss, token, ',');
point.true_clu_id = std::stoull(token);
point.timestamp = i + 1; // Example timestamp, could be any sequence
points.push_back(point);
}
file.close();
}
}
void limit(u64 num_points) {
if (num_points && num_points < points.size()) {
points.resize((size_t)num_points);
this->num_points = num_points;
}
}
};
std::ostream &operator<<(std::ostream &os, const Point &point);
std::ostream &operator<<(std::ostream &os, const Dataset &dataset);
#endif // PDSC_POINT_HPP