Skip to content

Commit

Permalink
WIP open project table
Browse files Browse the repository at this point in the history
  • Loading branch information
tischi committed Jun 25, 2024
1 parent 0d87181 commit 79c1929
Show file tree
Hide file tree
Showing 30 changed files with 446 additions and 164 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>org.embl.mobie</groupId>
<artifactId>mobie-viewer-fiji</artifactId>
<version>5.0.11</version>
<version>5.1.0</version>

<!-- force javadoc generation to fetch errors: -->
<!-- mvn javadoc:javadoc | grep error -->
Expand Down
49 changes: 33 additions & 16 deletions src/main/java/org/embl/mobie/MoBIE.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@
import net.imagej.ImageJ;
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.files.FileSourcesDataSetter;
import org.embl.mobie.lib.files.ImageFileSources;
import org.embl.mobie.lib.files.LabelFileSources;
import org.embl.mobie.lib.files.SourcesFromPathsCreator;
import org.embl.mobie.lib.data.*;
import org.embl.mobie.lib.hcs.HCSPlateAdder;
import org.embl.mobie.lib.hcs.Plate;
import org.embl.mobie.lib.hcs.Site;
Expand Down Expand Up @@ -97,7 +95,7 @@ public class MoBIE
public static ImageJ imageJ;

private String projectLocation;
private MoBIESettings settings;
private MoBIESettings settings = new MoBIESettings();
private Project project;
private Dataset dataset;
private String projectRoot = "";
Expand Down Expand Up @@ -151,13 +149,13 @@ public MoBIE( List< String > imagePaths, List< String > labelPaths, List< String

this.settings = settings;

final SourcesFromPathsCreator sourcesCreator = new SourcesFromPathsCreator( imagePaths, labelPaths, labelTablePaths, root, grid );
final GridSourcesFromPathsCreator sourcesCreator = new GridSourcesFromPathsCreator( imagePaths, labelPaths, labelTablePaths, root, grid );

final List< ImageFileSources > imageSources = sourcesCreator.getImageSources();
final List< LabelFileSources > labelSources = sourcesCreator.getLabelSources();
final List< ImageGridSources > imageSources = sourcesCreator.getImageSources();
final List< LabelGridSources > labelSources = sourcesCreator.getLabelSources();
Table regionTable = sourcesCreator.getRegionTable();

openImagesAndLabels( imageSources, labelSources, regionTable );
openImageAndLabelGrids( imageSources, labelSources, regionTable );
}

// open an image or object table
Expand All @@ -177,11 +175,31 @@ public MoBIE( String tablePath, List< String > imageColumns, List< String > labe
// Both can be useful and I guess we should keep on supporting both
final SourcesFromTableCreator sourcesCreator = new SourcesFromTableCreator( tablePath, imageColumns, labelColumns, root, pathMapping, grid );

final List< ImageFileSources > imageSources = sourcesCreator.getImageSources();
final List< LabelFileSources > labelSources = sourcesCreator.getLabelSources();
final List< ImageGridSources > imageSources = sourcesCreator.getImageSources();
final List< LabelGridSources > labelSources = sourcesCreator.getLabelSources();
Table regionTable = sourcesCreator.getRegionTable();

openImagesAndLabels( imageSources, labelSources, regionTable );
openImageAndLabelGrids( imageSources, labelSources, regionTable );
}

// Open a MoBIE table with MoBIETableColumnNames
// TODO: get rid of the boolean with for now is just not to have the same signature twice
public MoBIE( String tablePath, MoBIESettings settings, boolean isMoBIETable )
{
settings = settings;

IJ.log("\n# MoBIE" );
IJ.log("Opening data from table: " + tablePath );

final Table table = TableOpener.openDelimitedTextFile( tablePath );

initImageJAndMoBIE();
initProject( IOHelper.getFileName( tablePath ) );

TableSourcesDataSetter dataSetter = new TableSourcesDataSetter( table );
dataSetter.addToDataset( dataset );

initUiAndShowView( null );
}

private void initTableSaw()
Expand All @@ -205,13 +223,13 @@ private void openMoBIEProject() throws IOException
}

// TODO 2D or 3D?
private void openImagesAndLabels( List< ImageFileSources > images, List< LabelFileSources > labels, Table regionTable )
private void openImageAndLabelGrids( List< ImageGridSources > images, List< LabelGridSources > labels, Table regionTable )
{
initImageJAndMoBIE();

initProject( "" );

new FileSourcesDataSetter( images, labels, regionTable ).addDataAndDisplaysAndViews( dataset );
new GridSourcesDataSetter( images, labels, regionTable ).addDataAndDisplaysAndViews( dataset );

initUiAndShowView( null ); //dataset.views().keySet().iterator().next() );
}
Expand Down Expand Up @@ -239,8 +257,7 @@ private void initImageJAndMoBIE()

if ( settings.values.isOpenedFromCLI() )
{
// TODO: if possible open init the SciJava Services
// by different means
// TODO: if possible init the SciJava Services by different means
imageJ = new ImageJ(); // Init SciJava Services
imageJ.ui().showUI(); // Enable SciJava Command rendering
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public class ScreenShotMakerCommand extends DynamicCommand implements BdvPlaygro
@Parameter
public BdvHandle bdvHandle;

@Parameter(label="Sampling (in below units)", persist = false, callback = "showNumPixels", min = "0.0", style="format:#.00000", stepSize = "0.01")
@Parameter(label="Sampling (in below units)",
persist = false,
callback = "showNumPixels",
min = "0.0",
style="format:#.00000",
stepSize = "0.01")
public Double targetSamplingInXY = 1D;

@Parameter(label="Pixel unit", persist = false, choices = {"micrometer"} )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
import bdv.viewer.Source;
import bdv.viewer.SourceAndConverter;
import ij.IJ;
import net.imglib2.realtransform.AffineTransform3D;
import org.embl.mobie.DataStore;
import org.embl.mobie.command.CommandConstants;
import org.embl.mobie.lib.MoBIEHelper;
import org.embl.mobie.lib.image.Image;
import org.embl.mobie.lib.io.ImageDataInfo;
import org.embl.mobie.lib.serialize.transformation.AffineTransformation;
import org.embl.mobie.lib.serialize.transformation.Transformation;
import org.embl.mobie.lib.transform.TransformHelper;
import org.scijava.plugin.Parameter;
Expand All @@ -46,39 +49,59 @@
import java.util.Arrays;
import java.util.List;

@Plugin(type = BdvPlaygroundActionCommand.class, menuPath = CommandConstants.CONTEXT_MENU_ITEMS_ROOT + "Log Images Info")
@Plugin(type = BdvPlaygroundActionCommand.class,
menuPath = CommandConstants.CONTEXT_MENU_ITEMS_ROOT + "Log Images Info")
public class SourcesInfoCommand implements BdvPlaygroundActionCommand
{
static { net.imagej.patcher.LegacyInjector.preinit(); }

@Parameter
public BdvHandle bdvHandle;

@Parameter (label = "Show transformation history")
public Boolean showTransformationHistory = false;

@Override
public void run()
{
int t = bdvHandle.getViewerPanel().state().getCurrentTimepoint();
List< SourceAndConverter< ? > > visibleSacs = MoBIEHelper.getVisibleSacs( bdvHandle );
visibleSacs.forEach( sac ->
{
Image< ? > image = DataStore.sourceToImage().get( sac );
ImageDataInfo imageDataInfo = MoBIEHelper.fetchImageDataInfo( image );

Source< ? > source = sac.getSpimSource();
AffineTransform3D transform3D = new AffineTransform3D();
sac.getSpimSource().getSourceTransform( t, 0, transform3D );

IJ.log( "" );
IJ.log( "# " + source.getName() );
IJ.log( "" );
IJ.log( "Source URI: " + imageDataInfo.uri );
IJ.log( "Dataset index within URI: " + imageDataInfo.datasetId );
IJ.log( "Data type: " + source.getType().getClass().getSimpleName() );
IJ.log( "Shape: " + Arrays.toString( source.getSource( 0,0 ).dimensionsAsLongArray() ) );
IJ.log( "Shape: " + Arrays.toString( source.getSource( t,0 ).dimensionsAsLongArray() ) );
IJ.log( "Number of resolution levels: " + source.getNumMipmapLevels() );
IJ.log( "Voxel size: " + Arrays.toString( source.getVoxelDimensions().dimensionsAsDoubleArray() ) );

Image< ? > image = DataStore.sourceToImage().get( sac );
ArrayList< Transformation > transformations = TransformHelper.fetchAllImageTransformations( image );

transformations.forEach( transformation ->
Transformation imageTransformation = transformations.get( 0 );
if ( imageTransformation instanceof AffineTransformation )
{
IJ.log( "" );
IJ.log( transformation.toString() );
});
AffineTransform3D imageTransform = ( ( AffineTransformation ) imageTransformation ).getAffineTransform3D();
IJ.log( "Original image transformation: " + MoBIEHelper.print( imageTransform.getRowPackedCopy(), 3 ) );
AffineTransform3D additionalTransform = transform3D.copy().concatenate( imageTransform.inverse() );
IJ.log( "Additional MoBIE transformation: " + MoBIEHelper.print( additionalTransform.getRowPackedCopy(), 3 ) );
IJ.log( "Total transformation: " + MoBIEHelper.print( transform3D.getRowPackedCopy(), 3 ) );
}

IJ.log( "" );
if ( showTransformationHistory )
{
transformations.forEach( transformation ->
{
IJ.log( transformation.toString() );
} );
}
});
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/embl/mobie/lib/ImageDataAdder.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void addData( Dataset dataset, MoBIESettings settings )

private void addData( ImageData< ? > imageData, boolean isSegmentation )
{

final ImageDataFormat imageDataFormat = ImageDataFormat.ImageData;

if ( tableDataFormat != null )
Expand Down Expand Up @@ -153,7 +152,7 @@ private void addImageView( ImageData< ? > imageData, int datasetIndex, String im
dataset.views().put( view.getName(), view );
}

private void addSegmentationView( ImageData< ? > imageData, int setupId, String name )
private void addSegmentationView( ImageData< ? > imageData, int setupId, String name )
{
final SegmentationDisplay< ? > display = new SegmentationDisplay<>( name, Arrays.asList( name ) );
final double pixelWidth = imageData.getSourcePair( setupId ).getB().getVoxelDimensions().dimension( 0 );
Expand Down
79 changes: 32 additions & 47 deletions src/main/java/org/embl/mobie/lib/MoBIEHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@
import bdv.viewer.SourceAndConverter;
import mpicbg.spim.data.sequence.FinalVoxelDimensions;
import mpicbg.spim.data.sequence.VoxelDimensions;
import net.imglib2.Cursor;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.numeric.RealType;
import net.imglib2.util.Intervals;
import net.imglib2.util.ValuePair;
import net.imglib2.view.Views;
import org.embl.mobie.io.ImageDataOpener;
import org.embl.mobie.io.github.GitHubUtils;
import org.embl.mobie.io.imagedata.ImageData;
import org.embl.mobie.lib.image.Image;
import org.embl.mobie.lib.image.ImageDataImage;
import org.embl.mobie.lib.image.TransformedImage;
import org.embl.mobie.lib.io.ImageDataInfo;
import org.janelia.saalfeldlab.n5.universe.metadata.canonical.CanonicalDatasetMetadata;
import sc.fiji.bdvpg.scijava.services.SourceAndConverterBdvDisplayService;
import sc.fiji.bdvpg.services.SourceAndConverterServices;
Expand All @@ -61,6 +59,29 @@

public abstract class MoBIEHelper
{
public static ImageDataInfo fetchImageDataInfo( Image< ? > image )
{
if ( image instanceof ImageDataImage )
{
ImageDataInfo imageDataInfo = new ImageDataInfo();
imageDataInfo.uri = ( ( ImageDataImage ) image ).getUri();
imageDataInfo.datasetId = ( ( ImageDataImage ) image ).getSetupId();
return imageDataInfo;
}
else if ( image instanceof TransformedImage )
{
TransformedImage transformedImage = ( TransformedImage ) image;
Image< ? > wrappedImage = transformedImage.getWrappedImage();
return fetchImageDataInfo( wrappedImage );
}
else
{
ImageDataInfo imageDataInfo = new ImageDataInfo();
imageDataInfo.uri = "Could not determine URI of " + image.getClass().getSimpleName();
return imageDataInfo;
}
}

public static final String GRID_TYPE_HELP = "If the images are different and not too many, use Transformed for more flexible visualisation.\n" +
"If all images are identical use Stitched for better performance.";

Expand All @@ -73,12 +94,17 @@ public static String print(double[] array, int numSignificantDigits) {
DecimalFormat formatter = new DecimalFormat(pattern.toString());

StringBuilder result = new StringBuilder();
result.append( "(" );
for (int i = 0; i < array.length; i++) {
if (Math.abs(array[i]) < 1e-10) {
array[i] = 0.0; // Explicitly set to zero to remove negative sign
}
result.append(formatter.format(array[i]));
if (i < array.length - 1) {
result.append(", ");
}
}
result.append( ")" );
return result.toString();
}

Expand Down Expand Up @@ -311,47 +337,6 @@ public static List< String > getFullPaths( String regex, String root )
}
}

public static double[] estimateMinMax(
RandomAccessibleInterval<? extends RealType<?> > rai)
{
Cursor<? extends RealType<?>> cursor = Views.iterable(rai).cursor();
if (!cursor.hasNext()) return new double[]{0, 255};
long stepSize = Intervals.numElements(rai) / 10000 + 1;
int randomLimit = (int) Math.min(Integer.MAX_VALUE, stepSize);
Random random = new Random(42);
double min = cursor.next().getRealDouble();
double max = min;
while (cursor.hasNext()) {
double value = cursor.get().getRealDouble();
cursor.jumpFwd(stepSize + random.nextInt(randomLimit));
min = Math.min(min, value);
max = Math.max(max, value);
}
return new double[]{min, max};
}

public static <T extends RealType<T> > double[] computeMinMax( RandomAccessibleInterval<T> rai) {
Cursor<T> cursor = Views.iterable(rai).cursor();

// Initialize min and max with the first element
T type = cursor.next();
T min = type.copy();
T max = type.copy();

// Iterate over the remaining elements to find min and max
while (cursor.hasNext()) {
type = cursor.next();
if (type.compareTo(min) < 0) {
min.set(type);
}
if (type.compareTo(max) > 0) {
max.set(type);
}
}

return new double[]{ min.getRealDouble(), max.getRealDouble() };
}

public static String toURI( File file )
{
String string = file.toString();
Expand Down
Loading

0 comments on commit 79c1929

Please sign in to comment.