From a042689106d1642d5dbbcfbb7ac1b01b17bb786d Mon Sep 17 00:00:00 2001 From: Christian Tischer Date: Tue, 5 Mar 2024 16:22:28 +0100 Subject: [PATCH] Implement improve image path parsing for tables --- .../mobie/lib/color/lut/GlasbeyARGBLut.java | 12 +++++- .../mobie/lib/files/ImageFileSources.java | 41 +++++++++++-------- .../mobie/lib/files/LabelFileSources.java | 8 ++-- .../lib/files/SourcesFromPathsCreator.java | 8 ++-- 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/embl/mobie/lib/color/lut/GlasbeyARGBLut.java b/src/main/java/org/embl/mobie/lib/color/lut/GlasbeyARGBLut.java index fe2051ddf..4715dc046 100644 --- a/src/main/java/org/embl/mobie/lib/color/lut/GlasbeyARGBLut.java +++ b/src/main/java/org/embl/mobie/lib/color/lut/GlasbeyARGBLut.java @@ -31,6 +31,7 @@ import net.imglib2.type.numeric.ARGBType; import java.util.ArrayList; +import java.util.concurrent.ExecutionException; /** * Modified version of Glasbey LUT, @@ -62,8 +63,15 @@ public GlasbeyARGBLut( int alpha ) @Override public int getARGB( double x ) { - final int index = ( int ) Math.ceil( x * ( numColors - 1 ) ); - return indices.get( index ); + try + { + final int index = ( int ) Math.ceil( x * ( numColors - 1 ) ); + return indices.get( index ); + } + catch ( Exception e ) + { + throw new RuntimeException( e ); + } } @Override diff --git a/src/main/java/org/embl/mobie/lib/files/ImageFileSources.java b/src/main/java/org/embl/mobie/lib/files/ImageFileSources.java index f073b6baa..c1ddc4a44 100644 --- a/src/main/java/org/embl/mobie/lib/files/ImageFileSources.java +++ b/src/main/java/org/embl/mobie/lib/files/ImageFileSources.java @@ -31,6 +31,7 @@ import ij.IJ; import net.imglib2.realtransform.AffineTransform3D; import org.apache.commons.io.FilenameUtils; +import org.embl.mobie.io.util.IOHelper; import org.embl.mobie.lib.MoBIEHelper; import org.embl.mobie.lib.io.TableImageSource; import org.embl.mobie.lib.source.Metadata; @@ -111,6 +112,7 @@ public ImageFileSources( String name, Table table, String imageColumn, Integer c nameToPath = new LinkedHashMap<>(); // needed for joining the tables below when creating the region table int numRows = table.rowCount(); + if ( imageColumn.contains( "_IMG" ) ) { // Automic table @@ -132,26 +134,20 @@ public ImageFileSources( String name, Table table, String imageColumn, Integer c } } } - else if ( imageColumn.startsWith( "FileName_" ) ) + else if ( isCellProfilerColumn( imageColumn, table ) ) { - TableImageSource tableImageSource = new TableImageSource( imageColumn ); - - // Deal with CellProfiler tables - String postfix = tableImageSource.columnName.substring("FileName_".length()); + String postfix = imageColumn.substring("FileName_".length()); String folderColumn = "PathName_" + postfix; - if (table.containsColumn( folderColumn ) ) { - StringColumn absolutePathColumn = - table.stringColumn( folderColumn ) - .concatenate( File.separator ) - .concatenate( table.stringColumn( tableImageSource.columnName ) ); - absolutePathColumn.setName("AbsolutePath_" + postfix); - table.addColumns(absolutePathColumn); - root = null; // the paths are now absolute - tableImageSource = new TableImageSource( tableImageSource.name, "AbsolutePath_" + postfix, tableImageSource.channelIndex, pathMapping ); - } - - imageFileSources.add( new ImageFileSources( tableImageSource.name, table, tableImageSource.columnName, tableImageSource.channelIndex, root, pathMapping, gridType ) ); + for ( int rowIndex = 0; rowIndex < numRows; rowIndex++ ) + { + String fileName = table.getString( rowIndex, imageColumn ); + String folder = table.getString( rowIndex, folderColumn ); + String path = IOHelper.combinePath( folder, fileName ); + String imageName = createImageName( channelIndex, fileName ); + nameToFullPath.put( imageName, applyPathMapping( pathMapping, path ) ); + nameToPath.put( imageName, fileName ); + } } else { @@ -171,6 +167,17 @@ else if ( imageColumn.startsWith( "FileName_" ) ) dealWithTimepointsInObjectTableIfNeeded( name, table, imageColumn ); } + protected static boolean isCellProfilerColumn( String column, Table table ) + { + if ( ! column.startsWith( "FileName_" ) ) return false; + + String postfix = column.substring("FileName_".length()); + String folderColumn = "PathName_" + postfix; + boolean containsFolderColumn = table.containsColumn( folderColumn ); + + return containsFolderColumn; + } + protected static List< String > getFullPaths( String regex, String root ) { if ( root != null ) diff --git a/src/main/java/org/embl/mobie/lib/files/LabelFileSources.java b/src/main/java/org/embl/mobie/lib/files/LabelFileSources.java index 0f541ff65..4c8fb4769 100644 --- a/src/main/java/org/embl/mobie/lib/files/LabelFileSources.java +++ b/src/main/java/org/embl/mobie/lib/files/LabelFileSources.java @@ -77,15 +77,15 @@ public LabelFileSources( String name, Table table, String columnName, Integer ch } } - public LabelFileSources( String name, String labelsPath, Integer channelIndex, String root, String pathMapping, GridType grid ) + public LabelFileSources( String name, String labelsPath, Integer channelIndex, String root, GridType grid ) { - super( name, labelsPath, channelIndex, root, pathMapping, grid ); + super( name, labelsPath, channelIndex, root, grid ); } - public LabelFileSources( String name, String path, Integer channelIndex, String labelTablePath, String root, String pathMapping, GridType grid ) + public LabelFileSources( String name, String path, Integer channelIndex, String labelTablePath, String root, GridType grid ) { - super( name, path, channelIndex, root, pathMapping, grid ); + super( name, path, channelIndex, root, grid ); final List< String > labelTablePaths = getFullPaths( labelTablePath, root ); final ArrayList< String > labelMaskNames = new ArrayList<>( nameToFullPath.keySet() ); diff --git a/src/main/java/org/embl/mobie/lib/files/SourcesFromPathsCreator.java b/src/main/java/org/embl/mobie/lib/files/SourcesFromPathsCreator.java index d4ab797fd..bab9c47ab 100644 --- a/src/main/java/org/embl/mobie/lib/files/SourcesFromPathsCreator.java +++ b/src/main/java/org/embl/mobie/lib/files/SourcesFromPathsCreator.java @@ -43,15 +43,13 @@ public class SourcesFromPathsCreator public SourcesFromPathsCreator( List < String > imagePaths, List < String > labelPaths, List < String > labelTablePaths, String root, GridType grid ) { - // FIXME: Remove all the pathMapping from the Constructors here! - // images // imageSources = new ArrayList<>(); for ( String imagePath : imagePaths ) { final FileImageSource fileImageSource = new FileImageSource( imagePath ); - imageSources.add( new ImageFileSources( fileImageSource.name, fileImageSource.path, fileImageSource.channelIndex, root, null, grid ) ); + imageSources.add( new ImageFileSources( fileImageSource.name, fileImageSource.path, fileImageSource.channelIndex, root, grid ) ); } // segmentation images @@ -64,11 +62,11 @@ public SourcesFromPathsCreator( List < String > imagePaths, List < String > labe if ( labelTablePaths.size() > labelSourceIndex ) { final String labelTablePath = labelTablePaths.get( labelSourceIndex ); - labelSources.add( new LabelFileSources( fileImageSource.name, fileImageSource.path, fileImageSource.channelIndex, labelTablePath, root, null, grid ) ); + labelSources.add( new LabelFileSources( fileImageSource.name, fileImageSource.path, fileImageSource.channelIndex, labelTablePath, root, grid ) ); } else { - labelSources.add( new LabelFileSources( fileImageSource.name, fileImageSource.path, fileImageSource.channelIndex, root, null, grid ) ); + labelSources.add( new LabelFileSources( fileImageSource.name, fileImageSource.path, fileImageSource.channelIndex, root, grid ) ); } } }