-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new unit test PCATest using smile machine learning library
- Loading branch information
1 parent
791e6d2
commit 6883399
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/test/java/org/mastodon/mamut/feature/dimensionalityreduction/pca/PCATest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
|
||
} | ||
} |