rbcluster 0.0.1

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.
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Cluster.somcluster" do
4
+
5
+ it "calculates somcluster for a first data set" do
6
+ weight = [ 1,1,1,1,1 ]
7
+ data = [
8
+ [ 1.1, 2.2, 3.3, 4.4, 5.5],
9
+ [ 3.1, 3.2, 1.3, 2.4, 1.5],
10
+ [ 4.1, 2.2, 0.3, 5.4, 0.5],
11
+ [ 12.1, 2.0, 0.0, 5.0, 0.0]
12
+
13
+ ]
14
+ mask = [
15
+ [ 1, 1, 1, 1, 1],
16
+ [ 1, 1, 1, 1, 1],
17
+ [ 1, 1, 1, 1, 1],
18
+ [ 1, 1, 1, 1, 1]
19
+ ]
20
+
21
+ clusterid, celldata = Cluster.somcluster data, :mask => mask,
22
+ :weight => weight,
23
+ :transpose => false,
24
+ :nxgrid => 10,
25
+ :nygrid => 10,
26
+ :inittau => 0.02,
27
+ :niter => 100,
28
+ :dist => 'e'
29
+
30
+ clusterid.size.should == data.size
31
+ clusterid[0].size.should == 2
32
+ celldata[0][0].size.should == 5
33
+ end
34
+
35
+ it "calculates somcluster for a second data set" do
36
+ weight = [ 1,1 ]
37
+ data = [
38
+ [ 1.1, 1.2 ],
39
+ [ 1.4, 1.3 ],
40
+ [ 1.1, 1.5 ],
41
+ [ 2.0, 1.5 ],
42
+ [ 1.7, 1.9 ],
43
+ [ 1.7, 1.9 ],
44
+ [ 5.7, 5.9 ],
45
+ [ 5.7, 5.9 ],
46
+ [ 3.1, 3.3 ],
47
+ [ 5.4, 5.3 ],
48
+ [ 5.1, 5.5 ],
49
+ [ 5.0, 5.5 ],
50
+ [ 5.1, 5.2 ]
51
+ ]
52
+
53
+ mask = [
54
+ [ 1, 1 ],
55
+ [ 1, 1 ],
56
+ [ 1, 1 ],
57
+ [ 1, 1 ],
58
+ [ 1, 1 ],
59
+ [ 1, 1 ],
60
+ [ 1, 1 ],
61
+ [ 1, 1 ],
62
+ [ 1, 1 ],
63
+ [ 1, 1 ],
64
+ [ 1, 1 ],
65
+ [ 1, 1 ],
66
+ [ 1, 1 ]
67
+ ]
68
+
69
+ clusterid, celldata = Cluster.somcluster data, :mask => mask,
70
+ :weight => weight,
71
+ :transpose => false,
72
+ :nxgrid => 10,
73
+ :nygrid => 10,
74
+ :inittau => 0.02,
75
+ :niter => 100,
76
+ :dist => 'e'
77
+ clusterid.size.should == data.size
78
+ clusterid[0].size.should == 2
79
+ celldata[0][0].size.should == 2
80
+ end
81
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
+ require 'rbcluster'
3
+
@@ -0,0 +1,412 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Cluster.treecluster" do
4
+ context "first data set" do
5
+ let(:weight) { [ 1,1,1,1,1 ] }
6
+ let(:data) {
7
+ [
8
+ [ 1.1, 2.2, 3.3, 4.4, 5.5],
9
+ [ 3.1, 3.2, 1.3, 2.4, 1.5],
10
+ [ 4.1, 2.2, 0.3, 5.4, 0.5],
11
+ [ 12.1, 2.0, 0.0, 5.0, 0.0]
12
+ ]
13
+ }
14
+
15
+ let(:mask) {
16
+ [
17
+ [ 1, 1, 1, 1, 1],
18
+ [ 1, 1, 1, 1, 1],
19
+ [ 1, 1, 1, 1, 1],
20
+ [ 1, 1, 1, 1, 1]
21
+ ]
22
+ }
23
+
24
+ it "calculates pairwise average-linkage clustering" do
25
+ tree = Cluster.treecluster data, :mask => mask,
26
+ :weight => weight,
27
+ :transpose => false,
28
+ :method => 'a',
29
+ :dist => 'e'
30
+
31
+ tree.size.should == data.size - 1
32
+
33
+ tree[0].left.should == 2
34
+ tree[0].right.should == 1
35
+ tree[0].distance.should be_within(0.001).of(2.600)
36
+
37
+ tree[1].left.should == -1
38
+ tree[1].right.should == 0
39
+ tree[1].distance.should be_within(0.001).of(7.300)
40
+
41
+ tree[2].left.should == 3
42
+ tree[2].right.should == -2
43
+ tree[2].distance.should be_within(0.001).of(21.348)
44
+ end
45
+
46
+ it "calcultes pairwise single-linkage clustering" do
47
+ tree = Cluster.treecluster data, :mask => mask,
48
+ :weight => weight,
49
+ :transpose => false,
50
+ :method => 's',
51
+ :dist => 'e'
52
+
53
+ tree.size.should == data.size - 1
54
+
55
+ tree[0].left.should == 1
56
+ tree[0].right.should == 2
57
+ tree[0].distance.should be_within(0.001).of(2.600)
58
+
59
+ tree[1].left.should == 0
60
+ tree[1].right.should == -1
61
+ tree[1].distance.should be_within(0.001).of(5.800)
62
+
63
+ tree[2].left.should == -2
64
+ tree[2].right.should == 3
65
+ tree[2].distance.should be_within(0.001).of(12.908)
66
+ end
67
+
68
+ it "calculates pairwise centroid-linkage clustering" do
69
+ tree = Cluster.treecluster data, :mask => mask,
70
+ :weight => weight,
71
+ :transpose => false,
72
+ :method => 'c',
73
+ :dist => 'e'
74
+
75
+ tree.size.should == data.size - 1
76
+
77
+ tree[0].left.should == 1
78
+ tree[0].right.should == 2
79
+ tree[0].distance.should be_within(0.001).of(2.600)
80
+ tree[1].left.should == 0
81
+ tree[1].right.should == -1
82
+ tree[1].distance.should be_within(0.001).of(6.650)
83
+ tree[2].left.should == -2
84
+ tree[2].right.should == 3
85
+ tree[2].distance.should be_within(0.001).of(19.437)
86
+ end
87
+
88
+ it "calculates pairwise maximum-linkage clustering" do
89
+ tree = Cluster.treecluster data, :mask => mask,
90
+ :weight => weight,
91
+ :transpose => false,
92
+ :method => 'm',
93
+ :dist => 'e'
94
+
95
+ tree.size.should == data.size - 1
96
+
97
+ tree[0].left.should == 2
98
+ tree[0].right.should == 1
99
+ tree[0].distance.should be_within(0.001).of(2.600)
100
+ tree[1].left.should == -1
101
+ tree[1].right.should == 0
102
+ tree[1].distance.should be_within(0.001).of(8.800)
103
+ tree[2].left.should == 3
104
+ tree[2].right.should == -2
105
+ tree[2].distance.should be_within(0.001).of(32.508)
106
+ end
107
+ end
108
+
109
+ context "second data set" do
110
+ let(:weight) { [ 1,1 ] }
111
+ let(:data) {
112
+ [
113
+ [ 0.8223, 0.9295 ],
114
+ [ 1.4365, 1.3223 ],
115
+ [ 1.1623, 1.5364 ],
116
+ [ 2.1826, 1.1934 ],
117
+ [ 1.7763, 1.9352 ],
118
+ [ 1.7215, 1.9912 ],
119
+ [ 2.1812, 5.9935 ],
120
+ [ 5.3290, 5.9452 ],
121
+ [ 3.1491, 3.3454 ],
122
+ [ 5.1923, 5.3156 ],
123
+ [ 4.7735, 5.4012 ],
124
+ [ 5.1297, 5.5645 ],
125
+ [ 5.3934, 5.1823 ]
126
+ ]
127
+ }
128
+
129
+ let(:mask) {
130
+ [
131
+ [ 1, 1 ],
132
+ [ 1, 1 ],
133
+ [ 1, 1 ],
134
+ [ 1, 1 ],
135
+ [ 1, 1 ],
136
+ [ 1, 1 ],
137
+ [ 1, 1 ],
138
+ [ 1, 1 ],
139
+ [ 1, 1 ],
140
+ [ 1, 1 ],
141
+ [ 1, 1 ],
142
+ [ 1, 1 ],
143
+ [ 1, 1 ]
144
+ ]
145
+ }
146
+
147
+ it "calculates pairwise average-linkage clustering" do
148
+ tree = Cluster.treecluster data, :mask => mask,
149
+ :weight => weight,
150
+ :transpose => false,
151
+ :method => 'a',
152
+ :dist => 'e'
153
+
154
+ tree.size.should == data.size - 1
155
+ tree[0].left.should == 5
156
+ tree[0].right.should == 4
157
+ tree[0].distance.should be_within(0.001).of(0.003)
158
+
159
+ tree[1].left.should == 9
160
+ tree[1].right.should == 12
161
+ tree[1].distance.should be_within(0.001).of(0.029)
162
+
163
+ tree[2].left.should == 2
164
+ tree[2].right.should == 1
165
+ tree[2].distance.should be_within(0.001).of(0.061)
166
+
167
+ tree[3].left.should == 11
168
+ tree[3].right.should == -2
169
+ tree[3].distance.should be_within(0.001).of(0.070)
170
+
171
+ tree[4].left.should == -4
172
+ tree[4].right.should == 10
173
+ tree[4].distance.should be_within(0.001).of(0.128)
174
+
175
+ tree[5].left.should == 7
176
+ tree[5].right.should == -5
177
+ tree[5].distance.should be_within(0.001).of(0.224)
178
+
179
+ tree[6].left.should == -3
180
+ tree[6].right.should == 0
181
+ tree[6].distance.should be_within(0.001).of(0.254)
182
+
183
+ tree[7].left.should == -1
184
+ tree[7].right.should == 3
185
+ tree[7].distance.should be_within(0.001).of(0.391)
186
+
187
+ tree[8].left.should == -8
188
+ tree[8].right.should == -7
189
+ tree[8].distance.should be_within(0.001).of(0.532)
190
+
191
+ tree[9].left.should == 8
192
+ tree[9].right.should == -9
193
+ tree[9].distance.should be_within(0.001).of(3.234)
194
+
195
+ tree[10].left.should == -6
196
+ tree[10].right.should == 6
197
+ tree[10].distance.should be_within(0.001).of(4.636)
198
+
199
+ tree[11].left.should == -11
200
+ tree[11].right.should == -10
201
+ tree[11].distance.should be_within(0.001).of(12.741)
202
+ end
203
+
204
+ it "calculates pairwise single-linkage clustering" do
205
+ tree = Cluster.treecluster data, :mask => mask,
206
+ :weight => weight,
207
+ :transpose => false,
208
+ :method => 's',
209
+ :dist => 'e'
210
+
211
+ tree.size.should == data.size - 1
212
+
213
+ tree[0].left.should == 4
214
+ tree[0].right.should == 5
215
+ tree[0].distance.should be_within(0.001).of(0.003)
216
+
217
+ tree[1].left.should == 9
218
+ tree[1].right.should == 12
219
+ tree[1].distance.should be_within(0.001).of(0.029)
220
+
221
+ tree[2].left.should == 11
222
+ tree[2].right.should == -2
223
+ tree[2].distance.should be_within(0.001).of(0.033)
224
+
225
+ tree[3].left.should == 1
226
+ tree[3].right.should == 2
227
+ tree[3].distance.should be_within(0.001).of(0.061)
228
+
229
+ tree[4].left.should == 10
230
+ tree[4].right.should == -3
231
+ tree[4].distance.should be_within(0.001).of(0.077)
232
+
233
+ tree[5].left.should == 7
234
+ tree[5].right.should == -5
235
+ tree[5].distance.should be_within(0.001).of(0.092)
236
+
237
+ tree[6].left.should == 0
238
+ tree[6].right.should == -4
239
+ tree[6].distance.should be_within(0.001).of(0.242)
240
+
241
+ tree[7].left.should == -7
242
+ tree[7].right.should == -1
243
+ tree[7].distance.should be_within(0.001).of(0.246)
244
+
245
+ tree[8].left.should == 3
246
+ tree[8].right.should == -8
247
+ tree[8].distance.should be_within(0.001).of(0.287)
248
+
249
+ tree[9].left.should == -9
250
+ tree[9].right.should == 8
251
+ tree[9].distance.should be_within(0.001).of(1.936)
252
+
253
+ tree[10].left.should == -10
254
+ tree[10].right.should == -6
255
+ tree[10].distance.should be_within(0.001).of(3.432)
256
+
257
+ tree[11].left.should == 6
258
+ tree[11].right.should == -11
259
+ tree[11].distance.should be_within(0.001).of(3.535)
260
+ end
261
+
262
+ it "calculates pairwise centroid-linkage clustering" do
263
+ tree = Cluster.treecluster data, :mask => mask,
264
+ :weight => weight,
265
+ :transpose => false,
266
+ :method => 'c',
267
+ :dist => 'e'
268
+
269
+ tree.size.should == data.size - 1
270
+
271
+ tree[0].left.should == 4
272
+ tree[0].right.should == 5
273
+ tree[0].distance.should be_within(0.001).of(0.003)
274
+
275
+ tree[1].left.should == 12
276
+ tree[1].right.should == 9
277
+ tree[1].distance.should be_within(0.001).of(0.029)
278
+
279
+ tree[2].left.should == 1
280
+ tree[2].right.should == 2
281
+ tree[2].distance.should be_within(0.001).of(0.061)
282
+
283
+ tree[3].left.should == -2
284
+ tree[3].right.should == 11
285
+ tree[3].distance.should be_within(0.001).of(0.063)
286
+
287
+ tree[4].left.should == 10
288
+ tree[4].right.should == -4
289
+ tree[4].distance.should be_within(0.001).of(0.109)
290
+
291
+ tree[5].left.should == -5
292
+ tree[5].right.should == 7
293
+ tree[5].distance.should be_within(0.001).of(0.189)
294
+
295
+ tree[6].left.should == 0
296
+ tree[6].right.should == -3
297
+ tree[6].distance.should be_within(0.001).of(0.239)
298
+
299
+ tree[7].left.should == 3
300
+ tree[7].right.should == -1
301
+ tree[7].distance.should be_within(0.001).of(0.390)
302
+
303
+ tree[8].left.should == -7
304
+ tree[8].right.should == -8
305
+ tree[8].distance.should be_within(0.001).of(0.382)
306
+
307
+ tree[9].left.should == -9
308
+ tree[9].right.should == 8
309
+ tree[9].distance.should be_within(0.001).of(3.063)
310
+
311
+ tree[10].left.should == 6
312
+ tree[10].right.should == -6
313
+ tree[10].distance.should be_within(0.001).of(4.578)
314
+
315
+ tree[11].left.should == -10
316
+ tree[11].right.should == -11
317
+ tree[11].distance.should be_within(0.001).of(11.536)
318
+ end
319
+
320
+ it "calculates pairwise maximum-linkage clustering" do
321
+ tree = Cluster.treecluster data, :mask => mask,
322
+ :weight => weight,
323
+ :transpose => false,
324
+ :method => 'm',
325
+ :dist => 'e'
326
+
327
+ tree.size.should == data.size - 1
328
+
329
+ tree[0].left.should == 5
330
+ tree[0].right.should == 4
331
+ tree[0].distance.should be_within(0.001).of(0.003)
332
+
333
+ tree[1].left.should == 9
334
+ tree[1].right.should == 12
335
+ tree[1].distance.should be_within(0.001).of(0.029)
336
+
337
+ tree[2].left.should == 2
338
+ tree[2].right.should == 1
339
+ tree[2].distance.should be_within(0.001).of(0.061)
340
+
341
+ tree[3].left.should == 11
342
+ tree[3].right.should == 10
343
+ tree[3].distance.should be_within(0.001).of(0.077)
344
+
345
+ tree[4].left.should == -2
346
+ tree[4].right.should == -4
347
+ tree[4].distance.should be_within(0.001).of(0.216)
348
+
349
+ tree[5].left.should == -3
350
+ tree[5].right.should == 0
351
+ tree[5].distance.should be_within(0.001).of(0.266)
352
+
353
+ tree[6].left.should == -5
354
+ tree[6].right.should == 7
355
+ tree[6].distance.should be_within(0.001).of(0.302)
356
+
357
+ tree[7].left.should == -1
358
+ tree[7].right.should == 3
359
+ tree[7].distance.should be_within(0.001).of(0.425)
360
+
361
+ tree[8].left.should == -8
362
+ tree[8].right.should == -6
363
+ tree[8].distance.should be_within(0.001).of(0.968)
364
+
365
+ tree[9].left.should == 8
366
+ tree[9].right.should == 6
367
+ tree[9].distance.should be_within(0.001).of(3.975)
368
+
369
+ tree[10].left.should == -10
370
+ tree[10].right.should == -7
371
+ tree[10].distance.should be_within(0.001).of(5.755)
372
+
373
+ tree[11].left.should == -11
374
+ tree[11].right.should == -9
375
+ tree[11].distance.should be_within(0.001).of(22.734)
376
+ end
377
+ end
378
+
379
+ context "bad input" do
380
+ it "fails for a ragged matrix" do
381
+ ragged = [
382
+ [ 91.1, 92.2, 93.3, 94.4, 95.5],
383
+ [ 93.1, 93.2, 91.3, 92.4 ],
384
+ [ 94.1, 92.2, 90.3 ],
385
+ [ 12.1, 92.0, 90.0, 95.0, 90.0 ]
386
+ ]
387
+
388
+ lambda { Cluster.treecluster(ragged) }.should raise_error(ArgumentError)
389
+ end
390
+
391
+ it "fails for a matrix with bad cells" do
392
+ bad_cells = [
393
+ [ 7.1, 7.2, 7.3, 7.4, 7.5, ],
394
+ [ 7.1, 7.2, 7.3, 7.4, 'snoopy'],
395
+ [ 7.1, 7.2, 7.3, nil, nil]
396
+ ]
397
+
398
+ lambda { Cluster.treecluster(bad_cells) }.should raise_error(TypeError)
399
+ end
400
+
401
+ it "fails for a matrix with a bad row" do
402
+ bad_row = [
403
+ [ 23.1, 23.2, 23.3, 23.4, 23.5],
404
+ nil,
405
+ [ 23.1, 23.0, 23.0, 23.0, 23.0]
406
+ ]
407
+
408
+ lambda { Cluster.treecluster(bad_row) }.should raise_error(TypeError)
409
+ end
410
+ end
411
+ end
412
+