Skip to content

Commit

Permalink
FeatureSerializerTestUtils: refactoring
Browse files Browse the repository at this point in the history
The return type of the saveAndReload method is now Feature< T > instead of Feature< ? >
which means that the client code no longer neads to cast the returned object.

The checkFeatureProjectionEquality method now allows to test the projection values on
more than just one object.
  • Loading branch information
maarzt authored and tinevez committed May 7, 2023
1 parent 19de341 commit 481881d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.mastodon.feature.Feature;
import org.mastodon.feature.FeatureProjection;
import org.mastodon.feature.FeatureProjectionKey;
import org.mastodon.feature.FeatureSpec;
import org.mastodon.graph.io.RawGraphIO;
import org.mastodon.mamut.model.Link;
import org.mastodon.mamut.model.Model;
Expand All @@ -12,12 +14,16 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Set;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

import net.imglib2.util.Cast;

public class FeatureSerializerTestUtils
{

public static Feature< ? > saveAndReload( Context context, Model model, Feature< ? > feature ) throws IOException
public static < T > Feature< T > saveAndReload( Context context, Model model, Feature< T > feature ) throws IOException
{
File projectRoot = Files.createTempDirectory( "mamut" ).toFile();
File datasetXmlFile = Files.createTempFile( "project", ".xml" ).toFile();
Expand Down Expand Up @@ -45,58 +51,60 @@ public class FeatureSerializerTestUtils
{
throw new RuntimeException( "Could not find feature class.", e );
}
return modelReloaded.getFeatureModel().getFeature( feature.getSpec() );
return Cast.unchecked( modelReloaded.getFeatureModel().getFeature( feature.getSpec() ) );
}

/**
* Checks, if the two features have the same spec and the same projection values for a given object.
* <p>
* The latter is done by comparing the values of the projections
* of the two given features having the same key for the given object.
* Checks, if the two features have the same spec and the same projection values for the given objects.
*
* @param feature1 first feature to compare
* @param feature2 second feature to compare
* @param object object to compare the projections on
* @return {@code true} if the two features have the same spec and the same projection values for the given object,
* @param objects objects to compare the projections on
* @return {@code true} if the two features have the same spec and the same projection values for the given objects,
* {@code false} otherwise.
* @param <T> the type of the object
* @param <T> the type of the objects
*/
public static < T > boolean checkFeatureProjectionEquality( Feature< T > feature1, Feature< T > feature2, T object )
public static < T > boolean checkFeatureProjectionEquality( Feature< T > feature1, Feature< T > feature2, Collection< T > objects )
{
if ( feature1 == null || feature2 == null )
return false;
if ( !feature1.getSpec().equals( feature2.getSpec() ) )

FeatureSpec< ? extends Feature< T >, T > spec1 = feature1.getSpec();
FeatureSpec< ? extends Feature< T >, T > spec2 = feature2.getSpec();
if ( !spec1.equals( spec2 ) )
return false;
Set< FeatureProjection< T > > featureProjections1 = feature1.projections();
Set< FeatureProjection< T > > featureProjections2 = feature2.projections();
// check if the two features have the same projections
for ( FeatureProjection< T > featureProjection1 : featureProjections1 )
{
boolean found = false;
for ( FeatureProjection< T > featureProjection2 : featureProjections2 )
{
if ( featureProjection1.getKey().equals( featureProjection2.getKey() ) )
{
found = true;
break;
}
}
if ( !found )

Map< FeatureProjectionKey, FeatureProjection< T > > projections1 = feature1.projections().stream()
.collect( Collectors.toMap( FeatureProjection::getKey, x -> x ) );

Map< FeatureProjectionKey, FeatureProjection< T > > projections2 = feature1.projections().stream()
.collect( Collectors.toMap( FeatureProjection::getKey, x -> x ) );

if ( !projections1.keySet().equals( projections2.keySet() ) )
return false;

for ( FeatureProjectionKey key : projections1.keySet() )
if ( !checkProjectionEquals( projections1.get( key ), projections2.get( key ), objects ) )
return false;
}
// check if the two features have the same projection values for the given object
for ( FeatureProjection< T > featureProjection1 : featureProjections1 )

return true;
}

private static < T > boolean checkProjectionEquals(
FeatureProjection< T > projection1,
FeatureProjection< T > projection2,
Collection< T > objects )
{
for ( T object : objects )
{
for ( FeatureProjection< T > featureProjection2 : featureProjections2 )
{
if ( featureProjection1.getKey().equals( featureProjection2.getKey() ) )
{
double value1 = featureProjection1.value( object );
double value2 = featureProjection2.value( object );
if ( value1 != value2 )
return false;
}
}
boolean set1 = projection1.isSet( object );
boolean set2 = projection2.isSet( object );

if ( set1 != set2 )
return false;

if ( projection1.value( object ) != projection2.value( object ) )
return false;
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.scijava.Context;

import java.io.IOException;
import java.util.Collection;

import static org.junit.Assert.assertTrue;

Expand All @@ -21,23 +22,12 @@ public void testFeatureSerialization() throws IOException
try (Context context = new Context())
{
ExampleGraph2 exampleGraph2 = new ExampleGraph2();
Feature< BranchSpot > branchDepthFeature =
FeatureComputerTestUtils.getFeature( context, exampleGraph2.getModel(), BranchDepthFeature.SPEC );

BranchDepthFeature branchDepthFeatureReloaded = ( BranchDepthFeature ) FeatureSerializerTestUtils.saveAndReload( context,
exampleGraph2.getModel(), branchDepthFeature );
Feature< BranchSpot > feature = FeatureComputerTestUtils.getFeature( context, exampleGraph2.getModel(), BranchDepthFeature.SPEC );
Feature< BranchSpot > featureReloaded = FeatureSerializerTestUtils.saveAndReload( context, exampleGraph2.getModel(), feature );

// check that the feature has correct values after saving and reloading
assertTrue( FeatureSerializerTestUtils.checkFeatureProjectionEquality( branchDepthFeature, branchDepthFeatureReloaded,
exampleGraph2.branchSpotA ) );
assertTrue( FeatureSerializerTestUtils.checkFeatureProjectionEquality( branchDepthFeature, branchDepthFeatureReloaded,
exampleGraph2.branchSpotB ) );
assertTrue( FeatureSerializerTestUtils.checkFeatureProjectionEquality( branchDepthFeature, branchDepthFeatureReloaded,
exampleGraph2.branchSpotC ) );
assertTrue( FeatureSerializerTestUtils.checkFeatureProjectionEquality( branchDepthFeature, branchDepthFeatureReloaded,
exampleGraph2.branchSpotD ) );
assertTrue( FeatureSerializerTestUtils.checkFeatureProjectionEquality( branchDepthFeature, branchDepthFeatureReloaded,
exampleGraph2.branchSpotE ) );
Collection< BranchSpot > branchSpots = exampleGraph2.getModel().getBranchGraph().vertices();
assertTrue( FeatureSerializerTestUtils.checkFeatureProjectionEquality( feature, featureReloaded, branchSpots ) );
}
}
}

0 comments on commit 481881d

Please sign in to comment.