/* * This file is part of ELKI: * Environment for Developing KDD-Applications Supported by Index-Structures * * Copyright (C) 2018 * ELKI Development Team * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package de.lmu.ifi.dbs.elki.datasource.filter.normalization.instancewise; import static org.junit.Assert.assertEquals; import org.junit.Test; import de.lmu.ifi.dbs.elki.data.DoubleVector; import de.lmu.ifi.dbs.elki.data.type.TypeUtil; import de.lmu.ifi.dbs.elki.datasource.AbstractDataSourceTest; import de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle; import de.lmu.ifi.dbs.elki.math.MeanVariance; import de.lmu.ifi.dbs.elki.utilities.ELKIBuilder; /** * Test the mean-variance normalization filter. * * @author Matthew Arcifa */ public class InstanceMeanVarianceNormalizationTest extends AbstractDataSourceTest { /** * Test with default parameters. */ @Test public void defaultParameters() { String filename = UNITTEST + "normalization-test-1.csv"; InstanceMeanVarianceNormalization filter = new ELKIBuilder<>(InstanceMeanVarianceNormalization.class).build(); MultipleObjectsBundle bundle = readBundle(filename, filter); int dim = getFieldDimensionality(bundle, 0, TypeUtil.NUMBER_VECTOR_FIELD); // Verify that the resulting data has mean 0 and variance 1 in each row. MeanVariance mvs = new MeanVariance(); for(int row = 0; row < bundle.dataLength(); row++) { mvs.reset(); DoubleVector d = get(bundle, row, 0, DoubleVector.class); for(int col = 0; col < dim; col++) { final double v = d.doubleValue(col); if(v > Double.NEGATIVE_INFINITY && v < Double.POSITIVE_INFINITY) { mvs.put(v); } } assertEquals("Mean is not 0", 0., mvs.getMean(), 1e-14); assertEquals("Variance is not 1", 1., mvs.getNaiveVariance(), 1e-14); } } }