Skip to content

Commit

Permalink
Add 2 new methods to TagSetUtils
Browse files Browse the repository at this point in the history
* getTagSetNames, which return the names of the tag sets in the model
* getTagValue(), which returns the value of a branchSpot within a specified tag set
* unit tests for the 2 new methods are contained
  • Loading branch information
stefanhahmann committed Apr 12, 2024
1 parent 9e9a66a commit bdbb56f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/org/mastodon/util/TagSetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
*/
package org.mastodon.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import org.mastodon.mamut.model.Link;
import org.mastodon.mamut.model.Model;
import org.mastodon.mamut.model.ModelGraph;
import org.mastodon.mamut.model.Spot;
import org.mastodon.mamut.model.branch.BranchSpot;
import org.mastodon.model.tag.ObjTagMap;
import org.mastodon.model.tag.TagSetModel;
import org.mastodon.model.tag.TagSetStructure;
Expand Down Expand Up @@ -351,4 +354,32 @@ public static TagSetStructure.Tag findTag( final TagSetStructure.TagSet tagSet,
return tag;
throw new NoSuchElementException( "Did not find a tag with the given label: " + tagLabel );
}

/**
* Returns the names of all tag sets in the model.
* @param model the model to get the tag-set model from.
* @return the names of all tag sets in the model.
*/
public static List< String > getTagSetNames( final Model model )
{
List< String > tagSetNames = new ArrayList<>();
model.getTagSetModel().getTagSetStructure().getTagSets().forEach( tagSet -> tagSetNames.add( tagSet.getName() ) );
return tagSetNames;
}

/**
* Gets the tag label of the first spot in the given branchSpot within the given tagSet.
* @param model the model to which the branch belongs
* @param branchSpot the branch spot
* @param tagSet the tag set
* @return the tag label
*/
public static String getTagLabel( final Model model, final BranchSpot branchSpot, final TagSetStructure.TagSet tagSet )
{
if ( model == null || branchSpot == null || tagSet == null )
return null;
Spot first = TreeUtils.getFirstSpot( model, branchSpot );
TagSetStructure.Tag tag = TagSetUtils.getBranchTag( model, tagSet, first );
return tag == null ? null : tag.label();
}
}
36 changes: 36 additions & 0 deletions src/test/java/org/mastodon/util/TagSetUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@

import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class TagSetUtilsTest
Expand Down Expand Up @@ -110,4 +114,36 @@ public void tagSpotAndOutgoingEdges()
// 3 links are tagged: spot0 -> spot1, spot2 -> spot3, spot2 -> spot11
assertEquals( 3, model.getTagSetModel().getEdgeTags().getTaggedWith( tag0 ).size() );
}

@Test
public void testGetTagSetNames()
{
ExampleGraph1 exampleGraph1 = new ExampleGraph1();
String tagSetName1 = "TagSet1";
String tagSetName2 = "TagSet2";
String tagSetName3 = "TagSet2";
Collection< Pair< String, Integer > > emptyTagsAndColors = Collections.emptyList();
TagSetUtils.addNewTagSetToModel( exampleGraph1.getModel(), tagSetName1, emptyTagsAndColors );
TagSetUtils.addNewTagSetToModel( exampleGraph1.getModel(), tagSetName2, emptyTagsAndColors );
TagSetUtils.addNewTagSetToModel( exampleGraph1.getModel(), tagSetName3, emptyTagsAndColors );
Collection< String > tagSetNames = TagSetUtils.getTagSetNames( exampleGraph1.getModel() );
List< String > expected = Arrays.asList( tagSetName1, tagSetName2, tagSetName3 );
assertEquals( expected, tagSetNames );
}

@Test
public void testGetTagLabel()
{
ExampleGraph2 exampleGraph2 = new ExampleGraph2();
String tagSetName = "TagSet";
Pair< String, Integer > tag0 = Pair.of( "Tag", 0 );
Collection< Pair< String, Integer > > tagAndColor = Collections.singletonList( tag0 );
TagSetStructure.TagSet tagSet = TagSetUtils.addNewTagSetToModel( exampleGraph2.getModel(), tagSetName, tagAndColor );
TagSetStructure.Tag tag = tagSet.getTags().get( 0 );
TagSetUtils.tagBranch( exampleGraph2.getModel(), tagSet, tag, exampleGraph2.spot5 );
assertEquals( tag.label(), TagSetUtils.getTagLabel( exampleGraph2.getModel(), exampleGraph2.branchSpotD, tagSet ) );
assertNull( TagSetUtils.getTagLabel( null, exampleGraph2.branchSpotD, tagSet ) );
assertNull( TagSetUtils.getTagLabel( exampleGraph2.getModel(), null, tagSet ) );
assertNull( TagSetUtils.getTagLabel( exampleGraph2.getModel(), exampleGraph2.branchSpotD, null ) );
}
}

0 comments on commit bdbb56f

Please sign in to comment.