Skip to content

Commit

Permalink
Add PCA as new dimensionality reduction method to DimensionalityReduc…
Browse files Browse the repository at this point in the history
…tionView
  • Loading branch information
stefanhahmann committed Dec 4, 2024
1 parent a7141a9 commit 1e99b2f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -31,7 +31,8 @@
public enum DimensionalityReductionAlgorithm
{
UMAP( "UMAP", "Uniform Manifold Approximation and Projection for Dimension Reduction." ),
TSNE( "t-SNE", "t-distributed Stochastic Neighbor Embedding." );
TSNE( "t-SNE", "t-distributed Stochastic Neighbor Embedding." ),
PCA( "PCA", "Principal Component Analysis." ),;

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -37,13 +37,16 @@
import org.mastodon.graph.Edge;
import org.mastodon.graph.ReadOnlyGraph;
import org.mastodon.graph.Vertex;
import org.mastodon.mamut.feature.branch.dimensionalityreduction.pca.BranchPcaFeatureComputer;
import org.mastodon.mamut.feature.branch.dimensionalityreduction.tsne.BranchTSneFeatureComputer;
import org.mastodon.mamut.feature.branch.dimensionalityreduction.umap.BranchUmapFeatureComputer;
import org.mastodon.mamut.feature.dimensionalityreduction.pca.AbstractPcaFeatureComputer;
import org.mastodon.mamut.feature.dimensionalityreduction.tsne.TSneSettings;
import org.mastodon.mamut.feature.dimensionalityreduction.tsne.feature.AbstractTSneFeatureComputer;
import org.mastodon.mamut.feature.dimensionalityreduction.umap.UmapSettings;
import org.mastodon.mamut.feature.dimensionalityreduction.umap.feature.AbstractUmapFeatureComputer;
import org.mastodon.mamut.feature.dimensionalityreduction.util.InputDimension;
import org.mastodon.mamut.feature.spot.dimensionalityreduction.pca.SpotPcaFeatureComputer;
import org.mastodon.mamut.feature.spot.dimensionalityreduction.tsne.SpotTSneFeatureComputer;
import org.mastodon.mamut.feature.spot.dimensionalityreduction.umap.SpotUmapFeatureComputer;
import org.mastodon.mamut.model.Link;
Expand Down Expand Up @@ -210,6 +213,12 @@ public void setAlgorithm( final DimensionalityReductionAlgorithm algorithm )
throw new ArrayIndexOutOfBoundsException( "Not enough data for t-SNE computation." );
}
break;
case PCA:
AbstractPcaFeatureComputer< V, E, G > pcaFeatureComputer =
isModelGraph ? Cast.unchecked( new SpotPcaFeatureComputer( model, context ) )
: Cast.unchecked( new BranchPcaFeatureComputer( model, context ) );
pcaFeatureComputer.computeFeature( commonSettings, inputDimensions, graph );
break;
default:
throw new IllegalArgumentException( "Unknown algorithm: " + algorithm );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand Down Expand Up @@ -89,6 +89,8 @@ public class DimensionalityReductionView extends JFrame

private final JRadioButton tsneRadioButton;

private final JRadioButton pcaRadioButton;

private final JCheckBox standardizeFeaturesCheckBox;

private final JSpinner numberOfDimensionsInput;
Expand Down Expand Up @@ -133,6 +135,7 @@ public DimensionalityReductionView( final Model model, final Context context )

umapRadioButton = new JRadioButton( "UMAP" );
tsneRadioButton = new JRadioButton( "t-SNE" );
pcaRadioButton = new JRadioButton( "PCA" );

// Common settings
standardizeFeaturesCheckBox = new JCheckBox( "Standardize features" );
Expand Down Expand Up @@ -166,6 +169,7 @@ private void initSettings()
DimensionalityReductionAlgorithm algorithm = controller.getAlgorithm();
umapRadioButton.setSelected( algorithm == DimensionalityReductionAlgorithm.UMAP );
tsneRadioButton.setSelected( algorithm == DimensionalityReductionAlgorithm.TSNE );
pcaRadioButton.setSelected( algorithm == DimensionalityReductionAlgorithm.PCA );
CommonSettings settings = controller.getCommonSettings();
UmapSettings umapSettings = controller.getUmapSettings();
TSneSettings tSneSettings = controller.getTSneSettings();
Expand All @@ -192,10 +196,12 @@ private void initBehavior()

umapRadioButton.addActionListener( e -> updateAlgorithmSettings() );
tsneRadioButton.addActionListener( e -> updateAlgorithmSettings() );
pcaRadioButton.addActionListener( e -> updateAlgorithmSettings() );

ButtonGroup algorithmGroup = new ButtonGroup();
algorithmGroup.add( umapRadioButton );
algorithmGroup.add( tsneRadioButton );
algorithmGroup.add( pcaRadioButton );

CommonSettings commonSettings = controller.getCommonSettings();
UmapSettings umapSettings = controller.getUmapSettings();
Expand Down Expand Up @@ -224,12 +230,13 @@ private void initLayout()
{
add( canvas, BorderLayout.CENTER );

canvas.add( new JLabel( "Graph type:" ), "split 3" );
canvas.add( new JLabel( "Graph type:" ), "split 4" );
canvas.add( modelGraphRadioButton );
canvas.add( branchGraphRadioButton, "wrap" );
canvas.add( new JLabel( "Algorithm:" ), "split 3" );
canvas.add( new JLabel( "Algorithm:" ), "split 4" );
canvas.add( umapRadioButton );
canvas.add( tsneRadioButton, "wrap" );
canvas.add( tsneRadioButton );
canvas.add( pcaRadioButton, "wrap" );
canvas.add( standardizeFeaturesCheckBox, "wrap" );
canvas.add( new JLabel( "Number of dimensions:" ), SPLIT_2 );
canvas.add( numberOfDimensionsInput, WMIN_35_WRAP );
Expand Down Expand Up @@ -280,8 +287,10 @@ private void updateAlgorithmSettings()
DimensionalityReductionAlgorithm algorithm;
if ( umapRadioButton.isSelected() )
algorithm = DimensionalityReductionAlgorithm.UMAP;
else
else if ( tsneRadioButton.isSelected() )
algorithm = DimensionalityReductionAlgorithm.TSNE;
else
algorithm = DimensionalityReductionAlgorithm.PCA;
controller.setAlgorithm( algorithm );
addAlgorithmSpecificSettings( algorithm );
revalidate();
Expand All @@ -305,6 +314,10 @@ private void addAlgorithmSpecificSettings( final DimensionalityReductionAlgorith
algorithmSpecificSettingsPanel.add( new JLabel( "Maximum number of iterations:" ), SPLIT_2 );
algorithmSpecificSettingsPanel.add( maxIterationsInput, WMIN_35_WRAP );
break;
case PCA:
algorithmSpecificSettingsPanel.add( new JLabel( "" ), "wrap" );
algorithmSpecificSettingsPanel.add( new JLabel( "" ), "wrap" );
break;
default:
break;
}
Expand Down

0 comments on commit 1e99b2f

Please sign in to comment.