From 31ff6aa80cd76dea00c62b490d0dc269cd1cc2be Mon Sep 17 00:00:00 2001
From: ctrebeau <ctrebeau@pasteur.fr>
Date: Tue, 22 Oct 2024 17:08:00 +0200
Subject: [PATCH] Bug fix : memory isssue - the function
 "factory.create(input)" is replaced by
 "factory.create(input.dimensionAsLongArray)" to avoid using too much memory.

---
 .../ida/zellige/steps/construction/Interpolation.java     | 2 +-
 .../steps/projection/ReferenceSurfaceProjection.java      | 4 ++--
 .../selection/classification/AmplitudeClassification.java | 4 ++--
 .../steps/selection/classification/Classification.java    | 2 +-
 .../selection/classification/OtsuClassification.java      | 8 ++++----
 .../steps/selection/postTreatment/PostTreatment.java      | 2 +-
 .../postTreatment/islandSearch/IslandSearch.java          | 2 +-
 .../steps/selection/pretreatment/Pretreatment.java        | 2 +-
 .../zellige/steps/selection/util/ExtremaDetection.java    | 2 +-
 .../zellige/steps/selection/util/ExtremaDetection2D.java  | 2 +-
 10 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/construction/Interpolation.java b/src/main/java/fr/pasteur/ida/zellige/steps/construction/Interpolation.java
index 78a3590..877defc 100644
--- a/src/main/java/fr/pasteur/ida/zellige/steps/construction/Interpolation.java
+++ b/src/main/java/fr/pasteur/ida/zellige/steps/construction/Interpolation.java
@@ -170,7 +170,7 @@ public class Interpolation
         private Img< UnsignedShortType > execute( RandomAccessibleInterval< UnsignedShortType > input, int dimension )
         {
             ImgFactory< UnsignedShortType > factory = new ArrayImgFactory<>( new UnsignedShortType() );
-            Img< UnsignedShortType > interpolated = factory.create( input );
+            Img< UnsignedShortType > interpolated = factory.create( input.dimensionsAsLongArray() );
             ImgUtil.copy( input, interpolated );
             RandomAccess< UnsignedShortType > interpolatedAccess = interpolated.randomAccess();
             RandomAccess< UnsignedShortType > zMapAccess = input.randomAccess();
diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/projection/ReferenceSurfaceProjection.java b/src/main/java/fr/pasteur/ida/zellige/steps/projection/ReferenceSurfaceProjection.java
index a1a88f2..e30c964 100644
--- a/src/main/java/fr/pasteur/ida/zellige/steps/projection/ReferenceSurfaceProjection.java
+++ b/src/main/java/fr/pasteur/ida/zellige/steps/projection/ReferenceSurfaceProjection.java
@@ -194,7 +194,7 @@ public class ReferenceSurfaceProjection< T extends RealType< T > & NativeType< T
     public static < T extends RealType< T > & NativeType< T > > Img< BitType > getSegmentedSurfaceMask
             ( IterableInterval< T > segmentedSurface, int delta )
     {
-        Img< BitType > segmentedSurfaceMask = new ArrayImgFactory<>( new BitType() ).create( segmentedSurface );
+        Img< BitType > segmentedSurfaceMask = new ArrayImgFactory<>( new BitType() ).create( segmentedSurface.dimensionsAsLongArray() );
         Cursor< T > surfaceAccess = segmentedSurface.cursor();
         RandomAccess< BitType > mapAccess = segmentedSurfaceMask.randomAccess();
         while ( surfaceAccess.hasNext() )
@@ -223,7 +223,7 @@ public class ReferenceSurfaceProjection< T extends RealType< T > & NativeType< T
     public static < T extends RealType< T > & NativeType< T > > Img< T > getSegmentedSurface
     ( Img< UnsignedShortType > extractedHeightMap, RandomAccessibleInterval< T > originalInput, ImgFactory< T > factory )
     {
-        Img< T > segmentedSurface = factory.create( originalInput );
+        Img< T > segmentedSurface = factory.create( originalInput.dimensionsAsLongArray() );
         RandomAccess< T > surfaceAccess = segmentedSurface.randomAccess();
         RandomAccess< T > inputAccess = originalInput.randomAccess();
         Cursor< UnsignedShortType > heightMapCursor = extractedHeightMap.cursor();
diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/AmplitudeClassification.java b/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/AmplitudeClassification.java
index ac6e9de..badccd5 100644
--- a/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/AmplitudeClassification.java
+++ b/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/AmplitudeClassification.java
@@ -82,7 +82,7 @@ public class AmplitudeClassification< T extends RealType< T > & NativeType< T >
                                                                                     final ImgFactory< T > factory, double amplitudeThreshold ) throws DataValidationException
     {
         long start = System.currentTimeMillis();
-        Img< BitType > amplitude = new ArrayImgFactory<>( new BitType() ).create( input );
+        Img< BitType > amplitude = new ArrayImgFactory<>( new BitType() ).create( input.dimensionsAsLongArray() );
         AmplitudeClassification< T > classification =
                 new AmplitudeClassification<>( input, factory, amplitude, amplitudeThreshold );
         classification.run();
@@ -132,7 +132,7 @@ public class AmplitudeClassification< T extends RealType< T > & NativeType< T >
     private Img< T > getAmplitude(
             RandomAccessibleInterval< T > max, ImgFactory< T > factory, RandomAccessibleInterval< T > min )
     {
-        Img< T > amp = factory.create( max );
+        Img< T > amp = factory.create( max.dimensionsAsLongArray() );
         RandomAccess< T > maxAccess = max.randomAccess();
         RandomAccess< T > minAccess = min.randomAccess();
         RandomAccess< T > ampAccess = amp.randomAccess();
diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/Classification.java b/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/Classification.java
index 8934c3b..c16a3a2 100644
--- a/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/Classification.java
+++ b/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/Classification.java
@@ -153,7 +153,7 @@ public class Classification< T extends RealType< T > & NativeType< T > >
                                                       ImgFactory< BitType > factory,
                                                       RandomAccessibleInterval< BitType > thresholds )
     {
-        Img< BitType > output = factory.create( thresholds );
+        Img< BitType > output = factory.create( thresholds.dimensionsAsLongArray() );
         Cursor< BitType > amplitudeCursor = amplitude.localizingCursor();
         RandomAccess< BitType > thresholdAccess
                 = thresholds.randomAccess();
diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/OtsuClassification.java b/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/OtsuClassification.java
index 15561a8..47ac18f 100644
--- a/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/OtsuClassification.java
+++ b/src/main/java/fr/pasteur/ida/zellige/steps/selection/classification/OtsuClassification.java
@@ -64,7 +64,7 @@ public class OtsuClassification< T extends RealType< T > & NativeType< T > >
         this.input = input;
         this.output = output;
         this.userThreshold = userThreshold;
-        this.grid = factory.create( input );
+        this.grid = factory.create( input.dimensionsAsLongArray() );
     }
 
     /**
@@ -74,7 +74,7 @@ public class OtsuClassification< T extends RealType< T > & NativeType< T > >
     private OtsuClassification( final RandomAccessibleInterval< T > input, final ImgFactory< T > factory )
     {
         this.input = input;
-        this.grid = factory.create( input );
+        this.grid = factory.create( input.dimensionsAsLongArray() );
     }
 
     /**
@@ -91,7 +91,7 @@ public class OtsuClassification< T extends RealType< T > & NativeType< T > >
             long start = System.currentTimeMillis();
             // Prepare output.
             final ImgFactory< BitType > bitTypeImgFactory = net.imglib2.util.Util.getArrayOrCellImgFactory( input, new BitType() );
-            Img< BitType > binary = bitTypeImgFactory.create( input );
+            Img< BitType > binary = bitTypeImgFactory.create( input.dimensionsAsLongArray() );
             OtsuClassification< T > classification = new OtsuClassification<>( input, factory, binary, userThreshold );
             classification.run();
             long stop = System.currentTimeMillis();
@@ -131,7 +131,7 @@ public class OtsuClassification< T extends RealType< T > & NativeType< T > >
     {
         long start = System.currentTimeMillis();
         final ImgFactory< BitType > bitTypeImgFactory = net.imglib2.util.Util.getArrayOrCellImgFactory( input, new BitType() );
-        Img< BitType > output = bitTypeImgFactory.create( input );
+        Img< BitType > output = bitTypeImgFactory.create( input.dimensionsAsLongArray() );
         Cursor< T > sourceCursor = input.localizingCursor();
         Cursor< T > gridCursor = grid.cursor();
         RandomAccess< BitType > randomAccess = output.randomAccess();
diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/selection/postTreatment/PostTreatment.java b/src/main/java/fr/pasteur/ida/zellige/steps/selection/postTreatment/PostTreatment.java
index a9db304..e5863da 100644
--- a/src/main/java/fr/pasteur/ida/zellige/steps/selection/postTreatment/PostTreatment.java
+++ b/src/main/java/fr/pasteur/ida/zellige/steps/selection/postTreatment/PostTreatment.java
@@ -82,7 +82,7 @@ public class PostTreatment
     public static Img< FloatType > convertBitTypeIntoFloatType( IterableInterval< BitType > image )
     {
         final ImgFactory< FloatType > imgFactory = new ArrayImgFactory<>( new FloatType() );
-        final Img< FloatType > copy = imgFactory.create( image );
+        final Img< FloatType > copy = imgFactory.create( image.dimensionsAsLongArray() );
 
         Cursor< BitType > bitTypeCursor = image.cursor();
         Cursor< FloatType > floatTypeCursor = copy.cursor();
diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/selection/postTreatment/islandSearch/IslandSearch.java b/src/main/java/fr/pasteur/ida/zellige/steps/selection/postTreatment/islandSearch/IslandSearch.java
index 39d9d09..b0c5b03 100644
--- a/src/main/java/fr/pasteur/ida/zellige/steps/selection/postTreatment/islandSearch/IslandSearch.java
+++ b/src/main/java/fr/pasteur/ida/zellige/steps/selection/postTreatment/islandSearch/IslandSearch.java
@@ -79,7 +79,7 @@ public class IslandSearch
     {
         long start = System.currentTimeMillis();
         ImgFactory< BitType > factory = new ArrayImgFactory<>( new BitType() );
-        Img< BitType > output = factory.create( classifiedImage );
+        Img< BitType > output = factory.create( classifiedImage.dimensionsAsLongArray() );
         ImgUtil.copy( classifiedImage, output );
         new IslandSearch( classifiedImage, output, islandSize, connectivityType );
         long stop = System.currentTimeMillis();
diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/selection/pretreatment/Pretreatment.java b/src/main/java/fr/pasteur/ida/zellige/steps/selection/pretreatment/Pretreatment.java
index c257cb2..24c51b3 100644
--- a/src/main/java/fr/pasteur/ida/zellige/steps/selection/pretreatment/Pretreatment.java
+++ b/src/main/java/fr/pasteur/ida/zellige/steps/selection/pretreatment/Pretreatment.java
@@ -103,7 +103,7 @@ public class Pretreatment< T extends RealType< T > & NativeType< T > >
            RandomAccessibleInterval< T > image, ImgFactory< T > factory )
     {
         LOGGER.debug( "Staring normalization." );
-        Img< T > normalizedImage = factory.create( image );
+        Img< T > normalizedImage = factory.create( image.dimensionsAsLongArray() );
         ImgUtil.copy( image, normalizedImage );
         T min = normalizedImage.firstElement().createVariable();
         min.setReal( 0 );
diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/selection/util/ExtremaDetection.java b/src/main/java/fr/pasteur/ida/zellige/steps/selection/util/ExtremaDetection.java
index e2a32d5..6d1cd56 100644
--- a/src/main/java/fr/pasteur/ida/zellige/steps/selection/util/ExtremaDetection.java
+++ b/src/main/java/fr/pasteur/ida/zellige/steps/selection/util/ExtremaDetection.java
@@ -59,7 +59,7 @@ public class ExtremaDetection< T extends RealType< T > & NativeType< T > >
         parameterCheck( input, factory, type, method );
         this.input = input;
         this.factory = factory;
-        this.extrema = factory.create( input );
+        this.extrema = factory.create( input.dimensionsAsLongArray() );
         this.type = type;
         LOGGER.debug( "Partial derivative is : {}", method );
     }
diff --git a/src/main/java/fr/pasteur/ida/zellige/steps/selection/util/ExtremaDetection2D.java b/src/main/java/fr/pasteur/ida/zellige/steps/selection/util/ExtremaDetection2D.java
index 1ae97bb..e4507dc 100644
--- a/src/main/java/fr/pasteur/ida/zellige/steps/selection/util/ExtremaDetection2D.java
+++ b/src/main/java/fr/pasteur/ida/zellige/steps/selection/util/ExtremaDetection2D.java
@@ -79,7 +79,7 @@ public class ExtremaDetection2D< T extends RealType< T > & NativeType< T > >
     {
         {
             // Output for partial derivative.
-            RandomAccessibleInterval< T > sectionDerivative = factory.create( inputSection );
+            RandomAccessibleInterval< T > sectionDerivative = factory.create( inputSection.dimensionsAsLongArray() );
             // Partial derivative computation */
             Derivative.run( inputSection, sectionDerivative, method );
             computeExtrema( inputSection, sectionDerivative );
-- 
GitLab