From c74ff7df9c7a00098788a0fcfab2fa9befcd2663 Mon Sep 17 00:00:00 2001 From: Christian Tischer Date: Mon, 2 Dec 2024 14:52:13 +0100 Subject: [PATCH] Add automated spot radius configuring --- src/main/java/org/embl/mobie/MoBIE.java | 1 - .../ConfigureSpotRenderingCommand.java | 8 +++++- .../org/embl/mobie/lib/SpotImageCreator.java | 7 ++++- .../mobie/lib/image/SpotAnnotationImage.java | 28 +++++++++++++++---- .../lib/serialize/display/SpotDisplay.java | 2 +- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/embl/mobie/MoBIE.java b/src/main/java/org/embl/mobie/MoBIE.java index 0a1ab32f4..705315157 100644 --- a/src/main/java/org/embl/mobie/MoBIE.java +++ b/src/main/java/org/embl/mobie/MoBIE.java @@ -697,7 +697,6 @@ 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.get() ); diff --git a/src/main/java/org/embl/mobie/command/context/ConfigureSpotRenderingCommand.java b/src/main/java/org/embl/mobie/command/context/ConfigureSpotRenderingCommand.java index 635811fb8..0fc79efb7 100644 --- a/src/main/java/org/embl/mobie/command/context/ConfigureSpotRenderingCommand.java +++ b/src/main/java/org/embl/mobie/command/context/ConfigureSpotRenderingCommand.java @@ -73,7 +73,13 @@ private void initSpotRadiusItem() for ( SourceAndConverter sourceAndConverter : sourceAndConverters ) { final SpotAnnotationImage spotAnnotationImage = ( SpotAnnotationImage ) DataStore.sourceToImage().get( sourceAndConverter ); - spotRadiusItem.setValue( this, spotAnnotationImage.getRadius() ); + + Double radius = spotAnnotationImage.getRadius(); + if ( radius != null ) + spotRadiusItem.setValue( this, radius ); + else + spotRadiusItem.setValue( this, 1.0D ); + return; } } diff --git a/src/main/java/org/embl/mobie/lib/SpotImageCreator.java b/src/main/java/org/embl/mobie/lib/SpotImageCreator.java index 8076adbb5..3e04871d3 100644 --- a/src/main/java/org/embl/mobie/lib/SpotImageCreator.java +++ b/src/main/java/org/embl/mobie/lib/SpotImageCreator.java @@ -76,7 +76,12 @@ public SpotAnnotationImage< AnnotatedSpot > get() final DefaultAnnData< AnnotatedSpot > spotAnnData = new DefaultAnnData<>( tableModel ); - spotAnnotationImage = new SpotAnnotationImage( spotDataSource.getName(), spotAnnData, 1.0, spotDataSource.boundingBoxMin, spotDataSource.boundingBoxMax ); + spotAnnotationImage = new SpotAnnotationImage( + spotDataSource.getName(), + spotAnnData, + null, + spotDataSource.boundingBoxMin, + spotDataSource.boundingBoxMax ); } return spotAnnotationImage; diff --git a/src/main/java/org/embl/mobie/lib/image/SpotAnnotationImage.java b/src/main/java/org/embl/mobie/lib/image/SpotAnnotationImage.java index 1b3dd5600..383b25f1b 100644 --- a/src/main/java/org/embl/mobie/lib/image/SpotAnnotationImage.java +++ b/src/main/java/org/embl/mobie/lib/image/SpotAnnotationImage.java @@ -59,14 +59,19 @@ public class SpotAnnotationImage< AS extends AnnotatedSpot > implements Annotati private Source< ? extends Volatile< UnsignedIntType > > volatileSource = null; private KDTree< AS > kdTree; private RealMaskRealInterval mask; - private double radius; + private Double radius; private double[] boundingBoxMin; private double[] boundingBoxMax; private AffineTransform3D affineTransform3D; private Source< AnnotationType< AS > > source; private TransformedSource< AnnotationType< AS > > transformedSource; - public SpotAnnotationImage( String name, DefaultAnnData< AS > annData, double radius, @Nullable double[] boundingBoxMin, @Nullable double[] boundingBoxMax ) + public SpotAnnotationImage( + String name, + DefaultAnnData< AS > annData, + @Nullable Double radius, + @Nullable double[] boundingBoxMin, + @Nullable double[] boundingBoxMax ) { this.name = name; this.annData = annData; @@ -77,14 +82,17 @@ public SpotAnnotationImage( String name, DefaultAnnData< AS > annData, double ra createImage(); } - public double getRadius() + public Double getRadius() { return radius; } - public void setRadius( double radius ) + public void setRadius( Double radius ) { - this.radius = radius; + if ( radius != null ) + { + this.radius = radius; + } } private void createImage() @@ -107,6 +115,16 @@ private void createImage() mask = GeomMasks.closedBox( boundingBoxMin, boundingBoxMax ); + if ( radius == null) + { + // Assign each spot an area that is a fraction of the total + // covered area divided by the number of spots. + // A = Pi R^2 => R ~ Sqrt( A ) + double area = ( mask.realMax( 0 ) - mask.realMin( 0 ) ) + * ( mask.realMax( 1 ) - mask.realMin( 1 ) ); + radius = Math.sqrt( area / annotations.size() ) / 10.0; + } + // TODO: code duplication with RegionLabelImage final ArrayList< Integer > timePoints = configureTimePoints(); final Interval interval = Intervals.smallestContainingInterval( getMask() ); diff --git a/src/main/java/org/embl/mobie/lib/serialize/display/SpotDisplay.java b/src/main/java/org/embl/mobie/lib/serialize/display/SpotDisplay.java index 3bc930aaf..911c098a7 100644 --- a/src/main/java/org/embl/mobie/lib/serialize/display/SpotDisplay.java +++ b/src/main/java/org/embl/mobie/lib/serialize/display/SpotDisplay.java @@ -48,7 +48,7 @@ public class SpotDisplay< AR extends AnnotatedRegion > extends AbstractAnnotatio private Set< String > selectedSpotIds; - public double spotRadius = 1.0D; + public Double spotRadius; // Runtime