at.tuwien.ifs.somtoolbox.layers.metrics
Class AbstractMetric

java.lang.Object
  extended by at.tuwien.ifs.somtoolbox.layers.metrics.AbstractMetric
All Implemented Interfaces:
DistanceMetric, Comparable<DistanceMetric>
Direct Known Subclasses:
AbstractWeightedMetric, CosineMetric, L1Metric, L2Metric, L2MetricSparse, LInfinityMetric, LnAlphaMetric, LnMetric, MahalanobisMetric, MissingValueMetricWrapper

public abstract class AbstractMetric
extends Object
implements DistanceMetric

Implements a static method for metric instantiation and a method for mean vector calculation. Furthermore, implements the convenience methods to call the abstract method having two double arrays as arguments (see distance(double[], double[])). This method has to be implemented by classes actually implementing a certain metric.

Version:
$Id: AbstractMetric.java 3883 2010-11-02 17:13:23Z frank $
Author:
Michael Dittenbach, Rudolf Mayer

Nested Class Summary
 
Nested classes/interfaces inherited from interface at.tuwien.ifs.somtoolbox.layers.metrics.DistanceMetric
DistanceMetric.SparcseMetricModes
 
Field Summary
static String PACKAGE_NAME
           
 
Constructor Summary
AbstractMetric()
           
 
Method Summary
protected  void checkDimensions(double[] vector1, double[] vector2)
          Performs a check on wether the given vectors have the same dimension.
protected  void checkDimensions(cern.colt.matrix.DoubleMatrix1D vector1, cern.colt.matrix.DoubleMatrix1D vector2)
          Performs a check on wether the given vectors have the same dimension.
 int compareTo(DistanceMetric o)
           
abstract  double distance(double[] vector1, double[] vector2)
          Calculates the distance between two vectors provided by argument vector1 and vector2.
 double distance(double[] vector1, cern.colt.matrix.DoubleMatrix1D vector2)
          Calculates the distance between two vectors provided by argument vector1 and vector2.
 double distance(double[] vector, InputDatum data)
          Calculates the distance between two vectors provided by argument vector and datum.
 double distance(cern.colt.matrix.DoubleMatrix1D vector1, double[] vector2)
          Calculates the distance between two vectors provided by argument vector1 and vector2.
 double distance(cern.colt.matrix.DoubleMatrix1D vector1, cern.colt.matrix.DoubleMatrix1D vector2)
          Calculates the distance between two vectors provided by argument vector1 and vector2.
 double distance(cern.colt.matrix.DoubleMatrix1D vector, InputDatum datum)
          Calculates the distance between two vectors provided by argument vector and datum.
 double distance(InputDatum data, double[] vector)
          Calculates the distance between two vectors provided by argument datum and vector.
 double distance(InputDatum datum, cern.colt.matrix.DoubleMatrix1D vector)
          Calculates the distance between two vectors provided by argument datum and vector.
 double distance(InputDatum datum, InputDatum datum2)
          Calculates the distance between two vectors provided by argument datum and datum2.
static DistanceMetric instantiate(String mName)
          Instantiates a certain distance metric class specified by argument mName.
Note: for backwards compatibility, if the metric name contains the package prefix at.ec3.somtoolbox, this will be replaced by at.tuwien.ifs.somtoolbox.
static DistanceMetric instantiateNice(String metricName)
          Same as instantiate(String), but tries to get the metric with the specified name, and then with the package prefix, and throwing only a SOMToolboxException with the root cause nested.
static double[] meanVector(double[] vector1, double[] vector2)
          Calculates the mean vector of two double array vectors.
protected static void performanceTest(DistanceMetric metric, int dim)
          Can be used to do some performance testing to compare colt vs.
 void setMetricParams(String metricParamString)
          Empty implementation, subclasses needing to set parameters have to override this class.
 double transformValue(double value)
           
 double[] transformVector(double[] vector)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PACKAGE_NAME

public static final String PACKAGE_NAME
Constructor Detail

AbstractMetric

public AbstractMetric()
Method Detail

instantiate

public static DistanceMetric instantiate(String mName)
                                  throws ClassNotFoundException,
                                         InstantiationException,
                                         IllegalAccessException
Instantiates a certain distance metric class specified by argument mName.
Note: for backwards compatibility, if the metric name contains the package prefix at.ec3.somtoolbox, this will be replaced by at.tuwien.ifs.somtoolbox.

Parameters:
mName - the name of the metric.
Returns:
a distance metric object of class mName.
Throws:
ClassNotFoundException - if class denoted by argument mName is not found.
InstantiationException - if if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
IllegalAccessException - if the class or its nullary constructor is not accessible.

instantiateNice

public static DistanceMetric instantiateNice(String metricName)
                                      throws SOMToolboxException
Same as instantiate(String), but tries to get the metric with the specified name, and then with the package prefix, and throwing only a SOMToolboxException with the root cause nested.

Throws:
SOMToolboxException

meanVector

public static double[] meanVector(double[] vector1,
                                  double[] vector2)
                           throws MetricException
Calculates the mean vector of two double array vectors.

Parameters:
vector1 - first vector.
vector2 - second vector.
Returns:
the mean vector.
Throws:
MetricException - if the dimensionalities of the two vectors differ.

performanceTest

protected static void performanceTest(DistanceMetric metric,
                                      int dim)
Can be used to do some performance testing to compare colt vs. direct distance implementations.


distance

public abstract double distance(double[] vector1,
                                double[] vector2)
                         throws MetricException
Description copied from interface: DistanceMetric
Calculates the distance between two vectors provided by argument vector1 and vector2. A MetricException is thrown, if the dimensionalities of the two vectors differ.

Specified by:
distance in interface DistanceMetric
Parameters:
vector1 - first vector.
vector2 - second vector.
Returns:
the distance between vector1 and vector2.
Throws:
MetricException - if the dimensionalities of the two vectors differ.
See Also:
DistanceMetric.distance(double[], double[])

distance

public double distance(double[] vector1,
                       cern.colt.matrix.DoubleMatrix1D vector2)
                throws MetricException
Description copied from interface: DistanceMetric
Calculates the distance between two vectors provided by argument vector1 and vector2. A MetricException is thrown, if the dimensionalities of the two vectors differ.

Specified by:
distance in interface DistanceMetric
Parameters:
vector1 - first vector.
vector2 - second vector.
Returns:
the distance between vector1 and vector2.
Throws:
MetricException - if the dimensionalities of the two vectors differ.
See Also:
DistanceMetric.distance(double[], cern.colt.matrix.DoubleMatrix1D)

distance

public double distance(double[] vector,
                       InputDatum data)
                throws MetricException
Description copied from interface: DistanceMetric
Calculates the distance between two vectors provided by argument vector and datum. A MetricException is thrown, if the dimensionalities of the two vectors differ.

Specified by:
distance in interface DistanceMetric
Parameters:
vector - first vector.
data - input datum.
Returns:
the distance between vector and datum.
Throws:
MetricException - if the dimensionalities of the two vectors differ.
See Also:
DistanceMetric.distance(double[], at.tuwien.ifs.somtoolbox.data.InputDatum)

distance

public double distance(cern.colt.matrix.DoubleMatrix1D vector1,
                       double[] vector2)
                throws MetricException
Description copied from interface: DistanceMetric
Calculates the distance between two vectors provided by argument vector1 and vector2. A MetricException is thrown, if the dimensionalities of the two vectors differ.

Specified by:
distance in interface DistanceMetric
Parameters:
vector1 - first vector.
vector2 - second vector.
Returns:
the distance between vector1 and vector2.
Throws:
MetricException - if the dimensionalities of the two vectors differ.
See Also:
DistanceMetric.distance(cern.colt.matrix.DoubleMatrix1D, double[])

distance

public double distance(cern.colt.matrix.DoubleMatrix1D vector1,
                       cern.colt.matrix.DoubleMatrix1D vector2)
                throws MetricException
Description copied from interface: DistanceMetric
Calculates the distance between two vectors provided by argument vector1 and vector2. A MetricException is thrown, if the dimensionalities of the two vectors differ.

Specified by:
distance in interface DistanceMetric
Parameters:
vector1 - first vector.
vector2 - second vector.
Returns:
the distance between vector1 and vector2.
Throws:
MetricException - if the dimensionalities of the two vectors differ.
See Also:
DistanceMetric.distance(cern.colt.matrix.DoubleMatrix1D, cern.colt.matrix.DoubleMatrix1D)

distance

public double distance(cern.colt.matrix.DoubleMatrix1D vector,
                       InputDatum datum)
                throws MetricException
Description copied from interface: DistanceMetric
Calculates the distance between two vectors provided by argument vector and datum. A MetricException is thrown, if the dimensionalities of the two vectors differ.

Specified by:
distance in interface DistanceMetric
Parameters:
vector - first vector.
datum - input datum.
Returns:
the distance between vector and datum.
Throws:
MetricException - if the dimensionalities of the two vectors differ.
See Also:
DistanceMetric.distance(cern.colt.matrix.DoubleMatrix1D, at.tuwien.ifs.somtoolbox.data.InputDatum)

distance

public double distance(InputDatum data,
                       double[] vector)
                throws MetricException
Description copied from interface: DistanceMetric
Calculates the distance between two vectors provided by argument datum and vector. A MetricException is thrown, if the dimensionalities of the two vectors differ.

Specified by:
distance in interface DistanceMetric
Parameters:
data - input datum.
vector - first vector.
Returns:
the distance between datum and vector.
Throws:
MetricException - if the dimensionalities of the two vectors differ.
See Also:
DistanceMetric.distance(at.tuwien.ifs.somtoolbox.data.InputDatum, double[])

distance

public double distance(InputDatum datum,
                       cern.colt.matrix.DoubleMatrix1D vector)
                throws MetricException
Description copied from interface: DistanceMetric
Calculates the distance between two vectors provided by argument datum and vector. A MetricException is thrown, if the dimensionalities of the two vectors differ.

Specified by:
distance in interface DistanceMetric
Parameters:
datum - input datum.
vector - first vector.
Returns:
the distance between datum and vector.
Throws:
MetricException - if the dimensionalities of the two vectors differ.
See Also:
DistanceMetric.distance(at.tuwien.ifs.somtoolbox.data.InputDatum, cern.colt.matrix.DoubleMatrix1D)

distance

public double distance(InputDatum datum,
                       InputDatum datum2)
                throws MetricException
Description copied from interface: DistanceMetric
Calculates the distance between two vectors provided by argument datum and datum2. A MetricException is thrown, if the dimensionalities of the two vectors differ.

Specified by:
distance in interface DistanceMetric
Parameters:
datum - first input datum.
datum2 - second input datum.
Returns:
the distance between datum and vector.
Throws:
MetricException - if the dimensionalities of the two vectors differ.
See Also:
DistanceMetric.distance(at.tuwien.ifs.somtoolbox.data.InputDatum, at.tuwien.ifs.somtoolbox.data.InputDatum)

checkDimensions

protected void checkDimensions(double[] vector1,
                               double[] vector2)
                        throws MetricException
Performs a check on wether the given vectors have the same dimension.

Throws:
MetricException - If the given vectors have different dimensions.

checkDimensions

protected void checkDimensions(cern.colt.matrix.DoubleMatrix1D vector1,
                               cern.colt.matrix.DoubleMatrix1D vector2)
                        throws MetricException
Performs a check on wether the given vectors have the same dimension.

Throws:
MetricException - If the given vectors have different dimensions.

transformValue

public double transformValue(double value)
Specified by:
transformValue in interface DistanceMetric

transformVector

public double[] transformVector(double[] vector)
Specified by:
transformVector in interface DistanceMetric

setMetricParams

public void setMetricParams(String metricParamString)
                     throws SOMToolboxException
Empty implementation, subclasses needing to set parameters have to override this class.

Specified by:
setMetricParams in interface DistanceMetric
Throws:
SOMToolboxException

compareTo

public int compareTo(DistanceMetric o)
Specified by:
compareTo in interface Comparable<DistanceMetric>