-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The feature list to deserialize is specified at compile time. #27
Comments
Example in two classes: Class that saves a model with an added feature: package org.mastodon.revised.mamut;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import org.mastodon.features.DoubleFeature;
import org.mastodon.revised.model.mamut.Model;
import org.mastodon.revised.model.mamut.Spot;
import mpicbg.spim.data.SpimDataException;
public class TestSaveFeature
{
public static final String KEY = "TEST_FEATURE";
public static final File TEST_FILE = new File( "samples/test_features.raw" );
public static void main( final String[] args ) throws IOException, SpimDataException, InvocationTargetException, InterruptedException
{
final String bdvFile = "samples/datasethdf5.xml";
final String modelFile = "samples/model_revised.raw";
final MamutProject project = new MamutProject( new File( "." ), new File( bdvFile ), new File( modelFile ) );
/*
* Load Model
*/
final Model model = new Model();
if ( project.getRawModelFile() != null )
model.loadRaw( project.getRawModelFile() );
System.out.println( String.format( "Loaded a graph with %d vertices and %d edges.",
model.getGraph().vertices().size(), model.getGraph().edges().size() ) );
final DoubleFeature< Spot > test = new DoubleFeature<>( KEY, 10. );
for ( final Spot s : model.getGraph().vertices() )
s.feature( test ).set( 3.14 );
model.saveRaw( TEST_FILE );
System.out.println( "Saved to " + TEST_FILE );
}
} Class that reloads this model: package org.mastodon.revised.mamut;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import org.mastodon.features.Feature;
import org.mastodon.features.FeatureRegistry;
import org.mastodon.revised.model.mamut.Model;
import org.mastodon.revised.model.mamut.Spot;
import mpicbg.spim.data.SpimDataException;
public class TestLoadFeatures
{
public static void main( final String[] args ) throws IOException, SpimDataException, InvocationTargetException, InterruptedException
{
/*
* Load Model
*/
final Model model = new Model();
model.loadRaw( TestSaveFeature.TEST_FILE );
System.out.println( String.format( "Loaded a graph with %d vertices and %d edges.",
model.getGraph().vertices().size(), model.getGraph().edges().size() ) );
final Feature feature = FeatureRegistry.getFeature( TestSaveFeature.KEY );
System.out.println( "Feature with key " + TestSaveFeature.KEY + " is " + feature );
for ( final Spot s : model.getGraph().vertices() )
System.out.println( s + " feature " + s.feature( feature ).get() );
}
} Results of the latest:
|
A solution would be maybe to change the way features are handled via static methods, and have a These declarations could be saved and deserialized before the actual features are deserialized. The application could modify these declarations at runtime, adding or removing features at will. |
This is fixed by #36 (among others). |
Though features can be defined any time in an application, only those defined at compile time are deserialized properly.
I had a hard time understanding what was going on, and I think that this is linked to the fact that some features are handled in static initializers and in static methods
Here is roughly the code flow for deserialization of a
Model
:a
Model
instance is created. At this line (https://github.com/fiji/TrackMate3/blob/model-edit-actions/src/main/java/org/mastodon/revised/model/mamut/Model.java#L55) theModelFeatures
class is referenced.In the static initializer of this class, an
ObjFeature
is created for vertex labels (https://github.com/fiji/TrackMate3/blob/model-edit-actions/src/main/java/org/mastodon/revised/model/mamut/ModelFeatures.java#L11).Doing so, this feature is registered in a static field of
FeatureRegistry
.The feature serializer is registered in a static field of
FeatureSerializers
(https://github.com/fiji/TrackMate3/blob/model-edit-actions/src/main/java/org/mastodon/revised/model/mamut/ModelFeatures.java#L15)Now the model is instantiated and empty.
Calling `'loadRaw' will deserialize the graph (vertices and edges) and then call 'RawFeatureIO.readFeatureMaps( fileIdMap.vertices(), vertexFeatures, ois );' to deserialize features.
This method relies on the
FeatureRegistry
to get theFeature
instance corresponding to the String key stored in the raw file. If the feature with said key is not in the registry - i.e was not declared before the call toloadRaw
- the registry will return null.This happens for all features created after the model was instantiated or/and loaded.
The text was updated successfully, but these errors were encountered: