/*
 * This file is part of ELKI:
 * Environment for Developing KDD-Applications Supported by Index-Structures
 *
 * Copyright (C) 2022
 * 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 <http://www.gnu.org/licenses/>.
 */
package elki.datasource.filter.transform;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import elki.data.DoubleVector;
import elki.data.type.TypeUtil;
import elki.datasource.AbstractDataSourceTest;
import elki.datasource.bundle.MultipleObjectsBundle;
import elki.distance.minkowski.EuclideanDistance;
import elki.math.linearalgebra.CovarianceMatrix;
import elki.math.linearalgebra.SingularValueDecomposition;
import elki.utilities.ELKIBuilder;

/**
 * Test the fast MDS transformation filter.
 *
 * @author Matthew Arcifa
 * @since 0.7.5
 */
public class FastMultidimensionalScalingTransformTest extends AbstractDataSourceTest {
  /**
   * Test with parameters.
   */
  @Test
  public void parameters() {
    int pdim = 2;
    String filename = UNITTEST + "transformation-test-1.csv";
    FastMultidimensionalScalingTransform<DoubleVector, DoubleVector> filter = new ELKIBuilder<FastMultidimensionalScalingTransform<DoubleVector, DoubleVector>>(FastMultidimensionalScalingTransform.class) //
        .with(ClassicMultidimensionalScalingTransform.Par.DIM_ID, pdim) //
        .with(FastMultidimensionalScalingTransform.Par.RANDOM_ID, 0L) //
        .with(ClassicMultidimensionalScalingTransform.Par.DISTANCE_ID, EuclideanDistance.class) //
        .build();
    MultipleObjectsBundle filteredBundle = readBundle(filename, filter);
    // Load the test data again without a filter.
    MultipleObjectsBundle unfilteredBundle = readBundle(filename);
    int dimu = getFieldDimensionality(unfilteredBundle, 0, TypeUtil.NUMBER_VECTOR_FIELD);
    int dimf = getFieldDimensionality(filteredBundle, 0, TypeUtil.NUMBER_VECTOR_FIELD);
    assertEquals("Dimensionality not as requested", pdim, dimf);

    // Verify that the Euclidean distance between any two points is identical
    // before and after the MDS transform is performed - O(n^2)!

    // Calculate the covariance matricies of the filtered and unfiltered
    // bundles.
    CovarianceMatrix cmUnfil = new CovarianceMatrix(dimu);
    CovarianceMatrix cmFil = new CovarianceMatrix(dimf);

    for(int outer = 0; outer < filteredBundle.dataLength(); outer++) {
      DoubleVector dFil_1 = get(filteredBundle, outer, 0, DoubleVector.class);
      DoubleVector dUnfil_1 = get(unfilteredBundle, outer, 0, DoubleVector.class);
      cmUnfil.put(dUnfil_1);
      cmFil.put(dFil_1);

      for(int row = outer + 1; row < filteredBundle.dataLength(); row++) {
        DoubleVector dFil_2 = get(filteredBundle, row, 0, DoubleVector.class);
        DoubleVector dUnfil_2 = get(unfilteredBundle, row, 0, DoubleVector.class);
        final double distF = EuclideanDistance.STATIC.distance(dFil_1, dFil_2);
        final double distU = EuclideanDistance.STATIC.distance(dUnfil_1, dUnfil_2);
        assertEquals("Expected same distance", distU, distF, 1e-9);
      }
    }

    // Calculate the SVD of the covariance matrix of the unfiltered data.
    // Verify that this SVD represents the diagonals of the covariance matrix of
    // the filtered data.

    double[][] ncmFil = cmFil.destroyToPopulationMatrix();
    double[] dia = new SingularValueDecomposition(cmUnfil.destroyToPopulationMatrix()).getSingularValues();
    for(int ii = 0; ii < dia.length; ii++) {
      assertEquals("Unexpected covariance", dia[ii], ncmFil[ii][ii], 1e-7);
    }
  }
}
