java.lang.Object
de.unijena.cheminf.clustering.art2a.Utils

public class Utils extends Object
Library of helper records, static helper classes and static, thread-safe (stateless) utility methods for ART-2a and ART-2a-Euclid clustering.

Note: No checks are performed.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static class 
    Helper class: Cluster removal info.
    protected static final record 
    Helper record: Minimum and maximum value.
    protected static class 
    Helper class: Rho winner.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructor
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static void
    copyMatrix(float[][] aSourceMatrix, float[][] aDestinationMatrix)
    (Deep) Copies source matrix to destination matrix.
    protected static void
    copyRows(float[][] aSourceMatrix, float[][] aDestinationMatrix, int aNumberOfRows)
    (Deep) Copies specified number of rows of source matrix to destination matrix.
    protected static void
    copyVector(float[] aSourceVector, float[] aDestinationVector)
    (Deep) Copies source vector to destination vector.
    protected static void
    enhanceContrast(float[] aVector, float aThresholdForContrastEnhancement)
    Calculates contrast enhanced vector.
    protected static void
    fillMatrix(float[][] aMatrix, float aValue)
    Fills matrix with value.
    protected static void
    fillVector(boolean[] aVector, boolean aValue)
    Fills vector with value.
    protected static void
    fillVector(float[] aVector, float aValue)
    Fills vector with value.
    protected static void
    fillVector(int[] aVector, int aValue)
    Fills vector with value.
    protected static float
    getMeanDistance(float[][] aMatrix, int[] anIndicesOfRowVectors)
    Returns mean distance of all specified row vectors.
    protected static Utils.MinMaxValue[]
    getMinMaxComponents(float[][] aMatrix)
    Returns min-max components for matrix where MinMaxValue[j] corresponds to column j of the row vectors of the matrix.
    protected static float
    getScalarProduct(float[] aVector1, float[] aVector2)
    Calculates the scalar product (dot product) of aVector1 and aVector2.
    protected static float[]
    getScaledVector(float[] aVectorToBeScaled)
    Scales components of aVectorToBeScaled to interval [0,1].
    protected static float
    getSquaredDistance(float[] aVector1, float[] aVector2)
    Calculates the squared distance between aVector1 and aVector2.
    protected static float
    getSumOfComponents(float[] aVector)
    Calculates the sum of components of aVector.
    protected static float
    getSumOfSquaredDifferences(float[] aVector, float aValue)
    Calculates the sum of squared differences between the components of the specified vector and a value.
    protected static float
    getThresholdForContrastEnhancement(int aNumberOfComponents, float anOffsetForContrastEnhancement)
    Threshold for contrast enhancement
    protected static int[][]
    Creates clustering-based training and test data vector indices that cover a similar space.
    protected static float
    getVectorLength(float[] aVector)
    Calculates the length of aVector.
    protected static boolean
    hasLengthOfZero(float[] aVector)
    Checks if vector has a length of zero (i.e.
    protected static boolean
    hasNonFiniteComponent(float[][] aDataMatrix)
    Checks if data matrix has a non-finite component.
    protected static boolean
    isContrastEnhanced(float[] aVector, float aThresholdForContrastEnhancement)
    Calculates contrast enhanced vector.
    static boolean
    isDataMatrixValid(float[][] aDataMatrix)
    Checks if aDataMatrix is valid.
    protected static boolean
    isMatrixValid(float[][] aMatrix)
    Checks if matrix is valid.
    static boolean
    isNonFiniteComponentRemoval(float[][] aDataMatrix)
    Removes columns from data matrix with non-finite components.
    protected static void
    normalizeVector(float[] aVector)
    Calculates normalized (unit) vector of length 1.
    protected static void
    removeEmptyClusters(boolean[] aClusterUsageFlags, float[][] aClusterMatrix, int aNumberOfDetectedClusters, Utils.ClusterRemovalInfo aClusterRemovalInfo)
    Removes empty clusters from cluster matrix
    protected static void
    scaleVector(float[] aVectorToBeScaled, Utils.MinMaxValue[] aMinMaxComponents)
    Scales components of aVectorToBeScaled according to min-max components to interval [0,1] (see code and method getMinMaxComponents()).
    protected static void
    setContrastEnhancement(float[] aVector, float aThresholdForContrastEnhancement)
    Calculates contrast enhanced vector.
    protected static void
    setRowVector(float[][] aMatrix, float[] aRowVector, int anIndex)
    Sets copied (!) row vector at index in matrix.
    protected static void
    shuffleIndices(int[] anIndexArray, Random aRandomNumberGenerator)
    Randomly shuffles indices from 0 to (anIndices.Length - 1) in anIndexArray using Fisher-Yates shuffling (i.e.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Utils

      protected Utils()
      Constructor
  • Method Details

    • isDataMatrixValid

      public static boolean isDataMatrixValid(float[][] aDataMatrix)
      Checks if aDataMatrix is valid.
      Parameters:
      aDataMatrix - Data matrix with data row vectors (IS NOT CHANGED)
      Returns:
      True if aDataMatrix is valid, false otherwise.
    • isNonFiniteComponentRemoval

      public static boolean isNonFiniteComponentRemoval(float[][] aDataMatrix)
      Removes columns from data matrix with non-finite components. Note: If aDataMatrix is null, empty or has an invalid structure nothing is done and false is returned.
      Parameters:
      aDataMatrix - Data matrix with data row vectors (MAY BE CHANGED)
      Returns:
      True if aDataMatrix was changed (i.e. column removal was performed), false otherwise (i.e. data matrix is unchanged).
    • copyMatrix

      protected static void copyMatrix(float[][] aSourceMatrix, float[][] aDestinationMatrix)
      (Deep) Copies source matrix to destination matrix. Row vectors of destination matrix may not have been instantiated.
      Parameters:
      aSourceMatrix - Source matrix (IS NOT CHANGED)
      aDestinationMatrix - Destination matrix (MUST HAVE BEEN INSTANTIATED and MAY BE CHANGED)
    • copyRows

      protected static void copyRows(float[][] aSourceMatrix, float[][] aDestinationMatrix, int aNumberOfRows)
      (Deep) Copies specified number of rows of source matrix to destination matrix. Row vectors of destination matrix may not have been instantiated.
      Parameters:
      aSourceMatrix - Source matrix (IS NOT CHANGED)
      aDestinationMatrix - Destination matrix (MUST HAVE BEEN INSTANTIATED and MAY BE CHANGED)
      aNumberOfRows - Number of rows to be copied from source matrix to destination matrix
    • copyVector

      protected static void copyVector(float[] aSourceVector, float[] aDestinationVector)
      (Deep) Copies source vector to destination vector.
      Parameters:
      aSourceVector - Source vector (IS NOT CHANGED)
      aDestinationVector - Destination vector (MUST HAVE BEEN INSTANTIATED and MAY BE CHANGED)
    • enhanceContrast

      protected static void enhanceContrast(float[] aVector, float aThresholdForContrastEnhancement)
      Calculates contrast enhanced vector.
      Parameters:
      aVector - Vector to be contrast enhanced (MAY BE CHANGED)
      aThresholdForContrastEnhancement - Threshold for contrast enhancement
    • fillMatrix

      protected static void fillMatrix(float[][] aMatrix, float aValue)
      Fills matrix with value.
      Parameters:
      aMatrix - Matrix (MAY BE CHANGED)
      aValue - Value
    • fillVector

      protected static void fillVector(float[] aVector, float aValue)
      Fills vector with value.
      Parameters:
      aVector - Vector (MAY BE CHANGED)
      aValue - Value
    • fillVector

      protected static void fillVector(boolean[] aVector, boolean aValue)
      Fills vector with value.
      Parameters:
      aVector - Vector (MAY BE CHANGED)
      aValue - Value
    • fillVector

      protected static void fillVector(int[] aVector, int aValue)
      Fills vector with value.
      Parameters:
      aVector - Vector (MAY BE CHANGED)
      aValue - Value
    • getMeanDistance

      protected static float getMeanDistance(float[][] aMatrix, int[] anIndicesOfRowVectors)
      Returns mean distance of all specified row vectors.
      Parameters:
      aMatrix - Matrix with row vectors (IS NOT CHANGED)
      anIndicesOfRowVectors - Indices of row vectors of aMatrix
      Returns:
      Mean squared distance of all specified row vectors.
    • getMinMaxComponents

      protected static Utils.MinMaxValue[] getMinMaxComponents(float[][] aMatrix)
      Returns min-max components for matrix where MinMaxValue[j] corresponds to column j of the row vectors of the matrix. The min-max components may be used to scale row vectors to interval [0,1], see method scaleVector().
      Parameters:
      aMatrix - Matrix (IS NOT CHANGED)
      Returns:
      Min-max components
    • getScalarProduct

      protected static float getScalarProduct(float[] aVector1, float[] aVector2)
      Calculates the scalar product (dot product) of aVector1 and aVector2.
      Parameters:
      aVector1 - Vector 1 (IS NOT CHANGED)
      aVector2 - Vector 2 (IS NOT CHANGED)
      Returns:
      Scalar product (dot product)
    • getScaledVector

      protected static float[] getScaledVector(float[] aVectorToBeScaled)
      Scales components of aVectorToBeScaled to interval [0,1].
      Parameters:
      aVectorToBeScaled - Vector (IS NOT CHANGED)
      Returns:
      New scaled vector with components in interval [0,1] or new vector of length zero if all components of aVectorToBeScaled are the same.
    • getSquaredDistance

      protected static float getSquaredDistance(float[] aVector1, float[] aVector2)
      Calculates the squared distance between aVector1 and aVector2.
      Parameters:
      aVector1 - Vector 1 (IS NOT CHANGED)
      aVector2 - Vector 2 (IS NOT CHANGED)
      Returns:
      Squared distance
    • getSumOfComponents

      protected static float getSumOfComponents(float[] aVector)
      Calculates the sum of components of aVector.
      Parameters:
      aVector - Vector (IS NOT CHANGED)
      Returns:
      Sum of components
    • getSumOfSquaredDifferences

      protected static float getSumOfSquaredDifferences(float[] aVector, float aValue)
      Calculates the sum of squared differences between the components of the specified vector and a value.
      Parameters:
      aVector - Vector (IS NOT CHANGED)
      aValue - Value
      Returns:
      Sum of squared differences between the components of the specified vector and a value.
    • getThresholdForContrastEnhancement

      protected static float getThresholdForContrastEnhancement(int aNumberOfComponents, float anOffsetForContrastEnhancement)
      Threshold for contrast enhancement
      Parameters:
      aNumberOfComponents - Number of components
      anOffsetForContrastEnhancement - Offset for contrast enhancement
      Returns:
      Threshold for contrast enhancement
    • getVectorLength

      protected static float getVectorLength(float[] aVector)
      Calculates the length of aVector.
      Parameters:
      aVector - Vector (IS NOT CHANGED)
      Returns:
      Length of vector
    • hasLengthOfZero

      protected static boolean hasLengthOfZero(float[] aVector)
      Checks if vector has a length of zero (i.e. if all components are equal to zero).
      Parameters:
      aVector - Vector (IS NOT CHANGED)
      Returns:
      True: Vector has a length of zero, false: Otherwise
    • hasNonFiniteComponent

      protected static boolean hasNonFiniteComponent(float[][] aDataMatrix)
      Checks if data matrix has a non-finite component. Note: If aDataMatrix is null or empty nothing is done and false is returned.
      Parameters:
      aDataMatrix - Data matrix with data row vectors (IS NOT CHANGED)
      Returns:
      True: Data matrix has non-finite component, false: Otherwise
    • isContrastEnhanced

      protected static boolean isContrastEnhanced(float[] aVector, float aThresholdForContrastEnhancement)
      Calculates contrast enhanced vector.
      Parameters:
      aVector - Vector to be contrast enhanced (MAY BE CHANGED)
      aThresholdForContrastEnhancement - Threshold for contrast enhancement
      Returns:
      True if aVector is changed by contrast enhancement, false otherwise.
    • isMatrixValid

      protected static boolean isMatrixValid(float[][] aMatrix)
      Checks if matrix is valid.
      Parameters:
      aMatrix - Matrix
      Returns:
      True: Matrix is valid, false: Otherwise
    • normalizeVector

      protected static void normalizeVector(float[] aVector)
      Calculates normalized (unit) vector of length 1.
      Parameters:
      aVector - Vector to be normalized (MAY BE CHANGED)
    • removeEmptyClusters

      protected static void removeEmptyClusters(boolean[] aClusterUsageFlags, float[][] aClusterMatrix, int aNumberOfDetectedClusters, Utils.ClusterRemovalInfo aClusterRemovalInfo)
      Removes empty clusters from cluster matrix
      Parameters:
      aClusterUsageFlags - Flags for cluster usage. True: Cluster is used, false: Cluster is empty and has to be removed (IS NOT CHANGED)
      aClusterMatrix - Cluster matrix (MAY BE CHANGED)
      aNumberOfDetectedClusters - Number of detected clusters
      aClusterRemovalInfo - Cluster removal info (is set according to the operations performed, IS CHANGED)
    • scaleVector

      protected static void scaleVector(float[] aVectorToBeScaled, Utils.MinMaxValue[] aMinMaxComponents)
      Scales components of aVectorToBeScaled according to min-max components to interval [0,1] (see code and method getMinMaxComponents()).
      Parameters:
      aVectorToBeScaled - Vector to be scaled (MAY BE CHANGED)
      aMinMaxComponents - Min-max components
    • setContrastEnhancement

      protected static void setContrastEnhancement(float[] aVector, float aThresholdForContrastEnhancement)
      Calculates contrast enhanced vector.
      Parameters:
      aVector - Vector to be contrast enhanced (MAY BE CHANGED)
      aThresholdForContrastEnhancement - Threshold for contrast enhancement
    • setRowVector

      protected static void setRowVector(float[][] aMatrix, float[] aRowVector, int anIndex)
      Sets copied (!) row vector at index in matrix.
      Parameters:
      aMatrix - Matrix (MAY BE CHANGED)
      aRowVector - Row vector (IS NOT CHANGED)
      anIndex - Index of row vector in matrix
    • shuffleIndices

      protected static void shuffleIndices(int[] anIndexArray, Random aRandomNumberGenerator)
      Randomly shuffles indices from 0 to (anIndices.Length - 1) in anIndexArray using Fisher-Yates shuffling (i.e. the modern version introduced by Richard Durstenfeld). Note: No checks are performed.
      Parameters:
      anIndexArray - Array with indices from 0 to (anIndices.Length - 1)
      aRandomNumberGenerator - Random number generator
    • getTrainingAndTestIndices

      protected static int[][] getTrainingAndTestIndices(Art2aResult anArt2aResult)
      Creates clustering-based training and test data vector indices that cover a similar space. The first data vector index of each cluster (which is most similar to the cluster center) is assigned for training, all remaining data vector indices for test. Returns a 2-dimensional jagged integer array where index 0 is the array of training data vector indices and index 1 is the array of test data vector indices. Note: No checks are performed.
      Parameters:
      anArt2aResult - ART-2a result (IS NOT CHANGED)
      Returns:
      2-dimensional jagged integer array where index 0 is the array of training data vector indices and index 1 is the array of test data vector indices.