-
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.
Merge pull request #120 from mastodon-sc/t-sne
Add T-SNE dimensionality reduction method
- Loading branch information
Showing
53 changed files
with
3,235 additions
and
1,185 deletions.
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
58 changes: 58 additions & 0 deletions
58
...rg/mastodon/mamut/feature/branch/dimensionalityreduction/BranchOutputSerializerTools.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,58 @@ | ||
package org.mastodon.mamut.feature.branch.dimensionalityreduction; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
import org.mastodon.io.FileIdToObjectMap; | ||
import org.mastodon.io.ObjectToFileIdMap; | ||
import org.mastodon.io.properties.DoublePropertyMapSerializer; | ||
import org.mastodon.mamut.feature.branch.BranchFeatureSerializer; | ||
import org.mastodon.mamut.feature.dimensionalityreduction.AbstractOutputFeature; | ||
import org.mastodon.mamut.model.ModelGraph; | ||
import org.mastodon.mamut.model.Spot; | ||
import org.mastodon.mamut.model.branch.BranchSpot; | ||
import org.mastodon.mamut.model.branch.ModelBranchGraph; | ||
import org.mastodon.properties.DoublePropertyMap; | ||
|
||
public abstract class BranchOutputSerializerTools | ||
{ | ||
|
||
private BranchOutputSerializerTools() | ||
{ | ||
// prevent instantiation | ||
} | ||
|
||
public static void serialize( final AbstractOutputFeature< BranchSpot > feature, final ObjectToFileIdMap< Spot > idmap, | ||
final ObjectOutputStream oos, final ModelBranchGraph branchGraph, final ModelGraph graph ) throws IOException | ||
{ | ||
oos.writeInt( feature.getOutputMaps().size() ); | ||
for ( DoublePropertyMap< BranchSpot > outputMap : feature.getOutputMaps() ) | ||
{ | ||
final DoublePropertyMap< Spot > spotMap = BranchFeatureSerializer.branchSpotMapToMap( outputMap, branchGraph, graph ); | ||
final DoublePropertyMapSerializer< Spot > propertyMapSerializer = new DoublePropertyMapSerializer<>( spotMap ); | ||
propertyMapSerializer.writePropertyMap( idmap, oos ); | ||
} | ||
} | ||
|
||
public static < T extends AbstractOutputFeature< BranchSpot > > T deserialize( final FileIdToObjectMap< Spot > idmap, | ||
final ObjectInputStream ois, final ModelBranchGraph branchGraph, final ModelGraph graph, | ||
final Function< List< DoublePropertyMap< BranchSpot > >, T > featureCreator ) throws ClassNotFoundException, IOException | ||
{ | ||
int numDimensions = ois.readInt(); | ||
List< DoublePropertyMap< BranchSpot > > outputMaps = new ArrayList<>( numDimensions ); | ||
for ( int i = 0; i < numDimensions; i++ ) | ||
{ | ||
final DoublePropertyMap< Spot > spotMap = new DoublePropertyMap<>( graph.vertices(), Double.NaN ); | ||
DoublePropertyMapSerializer< Spot > serializer = new DoublePropertyMapSerializer<>( spotMap ); | ||
serializer.readPropertyMap( idmap, ois ); | ||
|
||
DoublePropertyMap< BranchSpot > output = BranchFeatureSerializer.mapToBranchSpotMap( spotMap, branchGraph ); | ||
outputMaps.add( output ); | ||
} | ||
return featureCreator.apply( outputMaps ); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...ava/org/mastodon/mamut/feature/branch/dimensionalityreduction/tsne/BranchTSneFeature.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,85 @@ | ||
/*- | ||
* #%L | ||
* mastodon-deep-lineage | ||
* %% | ||
* Copyright (C) 2022 - 2024 Stefan Hahmann | ||
* %% | ||
* 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 | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* #L% | ||
*/ | ||
package org.mastodon.mamut.feature.branch.dimensionalityreduction.tsne; | ||
|
||
import java.util.List; | ||
|
||
import org.mastodon.feature.Feature; | ||
import org.mastodon.feature.FeatureProjectionKey; | ||
import org.mastodon.feature.FeatureProjectionSpec; | ||
import org.mastodon.feature.FeatureSpec; | ||
import org.mastodon.feature.Multiplicity; | ||
import org.mastodon.mamut.feature.dimensionalityreduction.tsne.feature.AbstractTSneFeature; | ||
import org.mastodon.mamut.model.branch.BranchSpot; | ||
import org.mastodon.properties.DoublePropertyMap; | ||
import org.scijava.plugin.Plugin; | ||
|
||
/** | ||
* Represents a t-SNE feature for BranchSpots in the Mastodon project. | ||
* <br> | ||
* This feature is used to store the t-SNE outputs for BranchSpots. | ||
* <br> | ||
* The t-SNE outputs are stored in a list of {@link DoublePropertyMap}s. The size of the list is equal to the number of dimensions of the t-SNE output. | ||
*/ | ||
public class BranchTSneFeature extends AbstractTSneFeature< BranchSpot > | ||
{ | ||
public static final String KEY = "Branch t-SNE outputs"; | ||
|
||
private final BranchSpotTSneFeatureSpec adaptedSpec; | ||
|
||
public static final BranchSpotTSneFeatureSpec GENERIC_SPEC = new BranchSpotTSneFeatureSpec(); | ||
|
||
public BranchTSneFeature( final List< DoublePropertyMap< BranchSpot > > outputMaps ) | ||
{ | ||
super( outputMaps ); | ||
FeatureProjectionSpec[] projectionSpecs = | ||
projectionMap.keySet().stream().map( FeatureProjectionKey::getSpec ).toArray( FeatureProjectionSpec[]::new ); | ||
this.adaptedSpec = new BranchSpotTSneFeatureSpec( projectionSpecs ); | ||
} | ||
|
||
@Plugin( type = FeatureSpec.class ) | ||
public static class BranchSpotTSneFeatureSpec extends FeatureSpec< BranchTSneFeature, BranchSpot > | ||
{ | ||
public BranchSpotTSneFeatureSpec() | ||
{ | ||
super( KEY, HELP_STRING, BranchTSneFeature.class, BranchSpot.class, Multiplicity.SINGLE ); | ||
} | ||
|
||
public BranchSpotTSneFeatureSpec( final FeatureProjectionSpec... projectionSpecs ) | ||
{ | ||
super( KEY, HELP_STRING, BranchTSneFeature.class, BranchSpot.class, Multiplicity.SINGLE, projectionSpecs ); | ||
} | ||
} | ||
|
||
@Override | ||
public FeatureSpec< ? extends Feature< BranchSpot >, BranchSpot > getSpec() | ||
{ | ||
return adaptedSpec; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...mastodon/mamut/feature/branch/dimensionalityreduction/tsne/BranchTSneFeatureComputer.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,76 @@ | ||
/*- | ||
* #%L | ||
* mastodon-deep-lineage | ||
* %% | ||
* Copyright (C) 2022 - 2024 Stefan Hahmann | ||
* %% | ||
* 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 | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* #L% | ||
*/ | ||
package org.mastodon.mamut.feature.branch.dimensionalityreduction.tsne; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
import org.mastodon.RefPool; | ||
import org.mastodon.mamut.feature.dimensionalityreduction.tsne.feature.AbstractTSneFeature; | ||
import org.mastodon.mamut.feature.dimensionalityreduction.tsne.feature.AbstractTSneFeatureComputer; | ||
import org.mastodon.mamut.model.Model; | ||
import org.mastodon.mamut.model.branch.BranchLink; | ||
import org.mastodon.mamut.model.branch.BranchSpot; | ||
import org.mastodon.mamut.model.branch.ModelBranchGraph; | ||
import org.mastodon.properties.DoublePropertyMap; | ||
import org.scijava.Context; | ||
|
||
public class BranchTSneFeatureComputer extends AbstractTSneFeatureComputer< BranchSpot, BranchLink, ModelBranchGraph > | ||
{ | ||
|
||
public BranchTSneFeatureComputer( final Model model, final Context context ) | ||
{ | ||
super( model, context ); | ||
} | ||
|
||
@Override | ||
protected AbstractTSneFeature< BranchSpot > createFeatureInstance( final List< DoublePropertyMap< BranchSpot > > umapOutputMaps ) | ||
{ | ||
return new BranchTSneFeature( umapOutputMaps ); | ||
} | ||
|
||
@Override | ||
protected RefPool< BranchSpot > getRefPool() | ||
{ | ||
return model.getBranchGraph().vertices().getRefPool(); | ||
} | ||
|
||
@Override | ||
protected ReentrantReadWriteLock getLock( final ModelBranchGraph branchGraph ) | ||
{ | ||
return branchGraph.getLock(); | ||
} | ||
|
||
@Override | ||
protected Collection< BranchSpot > getVertices() | ||
{ | ||
return model.getBranchGraph().vertices(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...stodon/mamut/feature/branch/dimensionalityreduction/tsne/BranchTSneFeatureSerializer.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,72 @@ | ||
/*- | ||
* #%L | ||
* mastodon-deep-lineage | ||
* %% | ||
* Copyright (C) 2022 - 2024 Stefan Hahmann | ||
* %% | ||
* 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 | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* #L% | ||
*/ | ||
package org.mastodon.mamut.feature.branch.dimensionalityreduction.tsne; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
|
||
import org.mastodon.feature.FeatureSpec; | ||
import org.mastodon.feature.io.FeatureSerializer; | ||
import org.mastodon.io.FileIdToObjectMap; | ||
import org.mastodon.io.ObjectToFileIdMap; | ||
import org.mastodon.mamut.feature.branch.BranchFeatureSerializer; | ||
import org.mastodon.mamut.feature.branch.dimensionalityreduction.BranchOutputSerializerTools; | ||
import org.mastodon.mamut.model.ModelGraph; | ||
import org.mastodon.mamut.model.Spot; | ||
import org.mastodon.mamut.model.branch.BranchSpot; | ||
import org.mastodon.mamut.model.branch.ModelBranchGraph; | ||
import org.scijava.plugin.Plugin; | ||
|
||
/** | ||
* De-/serializes {@link BranchTSneFeature} | ||
*/ | ||
@Plugin( type = FeatureSerializer.class ) | ||
public class BranchTSneFeatureSerializer implements BranchFeatureSerializer< BranchTSneFeature, BranchSpot, Spot > | ||
{ | ||
@Override | ||
public FeatureSpec< BranchTSneFeature, BranchSpot > getFeatureSpec() | ||
{ | ||
return BranchTSneFeature.GENERIC_SPEC; | ||
} | ||
|
||
@Override | ||
public void serialize( final BranchTSneFeature feature, final ObjectToFileIdMap< Spot > idmap, final ObjectOutputStream oos, | ||
final ModelBranchGraph branchGraph, final ModelGraph graph ) throws IOException | ||
{ | ||
BranchOutputSerializerTools.serialize( feature, idmap, oos, branchGraph, graph ); | ||
} | ||
|
||
@Override | ||
public BranchTSneFeature deserialize( final FileIdToObjectMap< Spot > idmap, final ObjectInputStream ois, | ||
final ModelBranchGraph branchGraph, final ModelGraph graph ) throws ClassNotFoundException, IOException | ||
{ | ||
return BranchOutputSerializerTools.deserialize( idmap, ois, branchGraph, graph, BranchTSneFeature::new ); | ||
} | ||
} |
Oops, something went wrong.