-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
128 lines (107 loc) · 3.83 KB
/
main.cpp
File metadata and controls
128 lines (107 loc) · 3.83 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <iostream>
#include "dbscan.h"
#include <dlib/clustering.h>
#include <dlib/matrix.h>
#include "plot.h"
#include <experimental/filesystem>
#include <utility>
#include <unordered_map>
namespace fs = std::experimental::filesystem;
const std::vector<std::string> data_names{
"dataset0.csv"
//, "dataset1.csv",
//"dataset2.csv", "dataset3.csv",
//"dataset4.csv", "dataset5.csv"
};
const std::vector<std::string> colors{"black", "red", "blue", "green",
"cyan", "yellow", "brown", "magenta", "gray", "chartreuse", "honeydew"};
using DataType = double;
using Coords = std::vector<DataType>;
using PointCoords = std::pair<Coords, Coords>;
using Clusters = std::unordered_map<int, PointCoords>;
void PlotClusters(const Clusters& clusters,
const std::string& name,
const std::string& file_name) {
plotcpp::Plot plt;
plt.SetTerminal("png");
plt.SetOutput(file_name);
plt.SetTitle(name);
plt.SetXLabel("x");
plt.SetYLabel("y");
plt.SetAutoscale();
plt.GnuplotCommand("set grid");
auto draw_state = plt.StartDraw2D<Coords::const_iterator>();
for (const auto& cluster : clusters) {
std::stringstream params;
if(cluster.first == -1) continue;
params << "lc rgb '" << colors[cluster.first] << "'pt 7";
plt.AddDrawing(draw_state,
plotcpp::Points(
cluster.second.first.cbegin(), cluster.second.first.cend(),
cluster.second.second.cbegin(),
std::to_string(static_cast<size_t>(cluster.first)) + " cls", params.str()));
}
plt.EndDraw2D(draw_state);
plt.Flush();
}
//For debug here:
/*
void printResults(vector<Point>& points, int num_points)
{
int i = 0;
printf("Number of points: %u\n"
" x y cluster_id\n"
"-----------------------------\n"
, num_points);
while (i < num_points)
{
printf("%5.2lf %5.2lf %d\n",
points[i].x,
points[i].y,
points[i].clusterID);
++i;
}
}
*/
int main(int argc, char** argv)
{
if(argc > 3){
auto base_dir = fs::path(argv[1]);
const double epsilon = atof(argv[2]);
const double minimum_points = atoi(argv[3]);
for(auto & dataset : data_names ) {
auto dataset_name = base_dir / dataset;
if (fs::exists(dataset_name)){
std::ifstream file(dataset_name);
dlib::matrix<DataType> data;
file >> data;
vector<Point> points;
for ( int i = 0; i < data.nr(); i++){
points.emplace_back(data(i, 1), data(i, 2), UNCLASSIFIED);
}
Clusters clusters;
DBSCAN ds(minimum_points, epsilon, points);
ds.run();
for (const auto& point : ds.m_points){
clusters[point.clusterID].first.push_back(point.x);
clusters[point.clusterID].second.push_back(point.y);
//For debug here:
//std::cout << point.clusterID << point.x << point.y << std::endl;
}
//std::cout << " file name is: "<< dataset_name << "\n\n\n";
//printResults(ds.m_points, ds.getTotalPointSize()); //for debug here
//std::cout << "\n\n\n";
//std::cout <<__LINE__ << std::endl << std::endl;
PlotClusters(clusters, "DBSCAN clustering", "../results/" + dataset + "-dbscans.png");
}
else{
std::cerr << "Dataset file " << dataset_name << "missed. Please provide in the correct form.\n";
}
}
}
else {
std::cerr <<" Please provide the data's folder!!!\n\n";
}
return 0;
}