umappp 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/umappp/umappp.cpp +63 -15
- data/lib/umappp/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 715a0cccadbfa68578e1ff6b4998f539313fc31ede7dc1da6c28f51e4396ea91
|
4
|
+
data.tar.gz: '07851a541e04c0da5647b9787a889de05ed8d28605818a657b82eebed1b1ba95'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cee6f62c119793903786a0b983c7861f55d27a15b553846ae88d95fd40a335b01c0666e4cdb87b55d4b91218c5e48c5bda8304502716349b02453e7fdbdf5c04
|
7
|
+
data.tar.gz: 9cc07b731ead37a256335ae4067b95c41a008bd46bd643b7c82cd36158d26e0b65dcc536c017231ad1368a9b87c95fd02f1611fb2951a01886e18b95467319f4
|
data/ext/umappp/umappp.cpp
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
/*
|
2
|
+
* Uniform Manifold Approximation and Projection for Ruby
|
3
|
+
* https://github.com/kojix2/ruby-umappp
|
4
|
+
*/
|
3
5
|
|
4
6
|
#include <rice/rice.hpp>
|
5
7
|
#include <rice/stl.hpp>
|
8
|
+
#include <exception>
|
9
|
+
#include <string>
|
6
10
|
#include "numo.hpp"
|
7
11
|
#include "Umap.hpp"
|
8
12
|
|
@@ -11,8 +15,45 @@ typedef typename umappp::Umap<Float> Umap;
|
|
11
15
|
|
12
16
|
using namespace Rice;
|
13
17
|
|
14
|
-
|
18
|
+
extern "C" {
|
19
|
+
void *rb_thread_call_without_gvl(
|
20
|
+
void *(*func)(void *), void *data,
|
21
|
+
void (*ubf)(void *), void *ubf_data
|
22
|
+
);
|
23
|
+
}
|
24
|
+
|
25
|
+
// Data structure for running UMAP calculation without GVL.
|
26
|
+
struct UmapRunData {
|
27
|
+
Umap* umap_ptr;
|
28
|
+
knncolle::Base<int, Float>* knncolle_ptr;
|
29
|
+
int ndim;
|
30
|
+
std::vector<Float>* embedding;
|
31
|
+
std::string exception_message;
|
32
|
+
bool exception_thrown = false;
|
33
|
+
};
|
34
|
+
|
35
|
+
// Callback for UMAP calculation (executed without GVL).
|
36
|
+
static void* umap_run_without_gvl(void* data) {
|
37
|
+
UmapRunData* run_data = static_cast<UmapRunData*>(data);
|
38
|
+
try {
|
39
|
+
auto status = run_data->umap_ptr->initialize(
|
40
|
+
run_data->knncolle_ptr,
|
41
|
+
run_data->ndim,
|
42
|
+
run_data->embedding->data()
|
43
|
+
);
|
44
|
+
int epoch_limit = 0;
|
45
|
+
status.run(epoch_limit);
|
46
|
+
} catch (const std::exception& e) {
|
47
|
+
run_data->exception_message = e.what();
|
48
|
+
run_data->exception_thrown = true;
|
49
|
+
} catch (...) {
|
50
|
+
run_data->exception_message = "Unknown exception occurred in UMAP calculation.";
|
51
|
+
run_data->exception_thrown = true;
|
52
|
+
}
|
53
|
+
return nullptr;
|
54
|
+
}
|
15
55
|
|
56
|
+
// Returns default parameters from the Umappp C++ library.
|
16
57
|
Hash umappp_default_parameters(Object self)
|
17
58
|
{
|
18
59
|
Hash d;
|
@@ -36,8 +77,7 @@ Hash umappp_default_parameters(Object self)
|
|
36
77
|
return d;
|
37
78
|
}
|
38
79
|
|
39
|
-
//
|
40
|
-
|
80
|
+
// Main function to perform UMAP.
|
41
81
|
Object umappp_run(
|
42
82
|
Object self,
|
43
83
|
Hash params,
|
@@ -45,8 +85,6 @@ Object umappp_run(
|
|
45
85
|
int ndim,
|
46
86
|
int nn_method)
|
47
87
|
{
|
48
|
-
// Parameters are taken from a Ruby Hash object.
|
49
|
-
// If there is key, set the value.
|
50
88
|
if (ndim < 1)
|
51
89
|
{
|
52
90
|
throw std::runtime_error("ndim is less than 1");
|
@@ -166,8 +204,6 @@ Object umappp_run(
|
|
166
204
|
umap_ptr->set_parallel_optimization(parallel_optimization);
|
167
205
|
}
|
168
206
|
|
169
|
-
// initialize_from_matrix
|
170
|
-
|
171
207
|
const float *y = data.read_ptr();
|
172
208
|
size_t *shape = data.shape();
|
173
209
|
|
@@ -190,13 +226,25 @@ Object umappp_run(
|
|
190
226
|
|
191
227
|
std::vector<Float> embedding(ndim * nobs);
|
192
228
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
229
|
+
// Run UMAP calculation without GVL.
|
230
|
+
UmapRunData run_data = {
|
231
|
+
umap_ptr.get(),
|
232
|
+
knncolle_ptr.get(),
|
233
|
+
ndim,
|
234
|
+
&embedding
|
235
|
+
};
|
236
|
+
|
237
|
+
rb_thread_call_without_gvl(
|
238
|
+
umap_run_without_gvl,
|
239
|
+
&run_data,
|
240
|
+
NULL,
|
241
|
+
NULL
|
242
|
+
);
|
243
|
+
|
244
|
+
if (run_data.exception_thrown) {
|
245
|
+
throw std::runtime_error(run_data.exception_message);
|
246
|
+
}
|
198
247
|
|
199
|
-
// it is safe to cast to unsigned int
|
200
248
|
auto na = numo::SFloat({(unsigned int)nobs, (unsigned int)ndim});
|
201
249
|
std::copy(embedding.begin(), embedding.end(), na.write_ptr());
|
202
250
|
|
data/lib/umappp/version.rb
CHANGED