Skip to content

Commit

Permalink
Fix spots rendering for 2-D data
Browse files Browse the repository at this point in the history
  • Loading branch information
tischi committed Dec 2, 2024
1 parent 3793dbe commit 1d7c722
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/embl/mobie/MoBIE.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.embl.mobie.io.ImageDataFormat;
import org.embl.mobie.io.imagedata.ImageData;
import org.embl.mobie.io.util.IOHelper;
import org.embl.mobie.io.util.S3Utils;
import org.embl.mobie.lib.*;
import org.embl.mobie.lib.bdv.BdvViewingMode;
import org.embl.mobie.lib.data.*;
Expand Down Expand Up @@ -698,9 +697,10 @@ private void initDataSource( DataSource dataSource, String log )

if ( dataSource instanceof SpotDataSource )
{

// build spots image from spots table
final SpotImageCreator spotImageCreator = new SpotImageCreator( ( SpotDataSource ) dataSource, this );
DataStore.addImage( spotImageCreator.create() );
DataStore.addImage( spotImageCreator.get() );
}

if ( dataSource instanceof RegionTableSource )
Expand Down
37 changes: 25 additions & 12 deletions src/main/java/org/embl/mobie/lib/SpotImageCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,41 @@

public class SpotImageCreator
{
private final SpotDataSource spotDataSource;
private final MoBIE moBIE;
private SpotAnnotationImage< AnnotatedSpot > spotAnnotationImage;

public SpotImageCreator( SpotDataSource dataSource, MoBIE moBIE )
{
final SpotDataSource spotDataSource = dataSource;
final StorageLocation tableLocation = moBIE.getTableLocation( spotDataSource.tableData );
final TableDataFormat tableFormat = moBIE.getTableDataFormat( spotDataSource.tableData );
this.spotDataSource = dataSource;
this.moBIE = moBIE;
}

Table table = TableOpener.open( tableLocation, tableFormat );
public SpotAnnotationImage< AnnotatedSpot > get()
{
if ( spotAnnotationImage == null )
{
final StorageLocation tableLocation = moBIE.getTableLocation( spotDataSource.tableData );
final TableDataFormat tableFormat = moBIE.getTableDataFormat( spotDataSource.tableData );

// TODO: maybe make the spot column names mapping configurable?
final TableSawAnnotationCreator< TableSawAnnotatedSpot > annotationCreator = new TableSawAnnotatedSpotCreator( table );
Table table = TableOpener.open( tableLocation, tableFormat );

final TableSawAnnotationTableModel< AnnotatedSpot > tableModel = new TableSawAnnotationTableModel( dataSource.getName(), annotationCreator, tableLocation, tableFormat, table );
// TODO: maybe make the spot column names mapping configurable?
final TableSawAnnotationCreator< TableSawAnnotatedSpot > annotationCreator = new TableSawAnnotatedSpotCreator( table );

final DefaultAnnData< AnnotatedSpot > spotAnnData = new DefaultAnnData<>( tableModel );
final TableSawAnnotationTableModel< AnnotatedSpot > tableModel =
new TableSawAnnotationTableModel(
spotDataSource.getName(),
annotationCreator,
tableLocation,
tableFormat,
table );

spotAnnotationImage = new SpotAnnotationImage( spotDataSource.getName(), spotAnnData, 1.0, spotDataSource.boundingBoxMin, spotDataSource.boundingBoxMax );
}
final DefaultAnnData< AnnotatedSpot > spotAnnData = new DefaultAnnData<>( tableModel );

spotAnnotationImage = new SpotAnnotationImage( spotDataSource.getName(), spotAnnData, 1.0, spotDataSource.boundingBoxMin, spotDataSource.boundingBoxMax );
}

public SpotAnnotationImage< AnnotatedSpot > create()
{
return spotAnnotationImage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,15 @@ else if ( dataType.equals( CollectionTableConstants.SPOTS ) )
);
dataset.putDataSource( spotDataSource );

// build spots image from spots table
final SpotImageCreator spotImageCreator = new SpotImageCreator(
spotDataSource,
MoBIE.getInstance() );

SpotAnnotationImage< AnnotatedSpot > spotImage = spotImageCreator.create();
DataStore.addImage( spotImage );
// // build spots image from spots table
// final SpotImageCreator spotImageCreator = new SpotImageCreator(
// spotDataSource,
// MoBIE.getInstance() );
// SpotAnnotationImage< AnnotatedSpot > spotImage = spotImageCreator.create();
// DataStore.addImage( spotImage );

SpotDisplay< AnnotatedRegion > spotDisplay = new SpotDisplay<>( name );
spotDisplay.sources = Collections.singletonList( spotImage.getName() );
spotDisplay.sources = Collections.singletonList( spotDataSource.getName() );
display = spotDisplay;
}
else // default: intensities
Expand Down
40 changes: 31 additions & 9 deletions src/main/java/org/embl/mobie/lib/image/SpotAnnotationImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@
import bdv.tools.transformation.TransformedSource;
import bdv.viewer.Source;
import mpicbg.spim.data.sequence.FinalVoxelDimensions;
import net.imglib2.Interval;
import net.imglib2.KDTree;
import net.imglib2.RealLocalizable;
import net.imglib2.Sampler;
import net.imglib2.Volatile;
import net.imglib2.*;
import net.imglib2.neighborsearch.RadiusNeighborSearchOnKDTree;
import net.imglib2.position.FunctionRealRandomAccessible;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.realtransform.RealViews;
import net.imglib2.roi.RealMaskRealInterval;
import net.imglib2.roi.geom.GeomMasks;
import net.imglib2.type.numeric.integer.UnsignedIntType;
import net.imglib2.util.Intervals;
import net.imglib2.view.Views;
import org.embl.mobie.lib.annotation.AnnotatedSpot;
import org.embl.mobie.lib.source.AnnotationType;
import org.embl.mobie.lib.source.RealRandomAccessibleIntervalTimelapseSource;
Expand Down Expand Up @@ -92,23 +90,47 @@ public void setRadius( double radius )
private void createImage()
{
final ArrayList< AS > annotations = annData.getTable().annotations();
System.out.println("Building KDTree with numElements = " + annotations.size());
kdTree = new KDTree( annotations, annotations );

if ( boundingBoxMin == null )
boundingBoxMin = kdTree.minAsDoubleArray();
{
boundingBoxMin = new double[ 3 ];
kdTree.realMin( boundingBoxMin );
}

if ( boundingBoxMax == null )
boundingBoxMax = kdTree.maxAsDoubleArray();
{
boundingBoxMax = new double[ 3 ];
kdTree.realMax( boundingBoxMax );
}

mask = GeomMasks.closedBox( boundingBoxMin, boundingBoxMax );

// TODO: code duplication with RegionLabelImage
final ArrayList< Integer > timePoints = configureTimePoints();
final Interval interval = Intervals.smallestContainingInterval( getMask() );
final AS annotatedSpot = annData.getTable().annotation( 0 );
final FunctionRealRandomAccessible< AnnotationType< AS > > realRandomAccessible = new FunctionRealRandomAccessible( 3, new LocationToAnnotatedSpotSupplier(), () -> new AnnotationType<>( annotatedSpot ) );

RealRandomAccessible< AnnotationType< AS > > rra =
new FunctionRealRandomAccessible(
kdTree.numDimensions(),
new LocationToAnnotatedSpotSupplier(),
() -> new AnnotationType<>( annotatedSpot ) );
//final RealRandomAccessible interpolate = Views.interpolate( new NearestNeighborSearchOnKDTree( kdTree ), new NearestNeighborSearchInterpolatorFactory() );
source = new RealRandomAccessibleIntervalTimelapseSource( realRandomAccessible, interval, new AnnotationType<>( annotatedSpot ), new AffineTransform3D(), name, true, null, new FinalVoxelDimensions( "", 1, 1, 1 ) );

if ( kdTree.numDimensions() == 2 )
rra = RealViews.addDimension( rra );

source = new RealRandomAccessibleIntervalTimelapseSource(
rra,
interval,
new AnnotationType<>( annotatedSpot ),
new AffineTransform3D(),
name,
true,
null,
new FinalVoxelDimensions( "", 1, 1, 1 ) );
}

@Override
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/embl/mobie/lib/serialize/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ public Map< String, Object > getSources()

if ( display instanceof SpotDisplay )
{
// TODO:
// https://github.com/mobie/mobie.github.io/issues/88
final SpotDisplay< ? > spotDisplay = ( SpotDisplay< ? > ) display;
for ( String source : spotDisplay.sources )
sources.put( source, display );
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/org/embl/mobie/lib/source/AbstractBoundarySource.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ public RealRandomAccessible< T > getInterpolatedSource( final int t, final int l
protected ArrayList< Integer > boundaryDimensions()
{
final ArrayList< Integer > dimensions = new ArrayList<>();

if ( source.getSource( 0,0 ).dimension( 2 ) == 1 )
{
// 2D source
dimensions.add( 0 );
dimensions.add( 1 );
return dimensions;
}

if ( bounds != null )
{
// check whether the source is effectively 2D,
Expand All @@ -136,22 +145,15 @@ protected ArrayList< Integer > boundaryDimensions()
if ( sourceWidth > 3 * boundaryWidth )
dimensions.add( d );
}
}
else if ( source.getSource( 0,0 ).dimension( 2 ) == 1 )
{
// 2D source
dimensions.add( 0 );
dimensions.add( 1 );
}
else
{
// 3D source
dimensions.add( 0 );
dimensions.add( 1 );
dimensions.add( 2 );
return dimensions;
}

// 3D source
dimensions.add( 0 );
dimensions.add( 1 );
dimensions.add( 2 );
return dimensions;

}

protected double[] pixelBoundaryWidth( int t, int level )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static Table open( StorageLocation storageLocation, String chunk, TableDa
return (Table) storageLocation.data;
case TSV:
case CSV:
case PARQUET:
default:
return openTableFile( storageLocation, chunk, tableDataFormat );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public TableSawAnnotatedSpotCreator( Table table )
public TableSawAnnotatedSpot create( TableSawAnnotationTableModel< TableSawAnnotatedSpot > model, int rowIndex )
{
final Table table = model.getTable();
final float[] position = new float[ 3 ];
int numDimensions = zColumnIndex > -1 ? 3 : 2;
final float[] position = new float[ numDimensions ];
position[ 0 ] = ((Number) table.get( rowIndex, xColumnIndex )).floatValue();
position[ 1 ] = ((Number) table.get( rowIndex, yColumnIndex )).floatValue();

// FIXME kdTree issue: https://imagesc.zulipchat.com/#narrow/stream/327240-ImgLib2/topic/kdTree.20issue
if ( zColumnIndex > -1 )
position[ 2 ] = ((Number) table.get( rowIndex, zColumnIndex )).floatValue() + (float) ( 1e-3 * Math.random() );
Expand Down

0 comments on commit 1d7c722

Please sign in to comment.