Skip to content

Commit

Permalink
Add new unit test PCATest using smile machine learning library
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhahmann committed Nov 27, 2024
1 parent 791e6d2 commit 6883399
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.mastodon.mamut.feature.dimensionalityreduction.pca;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.lang.invoke.MethodHandles;

import org.junit.jupiter.api.Test;
import org.mastodon.mamut.feature.dimensionalityreduction.RandomDataTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import smile.data.DataFrame;
import smile.feature.extraction.PCA;

class PCATest
{
private static final Logger logger = LoggerFactory.getLogger( MethodHandles.lookup().lookupClass() );

@Test
void test()
{
int numCluster1 = 50;
int numCluster2 = 100;
double[][] inputData = RandomDataTools.generateSampleData( numCluster1, numCluster2 );
logger.debug( "dimensions rows: {}, columns:{}", inputData.length, inputData[ 0 ].length );

int targetDimensions = 2;

DataFrame dataFrame = DataFrame.of( inputData );
PCA pca = PCA.fit( dataFrame ).getProjection( targetDimensions );
double[][] pcaResult = pca.apply( inputData );

assertEquals( pcaResult.length, inputData.length );
assertEquals( targetDimensions, pcaResult[ 0 ].length );

for ( int i = 0; i < numCluster1; i++ )
assertTrue( pcaResult[ i ][ 0 ] < 0 );
for ( int i = numCluster1; i < numCluster1 + numCluster2; i++ )
assertTrue( pcaResult[ i ][ 0 ] > 0 );

}
}

0 comments on commit 6883399

Please sign in to comment.