You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by am...@apache.org on 2021/10/14 13:07:16 UTC
[sis] 01/02: feat(Core): Add a fallback to try and define a
background value
This is an automated email from the ASF dual-hosted git repository.
amanin pushed a commit to branch feat/default-background
in repository https://gitbox.apache.org/repos/asf/sis.git
commit 4048225cfccc5a38eec5d758f8b089aedbd13af0
Author: Alexis Manin <al...@geomatys.com>
AuthorDate: Thu Oct 14 13:24:45 2021 +0200
feat(Core): Add a fallback to try and define a background value
When user does not explicitly define a background value, a best effort strategy tries to find an appropriate fallback.
---
.../org/apache/sis/coverage/SampleDimension.java | 73 ++++++++++++++++++++++
.../apache/sis/coverage/SampleDimensionTest.java | 42 +++++++++++++
2 files changed, 115 insertions(+)
diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/SampleDimension.java b/core/sis-feature/src/main/java/org/apache/sis/coverage/SampleDimension.java
index fe97d55..551a3af 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/SampleDimension.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/SampleDimension.java
@@ -16,6 +16,7 @@
*/
package org.apache.sis.coverage;
+import java.util.Comparator;
import java.util.List;
import java.util.AbstractList;
import java.util.Arrays;
@@ -27,7 +28,10 @@ import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.io.Serializable;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import javax.measure.Unit;
+import org.apache.sis.util.logging.Logging;
import org.opengis.util.GenericName;
import org.opengis.util.InternationalString;
import org.opengis.referencing.operation.MathTransform1D;
@@ -1104,10 +1108,79 @@ defName: if (name == null) {
}
name = createLocalName(Vocabulary.formatInternational(Vocabulary.Keys.Untitled));
}
+
+ // Putting background defaulting in a dedicated method make it easier to disable/evolve.
+ verifyBackground();
+
return new SampleDimension(name, toNaN.background, UnmodifiableArrayList.wrap(categories, 0, count));
}
/**
+ * Try to define a background value if none has been defined by user.
+ * Note that this is a best-effort. It analyzes available categories types and names. If it finds a very common
+ * case in category naming/typing, it will promote the related category minimum value as background. Otherwise,
+ * the background value is left unset.
+ */
+ private void verifyBackground() {
+ if (toNaN.background != null) return;
+ final Logger logger = Logging.getLogger("org.apache.sis.coverage");
+ try {
+ Arrays.stream(categories, 0, count)
+ .map(this::score)
+ .filter(it -> it != null && it.score > 0)
+ .sorted(Comparator.comparing(BackgroundCandidate::getScore).reversed())
+ .findFirst()
+ .ifPresent(promoted -> {
+ logger.log(Level.FINE,
+ "No background value set by user. Defaulting to {}." +
+ "Use `setBackground()` to short this automatic choice.",
+ promoted.category);
+ toNaN.background = promoted.category.getSampleRange().getMinValue();
+ });
+ } catch (Exception e) {
+ logger.log(Level.FINEST, "Defaulting background value failed", e);
+ }
+ }
+
+ /**
+ * Compute a score for a category. The score represent the chances for the category to be a good replacement for
+ * an explicitly set background. The higher the score, the better. Note that any result with a 0 score should be
+ * rejected.
+ *
+ * @param candidate A category to evaluate as a candidate for background value. Do not accept null values.
+ * @return The input category with associated score. Can be null if input category is not a good candidate.
+ */
+ private BackgroundCandidate score(Category candidate) {
+ if (candidate.isQuantitative() || candidate.getSampleRange().getMinValue() == null) return null;
+
+ final String enName = candidate.getName()
+ .toString(Locale.ENGLISH)
+ .toLowerCase(Locale.ENGLISH);
+ if (enName.equals("background")) return new BackgroundCandidate(1f, candidate);
+ else if (enName.equals("fill-value") || enName.equals("fill_value") || enName.equals("fill")) return new BackgroundCandidate(0.9f, candidate);
+ else if (enName.equals("no-data") || enName.equals("no_data") || enName.equals("no data")) return new BackgroundCandidate(0.8f, candidate);
+ else if (enName.equals("missing-value") || enName.equals("missing_value") || enName.equals("missing value")) return new BackgroundCandidate(0.7f, candidate);
+ else if (enName.equals("missing-data") || enName.equals("missing_data") || enName.equals("missing data")) return new BackgroundCandidate(0.6f, candidate);
+ else return new BackgroundCandidate(0f, candidate);
+ }
+
+ /**
+ * Model a category associated with a score that represent its probability to be the best match for a background
+ * value.
+ */
+ private static final class BackgroundCandidate {
+ final float score;
+ final Category category;
+
+ public BackgroundCandidate(float score, Category category) {
+ this.score = score;
+ this.category = category;
+ }
+
+ public float getScore() { return score; }
+ }
+
+ /**
* Reset this builder to the same state than after construction.
* The sample dimension name, background values and all categories are discarded.
* This method can be invoked when the same builder is reused for creating more than one sample dimension.
diff --git a/core/sis-feature/src/test/java/org/apache/sis/coverage/SampleDimensionTest.java b/core/sis-feature/src/test/java/org/apache/sis/coverage/SampleDimensionTest.java
index 2d81b75..c5a1905 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/coverage/SampleDimensionTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/coverage/SampleDimensionTest.java
@@ -222,4 +222,46 @@ public final strictfp class SampleDimensionTest extends TestCase {
assertEquals("Clouds", categories.remove(1).getName().toString());
assertEquals(2, categories.size());
}
+
+ /**
+ * When user does not explicitly define a background category, ensure that we promote another category as background
+ * if and only if it matches <em>all</em> of the following requirements:
+ * <ul>
+ * <li>Is qualitative</li>
+ * <li>Named background, fill-value or no-data</li>
+ * <li>has a valid minimum value</li>
+ * </ul>
+ */
+ @Test
+ public void testDefaultBackground() {
+ SampleDimension s = new SampleDimension.Builder()
+ .addQuantitative("background", 0f, 0.99f, Units.UNITY)
+ .addQuantitative("no-data", 1f, 1.99f, Units.UNITY)
+ .addQuantitative("fill-value", 2f, 3f, Units.UNITY)
+ .build();
+ s.getBackground().ifPresent(bg
+ -> fail("No quantitative category should be promoted as background value, but background is defined: "+bg));
+
+ s = new SampleDimension.Builder()
+ .addQuantitative("background", 0f, 1f, Units.UNITY)
+ .addQualitative("fill-value", null, -1)
+ .build();
+ s.getBackground().ifPresent(bg
+ -> fail("Category with no valid minimum value should not be accepted as background, but background is defined: "+bg));
+
+ //tricky case: background should be priorized, but we must ensure that null minimum value discard it as a valid choice
+ s = new SampleDimension.Builder()
+ .addQualitative("background", null, -1)
+ .addQualitative("fill-value", 2, 3)
+ .build();
+ s.getBackground()
+ .filter(it -> it.intValue() == 2)
+ .orElseThrow(() -> new AssertionError("No background value defined, but we expect it to be 2"));
+
+ s = new SampleDimension.Builder()
+ .addQualitative("whatever", 0, 1)
+ .build();
+ s.getBackground().ifPresent(bg
+ -> fail("Only categories with a valid name should be promoted"));
+ }
}