1 /*
2  * This file is part of ELKI:
3  * Environment for Developing KDD-Applications Supported by Index-Structures
4  *
5  * Copyright (C) 2018
6  * ELKI Development Team
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 package de.lmu.ifi.dbs.elki.datasource.filter.normalization.instancewise;
22 
23 import static org.junit.Assert.assertEquals;
24 
25 import org.junit.Test;
26 
27 import de.lmu.ifi.dbs.elki.data.DoubleVector;
28 import de.lmu.ifi.dbs.elki.data.type.TypeUtil;
29 import de.lmu.ifi.dbs.elki.datasource.AbstractDataSourceTest;
30 import de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle;
31 import de.lmu.ifi.dbs.elki.math.MeanVariance;
32 import de.lmu.ifi.dbs.elki.utilities.ELKIBuilder;
33 
34 /**
35  * Test the mean-variance normalization filter.
36  *
37  * @author Matthew Arcifa
38  */
39 public class InstanceMeanVarianceNormalizationTest extends AbstractDataSourceTest {
40   /**
41    * Test with default parameters.
42    */
43   @Test
defaultParameters()44   public void defaultParameters() {
45     String filename = UNITTEST + "normalization-test-1.csv";
46     InstanceMeanVarianceNormalization<DoubleVector> filter = new ELKIBuilder<>(InstanceMeanVarianceNormalization.class).build();
47     MultipleObjectsBundle bundle = readBundle(filename, filter);
48     int dim = getFieldDimensionality(bundle, 0, TypeUtil.NUMBER_VECTOR_FIELD);
49 
50     // Verify that the resulting data has mean 0 and variance 1 in each row.
51     MeanVariance mvs = new MeanVariance();
52     for(int row = 0; row < bundle.dataLength(); row++) {
53       mvs.reset();
54       DoubleVector d = get(bundle, row, 0, DoubleVector.class);
55       for(int col = 0; col < dim; col++) {
56         final double v = d.doubleValue(col);
57         if(v > Double.NEGATIVE_INFINITY && v < Double.POSITIVE_INFINITY) {
58           mvs.put(v);
59         }
60       }
61       assertEquals("Mean is not 0", 0., mvs.getMean(), 1e-14);
62       assertEquals("Variance is not 1", 1., mvs.getNaiveVariance(), 1e-14);
63     }
64   }
65 }
66