umappp 0.1.2 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6213e0c925f92536e1a2698650ac79440f65c3d240eabc1943bd3e9b0f2a116e
4
- data.tar.gz: 3595cbb5a91b88787479355ba6abd5037aee31a83f57c958d9800bc4602fbb24
3
+ metadata.gz: fde6f3e2f4fc7014e87ddd4cdcb5fefe60d14e34364682a667436373293ec86e
4
+ data.tar.gz: aecbfebfbaa5181eeb6572cb27e8cd5621d52bb2b7912ffbdc786eada4e8d8c0
5
5
  SHA512:
6
- metadata.gz: 6cfa4967ccebe20f71dd61601e75cff681afb0de4688af74ce75353514d50834466a04a921c62bb2de00feb77493106b36278557bd60e544cee7749683da23da
7
- data.tar.gz: 84d6a55992cd0f0ec9847da7a106cd6e60edbdcf438e44941cb32d9e4bea831c5b836cec40cd0138379e4f83040cae3b86958ea50fc3eb28ecc6425bad8b87bd
6
+ metadata.gz: 21a24bf715c70961237d428679e7e2bd2b090843275ec989251645ef7b2cb579f6a130ae5a46cfedbcf34d25b72f6db9a41032dc0d7df39c62e93d39c5e36e2a
7
+ data.tar.gz: 5006adc1daa306597e2bfe961a6cbdc9bcf6a291d24bad200766551c8cf2f15b4096e544758da1040c0d0e46c7b6840cca58c1df3facc372000588e3c9a6da4c
@@ -18,7 +18,7 @@ using namespace Rice;
18
18
 
19
19
  // This function is used to view default parameters from Ruby.
20
20
 
21
- Hash umap_default_parameters(Object self)
21
+ Hash umappp_default_parameters(Object self)
22
22
  {
23
23
  Hash d;
24
24
  d[Symbol("local_connectivity")] = Umap::Defaults::local_connectivity;
@@ -42,7 +42,7 @@ Hash umap_default_parameters(Object self)
42
42
 
43
43
  // Function to perform umap.
44
44
 
45
- Object umap_run(
45
+ Object umappp_run(
46
46
  Object self,
47
47
  Hash params,
48
48
  numo::SFloat data,
@@ -51,120 +51,115 @@ Object umap_run(
51
51
  int tick = 0)
52
52
  {
53
53
  // Parameters are taken from a Ruby Hash object.
54
- // If there is no key, set the default value.
54
+ // If there is key, set the value.
55
55
 
56
+ auto umap_ptr = new Umap;
57
+
56
58
  double local_connectivity = Umap::Defaults::local_connectivity;
57
59
  if (RTEST(params.call("has_key?", Symbol("local_connectivity"))))
58
60
  {
59
61
  local_connectivity = params.get<double>(Symbol("local_connectivity"));
62
+ umap_ptr->set_local_connectivity(local_connectivity);
60
63
  }
61
64
 
62
65
  double bandwidth = Umap::Defaults::bandwidth;
63
66
  if (RTEST(params.call("has_key?", Symbol("bandwidth"))))
64
67
  {
65
68
  bandwidth = params.get<double>(Symbol("bandwidth"));
69
+ umap_ptr->set_bandwidth(bandwidth);
66
70
  }
67
71
 
68
72
  double mix_ratio = Umap::Defaults::mix_ratio;
69
73
  if (RTEST(params.call("has_key?", Symbol("mix_ratio"))))
70
74
  {
71
75
  mix_ratio = params.get<double>(Symbol("mix_ratio"));
76
+ umap_ptr->set_mix_ratio(mix_ratio);
72
77
  }
73
78
 
74
79
  double spread = Umap::Defaults::spread;
75
80
  if (RTEST(params.call("has_key?", Symbol("spread"))))
76
81
  {
77
82
  spread = params.get<double>(Symbol("spread"));
83
+ umap_ptr->set_spread(spread);
78
84
  }
79
85
 
80
86
  double min_dist = Umap::Defaults::min_dist;
81
87
  if (RTEST(params.call("has_key?", Symbol("min_dist"))))
82
88
  {
83
89
  min_dist = params.get<double>(Symbol("min_dist"));
90
+ umap_ptr->set_min_dist(min_dist);
84
91
  }
85
92
 
86
93
  double a = Umap::Defaults::a;
87
94
  if (RTEST(params.call("has_key?", Symbol("a"))))
88
95
  {
89
96
  a = params.get<double>(Symbol("a"));
97
+ umap_ptr->set_a(a);
90
98
  }
91
99
 
92
100
  double b = Umap::Defaults::b;
93
101
  if (RTEST(params.call("has_key?", Symbol("b"))))
94
102
  {
95
103
  b = params.get<double>(Symbol("b"));
104
+ umap_ptr->set_b(b);
96
105
  }
97
106
 
98
107
  double repulsion_strength = Umap::Defaults::repulsion_strength;
99
108
  if (RTEST(params.call("has_key?", Symbol("repulsion_strength"))))
100
109
  {
101
110
  repulsion_strength = params.get<double>(Symbol("repulsion_strength"));
111
+ umap_ptr->set_repulsion_strength(repulsion_strength);
102
112
  }
103
113
 
104
114
  int num_epochs = Umap::Defaults::num_epochs;
105
115
  if (RTEST(params.call("has_key?", Symbol("num_epochs"))))
106
116
  {
107
117
  num_epochs = params.get<int>(Symbol("num_epochs"));
118
+ umap_ptr->set_num_epochs(num_epochs);
108
119
  }
109
120
 
110
121
  double learning_rate = Umap::Defaults::learning_rate;
111
122
  if (RTEST(params.call("has_key?", Symbol("learning_rate"))))
112
123
  {
113
124
  learning_rate = params.get<double>(Symbol("learning_rate"));
125
+ umap_ptr->set_learning_rate(learning_rate);
114
126
  }
115
127
 
116
128
  double negative_sample_rate = Umap::Defaults::negative_sample_rate;
117
129
  if (RTEST(params.call("has_key?", Symbol("negative_sample_rate"))))
118
130
  {
119
131
  negative_sample_rate = params.get<double>(Symbol("negative_sample_rate"));
132
+ umap_ptr->set_negative_sample_rate(negative_sample_rate);
120
133
  }
121
134
 
122
135
  int num_neighbors = Umap::Defaults::num_neighbors;
123
136
  if (RTEST(params.call("has_key?", Symbol("num_neighbors"))))
124
137
  {
125
138
  num_neighbors = params.get<int>(Symbol("num_neighbors"));
139
+ umap_ptr->set_num_neighbors(num_neighbors);
126
140
  }
127
141
 
128
142
  int seed = Umap::Defaults::seed;
129
143
  if (RTEST(params.call("has_key?", Symbol("seed"))))
130
144
  {
131
145
  seed = params.get<int>(Symbol("seed"));
146
+ umap_ptr->set_seed(seed);
132
147
  }
133
148
 
134
149
  bool batch = Umap::Defaults::batch;
135
150
  if (RTEST(params.call("has_key?", Symbol("batch"))))
136
151
  {
137
152
  batch = params.get<bool>(Symbol("batch"));
153
+ umap_ptr->set_batch(batch);
138
154
  }
139
155
 
140
156
  int num_threads = Umap::Defaults::num_threads;
141
157
  if (RTEST(params.call("has_key?", Symbol("num_threads"))))
142
158
  {
143
159
  num_threads = params.get<int>(Symbol("num_threads"));
160
+ umap_ptr->set_num_threads(num_threads);
144
161
  }
145
-
146
- // setup_parameters
147
-
148
- auto umap_ptr = new Umap;
149
- umap_ptr->set_local_connectivity(local_connectivity);
150
- umap_ptr->set_bandwidth(bandwidth);
151
- umap_ptr->set_mix_ratio(mix_ratio);
152
- umap_ptr->set_spread(spread);
153
- umap_ptr->set_min_dist(min_dist);
154
- umap_ptr->set_a(a);
155
- umap_ptr->set_b(b);
156
-
157
- umap_ptr->set_repulsion_strength(repulsion_strength);
158
- umap_ptr->set_num_epochs(num_epochs);
159
- umap_ptr->set_learning_rate(learning_rate);
160
- umap_ptr->set_negative_sample_rate(negative_sample_rate);
161
-
162
- umap_ptr->set_num_neighbors(num_neighbors);
163
- umap_ptr->set_seed(seed);
164
- umap_ptr->set_batch(batch);
165
-
166
- umap_ptr->set_num_threads(num_threads);
167
-
162
+
168
163
  // initialize_from_matrix
169
164
 
170
165
  const float *y = data.read_ptr();
@@ -220,6 +215,6 @@ extern "C" void Init_umappp()
220
215
  {
221
216
  Module rb_mUmappp =
222
217
  define_module("Umappp")
223
- .define_singleton_method("umap_run", &umap_run)
224
- .define_singleton_method("default_parameters", &umap_default_parameters);
218
+ .define_singleton_method("umappp_run", &umappp_run)
219
+ .define_singleton_method("umappp_default_parameters", &umappp_default_parameters);
225
220
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Umappp
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
5
5
  end
data/lib/umappp.rb CHANGED
@@ -6,9 +6,19 @@ require_relative "umappp/umappp"
6
6
 
7
7
  # Uniform Manifold Approximation and Projection
8
8
  module Umappp
9
+ # Make wrapper methods for the C++ function generated by Rice private
10
+ private_class_method :umappp_run
11
+ private_class_method :umappp_default_parameters
9
12
 
10
- # Run UMAP
11
- # @param embedding [Array, Numo::SFloat]
13
+ # View the default parameters defined within the Umappp C++ library structure.
14
+ def self.default_parameters
15
+ # {method: :annoy, ndim: 2, tick: 0}.merge
16
+ umappp_default_parameters
17
+ end
18
+
19
+ # Runs the Uniform Manifold Approximation and Projection (UMAP) dimensional
20
+ # reduction technique.
21
+ # @param embedding [Array, Numo::SFloat]
12
22
  # @param method [Symbol]
13
23
  # @param ndim [Integer]
14
24
  # @param tick [Integer]
@@ -28,14 +38,17 @@ module Umappp
28
38
  # @param batch [Boolean]
29
39
  # @param num_threads [Integer]
30
40
 
31
- def self.run(ary, method: :annoy, ndim: 2, tick: 0, **params)
41
+ def self.run(embedding, method: :annoy, ndim: 2, tick: 0, **params)
32
42
  unless (u = (params.keys - default_parameters.keys)).empty?
33
43
  raise ArgumentError, "[umappp.rb] unknown option : #{u.inspect}"
34
44
  end
35
45
 
36
46
  nnmethod = %i[annoy vptree].index(method.to_sym)
37
- data = Numo::SFloat.cast(ary)
47
+ raise ArgumentError, "method must be :annoy or :vptree" if nnmethod.nil?
48
+
49
+ embedding2 = Numo::SFloat.cast(embedding)
50
+ raise ArgumentError, "embedding must be a 2D array" if embedding2.ndim <= 1
38
51
 
39
- Umappp.umap_run(params, data, ndim, nnmethod, tick)
52
+ umappp_run(params, embedding2, ndim, nnmethod, tick)
40
53
  end
41
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: umappp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - kojix2
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-10 00:00:00.000000000 Z
11
+ date: 2022-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray