forked from necst/DBSCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyDBSCAN.cpp
More file actions
125 lines (93 loc) · 2.78 KB
/
PyDBSCAN.cpp
File metadata and controls
125 lines (93 loc) · 2.78 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
#include <boost/python.hpp>
#include "dbscan.h"
using namespace boost::python;
using namespace clustering;
struct ublas_matrix_to_python
{
static PyObject* convert( DBSCAN::ClusterData const& C )
{
PyObject * result = PyList_New( C.size1() );
for (size_t i = 0; i < C.size1(); ++i)
{
PyObject * l = PyList_New( C.size2() );
for (size_t j = 0; j < C.size2(); ++j)
{
PyList_SetItem( l, j, PyFloat_FromDouble( C(i, j) ) );
}
PyList_SetItem( result, i, l );
}
return result;
}
};
class PyDBSCAN : public DBSCAN
{
public:
PyDBSCAN() : DBSCAN() {}
void pyfit( boost::python::list& pylist )
{
auto num_samples = boost::python::len( pylist );
if ( num_samples == 0 )
{
return;
}
boost::python::extract< boost::python::list > first_elem( pylist[ 0 ] );
auto num_features = boost::python::len( first_elem );
DBSCAN::ClusterData C( num_samples, num_features );
for ( int i = 0; i < num_samples; ++i )
{
boost::python::list sublist = boost::python::extract< boost::python::list >( pylist[ i ] );
for ( int j = 0; j < num_features; ++j )
{
C(i, j) = boost::python::extract<double>( sublist[ j ] );
}
}
fit( C );
}
void pywfit( boost::python::list& pylist, boost::python::list& pyweights )
{
auto num_samples = boost::python::len( pylist );
if ( num_samples == 0 )
{
return;
}
boost::python::extract< boost::python::list > first_elem( pylist[ 0 ] );
auto num_features = boost::python::len( first_elem );
DBSCAN::ClusterData C( num_samples, num_features );
for ( int i = 0; i < num_samples; ++i )
{
boost::python::list sublist = boost::python::extract< boost::python::list >( pylist[ i ] );
for ( int j = 0; j < num_features; ++j )
{
C(i, j) = boost::python::extract<double>( sublist[ j ] );
}
}
auto num_weights = boost::python::len( pyweights );
DBSCAN::FeaturesWeights W( num_weights );
for ( int i = 0; i < num_weights; ++i )
{
W(i) = boost::python::extract<double>( pyweights[ i ] );
}
wfit( C, W );
}
boost::python::list pyget_labels()
{
boost::python::list list;
for (const auto & l : get_labels() )
{
list.append(l);
}
return list;
}
};
BOOST_PYTHON_MODULE(pydbscan)
{
def( "gen_cluster_data", & PyDBSCAN::gen_cluster_data );
//def( "send_stat", & python_send_stat );
class_< PyDBSCAN >( "DBSCAN" )
.def( "init" , & PyDBSCAN::init )
.def( "fit" , & PyDBSCAN::pyfit )
.def( "wfit" , & PyDBSCAN::pywfit )
.def( "get_labels" , & PyDBSCAN::pyget_labels )
;
to_python_converter<DBSCAN::ClusterData, ublas_matrix_to_python, false>();
}