Skip to content

Commit

Permalink
Merge pull request #53 from mastodon-sc/fix-merge-datasets
Browse files Browse the repository at this point in the history
Fix "File > Merge Two Projects..."
  • Loading branch information
maarzt authored Jan 11, 2024
2 parents 641411b + acbab24 commit 1c2c684
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.mastodon.app.MastodonIcons;
import org.mastodon.app.ui.ViewMenuBuilder;
import org.mastodon.mamut.KeyConfigScopes;
import org.mastodon.mamut.MainWindow;
import org.mastodon.mamut.ProjectModel;
import org.mastodon.mamut.io.ProjectCreator;
import org.mastodon.mamut.io.project.MamutImagePlusProject;
Expand Down Expand Up @@ -397,10 +398,14 @@ private void mergeProjects()

final Dataset dsA = new Dataset( pathA );
final Dataset dsB = new Dataset( pathB );

final ProjectModel projectMerged = ProjectCreator.createProjectFromBdvFile( dsA.project().getDatasetXmlFile(), pluginAppModel.getContext() );
final MergeDatasets.OutputDataSet output = new MergeDatasets.OutputDataSet( projectMerged );
final MergeDatasets.OutputDataSet output = new MergeDatasets.OutputDataSet( projectMerged.getModel() );
MergeDatasets.merge( dsA, dsB, output, distCutoff, mahalanobisDistCutoff, ratioThreshold );
// close currently open instance of Mastodon
pluginAppModel.close();
// start a new instance of Mastodon that shows the result of the merge operation
new MainWindow( projectMerged ).setVisible( true );
}
catch( final Exception e )
{
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/mastodon/mamut/tomancak/merging/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
*/
package org.mastodon.mamut.tomancak.merging;

import static org.mastodon.mamut.tomancak.merging.MergingUtil.getMaxNonEmptyTimepoint;
import static org.mastodon.mamut.tomancak.merging.MergingUtil.getNumTimepoints;

import java.io.IOException;

import org.mastodon.mamut.io.project.MamutProject;
Expand All @@ -47,26 +44,30 @@ public class Dataset
{
private final MamutProject project;

private final int numTimepoints;

private final Model model;

private final int maxNonEmptyTimepoint;

public Dataset( final String path ) throws IOException
{
project = MamutProjectIO.load( path );
numTimepoints = getNumTimepoints( project );
model = new Model();
try (final MamutProject.ProjectReader reader = project.openForReading())
{
model.loadRaw( reader );
}
maxNonEmptyTimepoint = getMaxNonEmptyTimepoint( model, numTimepoints );

maxNonEmptyTimepoint = maxTimepoint( model );
verify();
}

private int maxTimepoint( Model model )
{
int max = 0;
for ( Spot spot : model.getGraph().vertices() )
max = Math.max( max, spot.getTimepoint() );
return max;
}

public Model model()
{
return model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@
import org.mastodon.model.tag.TagSetStructure;
import org.mastodon.model.tag.TagSetStructure.Tag;
import org.mastodon.model.tag.TagSetStructure.TagSet;
import org.mastodon.views.bdv.SharedBigDataViewerData;
import org.scijava.Context;

public class MergeDatasets
{
public static class OutputDataSet
{
private File datasetXmlFile;

private final ProjectModel model;
private final Model model;

private final TagSetStructure tagSetStructure;

Expand All @@ -55,9 +57,9 @@ public static class OutputDataSet

private final TagSet labelConflictTagSet;

public OutputDataSet( final ProjectModel projectMerged )
public OutputDataSet( final Model model )
{
this.model = projectMerged;
this.model = model;
tagSetStructure = new TagSetStructure();
conflictTagSet = tagSetStructure.createTagSet( "Merge Conflict" );
tagConflictTagSet = tagSetStructure.createTagSet( "Merge Conflict (Tags)" );
Expand All @@ -70,53 +72,51 @@ public void setDatasetXmlFile( final File file )
}

/**
* @param projectRoot
* where to store the new project
* @throws IOException
* if an input/ouput problem occurs.
* @param projectRoot where to store the new project
* @throws IOException if an input/ouput problem occurs.
*/
public void saveProject( final File projectRoot ) throws IOException
public void saveProject( final Context context, final SharedBigDataViewerData sbd, final File projectRoot ) throws IOException
{
if ( datasetXmlFile == null )
throw new IllegalStateException();

// Make a new project model with the merged project, but pointing to the specified datasetXmlFile.
final MamutProject project = new MamutProject( projectRoot, datasetXmlFile );
ProjectModel projectToSave = ProjectModel.create( model.getContext(), getModel(), model.getSharedBdvData(), project );
ProjectModel projectToSave = ProjectModel.create( context, getModel(), sbd, project );
ProjectSaver.saveProject( projectRoot, projectToSave );
}

public Model getModel()
{
return model.getModel();
return model;
}

public Tag addSourceTag( final String name, final int color )
{
final TagSet ts = tagSetStructure.createTagSet( "Merge Source " + name );
final Tag tag = ts.createTag( name, color );
model.getModel().getTagSetModel().setTagSetStructure( tagSetStructure );
model.getTagSetModel().setTagSetStructure( tagSetStructure );
return tag;
}

public Tag addConflictTag( final String name, final int color )
{
final Tag tag = conflictTagSet.createTag( name, color );
model.getModel().getTagSetModel().setTagSetStructure( tagSetStructure );
model.getTagSetModel().setTagSetStructure( tagSetStructure );
return tag;
}

public Tag addTagConflictTag( final String name, final int color )
{
final Tag tag = tagConflictTagSet.createTag( name, color );
model.getModel().getTagSetModel().setTagSetStructure( tagSetStructure );
model.getTagSetModel().setTagSetStructure( tagSetStructure );
return tag;
}

public Tag addLabelConflictTag( final String name, final int color )
{
final Tag tag = labelConflictTagSet.createTag( name, color );
model.getModel().getTagSetModel().setTagSetStructure( tagSetStructure );
model.getTagSetModel().setTagSetStructure( tagSetStructure );
return tag;
}

Expand All @@ -127,7 +127,7 @@ public TagSetStructure getTagSetStructure()

public void updateTagSetModel()
{
model.getModel().getTagSetModel().setTagSetStructure( tagSetStructure );
model.getTagSetModel().setTagSetStructure( tagSetStructure );
}
}

Expand Down
65 changes: 0 additions & 65 deletions src/main/java/org/mastodon/mamut/tomancak/merging/MergingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,9 @@
*/
package org.mastodon.mamut.tomancak.merging;

import org.mastodon.mamut.io.project.MamutProject;
import org.mastodon.mamut.model.Model;
import org.mastodon.mamut.model.Spot;
import org.mastodon.mamut.model.SpotPool;
import org.mastodon.properties.ObjPropertyMap;
import org.mastodon.spatial.SpatialIndex;
import org.mastodon.spatial.SpatioTemporalIndex;
import org.mastodon.util.DummySpimData;

import bdv.spimdata.SpimDataMinimal;
import bdv.spimdata.XmlIoSpimDataMinimal;
import mpicbg.spim.data.SpimDataException;

public class MergingUtil
{
Expand All @@ -58,60 +49,4 @@ public static boolean hasLabel( final Spot spot )
final ObjPropertyMap< Spot, String > labels = ( ObjPropertyMap< Spot, String > ) pool.labelProperty();
return labels.isSet( spot );
}

/**
* Returns number of time-points in {@code project}. To to that, loads
* {@code spimdata} for {@code project}.
*
* @param project
* the project.
* @return the number of time-points in the project.
*/
public static int getNumTimepoints( final MamutProject project )
{
try
{
final String spimDataXmlFilename = project.getDatasetXmlFile().getAbsolutePath();
SpimDataMinimal spimData = DummySpimData.tryCreate( project.getDatasetXmlFile().getName() );
if ( spimData == null )
spimData = new XmlIoSpimDataMinimal().load( spimDataXmlFilename );
return spimData.getSequenceDescription().getTimePoints().size();
}
catch ( final SpimDataException e )
{
throw new RuntimeException( e );
}
}

// Helper: max timepoint that has at least one spot

/**
* Returns the largest timepoint (index) where model has a least one spot.
*
* @param model
* the model.
* @param numTimepoints
* the number of time-points in the model.
* @return the largest timepoint (index) where model has a least one spot.
*/
public static int getMaxNonEmptyTimepoint( final Model model, final int numTimepoints )
{
int maxNonEmptyTimepoint = 0;
final SpatioTemporalIndex< Spot > spatioTemporalIndex = model.getSpatioTemporalIndex();
spatioTemporalIndex.readLock().lock();
try
{
for ( int t = 0; t < numTimepoints; ++t )
{
final SpatialIndex< Spot > index = spatioTemporalIndex.getSpatialIndex( t );
if ( index.size() > 0 )
maxNonEmptyTimepoint = t;
}
}
finally
{
spatioTemporalIndex.readLock().unlock();
}
return maxNonEmptyTimepoint;
}
}

0 comments on commit 1c2c684

Please sign in to comment.