    1   package baseCode.dataFilter;
    2
    3   import baseCode.dataFilter.RowLevelFilter;
    4   import baseCode.dataStructure.matrix.DenseDoubleMatrix2DNamed;
    5
    6   /***
    7    *
    8    * <p>
    9    * Copyright (c) 2004 Columbia University
    10   *
    11   * @author pavlidis
    12   * @version $Id: TestRowLevelFilter.java,v 1.1 2005/03/17 13:58:41 pavlidis Exp $
    13   */
    14  public class TestRowLevelFilter extends AbstractTestFilter {
    15     RowLevelFilter f = null;
    16
    17     /*
    18      * @see TestCase#setUp()
    19      */
    20     protected void setUp() throws Exception {
    21        super.setUp();
    22        f = new RowLevelFilter();
    23     }
    24
    25     /*
    26      * @see TestCase#tearDown()
    27      */
    28     protected void tearDown() throws Exception {
    29        super.tearDown();
    30     }
    31
    32     /*
    33      * Class under test for NamedMatrix filter(NamedMatrix)
    34      */
    35     public final void testFilterMax() {
    36        f.setLowCut( 100.0, false );
    37        f.setRemoveAllNegative( true ); // irrelevant.
    38        f.setMethod( RowLevelFilter.MAX ); // this is the default.
    39        DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    40              .filter( testdata );
    41        int actualReturn = filtered.rows();
    42        int expectedReturn = 26;
    43        assertEquals( "return value", expectedReturn, actualReturn );
    44     }
    45
    46     public final void testFilterMaxTooHigh() {
    47        f.setLowCut( 100000000.0, false );
    48        f.setRemoveAllNegative( true ); // irrelevant.
    49        f.setMethod( RowLevelFilter.MAX ); // this is the default.
    50        DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    51              .filter( testdata );
    52        int actualReturn = filtered.rows();
    53        int expectedReturn = 0;
    54        assertEquals( "return value", expectedReturn, actualReturn );
    55     }
    56
    57     public final void testFilterMin() {
    58        f.setLowCut( 100.0, false );
    59        f.setRemoveAllNegative( true );
    60        f.setMethod( RowLevelFilter.MIN );
    61        DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    62              .filter( testdata );
    63        int actualReturn = filtered.rows();
    64        int expectedReturn = 16;
    65        assertEquals( "return value", expectedReturn, actualReturn );
    66     }
    67
    68     public final void testFilterMean() {
    69        f.setLowCut( 100.0, false );
    70        f.setRemoveAllNegative( true );
    71        f.setMethod( RowLevelFilter.MEAN );
    72        DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    73              .filter( testdata );
    74        int actualReturn = filtered.rows();
    75        int expectedReturn = 21;
    76        assertEquals( "return value", expectedReturn, actualReturn );
    77     }
    78
    79     public final void testFilterMedian() {
    80        f.setLowCut( 100.0, false );
    81        f.setRemoveAllNegative( true ); // irrelevant
    82        f.setMethod( RowLevelFilter.MEDIAN );
    83        DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    84              .filter( testdata );
    85        int actualReturn = filtered.rows();
    86        int expectedReturn = 20;
    87        assertEquals( "return value", expectedReturn, actualReturn );
    88     }
    89
    90     public final void testFilterMaxFraction() {
    91        f.setLowCut( 0.3, true );
    92        f.setRemoveAllNegative( false );
    93        DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    94              .filter( testdata );
    95        int actualReturn = filtered.rows();
    96        int expectedReturn = 21;
    97        assertEquals( "return value", expectedReturn, actualReturn );
    98     }
    99
    100    public final void testFilterMaxFractionAll() {
    101       f.setLowCut( 1.0, true );
    102       f.setRemoveAllNegative( false );
    103       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    104             .filter( testdata );
    105       int actualReturn = filtered.rows();
    106       int expectedReturn = 1; // we don't actually ever remove all items.
    107       assertEquals( "return value", expectedReturn, actualReturn );
    108    }
    109
    110    public final void testFilterMaxFractionNone() {
    111       f.setLowCut( 0.0, true );
    112       f.setRemoveAllNegative( false );
    113       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    114             .filter( testdata );
    115       int actualReturn = filtered.rows();
    116       int expectedReturn = 30;
    117       assertEquals( "return value", expectedReturn, actualReturn );
    118    }
    119
    120    public final void testFilterMaxFractionNoneRemoveNeg() {
    121       f.setLowCut( 0.0, true );
    122       f.setRemoveAllNegative( true );
    123       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    124             .filter( testdata );
    125       int actualReturn = filtered.rows();
    126       int expectedReturn = 27;
    127       assertEquals( "return value", expectedReturn, actualReturn );
    128    }
    129
    130    public final void testFilterFractionMaxRemoveNeg() {
    131       f.setLowCut( 0.6, true );
    132       f.setRemoveAllNegative( true ); // removes 3 of 30 rows. 27 * 0.6 =16.2,
    133       // ceil 17, + 3 = 20
    134       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    135             .filter( testdata );
    136       int actualReturn = filtered.rows();
    137       int expectedReturn = 11;
    138       assertEquals( "return value", expectedReturn, actualReturn );
    139    }
    140
    141    public final void testFilterBadFraction() {
    142       try {
    143          f.setLowCut( 110.3, true );
    144          f.filter( testdata );
    145          fail( "Should have gotten an exception" );
    146       } catch ( RuntimeException success ) {
    147
    148       }
    149    }
    150
    151    public final void testFilterNoFilter() {
    152       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    153             .filter( testdata );
    154       assertEquals( "return value", testdata, filtered );
    155    }
    156
    157    public final void testFilterMaxHigh() {
    158       f.setHighCut( 1000.0, false );
    159       f.setRemoveAllNegative( true ); // irrelevant.
    160       f.setMethod( RowLevelFilter.MAX ); // this is the default.
    161       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    162             .filter( testdata );
    163       int actualReturn = filtered.rows();
    164       int expectedReturn = 16;
    165       assertEquals( "return value", expectedReturn, actualReturn );
    166    }
    167
    168    public final void testFilterMinHigh() {
    169       f.setHighCut( 1000.0, false );
    170       f.setRemoveAllNegative( true );
    171       f.setMethod( RowLevelFilter.MIN );
    172       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    173             .filter( testdata );
    174       int actualReturn = filtered.rows();
    175       int expectedReturn = 24;
    176       assertEquals( "return value", expectedReturn, actualReturn );
    177    }
    178
    179    public final void testFilterMeanHigh() {
    180       f.setHighCut( 1000.0, false );
    181       f.setRemoveAllNegative( true );
    182       f.setMethod( RowLevelFilter.MEAN );
    183       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    184             .filter( testdata );
    185       int actualReturn = filtered.rows();
    186       int expectedReturn = 21;
    187       assertEquals( "return value", expectedReturn, actualReturn );
    188    }
    189
    190    public final void testFilterMedianHigh() {
    191       f.setHighCut( 1000.0, false );
    192       f.setRemoveAllNegative( true ); // irrelevant
    193       f.setMethod( RowLevelFilter.MEDIAN );
    194       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    195             .filter( testdata );
    196       int actualReturn = filtered.rows();
    197       int expectedReturn = 22;
    198       assertEquals( "return value", expectedReturn, actualReturn );
    199    }
    200
    201    public final void testFilterMaxFractionHigh() {
    202       f.setHighCut( 0.3, true );
    203       f.setRemoveAllNegative( false );
    204       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    205             .filter( testdata );
    206       int actualReturn = filtered.rows();
    207       int expectedReturn = 21;
    208       assertEquals( "return value", expectedReturn, actualReturn );
    209    }
    210
    211    public final void testFilterFractionMaxRemoveNegHigh() {
    212       f.setHighCut( 0.3, true );
    213       f.setRemoveAllNegative( true );
    214       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    215             .filter( testdata );
    216       int actualReturn = filtered.rows();
    217       int expectedReturn = 19;
    218       assertEquals( "return value", expectedReturn, actualReturn );
    219    }
    220
    221    public final void testFilterFractionMaxRemoveHighAndLow() {
    222       f.setHighCut( 0.3, true );
    223       f.setLowCut( 100, false );
    224       f.setRemoveAllNegative( true );
    225       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    226             .filter( testdata );
    227       int actualReturn = filtered.rows();
    228       int expectedReturn = 15;
    229       assertEquals( "return value", expectedReturn, actualReturn );
    230    }
    231
    232    public final void testFilterFractionMaxRemoveHighAndLowBothFraction() {
    233       f.setHighCut( 0.3, true );
    234       f.setLowCut( 0.1, true );
    235       f.setRemoveAllNegative( true );
    236       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    237             .filter( testdata );
    238       int actualReturn = filtered.rows();
    239       int expectedReturn = 14; // 3 neg, 0.1*27 = 3, 0.3*27 = 9, inclusive at
    240       // top means 10 + 3 + 3 = 16, from 30 is 14.
    241       assertEquals( "return value", expectedReturn, actualReturn );
    242    }
    243
    244    public final void testFilterFractionMaxRemoveHighAndLowBothLevels() {
    245       f.setHighCut( 1000, false );
    246       f.setLowCut( 100, false );
    247       f.setRemoveAllNegative( true ); // irrelevant
    248       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    249             .filter( testdata );
    250       int actualReturn = filtered.rows();
    251       int expectedReturn = 12;
    252       assertEquals( "return value", expectedReturn, actualReturn );
    253    }
    254
    255    public final void testFilterFractionMaxRemoveHighAndLowBothLevelsNoNegRemove() {
    256       f.setHighCut( 1000, false );
    257       f.setLowCut( 100, false );
    258       f.setRemoveAllNegative( false );
    259       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    260             .filter( testdata );
    261       int actualReturn = filtered.rows();
    262       int expectedReturn = 12;
    263       assertEquals( "return value", expectedReturn, actualReturn );
    264    }
    265
    266    public final void testFilterFractionMaxRemoveHighAndLowBothLevelsNegLow() {
    267       f.setHighCut( 1000, false );
    268       f.setLowCut( -100, false );
    269       f.setRemoveAllNegative( true );
    270       DenseDoubleMatrix2DNamed filtered = ( DenseDoubleMatrix2DNamed ) f
    271             .filter( testdata );
    272       int actualReturn = filtered.rows();
    273       int expectedReturn = 14;
    274       assertEquals( "return value", expectedReturn, actualReturn );
    275    }
    276
    277 }