/*
 * The baseCode project
 * 
 * Copyright (c) 2013 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package ubic.basecode.dataStructure.matrix;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.InputStream;
import java.util.Arrays;

import org.junit.Before;
import org.junit.Test;

import cern.colt.matrix.DoubleMatrix2D;
import cern.colt.matrix.impl.DenseDoubleMatrix2D;
import cern.colt.matrix.linalg.Algebra;
import ubic.basecode.io.reader.DoubleMatrixReader;
import ubic.basecode.io.reader.TestDoubleMatrixReader;

/**
 * @author Paul
 * @version $Id$
 */
public class MatrixUtilTest {

    DoubleMatrix2D testData;

    @Before
    public void setUp() throws Exception {

        DoubleMatrixReader reader = new DoubleMatrixReader();

        InputStream is = TestDoubleMatrixReader.class.getResourceAsStream( "/data/testdatamissing.txt" );
        testData = new DenseDoubleMatrix2D( reader.read( is ).asDoubles() );
        is.close();
    }

    @Test
    public void testDropColumn() {
        assertEquals( 12, testData.columns() );
        assertEquals( 11, MatrixUtil.dropColumn( testData, 1 ).columns() );
    }

    @Test
    public void testSelectColumns() {
        assertEquals( 12, testData.columns() );
        assertEquals( 3, MatrixUtil.selectColumns( testData, Arrays.asList( new Integer[] { 2, 3, 8 } ) ).columns() );
    }

    @Test
    public void testSelectColumnsAndRows() {
        Algebra A = new Algebra();
        DoubleMatrix2D square = A.mult( testData, A.transpose( testData ) );
        assertEquals( square.rows(), square.columns() );
        assertEquals( 3, MatrixUtil.selectColumnsAndRows( square, Arrays.asList( new Integer[] { 2, 3, 4 } ) ).rows() );
        assertEquals( 3,
                MatrixUtil.selectColumnsAndRows( square, Arrays.asList( new Integer[] { 2, 3, 4 } ) ).columns() );

    }

    @Test
    public void testSelectColumnsAndRowsBad() {
        try {
            MatrixUtil.selectColumnsAndRows( testData, Arrays.asList( new Integer[] { 2, 3, 4 } ) );
            fail( "Should have gotten illegalarg ex" );
        } catch ( IllegalArgumentException e ) {
            // expected
        }
    }

    @Test
    public void testSelectRows() {
        assertEquals( 3, MatrixUtil.selectRows( testData, Arrays.asList( new Integer[] { 2, 3, 4 } ) ).rows() );
    }

}

@@ -674,16 +674,66 @@
 atrix;%0A%0A
+import static org.junit.Assert.assertArrayEquals;%0A
 import s
@@ -910,16 +910,56 @@
 .Test;%0A%0A
+import cern.colt.matrix.DoubleMatrix1D;%0A
 import c
@@ -1202,16 +1202,17 @@
 @author 
+ 
 Paul%0A * 
@@ -2994,10 +2994,942 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testRemoveMissing1() %7B%0A        DoubleMatrix1D v1 = testData.viewRow( 1 ); // first value is NaN%0A        DoubleMatrix1D actual = MatrixUtil.removeMissing( v1 );%0A        DoubleMatrix1D expected = new DenseDoubleMatrix1D(%0A                new double%5B%5D %7B 172.5, 242.1, -8.8, 148.8, 190.3, 155.1, 205.3, 337.8, 276, -64.2, 295.4 %7D );%0A%0A        assertArrayEquals( expected.toArray(), actual.toArray(), 0.1 );%0A%0A    %7D%0A%0A    @Test%0A    public void testRemoveMissing2() %7B%0A        DoubleMatrix1D v1 = new DenseDoubleMatrix1D( new double%5B%5D %7B 1, 2, Double.NaN, 4, 5 %7D );%0A%0A        DoubleMatrix1D v2 = new DenseDoubleMatrix1D( new double%5B%5D %7B 11, 12, 13, 14, 15 %7D );%0A%0A        DoubleMatrix1D actual = MatrixUtil.removeMissing( v1, v2 );%0A        DoubleMatrix1D expected = new DenseDoubleMatrix1D(%0A                new double%5B%5D %7B 11, 12, 14, 15 %7D );%0A%0A        assertArrayEquals( expected.toArray(), actual.toArray(), 0.1 );%0A    %7D%0A%0A
 %7D%0A

add test for new removemissing (two methods)