Skip to content

Commit 7ca58f2

Browse files
author
Stefan Hahmann
committed
Add PCA Feature, Computer and Serializer classes
1 parent 44ffcac commit 7ca58f2

8 files changed

Lines changed: 628 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*-
2+
* #%L
3+
* mastodon-deep-lineage
4+
* %%
5+
* Copyright (C) 2022 - 2024 Stefan Hahmann
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.mastodon.mamut.feature.branch.dimensionalityreduction.pca;
30+
31+
import java.util.List;
32+
33+
import org.mastodon.feature.Feature;
34+
import org.mastodon.feature.FeatureProjectionKey;
35+
import org.mastodon.feature.FeatureProjectionSpec;
36+
import org.mastodon.feature.FeatureSpec;
37+
import org.mastodon.feature.Multiplicity;
38+
import org.mastodon.mamut.feature.dimensionalityreduction.pca.AbstractPcaFeature;
39+
import org.mastodon.mamut.model.branch.BranchSpot;
40+
import org.mastodon.properties.DoublePropertyMap;
41+
import org.scijava.plugin.Plugin;
42+
43+
/**
44+
* Represents a PCA feature for BranchSpots in the Mastodon project.
45+
* <br>
46+
* This feature is used to store the PCA outputs for BranchSpots.
47+
* <br>
48+
* The PCA outputs are stored in a list of {@link DoublePropertyMap}s. The size of the list is equal to the number of dimensions of the PCA output.
49+
*/
50+
public class BranchPcaFeature extends AbstractPcaFeature< BranchSpot >
51+
{
52+
public static final String KEY = "Branch PCA outputs";
53+
54+
private final BranchSpotPcaFeatureSpec adaptedSpec;
55+
56+
public static final BranchSpotPcaFeatureSpec GENERIC_SPEC = new BranchSpotPcaFeatureSpec();
57+
58+
public BranchPcaFeature( final List< DoublePropertyMap< BranchSpot > > outputMaps )
59+
{
60+
super( outputMaps );
61+
FeatureProjectionSpec[] projectionSpecs =
62+
projectionMap.keySet().stream().map( FeatureProjectionKey::getSpec ).toArray( FeatureProjectionSpec[]::new );
63+
this.adaptedSpec = new BranchSpotPcaFeatureSpec( projectionSpecs );
64+
}
65+
66+
@Plugin( type = FeatureSpec.class )
67+
public static class BranchSpotPcaFeatureSpec extends FeatureSpec< BranchPcaFeature, BranchSpot >
68+
{
69+
public BranchSpotPcaFeatureSpec()
70+
{
71+
super( KEY, HELP_STRING, BranchPcaFeature.class, BranchSpot.class, Multiplicity.SINGLE );
72+
}
73+
74+
public BranchSpotPcaFeatureSpec( final FeatureProjectionSpec... projectionSpecs )
75+
{
76+
super( KEY, HELP_STRING, BranchPcaFeature.class, BranchSpot.class, Multiplicity.SINGLE, projectionSpecs );
77+
}
78+
}
79+
80+
@Override
81+
public FeatureSpec< ? extends Feature< BranchSpot >, BranchSpot > getSpec()
82+
{
83+
return adaptedSpec;
84+
}
85+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*-
2+
* #%L
3+
* mastodon-deep-lineage
4+
* %%
5+
* Copyright (C) 2022 - 2024 Stefan Hahmann
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.mastodon.mamut.feature.branch.dimensionalityreduction.pca;
30+
31+
import java.util.Collection;
32+
import java.util.List;
33+
import java.util.concurrent.locks.ReentrantReadWriteLock;
34+
35+
import org.mastodon.RefPool;
36+
import org.mastodon.mamut.feature.dimensionalityreduction.pca.AbstractPcaFeature;
37+
import org.mastodon.mamut.feature.dimensionalityreduction.pca.AbstractPcaFeatureComputer;
38+
import org.mastodon.mamut.model.Model;
39+
import org.mastodon.mamut.model.branch.BranchLink;
40+
import org.mastodon.mamut.model.branch.BranchSpot;
41+
import org.mastodon.mamut.model.branch.ModelBranchGraph;
42+
import org.mastodon.properties.DoublePropertyMap;
43+
import org.scijava.Context;
44+
45+
public class BranchPcaFeatureComputer extends AbstractPcaFeatureComputer< BranchSpot, BranchLink, ModelBranchGraph >
46+
{
47+
48+
public BranchPcaFeatureComputer( final Model model, final Context context )
49+
{
50+
super( model, context );
51+
}
52+
53+
@Override
54+
protected AbstractPcaFeature< BranchSpot > createFeatureInstance( final List< DoublePropertyMap< BranchSpot > > umapOutputMaps )
55+
{
56+
return new BranchPcaFeature( umapOutputMaps );
57+
}
58+
59+
@Override
60+
protected RefPool< BranchSpot > getRefPool()
61+
{
62+
return model.getBranchGraph().vertices().getRefPool();
63+
}
64+
65+
@Override
66+
protected ReentrantReadWriteLock getLock( final ModelBranchGraph branchGraph )
67+
{
68+
return branchGraph.getLock();
69+
}
70+
71+
@Override
72+
protected Collection< BranchSpot > getVertices()
73+
{
74+
return model.getBranchGraph().vertices();
75+
}
76+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*-
2+
* #%L
3+
* mastodon-deep-lineage
4+
* %%
5+
* Copyright (C) 2022 - 2024 Stefan Hahmann
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.mastodon.mamut.feature.branch.dimensionalityreduction.pca;
30+
31+
import java.io.IOException;
32+
import java.io.ObjectInputStream;
33+
import java.io.ObjectOutputStream;
34+
35+
import org.mastodon.feature.FeatureSpec;
36+
import org.mastodon.feature.io.FeatureSerializer;
37+
import org.mastodon.io.FileIdToObjectMap;
38+
import org.mastodon.io.ObjectToFileIdMap;
39+
import org.mastodon.mamut.feature.branch.BranchFeatureSerializer;
40+
import org.mastodon.mamut.feature.branch.dimensionalityreduction.BranchOutputSerializerTools;
41+
import org.mastodon.mamut.model.ModelGraph;
42+
import org.mastodon.mamut.model.Spot;
43+
import org.mastodon.mamut.model.branch.BranchSpot;
44+
import org.mastodon.mamut.model.branch.ModelBranchGraph;
45+
import org.scijava.plugin.Plugin;
46+
47+
/**
48+
* De-/serializes {@link BranchPcaFeature}
49+
*/
50+
@Plugin( type = FeatureSerializer.class )
51+
public class BranchPcaFeatureSerializer implements BranchFeatureSerializer< BranchPcaFeature, BranchSpot, Spot >
52+
{
53+
@Override
54+
public FeatureSpec< BranchPcaFeature, BranchSpot > getFeatureSpec()
55+
{
56+
return BranchPcaFeature.GENERIC_SPEC;
57+
}
58+
59+
@Override
60+
public void serialize( final BranchPcaFeature feature, final ObjectToFileIdMap< Spot > idmap, final ObjectOutputStream oos,
61+
final ModelBranchGraph branchGraph, final ModelGraph graph ) throws IOException
62+
{
63+
BranchOutputSerializerTools.serialize( feature, idmap, oos, branchGraph, graph );
64+
}
65+
66+
@Override
67+
public BranchPcaFeature deserialize( final FileIdToObjectMap< Spot > idmap, final ObjectInputStream ois,
68+
final ModelBranchGraph branchGraph, final ModelGraph graph ) throws ClassNotFoundException, IOException
69+
{
70+
return BranchOutputSerializerTools.deserialize( idmap, ois, branchGraph, graph, BranchPcaFeature::new );
71+
}
72+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*-
2+
* #%L
3+
* mastodon-deep-lineage
4+
* %%
5+
* Copyright (C) 2022 - 2024 Stefan Hahmann
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.mastodon.mamut.feature.dimensionalityreduction.pca;
30+
31+
import java.util.List;
32+
33+
import org.mastodon.graph.Vertex;
34+
import org.mastodon.mamut.feature.dimensionalityreduction.AbstractOutputFeature;
35+
import org.mastodon.properties.DoublePropertyMap;
36+
37+
/**
38+
* This generic feature is used to store the PCA outputs.
39+
* <br>
40+
* The PCA outputs are stored in a list of {@link DoublePropertyMap}s. The size of the list is equal to the number of dimensions of the PCA output.
41+
*/
42+
public abstract class AbstractPcaFeature< V extends Vertex< ? > > extends AbstractOutputFeature< V >
43+
{
44+
private static final String PROJECTION_NAME_TEMPLATE = "PCA%d";
45+
46+
protected static final String HELP_STRING =
47+
"Computes the PCA according to the selected input dimensions and the number of target dimensions.";
48+
49+
protected AbstractPcaFeature( final List< DoublePropertyMap< V > > outputMaps )
50+
{
51+
super( outputMaps );
52+
}
53+
54+
@Override
55+
protected String getProjectionNameTemplate()
56+
{
57+
return PROJECTION_NAME_TEMPLATE;
58+
}
59+
}

0 commit comments

Comments
 (0)