Skip to content

Commit

Permalink
Compute distances to table rows WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tischi committed Dec 20, 2023
1 parent 30dd852 commit 3886102
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/embl/mobie/lib/annotation/Annotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ public interface Annotation extends Location
// (typically: feature = column in an annotation table)
Double getNumber( String feature );

// For adding manual annotations
// For adding text annotations
void setString( String columnName, String value );

// For adding numeric annotations
void setNumber( String columnName, double value );

// Transform the spatial coordinates of this annotation.
// Note that there are also methods to transform annotations,
// which create a copy of the annotation;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/embl/mobie/lib/bdv/view/SliceViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ private void installContextMenuAndKeyboardShortCuts( )
final Set< String > actionsKeys = sacService.getActionsKeys();

final ArrayList< String > actions = new ArrayList< String >();
actions.add( sacService.getCommandName( ScreenShotMakerCommand.class ) );
actions.add( sacService.getCommandName( ShowRasterImagesCommand.class ) );
actions.add( sacService.getCommandName( ViewerTransformLoggerCommand.class ) );
actions.add( sacService.getCommandName( SourceInfoLoggerCommand.class ) );
actions.add( sacService.getCommandName( BigWarpRegistrationCommand.class ) );
actions.add( sacService.getCommandName( ManualRegistrationCommand.class ) );
actions.add( sacService.getCommandName( FlipCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( ScreenShotMakerCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( ShowRasterImagesCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( ViewerTransformLoggerCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( SourceInfoLoggerCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( BigWarpRegistrationCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( ManualRegistrationCommand.class ) );
actions.add( SourceAndConverterService.getCommandName( FlipCommand.class ) );
actions.add( UNDO_SEGMENT_SELECTIONS );
actions.add( LOAD_ADDITIONAL_VIEWS );
actions.add( SAVE_CURRENT_SETTINGS_AS_VIEW );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ default ValuePair< Double, Double > getColumnMinMax( String columnName, ArrayLis
Pair< Double, Double > getMinMax( String columnName ); // for contrast limits during rendering
ArrayList< A > annotations();
void addStringColumn( String columnName );
void addNumericColumn( String columnName );
StorageLocation getStorageLocation();
void transform( AffineTransform3D affineTransform3D );
void addAnnotationListener( AnnotationListener< A > listener );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ public void addStringColumn( String columnName )
listener.columnsAdded( null );
}

@Override
public void addNumericColumn( String columnName )
{
if ( columnNames().contains( columnName ) )
return;

for ( AnnotationTableModel< A > tableModel : tableModels )
{
if ( ! tableModel.columnNames().contains( columnName ) )
{
tableModel.addNumericColumn( columnName );
}
}

for ( AnnotationListener< A > listener : listeners.list )
listener.columnsAdded( null );
}

@Override
public StorageLocation getStorageLocation()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,14 @@ public void setString( String columnName, String value )
// TODO current implementation would require making
// the static field mutable, which would not
// work for multiple tables using the DefaultAnnotatedSegment
// columnToValue.put( columnName, value );
// columnToClass.put( columnName, String.class );
// columnToValue.put( columnName, value );
// columnToClass.put( columnName, String.class );
throw new UnsupportedOperationException( "Adding values to " + this.getClass() + " is not implemented." );
}

@Override
public void setNumber( String columnName, double value )
{
throw new UnsupportedOperationException( "Adding values to " + this.getClass() + " is not implemented." );
}

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/org/embl/mobie/lib/table/DistanceComputer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.embl.mobie.lib.table;

import ij.gui.GenericDialog;
import org.embl.mobie.lib.annotation.Annotation;
import org.embl.mobie.lib.select.SelectionModel;

import java.util.Set;

public class DistanceComputer
{
public static < A extends Annotation > void showUI( AnnotationTableModel< A > tableModel, SelectionModel< A > selectionModel )
{
// show dialog
//
final GenericDialog gd = new GenericDialog( "" );
gd.addStringField( "Distance Columns RegEx", ".*" );
gd.addStringField( "Results Column Name", "distance" );
gd.showDialog();
if( gd.wasCanceled() ) return;
final String columnsRegEx = gd.getNextChoice();
final String resultColumnName = gd.getNextString();

// TODO
// tableModel.addNumericColumn( resultColumnName );
//
// selectedColumns = tableModel.columnNames().stream() // TODO: select columns that match columnsRegEx
//
// // for all selected selectedColumns compute the average or median value
// // of all selectedAnnotations
// Set< A > selectedAnnotations = selectionModel.getSelected();
// for ( A annotation : selectedAnnotations )
// {
// annotation.getNumber( column ) // TODO
// }
//
// // for all annotations compute the Euclidean distance
// // to the above computed average of the selected annotations
//
// tableModel.annotations()
// tableModel.annotation( index ).setString( );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public void addStringColumn( String columnName )
throw new UnsupportedOperationException( this.getClass().getName() + " does not support adding table columns." );
}

@Override
public void addNumericColumn( String columnName )
{
throw new UnsupportedOperationException( this.getClass().getName() + " does not support adding table columns." );
}

@Override
public StorageLocation getStorageLocation()
{
Expand All @@ -154,7 +160,7 @@ public synchronized void transform( AffineTransform3D affineTransform3D )
public void addAnnotationListener( AnnotationListener< AnnotatedSegment > listener )
{
listeners.add( listener );
if ( annotations.size() > 0 )
if ( ! annotations.isEmpty() )
listener.annotationsAdded( annotations );
}

Expand Down
29 changes: 20 additions & 9 deletions src/main/java/org/embl/mobie/lib/table/TableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import de.embl.cba.tables.TableUIs;
import ij.IJ;
import ij.gui.GenericDialog;
import loci.poi.hssf.extractor.ExcelExtractor;
import org.embl.mobie.io.util.IOHelper;
import org.embl.mobie.lib.annotation.AnnotationUI;
import org.embl.mobie.lib.annotation.Annotation;
Expand All @@ -53,10 +52,7 @@
import org.embl.mobie.lib.ui.UserInterfaceHelper;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
Expand Down Expand Up @@ -169,6 +165,8 @@ private JMenuBar createMenuBar()
// menuBar.add( createPlotMenu() ); we have this already in the MoBIE UI
}

menuBar.add( createComputeMenu() );

return menuBar;
}

Expand All @@ -182,6 +180,13 @@ private JMenu createSelectionMenu()
return menu;
}

private JMenu createComputeMenu()
{
JMenu menu = new JMenu( "Compute" );
menu.add( createComputeDistanceMenuItem() );
return menu;
}

private JMenu createAnnotateMenu()
{
JMenu menu = new JMenu( "Annotate" );
Expand Down Expand Up @@ -241,9 +246,7 @@ private JMenuItem createAddStringColumnMenuItem()
{
final JMenuItem menuItem = new JMenuItem( "Add Text Column..." );
menuItem.addActionListener( e ->
new Thread( () -> {
showAddStringColumnDialog();
}).start()
new Thread( this::showAddStringColumnDialog ).start()
);
return menuItem;
}
Expand Down Expand Up @@ -318,12 +321,20 @@ private JMenuItem createSelectAllMenuItem()
return menuItem;
}

private JMenuItem createComputeDistanceMenuItem()
{
final JMenuItem menuItem = new JMenuItem( "Compute Distance to Selected Rows..." );
menuItem.addActionListener( e ->
SwingUtilities.invokeLater( ()
-> DistanceComputer.showUI( tableModel, selectionModel ) ) );
return menuItem;
}

private JMenuItem createSelectEqualToMenuItem()
{
final JMenuItem menuItem = new JMenuItem( "Select Equal To..." );
menuItem.addActionListener( e ->
SwingUtilities.invokeLater( () ->
selectEqualTo() ) );
SwingUtilities.invokeLater( this::selectEqualTo ) );
return menuItem;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ public void addStringColumn( String columnName )
tableModel.addStringColumn( columnName );
}

@Override
public void addNumericColumn( String columnName )
{
tableModel.addNumericColumn( columnName );
}

@Override
public StorageLocation getStorageLocation()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public Object getValue( String feature )
{
try
{
final Object object = model.getTable().get( rowIndex, model.getTable().columnIndex( feature ) );
return object;
return model.getTable().get( rowIndex, model.getTable().columnIndex( feature ) );
}
catch ( Exception e )
{
Expand All @@ -72,4 +71,10 @@ public void setString( String columnName, String value )
model.getTable().stringColumn( columnName ).set( rowIndex, value );
}

@Override
public void setNumber( String columnName, double value )
{
model.getTable().doubleColumn( columnName ).set( rowIndex, value );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.embl.mobie.lib.table.DefaultValues;
import org.embl.mobie.lib.table.TableDataFormat;
import tech.tablesaw.api.ColumnType;
import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;

Expand Down Expand Up @@ -352,6 +353,23 @@ public void addStringColumn( String columnName )
listener.columnsAdded( Collections.singleton( columnName ) );
}

@Override
public void addNumericColumn( String columnName )
{
update();

if ( table.containsColumn( columnName ) )
throw new UnsupportedOperationException("Column " + columnName + " exists already.");

final double[] doubles = new double[ table.rowCount() ];
Arrays.fill( doubles, 0.0 );
final DoubleColumn doubleColumn = DoubleColumn.create( columnName, doubles );
table.addColumns( doubleColumn );

for ( AnnotationListener< A > listener : listeners.list )
listener.columnsAdded( Collections.singleton( columnName ) );
}

@Override
public StorageLocation getStorageLocation()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ public void setString( String columnName, String value )
annotatedSegment.setString( columnName, value );
}

@Override
public void setNumber( String columnName, double value )
{
annotatedSegment.setNumber( columnName, value );
}

@Override
public void transform( AffineTransform3D affineTransform3D )
{
Expand Down

0 comments on commit 3886102

Please sign in to comment.