1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ubic.basecode.dataStructure.matrix;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21
22 import java.util.List;
23
24 import org.junit.Test;
25
26 import ubic.basecode.io.reader.DoubleMatrixReader;
27 import cern.colt.list.DoubleArrayList;
28 import cern.colt.matrix.DoubleMatrix1D;
29
30
31
32
33
34
35
36 public abstract class AbstractDoubleMatrixTest {
37 protected DoubleMatrixReader f = new DoubleMatrixReader();
38 protected double[][] testArray = { { 1, 2, 3, 4 }, { 11, 12, 13, 14 }, { 21, Double.NaN, 23, 24 } };
39 protected DoubleMatrix<String, String> testdata;
40 protected DoubleMatrix<String, String> testM;
41
42
43
44
45 @Test
46 public void testColumns() {
47 assertEquals( 12, testdata.columns() );
48 }
49
50 @Test
51 public void testCopy() {
52 DoubleMatrix<String, String> actual = testdata.copy();
53 double[][] tt = testdata.asArray();
54
55 for ( int i = 0; i < tt.length; i++ ) {
56 assertEquals( testdata.getRowName( i ), actual.getRowName( i ) );
57 int len = tt[i].length;
58 for ( int j = 0; j < len; j++ ) {
59 assertEquals( testdata.getColName( j ), actual.getColName( j ) );
60 assertEquals( tt[i][j], actual.get( i, j ), 0.00001 );
61 }
62 }
63
64 }
65
66
67
68
69 @Test
70 public void testGet() {
71 assertEquals( 27873.8, testdata.get( 2, 4 ), 0.0001 );
72 }
73
74
75
76
77 @Test
78 public void testGet2() {
79 assertEquals( 24.0, testM.get( 2, 3 ), 0.0001 );
80 assertEquals( 1.0, testM.get( 0, 0 ), 0.0001 );
81 assertEquals( 13.0, testM.get( 1, 2 ), 0.0001 );
82 assertEquals( Double.NaN, testM.get( 2, 1 ), 0.0001 );
83 assertEquals( 23.0, testM.get( 2, 2 ), 0.0001 );
84 }
85
86
87
88
89 @Test
90 public void testGetByKeys() {
91 assertEquals( 24.0, testM.getByKeys( "c", "z" ), 0.0001 );
92 assertEquals( 1.0, testM.getByKeys( "a", "w" ), 0.0001 );
93 assertEquals( 13.0, testM.getByKeys( "b", "y" ), 0.0001 );
94 assertEquals( Double.NaN, testM.getByKeys( "c", "x" ), 0.0001 );
95 assertEquals( 23.0, testM.getByKeys( "c", "y" ), 0.0001 );
96 }
97
98
99
100
101 @Test
102 public void testGetColByName() {
103 double[] actual = testM.getColumnByName( "x" );
104 assertEquals( 2, actual[0], 0.000001 );
105 assertEquals( 12, actual[1], 0.000001 );
106 assertEquals( Double.NaN, actual[2], 0.0001 );
107 }
108
109 @Test(expected = IllegalArgumentException.class)
110 public void testGetColByNameFail() {
111 testM.getColumnByName( "nnnn" );
112 }
113
114 @Test(expected = IllegalArgumentException.class)
115 public void testGetColByNameNull() {
116 testM.getColumnByName( null );
117 }
118
119
120
121
122 @Test
123 public void testGetColObj() {
124 Double[] c = testdata.getColObj( 4 );
125 assertEquals( 30, c.length );
126 }
127
128
129
130
131 @Test
132 public void testGetColObj2() {
133 Double[] actual = testM.getColObj( 0 );
134 assertEquals( 3, actual.length );
135 assertEquals( 1.0, actual[0], 0.000001 );
136 assertEquals( 11.0, actual[1], 0.000001 );
137 assertEquals( 21.0, actual[2], 0.000001 );
138 }
139
140 @Test
141 public void testGetColRange() {
142 DoubleMatrix<String, String> range = testdata.getColRange( 1, 4 );
143 assertEquals( 4, range.columns() );
144 assertEquals( 30, range.rows() );
145 }
146
147
148
149
150 @Test
151 public void testGetColumn() {
152 double[] actual = testM.getColumn( 1 );
153 assertEquals( 3, actual.length );
154 assertEquals( 2, actual[0], 0.000001 );
155 assertEquals( 12.0, actual[1], 0.000001 );
156 assertEquals( Double.NaN, actual[2], 0.0001 );
157 }
158
159
160
161
162 @Test
163 public void testGetObject() {
164 assertEquals( 4.0, testM.getObject( 0, 3 ), 0.000001 );
165 }
166
167
168
169
170 @Test
171 public void testGetRow() {
172 double[] res = testdata.getRow( 2 );
173 assertEquals( 27873.8, res[4], 0.001 );
174 }
175
176
177
178
179 @Test
180 public void testGetRow2() {
181 double[] actual = testM.getRow( 1 );
182 assertEquals( 4, actual.length );
183 assertEquals( 11.0, actual[0], 0.000001 );
184 assertEquals( 12.0, actual[1], 0.000001 );
185 assertEquals( 13.0, actual[2], 0.000001 );
186 assertEquals( 14.0, actual[3], 0.000001 );
187 }
188
189
190
191
192 public void testGetRowArrayList() {
193 DoubleArrayList res = testdata.getRowArrayList( 2 );
194 assertEquals( 27873.8, res.get( 4 ), 0.001 );
195 }
196
197
198
199
200 @Test
201 public void testGetRowArrayList2() {
202 DoubleArrayList actual = testM.getRowArrayList( 2 );
203 assertEquals( 4, actual.size() );
204 assertEquals( 21.0, actual.get( 0 ), 0.000001 );
205 assertEquals( Double.NaN, actual.get( 1 ), 0.0001 );
206 assertEquals( 23.0, actual.get( 2 ), 0.000001 );
207 assertEquals( 24.0, actual.get( 3 ), 0.000001 );
208 }
209
210
211
212
213 @Test
214 public void testGetRowByName() {
215 double[] actual = testM.getRowByName( "b" );
216 assertEquals( 4, actual.length );
217 assertEquals( 11.0, actual[0], 0.000001 );
218 assertEquals( 12.0, actual[1], 0.000001 );
219 assertEquals( 13.0, actual[2], 0.000001 );
220 assertEquals( 14.0, actual[3], 0.000001 );
221 }
222
223 @Test(expected = IllegalArgumentException.class)
224 public void testGetRowByNameFail() {
225 testM.getRowByName( "nnnn" );
226 }
227
228 @Test(expected = IllegalArgumentException.class)
229 public void testGetRowByNameNull() {
230 testM.getRowByName( null );
231 }
232
233
234
235
236 @Test
237 public void testGetRowObj() {
238 Double[] rowRange = testdata.getRowObj( 4 );
239 assertEquals( 12, rowRange.length );
240 }
241
242
243
244
245 @Test
246 public void testGetRowObj2() {
247 Double[] actual = testM.getRowObj( 0 );
248 assertEquals( 4, actual.length );
249 assertEquals( 1.0, actual[0], 0.000001 );
250 assertEquals( 2.0, actual[1], 0.000001 );
251 assertEquals( 3.0, actual[2], 0.000001 );
252 assertEquals( 4.0, actual[3], 0.000001 );
253 }
254
255 @Test
256 public void testGetRowRange() {
257 DoubleMatrix<String, String> rowRange = testdata.getRowRange( 1, 4 );
258 assertEquals( 12, rowRange.columns() );
259 assertEquals( 4, rowRange.rows() );
260 }
261
262
263
264
265 @Test
266 public void testIsMissing() throws Exception {
267 assertTrue( !testdata.isMissing( 5, 3 ) );
268 DoubleMatrix<String, String> md = f.read( FastRowAccessDoubleMatrixTest.class
269 .getResourceAsStream( "/data/testdatamissing.txt" ) );
270 assertTrue( md.isMissing( 5, 3 ) );
271 }
272
273
274
275
276 @Test
277 public void testIsMissing2() {
278 assertFalse( testM.isMissing( 2, 2 ) );
279 assertTrue( testM.isMissing( 2, 1 ) );
280 }
281
282
283
284
285 @Test
286 public void testRows() {
287 assertTrue( testdata.rows() == 30 );
288 }
289
290
291
292
293 @Test
294 public void testRows2() {
295 assertEquals( 3, testM.rows() );
296 }
297
298 @Test
299 public void testSet() {
300 testdata.set( 1, 2, 3.0 );
301 assertEquals( 3.0, testdata.get( 1, 2 ), 0.00001 );
302 }
303
304
305
306
307 @Test
308 public void testSet2() {
309 testM.set( 2, 2, 666.0 );
310 double[] actual = testM.getRow( 2 );
311 assertEquals( 666.0, actual[2], 0.00001 );
312 }
313
314
315
316
317 public void testSetIntIntDouble() {
318 testdata.set( 1, 2, 3.0 );
319 assertEquals( 3.0, testdata.get( 1, 2 ), 0.00001 );
320 }
321
322
323
324
325 @Test
326 public void testSetIntIntObject() {
327 testdata.set( 1, 2, new Double( 3.0 ) );
328 assertEquals( 3.0, testdata.get( 1, 2 ), 0.00001 );
329 }
330
331 @Test
332 public void testSize() {
333 assertEquals( 12, testM.size() );
334 }
335
336 @Test
337 public void testSortByColumnAbsoluteValue() {
338 for ( int i = 0; i < testdata.columns(); i++ ) {
339 List<String> sortByColumnAbsoluteValues = testdata.sortByColumnAbsoluteValues( i, false );
340 Double last = 0.0;
341 assertTrue( !sortByColumnAbsoluteValues.isEmpty() );
342 for ( String string : sortByColumnAbsoluteValues ) {
343 Double d = testdata.getRowByName( string )[i];
344 assertNotNull( d );
345 assertTrue( "at column " + i + " got " + d + " and prev " + last, Math.abs( d ) >= Math.abs( last ) );
346
347 last = d;
348 assertNotNull( last );
349 }
350 }
351
352 for ( int i = 0; i < testdata.columns(); i++ ) {
353 double last = Double.MAX_VALUE;
354 List<String> sortByColumnAbsoluteValues = testdata.sortByColumnAbsoluteValues( i, true );
355 assertTrue( !sortByColumnAbsoluteValues.isEmpty() );
356
357 for ( String string : sortByColumnAbsoluteValues ) {
358 double d = testdata.getRowByName( string )[i];
359
360 assertTrue( "at column " + i + " got " + d + " and prev " + last, Math.abs( d ) <= Math.abs( last ) );
361
362 last = d;
363 }
364 }
365 }
366
367 @Test
368 public void testSubsetColumns() {
369 List<String> c = testdata.getColNames();
370 List<String> subList = c.subList( 1, 3 );
371 DoubleMatrix<String, String> s = testdata.subsetColumns( subList );
372 assertEquals( 2, s.columns() );
373 assertEquals( c.get( 1 ), s.getColNames().get( 0 ) );
374 }
375
376 @Test
377 public void testSubsetRows() {
378 List<String> rowNames = testdata.getRowNames();
379 List<String> subList = rowNames.subList( 1, 3 );
380 DoubleMatrix<String, String> subsetRows = testdata.subsetRows( subList );
381 assertEquals( 2, subsetRows.rows() );
382 }
383
384 @Test
385 public void testToArray() {
386 double[][] actual = testM.asArray();
387 for ( int i = 0; i < testArray.length; i++ ) {
388 int len = testArray[i].length;
389 for ( int j = 0; j < len; j++ ) {
390 assertEquals( testArray[i][j], actual[i][j], 0.0001 );
391 }
392 }
393
394 }
395
396
397
398
399 @Test
400 public void testToString() {
401 String actual = testM.toString();
402 assertEquals(
403 "# 3x4 matrix\nlabel\tw\tx\ty\tz\na\t1.000\t2.000\t3.000\t4.000\nb\t11.00\t12.00\t13.00\t14.00\nc\t21.00\t\t23.00\t24.00\n",
404 actual );
405 }
406
407 @Test
408 public void testViewColumn() {
409 DoubleMatrix1D actual = testM.viewColumn( 0 );
410 assertEquals( 3, actual.size() );
411 assertEquals( 1.0, actual.get( 0 ), 0.000001 );
412 assertEquals( 11.0, actual.get( 1 ), 0.000001 );
413 assertEquals( 21.0, actual.get( 2 ), 0.000001 );
414 }
415
416
417
418
419 @Test
420 public void testViewRow() {
421 DoubleMatrix1D res = testdata.viewRow( 1 );
422 assertEquals( 242.1, res.get( 2 ), 0.001 );
423 }
424
425
426
427
428 @Test
429 public void testViewRow2() {
430 DoubleMatrix1D actual = testM.viewRow( 0 );
431 assertEquals( 4, actual.size() );
432 assertEquals( 1.0, actual.get( 0 ), 0.000001 );
433 assertEquals( 2.0, actual.get( 1 ), 0.000001 );
434 assertEquals( 3.0, actual.get( 2 ), 0.000001 );
435 assertEquals( 4.0, actual.get( 3 ), 0.000001 );
436
437 }
438
439 }