Skip to content
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

Mamut export and TrackMate import redux. #43

Merged
merged 19 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/main/java/org/mastodon/revised/mamut/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,22 @@ public MainWindow( final WindowManager windowManager )
button_gbc_left.gridy = gridy;
buttonsPanel.add( createProjectButton, button_gbc_left );

final JButton importButton = new JButton( actionMap.get( ProjectManager.IMPORT_TGMM ) );
importButton.setText( "import tgmm" );
final JButton importTgmmButton = new JButton( actionMap.get( ProjectManager.IMPORT_TGMM ) );
importTgmmButton.setText( "import tgmm" );
button_gbc_right.gridy = gridy;
buttonsPanel.add( importButton, button_gbc_right );
buttonsPanel.add( importTgmmButton, button_gbc_right );

++gridy;

final JButton importMamutButton = new JButton( actionMap.get( ProjectManager.IMPORT_MAMUT ) );
importMamutButton.setText( "import mamut" );
button_gbc_left.gridy = gridy;
buttonsPanel.add( importMamutButton, button_gbc_left );

final JButton exportMamutButton = new JButton( actionMap.get( ProjectManager.EXPORT_MAMUT ) );
exportMamutButton.setText( "export mamut" );
button_gbc_right.gridy = gridy;
buttonsPanel.add( exportMamutButton, button_gbc_right );

++gridy;

Expand Down Expand Up @@ -178,6 +190,8 @@ public static void addMenus( final ViewMenu menu, final ActionMap actionMap )
item( ProjectManager.SAVE_PROJECT ),
separator(),
item( ProjectManager.IMPORT_TGMM ),
item( ProjectManager.IMPORT_MAMUT ),
item( ProjectManager.EXPORT_MAMUT ),
separator(),
item( WindowManager.PREFERENCES_DIALOG )
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class MamutMenuBuilder extends ViewMenuBuilder
menuTexts.put( ProjectManager.LOAD_PROJECT, "Load Project" );
menuTexts.put( ProjectManager.SAVE_PROJECT, "Save Project" );
menuTexts.put( ProjectManager.IMPORT_TGMM, "Import TGMM tracks" );
menuTexts.put( ProjectManager.IMPORT_MAMUT, "Import MaMuT project" );
menuTexts.put( ProjectManager.EXPORT_MAMUT, "Export MaMuT project" );

menuTexts.put( WindowManager.NEW_BDV_VIEW, "New Bdv" );
menuTexts.put( WindowManager.NEW_TRACKSCHEME_VIEW, "New Trackscheme" );
Expand Down
102 changes: 93 additions & 9 deletions src/main/java/org/mastodon/revised/mamut/ProjectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.mastodon.revised.bdv.overlay.ui.RenderSettingsManager;
import org.mastodon.revised.mamut.feature.MamutFeatureComputerService;
import org.mastodon.revised.model.mamut.Model;
import org.mastodon.revised.model.mamut.trackmate.MamutExporter;
import org.mastodon.revised.model.mamut.trackmate.TrackMateImporter;
import org.mastodon.revised.model.tag.TagSetStructure;
import org.mastodon.revised.trackscheme.display.style.TrackSchemeStyleManager;
import org.mastodon.revised.ui.keymap.KeymapManager;
Expand All @@ -37,11 +39,15 @@ public class ProjectManager
public static final String LOAD_PROJECT = "load project";
public static final String SAVE_PROJECT = "save project";
public static final String IMPORT_TGMM = "import tgmm";
public static final String IMPORT_MAMUT = "import mamut";
public static final String EXPORT_MAMUT = "export mamut";

static final String[] CREATE_PROJECT_KEYS = new String[] { "not mapped" };
static final String[] LOAD_PROJECT_KEYS = new String[] { "not mapped" };
static final String[] SAVE_PROJECT_KEYS = new String[] { "not mapped" };
static final String[] IMPORT_TGMM_KEYS = new String[] { "not mapped" };
static final String[] IMPORT_MAMUT_KEYS = new String[] { "not mapped" };
static final String[] EXPORT_MAMUT_KEYS = new String[] { "not mapped" };

private final WindowManager windowManager;

Expand All @@ -59,6 +65,10 @@ public class ProjectManager

private final AbstractNamedAction importTgmmAction;

private final AbstractNamedAction importMamutAction;

private final AbstractNamedAction exportMamutAction;

public ProjectManager( final WindowManager windowManager )
{
this.windowManager = windowManager;
Expand All @@ -69,6 +79,8 @@ public ProjectManager( final WindowManager windowManager )
loadProjectAction = new RunnableAction( LOAD_PROJECT, this::loadProject );
saveProjectAction = new RunnableAction( SAVE_PROJECT, this::saveProject );
importTgmmAction = new RunnableAction( IMPORT_TGMM, this::importTgmm );
importMamutAction = new RunnableAction( IMPORT_MAMUT, this::importMamut );
exportMamutAction = new RunnableAction( EXPORT_MAMUT, this::exportMamut );

updateEnabledActions();
}
Expand All @@ -78,6 +90,7 @@ private void updateEnabledActions()
final boolean projectOpen = ( project != null );
saveProjectAction.setEnabled( projectOpen );
importTgmmAction.setEnabled( projectOpen );
exportMamutAction.setEnabled( projectOpen );
}

/**
Expand All @@ -93,6 +106,8 @@ public void install( final Actions actions )
actions.namedAction( loadProjectAction, LOAD_PROJECT_KEYS );
actions.namedAction( saveProjectAction, SAVE_PROJECT_KEYS );
actions.namedAction( importTgmmAction, IMPORT_TGMM_KEYS );
actions.namedAction( importMamutAction, IMPORT_MAMUT_KEYS );
actions.namedAction( exportMamutAction, EXPORT_MAMUT_KEYS );
}

public synchronized void createProject()
Expand Down Expand Up @@ -176,11 +191,26 @@ public synchronized void saveProject()
}
}

public synchronized void saveProject( final File projectFolder ) throws IOException
{
if ( project == null )
return;

project.setProjectFolder( projectFolder );
new MamutProjectIO().save( project );

final Model model = windowManager.getAppModel().getModel();
model.saveRaw( project );

updateEnabledActions();
}

/**
* Open a project. If {@code project.getProjectFolder() == null} this is a new project and data structures are initialized as empty.
* The image data {@code project.getDatasetXmlFile()} must always be set.
*
* @param project
*
* @throws IOException
* @throws SpimDataException
*/
Expand Down Expand Up @@ -257,29 +287,83 @@ public synchronized void open( final MamutProject project ) throws IOException,
updateEnabledActions();
}

public synchronized void saveProject( final File projectFolder ) throws IOException
public synchronized void importTgmm()
{
if ( project == null )
return;

project.setProjectFolder( projectFolder );
new MamutProjectIO().save( project );
final MamutAppModel appModel = windowManager.getAppModel();
tgmmImportDialog.showImportDialog( appModel.getSharedBdvData().getSpimData(), appModel.getModel() );

final Model model = windowManager.getAppModel().getModel();
model.saveRaw( project );
updateEnabledActions();
}

public synchronized void importMamut()
{
final Component parent = null; // TODO
final File file = FileChooser.chooseFile(
parent,
null,
new XmlFileFilter(),
"Import MaMuT Project",
FileChooser.DialogType.LOAD );
if ( file == null )
return;

try
{
final TrackMateImporter importer = new TrackMateImporter( file );
open( importer.createProject() );
importer.readModel( windowManager.getAppModel().getModel() );
}
catch ( final IOException | SpimDataException e )
{
e.printStackTrace();
}

updateEnabledActions();
}

public void importTgmm()
public synchronized void exportMamut()
{
if ( project == null )
return;

final MamutAppModel appModel = windowManager.getAppModel();
tgmmImportDialog.showImportDialog( appModel.getSharedBdvData().getSpimData(), appModel.getModel() );
final String filename = getProprosedMamutExportFileName( project );

updateEnabledActions();
final Component parent = null; // TODO
final File file = FileChooser.chooseFile(
parent,
filename,
new XmlFileFilter(),
"Export As MaMuT Project",
FileChooser.DialogType.SAVE );
if ( file == null )
return;

try
{
MamutExporter.export( file, windowManager.getAppModel().getModel(), project );
}
catch ( final IOException e )
{
e.printStackTrace();
}
}

private static String getProprosedMamutExportFileName( final MamutProject project )
{
final File pf = project.getProjectFolder();
if ( pf != null )
{
return new File( pf.getParentFile(), pf.getName() + "_mamut.xml" ).getAbsolutePath();
}
else
{
final File f = project.getDatasetXmlFile();
final String fn = f.getName();
return new File( f.getParentFile(), fn.substring( 0, fn.length() - ".xml".length() ) + "_mamut.xml" ).getAbsolutePath();
}
}

private static String getProposedProjectFolder( final MamutProject project )
Expand Down
144 changes: 144 additions & 0 deletions src/main/java/org/mastodon/revised/model/mamut/ModelUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package org.mastodon.revised.model.mamut;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.mastodon.revised.model.feature.Feature;
import org.mastodon.revised.model.feature.FeatureModel;
import org.mastodon.revised.model.feature.FeatureProjection;

public class ModelUtils
{

public static final String dump( final Model model )
{
final ModelGraph graph = model.getGraph();
final FeatureModel featureModel = model.getFeatureModel();

final StringBuilder str = new StringBuilder();
str.append( "Model " + model.toString() + "\n" );

/*
* Collect spot feature headers.
*/

final Map< String, FeatureProjection< Spot > > sfs = new LinkedHashMap<>();
final Set< Feature< ?, ? > > set1 = featureModel.getFeatureSet( Spot.class );
final List< Feature< ?, ? > > spotFeatures = set1 == null ? new ArrayList<>() : new ArrayList<>( set1 );
spotFeatures.sort( ( f1, f2 ) -> f1.getKey().compareTo( f2.getKey() ) );
for ( final Feature< ?, ? > feature : spotFeatures )
{
@SuppressWarnings( "unchecked" )
final Feature< Spot, ? > sf = ( Feature< Spot, ? > ) feature;
final Map< String, FeatureProjection< Spot > > projections = sf.getProjections();
sfs.putAll( projections );
}

/*
* Loop over all spots.
*/

str.append( "Spots:\n" );
final String h1a = String.format( "%9s %9s %6s %9s %9s %9s",
"Id", "Label", "Frame", "X", "Y", "Z" );
str.append( h1a );

final int[] spotColumnHeaderWidth = new int[ sfs.size() ];
int i = 0;
for ( final String pn : sfs.keySet() )
{
spotColumnHeaderWidth[ i ] = pn.length() + 2;
str.append( String.format( " %" + spotColumnHeaderWidth[ i ] + "s", pn ) );
i++;
}

str.append( '\n' );
final char[] sline = new char[ h1a.length() + Arrays.stream( spotColumnHeaderWidth ).sum() + 2 * spotColumnHeaderWidth.length ];
Arrays.fill( sline, '-' );
str.append( sline );
str.append( '\n' );

for ( final Spot spot : graph.vertices() )
{
final String h1b = String.format( "%9d %9s %6d %9.1f %9.1f %9.1f",
spot.getInternalPoolIndex(), spot.getLabel(), spot.getTimepoint(),
spot.getDoublePosition( 0 ), spot.getDoublePosition( 1 ), spot.getDoublePosition( 2 ) );

str.append( h1b );
i = 0;
for ( final String pn : sfs.keySet() )
{
if ( sfs.get( pn ).isSet( spot ) )
str.append( String.format( " %" + spotColumnHeaderWidth[ i ] + ".1f", sfs.get( pn ).value( spot ) ) );
else
str.append( String.format( " %" + spotColumnHeaderWidth[ i ] + "s", "unset" ) );
i++;
}
str.append( '\n' );
}

/*
* Collect link feature headers.
*/

final Map< String, FeatureProjection< Link > > lfs = new LinkedHashMap<>();
final Set< Feature< ?, ? > > set2 = featureModel.getFeatureSet( Link.class );
final List< Feature< ?, ? > > linkFeatures = set2 == null ? new ArrayList<>() : new ArrayList<>( set2 );
linkFeatures.sort( ( f1, f2 ) -> f1.getKey().compareTo( f2.getKey() ) );
for ( final Feature< ?, ? > feature : linkFeatures )
{
@SuppressWarnings( "unchecked" )
final Feature< Link, ? > lf = ( Feature< Link, ? > ) feature;
final Map< String, FeatureProjection< Link > > projections = lf.getProjections();
lfs.putAll( projections );
}

/*
* Loop over all links.
*/

str.append( "Links:\n" );
final String h2a = String.format( "%9s %9s %9s", "Id", "Source Id", "Target Id" );
str.append( h2a );

final int[] linkColumnHeaderWidth = new int[ lfs.size() ];
i = 0;
for ( final String pn : lfs.keySet() )
{
linkColumnHeaderWidth[ i ] = pn.length() + 2;
str.append( String.format( " %" + linkColumnHeaderWidth[ i ] + "s", pn ) );
i++;
}

str.append( '\n' );
final char[] lline = new char[ h2a.length() + Arrays.stream( linkColumnHeaderWidth ).sum() + 2 * linkColumnHeaderWidth.length ];
Arrays.fill( lline, '-' );
str.append( lline );
str.append( '\n' );

final Spot ref = graph.vertexRef();
for ( final Link link : graph.edges() )
{
final String h1b = String.format( "%9d %9d %9d", link.getInternalPoolIndex(),
link.getSource( ref ).getInternalPoolIndex(), link.getTarget( ref ).getInternalPoolIndex() );
str.append( h1b );
i = 0;
for ( final String pn : lfs.keySet() )
{
if ( lfs.get( pn ).isSet( link ) )
str.append( String.format( " %" + linkColumnHeaderWidth[ i ] + ".1f", lfs.get( pn ).value( link ) ) );
else
str.append( String.format( " %" + linkColumnHeaderWidth[ i ] + "s", "unset" ) );
i++;
}
str.append( '\n' );
}

return str.toString();
}

}
Loading