diff --git a/src/main/java/fr/pasteur/ida/zellige/ReferenceSurfaceExtraction.java b/src/main/java/fr/pasteur/ida/zellige/ReferenceSurfaceExtraction.java index 5dc328f05385264152873b68bf5578c26d3e0150..8719f7491f912ed682f8b41ac790270d53546d44 100644 --- a/src/main/java/fr/pasteur/ida/zellige/ReferenceSurfaceExtraction.java +++ b/src/main/java/fr/pasteur/ida/zellige/ReferenceSurfaceExtraction.java @@ -124,174 +124,171 @@ public class ReferenceSurfaceExtraction< T extends RealType< T > & NativeType< T public static < T extends RealType< T > & NativeType< T > > void run(String CSV_FILE)throws IOException, ParameterException { ImageJ ij = new ImageJ(); -// ij.launch( args ); setLogStatus(); - try ( Stream< String > lines = Files.lines( Paths.get( CSV_FILE ) ) ) + try ( Stream< String > lines = Files.lines( Paths.get( CSV_FILE ) ) )//try-with-resources ensures the stream is closed properly. { List< List< String > > records = lines.map( line -> Arrays.asList( line.split( SEMI_COLON_DELIMITER ) ) ) .collect( Collectors.toList() ); LOGGER.debug( "the csv is loaded" ); for ( int i = 1; i < records.size(); i++ ) // skip the header { - List< String > line = records.get( i ); - // Input - String inputPath = line.get( 0 ); - Dataset dataset = ArgumentCheck.input( ij.scifio().datasetIO().open( inputPath ) ); - String inputName = FilenameUtils.removeExtension(dataset.getName()); - String id = uniqueID(); - Path datasetLocation = Paths.get(inputPath).getParent(); - - Path outputPath = Files.createDirectories(Paths.get(String.format("%s\\%s%s",datasetLocation.toString() , inputName, id))); - setLogFilePath( String.format("%s\\log.txt", outputPath) ); - - int nbChannel = ( int ) dataset.getChannels() > 0 ? ( int ) dataset.getChannels(): 1; - ArrayList< ColorTable > colorTables = new ArrayList<>(); - for ( int channel = 0; channel < nbChannel ; channel++ ) + try { - colorTables.add(dataset.getImgPlus().getColorTable( channel )); - } + List< String > line = records.get( i ); + // Input + String inputPath = line.get( 0 ); + Dataset dataset = ArgumentCheck.input( ij.scifio().datasetIO().open( inputPath ) ); + String inputName = FilenameUtils.removeExtension( dataset.getName() ); + String id = uniqueID(); + Path datasetLocation = Paths.get( inputPath ).getParent(); + + Path outputPath = Files.createDirectories( Paths.get( String.format( "%s\\%s%s", datasetLocation.toString(), inputName, id ) ) ); + setLogFilePath( String.format( "%s\\log.txt", outputPath ) ); + + int nbChannel = ( int ) dataset.getChannels() > 0 ? ( int ) dataset.getChannels() : 1; + ArrayList< ColorTable > colorTables = new ArrayList<>(); + for ( int channel = 0; channel < nbChannel; channel++ ) + { + colorTables.add( dataset.getImgPlus().getColorTable( channel ) ); + } - // Preprocessing Parameters - LOGGER.info( "Preprocessing parameters" ); - LOGGER.info( "------------------------" ); - int bin = ArgumentCheck.bin( line.get( 1 ) ); - int channel = ArgumentCheck.channel( line.get( 2 ), nbChannel ); - - - // Selection Parameters - LOGGER.info( "Selection parameters" ); - LOGGER.info( "--------------------" ); - int amplitude = ArgumentCheck.amplitude( line.get( 3 ) ); - int otsu = ArgumentCheck.otsu( line.get( 4 ) ); - int islandSize = ArgumentCheck.islandSize( line.get( 5 ) ); - double sigmaXY = ArgumentCheck.sigmaXY( line.get( 6 ) ); - double sigmaZ = ArgumentCheck.sigmaZ( line.get( 7 ) ); - - // Construction Parameters - LOGGER.info( "Construction parameters" ); - LOGGER.info( "-----------------------" ); - int FIRST_ROUND = 1; - double startingOsSize1 = ArgumentCheck.startingOsSize( line.get( 8 ), FIRST_ROUND ); - int overlap1 = ArgumentCheck.overlap( line.get( 9 ), FIRST_ROUND ); - double connexityRate1 = ArgumentCheck.connexity( line.get( 10 ), FIRST_ROUND ); - int SECOND_ROUND = 2; - double startingOsSize2 = ArgumentCheck.startingOsSize( line.get( 11 ), SECOND_ROUND ); - int overlap2 = ArgumentCheck.overlap( line.get( 12 ), SECOND_ROUND ); - double connexityRate2 = ArgumentCheck.connexity( line.get( 13 ), SECOND_ROUND ); - double surfaceMinSizeFactor = Double.parseDouble( line.get( 14 ) ); - - // Projection Parameters - LOGGER.info( "\nProjection parameters" ); - LOGGER.info( "--------------------" ); - ArrayList< ChannelProjectionParameter > pp = new ArrayList<>(); - for ( int n = 0; n < nbChannel; n++ ) - { - LOGGER.info( " Channel {}", (n+1 )); + // Preprocessing Parameters + LOGGER.info( "Preprocessing parameters" ); + LOGGER.info( "------------------------" ); + int bin = ArgumentCheck.bin( line.get( 1 ) ); + int channel = ArgumentCheck.channel( line.get( 2 ), nbChannel ); + + + // Selection Parameters + LOGGER.info( "Selection parameters" ); + LOGGER.info( "--------------------" ); + int amplitude = ArgumentCheck.amplitude( line.get( 3 ) ); + int otsu = ArgumentCheck.otsu( line.get( 4 ) ); + int islandSize = ArgumentCheck.islandSize( line.get( 5 ) ); + double sigmaXY = ArgumentCheck.sigmaXY( line.get( 6 ) ); + double sigmaZ = ArgumentCheck.sigmaZ( line.get( 7 ) ); + + // Construction Parameters + LOGGER.info( "Construction parameters" ); + LOGGER.info( "-----------------------" ); + int FIRST_ROUND = 1; + double startingOsSize1 = ArgumentCheck.startingOsSize( line.get( 8 ), FIRST_ROUND ); + int overlap1 = ArgumentCheck.overlap( line.get( 9 ), FIRST_ROUND ); + double connexityRate1 = ArgumentCheck.connexity( line.get( 10 ), FIRST_ROUND ); + int SECOND_ROUND = 2; + double startingOsSize2 = ArgumentCheck.startingOsSize( line.get( 11 ), SECOND_ROUND ); + int overlap2 = ArgumentCheck.overlap( line.get( 12 ), SECOND_ROUND ); + double connexityRate2 = ArgumentCheck.connexity( line.get( 13 ), SECOND_ROUND ); + double surfaceMinSizeFactor = Double.parseDouble( line.get( 14 ) ); + + // Projection Parameters + LOGGER.info( "\nProjection parameters" ); + LOGGER.info( "--------------------" ); + ArrayList< ChannelProjectionParameter > pp = new ArrayList<>(); + for ( int n = 0; n < nbChannel; n++ ) + { + LOGGER.info( " Channel {}", ( n + 1 ) ); // int channelNb = n * 1; - int delta = ArgumentCheck.delta( line.get( 15 + (3 * n) ), n ); - int offset = ArgumentCheck.offset( line.get( 16 + (3 * n) ), n ); - String method = ArgumentCheck.method( line.get( 17 +(3 * n) ), n ); - ChannelProjectionParameter parameters = new ChannelProjectionParameter( delta, offset, method ); - pp.add( parameters ); - } - ProjectionParameters projectionParameters =new ProjectionParameters( pp ); - LOGGER.debug( "All argument are checked." ); + int delta = ArgumentCheck.delta( line.get( 15 + ( 3 * n ) ), n ); + int offset = ArgumentCheck.offset( line.get( 16 + ( 3 * n ) ), n ); + String method = ArgumentCheck.method( line.get( 17 + ( 3 * n ) ), n ); + ChannelProjectionParameter parameters = new ChannelProjectionParameter( delta, offset, method ); + pp.add( parameters ); + } + ProjectionParameters projectionParameters = new ProjectionParameters( pp ); + LOGGER.debug( "All argument are checked." ); - //Parameters - SelectionParameters selectionParameters = new SelectionParameters( amplitude, otsu, sigmaZ, sigmaXY, islandSize ); - PretreatmentParameters pretreatmentParameters = new PretreatmentParameters( channel, bin ); + //Parameters + SelectionParameters selectionParameters = new SelectionParameters( amplitude, otsu, sigmaZ, sigmaXY, islandSize ); + PretreatmentParameters pretreatmentParameters = new PretreatmentParameters( channel, bin ); // ClassificationParameters classificationParameters = new ClassificationParameters( amplitude, otsu ); // PostTreatmentParameters postTreatmentParameters = new PostTreatmentParameters( sigmaXY, sigmaZ, islandSize, CONNEXITY ); - ConstructionParameters[] constructionParameters = new ConstructionParameters[2]; - constructionParameters [0] = new ConstructionParameters( startingOsSize1, overlap1, connexityRate1, surfaceMinSizeFactor ); - constructionParameters [1] = new ConstructionParameters( startingOsSize2, overlap2, connexityRate2, surfaceMinSizeFactor ); + ConstructionParameters[] constructionParameters = new ConstructionParameters[ 2 ]; + constructionParameters[ 0 ] = new ConstructionParameters( startingOsSize1, overlap1, connexityRate1, surfaceMinSizeFactor ); + constructionParameters[ 1 ] = new ConstructionParameters( startingOsSize2, overlap2, connexityRate2, surfaceMinSizeFactor ); - //set the input - Img<T> input ; - if ( nbChannel == 1 ) - { - try + //set the input + Img< T > input; + if ( nbChannel == 1 ) { - input = ( create4dimImage( dataset ) ); + try + { + input = ( create4dimImage( dataset ) ); + } + catch ( Exception e ) + { + throw new RuntimeException( e ); + } } - catch ( Exception e ) + else { - throw new RuntimeException( e ); + input = ( ( Img< T > ) dataset.getImgPlus() ); } - } - else - { - input = ( ( Img< T > ) dataset.getImgPlus() ); - } - // Set the target channel - RandomAccessibleInterval<T> inputChannel = Views.hyperSlice( input, 2, (channel -1) ); + // Set the target channel + RandomAccessibleInterval< T > inputChannel = Views.hyperSlice( input, 2, ( channel - 1 ) ); - // Preprocessing and selection steps - ImageJFunctions.show(input, "input"); - Img <FloatType > selectedPixels = Selection.run(inputChannel, pretreatmentParameters, selectionParameters ); + // Preprocessing and selection steps + ImageJFunctions.show( input, "input" ); + Img< FloatType > selectedPixels = Selection.run( inputChannel, pretreatmentParameters, selectionParameters ); - // Construction step - ArrayList< ReferenceSurface< T > > referenceSurfaces = Construction.run(constructionParameters, input, input.factory(), selectedPixels, bin); + // Construction step + ArrayList< ReferenceSurface< T > > referenceSurfaces = Construction.run( constructionParameters, input, input.factory(), selectedPixels, bin ); - // Projection step - ArrayList< ImgPlus<T> > finalProjections = new ArrayList<>(); + // Projection step + ArrayList< ImgPlus< T > > finalProjections = new ArrayList<>(); - for ( int j = 0; j < referenceSurfaces.size(); j++ ) - { - ArrayList<Img<T>> channelProjections = new ArrayList<>(); - Img< UnsignedShortType > zMap = referenceSurfaces.get(j).getzMap(); - for ( int k = 0; k < nbChannel; k++ ) + for ( int j = 0; j < referenceSurfaces.size(); j++ ) { - ChannelProjectionParameter cpp = projectionParameters.getChannelProjectionParameters().get( k ); - RandomAccessibleInterval<T> channelImg = Views.hyperSlice( input, 2, k ); - Img<T> subVolume = ReferenceSurfaceProjection.getSubStack( channelImg, input.factory(),zMap, cpp.getDelta(), cpp.getOffset()); - - Img<T> p = ReferenceSurfaceProjection.run( subVolume, cpp.getMethod()); - channelProjections.add(k, p ); + ArrayList< Img< T > > channelProjections = new ArrayList<>(); + Img< UnsignedShortType > zMap = referenceSurfaces.get( j ).getzMap(); + for ( int k = 0; k < nbChannel; k++ ) + { + ChannelProjectionParameter cpp = projectionParameters.getChannelProjectionParameters().get( k ); + RandomAccessibleInterval< T > channelImg = Views.hyperSlice( input, 2, k ); + Img< T > subVolume = ReferenceSurfaceProjection.getSubStack( channelImg, input.factory(), zMap, cpp.getDelta(), cpp.getOffset() ); + + Img< T > p = ReferenceSurfaceProjection.run( subVolume, cpp.getMethod() ); + channelProjections.add( k, p ); + } + Img< T > temp = ReferenceSurfaceProjection.regroupChannels( channelProjections ); + String projectionName = String.format( "%s_proj_%d", inputName, ( j + 1 ) ); + ImgPlus< T > finalProjection = ReferenceSurfaceProjection.setColorsAndName( temp, colorTables, projectionName ); + finalProjections.add( finalProjection ); } - Img<T> temp = ReferenceSurfaceProjection.regroupChannels( channelProjections ); - String projectionName = String.format( "%s_proj_%d", inputName, (j+1) ); - ImgPlus< T > finalProjection = ReferenceSurfaceProjection.setColorsAndName( temp, colorTables, projectionName ); - finalProjections.add( finalProjection ); + ImgSaver saver = new ImgSaver(); + for ( int j = 0; j < referenceSurfaces.size(); j++ ) + { + ImgPlus< T > projection = finalProjections.get( j ); + String imgLocation = String.format( "%s\\%s%s", outputPath, projection.getName(), ".tif" ); + saver.saveImg( imgLocation, projection ); + } + LOGGER.debug( "End of process" ); + fileAppender.stop(); + } + catch (DataValidationException | TimeLapseException | NoA3DStackException e ) + { + LOGGER.info( "Error processing line {} : {}", i, e.getMessage() ); + fileAppender.stop(); } - ImgSaver saver = new ImgSaver(); - for ( int j = 0; j < referenceSurfaces.size(); j++ ) + catch(ImageTooLargeException | NoClassificationException | NoSurfaceFoundException e) { - ImgPlus<T> projection = finalProjections.get(j); - String imgLocation = String.format( "%s\\%s%s",outputPath , projection.getName(), ".tif"); - saver.saveImg(imgLocation, projection ) ; + LOGGER.info( "" ); + fileAppender.stop(); } - LOGGER.debug("End of process"); - fileAppender.stop(); + } } - catch ( TimeLapseException e ) - { - throw new RuntimeException( e ); - } - catch ( DataValidationException e ) - { - throw new RuntimeException( e ); - } - catch ( NoSurfaceFoundException e ) - { - throw new RuntimeException( e ); - } - catch ( NoClassificationException e ) - { - throw new RuntimeException( e ); - } - catch ( ImageTooLargeException e ) - { - throw new RuntimeException( e ); - } - catch ( NoA3DStackException e ) + catch ( IOException e ) { + LOGGER.info( "Failed to open file: {}", CSV_FILE ); throw new RuntimeException( e ); } + + + } /** * Creates a new LogAppender to display the program state in the GUI. diff --git a/src/main/java/fr/pasteur/ida/zellige/gui/ChannelPanel.java b/src/main/java/fr/pasteur/ida/zellige/gui/ChannelPanel.java index 5c17c399025d728e39eb97a3687b5a3f77f2b702..ef5d7239533c855c31cb2033c974e9c0582024c5 100644 --- a/src/main/java/fr/pasteur/ida/zellige/gui/ChannelPanel.java +++ b/src/main/java/fr/pasteur/ida/zellige/gui/ChannelPanel.java @@ -51,8 +51,9 @@ public class ChannelPanel extends Pane implements Initializable ObservableList< String > list = FXCollections.observableArrayList( "MIP", "Mean" ); method.setItems( list ); method.getSelectionModel().selectFirst(); + SpinnerValueFactory< Integer > valueFactory2 = - new SpinnerValueFactory.IntegerSpinnerValueFactory( 0, 30, 0, 1 ); + new SpinnerValueFactory.IntegerSpinnerValueFactory( -3, 3, 0, 1 ); offset.setValueFactory( valueFactory2 ); } @@ -76,4 +77,13 @@ public class ChannelPanel extends Pane implements Initializable { return view; } + + + public void setSpinners (int nbSlices) + { + deltaZ.setValueFactory( new SpinnerValueFactory.IntegerSpinnerValueFactory( 0, nbSlices - 1, 1, 1 )); + + offset.setValueFactory( new SpinnerValueFactory.IntegerSpinnerValueFactory( - (nbSlices - 1), nbSlices - 1, 1, 1 )); + + } } diff --git a/src/main/java/fr/pasteur/ida/zellige/gui/ReferenceSurfacePanel.java b/src/main/java/fr/pasteur/ida/zellige/gui/ReferenceSurfacePanel.java index d1ab05ec00eccd6ee59d35cab3bd7e4f9b4793f4..1b75b4e89512fbeca86001370587023a82be8c0b 100644 --- a/src/main/java/fr/pasteur/ida/zellige/gui/ReferenceSurfacePanel.java +++ b/src/main/java/fr/pasteur/ida/zellige/gui/ReferenceSurfacePanel.java @@ -18,14 +18,13 @@ public class ReferenceSurfacePanel < T extends RealType< T > & NativeType< T > > private TabPane channelTools; - @FXML private Button run; private final ProjectionProperties<T> projectionProperties; - public ReferenceSurfacePanel( int channels) + public ReferenceSurfacePanel( int channels, int nbSlices) { FXMLLoader fxmlLoader = new FXMLLoader( ReferenceSurfacePanel.class.getClassLoader(). getResource( "fr.pasteur.ida.zellige.gui.view/ReferenceSurfacePanel.fxml" ) ); @@ -43,6 +42,7 @@ public class ReferenceSurfacePanel < T extends RealType< T > & NativeType< T > > { Tab tab = new Tab( "C" + i ); ChannelPanel pane = new ChannelPanel(); + pane.setSpinners( nbSlices ); tab.setContent( pane ); getChannelTools().getTabs().add( tab ); diff --git a/src/main/java/fr/pasteur/ida/zellige/gui/controller/PreprocessingController.java b/src/main/java/fr/pasteur/ida/zellige/gui/controller/PreprocessingController.java index d5753bff91f63594baacb161302dbeac7f76631c..073f0781e2c9d7c3f28d57c3e5c29281f3dc6b4c 100644 --- a/src/main/java/fr/pasteur/ida/zellige/gui/controller/PreprocessingController.java +++ b/src/main/java/fr/pasteur/ida/zellige/gui/controller/PreprocessingController.java @@ -132,7 +132,7 @@ public class PreprocessingController< T extends RealType< T > & NativeType< T > StackPane stackPane = parentController.getProjectionController().getStackPane(); stackPane.getChildren().clear(); parentController.getProjectionController().getModel().getNumberOfChannels().set( getModel().getNbOfChannels().get() ); - ReferenceSurfacePanel<T> panel = new ReferenceSurfacePanel<>( getModel().getNbOfChannels().get() ); + ReferenceSurfacePanel<T> panel = new ReferenceSurfacePanel<>( getModel().getNbOfChannels().get(), (int) getModel().getInput().getValue().dimension( 3 ) ); stackPane.getChildren().add( panel ); } else diff --git a/src/main/java/fr/pasteur/ida/zellige/gui/controller/ProjectionController.java b/src/main/java/fr/pasteur/ida/zellige/gui/controller/ProjectionController.java index 4521dec90b4abceb22c3b576a9866238ef1e29c2..d9f019567c21064a490f4eb1ef45cae994e462ec 100644 --- a/src/main/java/fr/pasteur/ida/zellige/gui/controller/ProjectionController.java +++ b/src/main/java/fr/pasteur/ida/zellige/gui/controller/ProjectionController.java @@ -121,7 +121,7 @@ public class ProjectionController< T extends RealType< T > & NativeType< T > > i for ( int i = 0; i < getModel().getReferenceSurfaces().get().size(); i++ ) { // sets a new projection panel according to the input channel number. - ReferenceSurfacePanel< T > panel = new ReferenceSurfacePanel<>( channels ); + ReferenceSurfacePanel< T > panel = new ReferenceSurfacePanel<>( channels, (int) parentController.getPreprocessingController().getModel().getInput().getValue().dimension( 3 ) ); // Adding properties to each reference surfaces getModel().getProjectionProperties().get().add( panel.getProjectionProperties() ); stackPane.getChildren().add( panel );