Skip to content

Commit b3bd485

Browse files
committed
Merge branch 'release-1.0.24'
2 parents 54503ed + 7826c7c commit b3bd485

9 files changed

Lines changed: 169 additions & 100 deletions

File tree

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<attributes>
55
<attribute name="optional" value="true"/>
66
<attribute name="maven.pomderived" value="true"/>
7+
<attribute name="test" value="true"/>
78
</attributes>
89
</classpathentry>
910
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src">

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<name>baseCode</name>
66
<groupId>baseCode</groupId>
77
<artifactId>baseCode</artifactId>
8-
<version>1.0.23</version>
8+
<version>1.0.24</version>
99
<inceptionYear>2003</inceptionYear>
1010
<description>
1111
<![CDATA[Data structures, math and statistics tools, and utilities that are often needed across projects.]]>

src/ubic/basecode/dataStructure/matrix/MatrixUtil.java

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131

3232
/**
3333
* @author Paul
34-
* @version $Id$
3534
*/
3635
public class MatrixUtil {
3736

3837
/**
39-
* @param d
40-
* @return true if any of the values are very close to zero.
38+
* @param d
39+
* @return true if any of the values are very close to zero.
4140
*/
4241
public static boolean containsNearlyZeros( DoubleMatrix1D d ) {
4342
for ( int i = 0; i < d.size(); i++ ) {
@@ -49,7 +48,7 @@ public static boolean containsNearlyZeros( DoubleMatrix1D d ) {
4948
/**
5049
* Extract the diagonal from a matrix.
5150
*
52-
* @param matrix
51+
* @param matrix
5352
* @return
5453
*/
5554
public static DoubleMatrix1D diagonal( DoubleMatrix2D matrix ) {
@@ -63,8 +62,8 @@ public static DoubleMatrix1D diagonal( DoubleMatrix2D matrix ) {
6362
}
6463

6564
/**
66-
* @param n
67-
* @param indexToDrop
65+
* @param n
66+
* @param indexToDrop
6867
* @return
6968
*/
7069
public static DoubleMatrix2D dropColumn( DoubleMatrix2D n, int indexToDrop ) {
@@ -108,7 +107,7 @@ public static DoubleMatrix2D dropColumns( DoubleMatrix2D n, Collection<Integer>
108107
/**
109108
* Makes a copy
110109
*
111-
* @param list
110+
* @param list
112111
* @return
113112
*/
114113
public static DoubleMatrix1D fromList( DoubleArrayList list ) {
@@ -120,12 +119,12 @@ public static DoubleMatrix1D fromList( DoubleArrayList list ) {
120119
}
121120

122121
/**
123-
* @param <R>
124-
* @param <C>
125-
* @param <V>
126-
* @param matrix
127-
* @param rowIndex
128-
* @param colIndex
122+
* @param <R>
123+
* @param <C>
124+
* @param <V>
125+
* @param matrix
126+
* @param rowIndex
127+
* @param colIndex
129128
* @return
130129
*/
131130
public static <R, C, V> V getObject( Matrix2D<R, C, V> matrix, int rowIndex, int colIndex ) {
@@ -139,11 +138,11 @@ public static <R, C, V> V getObject( Matrix2D<R, C, V> matrix, int rowIndex, int
139138
}
140139

141140
/**
142-
* @param <R>
143-
* @param <C>
144-
* @param <V>
145-
* @param matrix
146-
* @param rowIndex
141+
* @param <R>
142+
* @param <C>
143+
* @param <V>
144+
* @param matrix
145+
* @param rowIndex
147146
* @return
148147
*/
149148
public static <R, C, V> V[] getRow( Matrix2D<R, C, V> matrix, int rowIndex ) {
@@ -178,8 +177,8 @@ public static DoubleMatrix1D multWithMissing( DoubleMatrix1D a, DoubleMatrix2D b
178177
}
179178

180179
/**
181-
* @param a
182-
* @param b
180+
* @param a
181+
* @param b
183182
* @return
184183
*/
185184
public static DoubleMatrix1D multWithMissing( DoubleMatrix2D a, DoubleMatrix1D b ) {
@@ -212,8 +211,8 @@ public static DoubleMatrix1D multWithMissing( DoubleMatrix2D a, DoubleMatrix1D b
212211
/**
213212
* Multiple two matrices, tolerate missing values.
214213
*
215-
* @param a
216-
* @param b
214+
* @param a
215+
* @param b
217216
* @return
218217
*/
219218
public static DoubleMatrix2D multWithMissing( DoubleMatrix2D a, DoubleMatrix2D b ) {
@@ -245,16 +244,16 @@ public static DoubleMatrix2D multWithMissing( DoubleMatrix2D a, DoubleMatrix2D b
245244
}
246245

247246
public static List<Integer> notNearlyZeroIndices( DoubleMatrix1D d ) {
248-
List<Integer> result = new ArrayList<Integer>();
247+
List<Integer> result = new ArrayList<>();
249248
for ( int i = 0; i < d.size(); i++ ) {
250249
if ( Math.abs( d.getQuick( i ) ) > Constants.SMALL ) result.add( i );
251250
}
252251
return result;
253252
}
254253

255254
/**
256-
* @param data
257-
* @return a copy of the data with missing values removed (might be empty!)
255+
* @param data
256+
* @return a copy of the data with missing values removed (might be empty!)
258257
*/
259258
public static DoubleMatrix1D removeMissing( DoubleMatrix1D data ) {
260259
int sizeWithoutMissingValues = sizeWithoutMissingValues( data );
@@ -272,6 +271,31 @@ public static DoubleMatrix1D removeMissing( DoubleMatrix1D data ) {
272271
return r;
273272
}
274273

274+
/**
275+
* Remove values from data corresponding to missing values in reference.
276+
*
277+
* @param reference
278+
* @param data
279+
* @return
280+
*/
281+
public static DoubleMatrix1D removeMissing( DoubleMatrix1D reference, DoubleMatrix1D data ) {
282+
if ( data.size() != reference.size() ) throw new IllegalArgumentException( "Reference and data must have same size" );
283+
int sizeWithoutMissingValues = sizeWithoutMissingValues( reference );
284+
if ( sizeWithoutMissingValues == reference.size() ) return data; // no missing values.
285+
DoubleMatrix1D r = new DenseDoubleMatrix1D( sizeWithoutMissingValues );
286+
double[] elements = data.toArray();
287+
double[] refels = reference.toArray();
288+
int size = data.size();
289+
int j = 0;
290+
for ( int i = 0; i < size; i++ ) {
291+
if ( Double.isNaN( refels[i] ) || Double.isInfinite( refels[i] ) ) {
292+
continue;
293+
}
294+
r.set( j++, elements[i] );
295+
}
296+
return r;
297+
}
298+
275299
public static DoubleMatrix1D select( DoubleMatrix1D v, Collection<Integer> selected ) {
276300
DoubleMatrix1D result = new DenseDoubleMatrix1D( selected.size() );
277301
int k = 0;
@@ -301,8 +325,8 @@ public static DoubleMatrix2D selectColumns( DoubleMatrix2D n, Collection<Integer
301325
}
302326

303327
/**
304-
* @param n square matrix
305-
* @param selected
328+
* @param n square matrix
329+
* @param selected
306330
* @return
307331
*/
308332
public static DoubleMatrix2D selectColumnsAndRows( DoubleMatrix2D n, Collection<Integer> selected ) {
@@ -371,7 +395,7 @@ public static int sizeWithoutMissingValues( DoubleMatrix1D list ) {
371395
/**
372396
* Makes a copy
373397
*
374-
* @param vector
398+
* @param vector
375399
* @return
376400
*/
377401
public static DoubleArrayList toList( DoubleMatrix1D vector ) {

src/ubic/basecode/math/Stats.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@
2222
import java.util.Set;
2323

2424
import cern.colt.list.DoubleArrayList;
25-
import cern.colt.matrix.DoubleFactory2D;
26-
import cern.colt.matrix.DoubleMatrix1D;
27-
import cern.colt.matrix.DoubleMatrix2D;
28-
import cern.colt.matrix.linalg.Algebra;
29-
import cern.jet.math.Functions;
3025
import cern.jet.stat.Descriptive;
31-
import ubic.basecode.dataStructure.matrix.MatrixUtil;
3226

3327
/**
3428
* Miscellaneous functions used for statistical analysis. Some are optimized or specialized versions of methods that can
@@ -281,7 +275,7 @@ public static double quantile( int index, double[] values, int effectiveSize ) {
281275
* @return double
282276
*/
283277
public static double range( DoubleArrayList data ) {
284-
return Descriptive.max( data ) - Descriptive.min( data );
278+
return DescriptiveWithMissing.max( data ) - DescriptiveWithMissing.min( data );
285279
}
286280

287281
private Stats() { /* block instantiation */

src/ubic/basecode/math/linalg/SingularValueDecomposition.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* The baseCode project
3-
*
3+
*
44
* Copyright (c) 2008 University of British Columbia
5-
*
5+
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
88
* You may obtain a copy of the License at
@@ -27,15 +27,15 @@
2727

2828
import org.apache.commons.lang3.time.StopWatch;
2929

30-
import ubic.basecode.dataStructure.matrix.DenseDoubleMatrix;
31-
import ubic.basecode.dataStructure.matrix.DoubleMatrix;
3230
import cern.colt.matrix.DoubleMatrix2D;
3331
import cern.colt.matrix.impl.DenseDoubleMatrix2D;
32+
import ubic.basecode.dataStructure.matrix.DenseDoubleMatrix;
33+
import ubic.basecode.dataStructure.matrix.DoubleMatrix;
3434

3535
/**
3636
* SVD for DoubleMatrix.
37-
*
38-
* @author paul
37+
*
38+
* @author paul
3939
* @version $Id$
4040
*/
4141
public class SingularValueDecomposition<R, C> {
@@ -62,7 +62,7 @@ public SingularValueDecomposition( DoubleMatrix<R, C> matrix ) {
6262

6363
computeSVD( dm );
6464

65-
List<Integer> componentIds = new ArrayList<Integer>();
65+
List<Integer> componentIds = new ArrayList<>();
6666

6767
if ( rowNames.size() == 0 ) { // sanity check
6868
throw new IllegalStateException( "No row names!" );
@@ -72,46 +72,46 @@ public SingularValueDecomposition( DoubleMatrix<R, C> matrix ) {
7272
componentIds.add( i );
7373
}
7474

75-
this.uMatrix = new DenseDoubleMatrix<R, Integer>( svd.getU().toArray() );
75+
this.uMatrix = new DenseDoubleMatrix<>( svd.getU().toArray() );
7676
uMatrix.setRowNames( this.rowNames );
7777
uMatrix.setColumnNames( componentIds );
7878

79-
this.vMatrix = new DenseDoubleMatrix<Integer, C>( svd.getV().toArray() );
79+
this.vMatrix = new DenseDoubleMatrix<>( svd.getV().toArray() );
8080
vMatrix.setRowNames( componentIds );
8181
vMatrix.setColumnNames( this.columnNames );
8282

83-
this.sMatrix = new DenseDoubleMatrix<Integer, Integer>( svd.getS().toArray() );
83+
this.sMatrix = new DenseDoubleMatrix<>( svd.getS().toArray() );
8484
sMatrix.setRowNames( componentIds );
8585
sMatrix.setColumnNames( componentIds );
8686
}
8787

8888
/**
8989
* @return the condition number of the matrix
90-
* @see cern.colt.matrix.linalg.SingularValueDecomposition#cond()
90+
* @see cern.colt.matrix.linalg.SingularValueDecomposition#cond()
9191
*/
9292
public double cond() {
9393
return svd.cond();
9494
}
9595

9696
/**
9797
* @return
98-
* @see cern.colt.matrix.linalg.SingularValueDecomposition#getS()
98+
* @see cern.colt.matrix.linalg.SingularValueDecomposition#getS()
9999
*/
100100
public DoubleMatrix<Integer, Integer> getS() {
101101
return this.sMatrix;
102102
}
103103

104104
/**
105105
* @return
106-
* @see cern.colt.matrix.linalg.SingularValueDecomposition#getSingularValues()
106+
* @see cern.colt.matrix.linalg.SingularValueDecomposition#getSingularValues()
107107
*/
108108
public double[] getSingularValues() {
109109
return svd.getSingularValues();
110110
}
111111

112112
/**
113113
* @return
114-
* @see cern.colt.matrix.linalg.SingularValueDecomposition#getU()
114+
* @see cern.colt.matrix.linalg.SingularValueDecomposition#getU()
115115
*/
116116
public DoubleMatrix<R, Integer> getU() {
117117
return this.uMatrix;
@@ -120,31 +120,31 @@ public DoubleMatrix<R, Integer> getU() {
120120

121121
/**
122122
* @return
123-
* @see cern.colt.matrix.linalg.SingularValueDecomposition#getV()
123+
* @see cern.colt.matrix.linalg.SingularValueDecomposition#getV()
124124
*/
125125
public DoubleMatrix<Integer, C> getV() {
126126
return this.vMatrix;
127127
}
128128

129129
/**
130130
* @return
131-
* @see cern.colt.matrix.linalg.SingularValueDecomposition#norm2()
131+
* @see cern.colt.matrix.linalg.SingularValueDecomposition#norm2()
132132
*/
133133
public double norm2() {
134134
return svd.norm2();
135135
}
136136

137137
/**
138138
* @return
139-
* @see cern.colt.matrix.linalg.SingularValueDecomposition#rank()
139+
* @see cern.colt.matrix.linalg.SingularValueDecomposition#rank()
140140
*/
141141
public int rank() {
142142
return svd.rank();
143143
}
144144

145145
/**
146146
* @return
147-
* @see cern.colt.matrix.linalg.SingularValueDecomposition#toString()
147+
* @see cern.colt.matrix.linalg.SingularValueDecomposition#toString()
148148
*/
149149
@Override
150150
public String toString() {
@@ -158,7 +158,7 @@ private void computeSVD( final DoubleMatrix2D dm ) {
158158
/*
159159
* This fails to converge some times, we have to bail.
160160
*/
161-
FutureTask<cern.colt.matrix.linalg.SingularValueDecomposition> svdFuture = new FutureTask<cern.colt.matrix.linalg.SingularValueDecomposition>(
161+
FutureTask<cern.colt.matrix.linalg.SingularValueDecomposition> svdFuture = new FutureTask<>(
162162
new Callable<cern.colt.matrix.linalg.SingularValueDecomposition>() {
163163
@Override
164164
public cern.colt.matrix.linalg.SingularValueDecomposition call() {

0 commit comments

Comments
 (0)