Search in sources :

Example 91 with Output

use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method timeDistanceSameValue.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void timeDistanceSameValue(int seed) {
    final Random random = new Random(seed);
    final LocalTime value = LocalTime.of(random.nextInt(24), random.nextInt(60));
    Feature x = FeatureFactory.newTimeFeature("x", value);
    Feature y = FeatureFactory.newTimeFeature("y", value);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    assertEquals(Type.TIME, ox.getType());
    assertEquals(0.0, Math.abs(distance));
    // Use a random threshold, mustn't make a difference
    distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
    assertEquals(0.0, Math.abs(distance));
}
Also used : Random(java.util.Random) LocalTime(java.time.LocalTime) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 92 with Output

use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method DoubleDistanceNull.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void DoubleDistanceNull(int seed) {
    final Random random = new Random(seed);
    final double value = random.nextDouble() * 1000;
    // Null as a goal
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
        Feature predictionFeature = FeatureFactory.newNumericalFeature("x", value);
        Feature goalFeature = FeatureFactory.newNumericalFeature("x", null);
        Output predictionOutput = outputFromFeature(predictionFeature);
        Output goalOutput = outputFromFeature(goalFeature);
        CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    });
    assertEquals("Unsupported NaN or NULL for numeric feature 'x'", exception.getMessage());
    // Null as a prediction
    exception = assertThrows(IllegalArgumentException.class, () -> {
        Feature predictionFeature = FeatureFactory.newNumericalFeature("x", null);
        Feature goalFeature = FeatureFactory.newNumericalFeature("x", value);
        Output predictionOutput = outputFromFeature(predictionFeature);
        Output goalOutput = outputFromFeature(goalFeature);
        CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    });
    assertEquals("Unsupported NaN or NULL for numeric feature 'x'", exception.getMessage());
    // Null as both prediction and goal
    exception = assertThrows(IllegalArgumentException.class, () -> {
        Feature predictionFeature = FeatureFactory.newNumericalFeature("x", null);
        Feature goalFeature = FeatureFactory.newNumericalFeature("x", null);
        Output predictionOutput = outputFromFeature(predictionFeature);
        Output goalOutput = outputFromFeature(goalFeature);
        CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    });
    assertEquals("Unsupported NaN or NULL for numeric feature 'x'", exception.getMessage());
}
Also used : Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 93 with Output

use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method objectDistanceNull.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void objectDistanceNull(int seed) {
    final Random random = new Random(seed);
    final ByteBuffer value = ByteBuffer.wrap("foo".getBytes());
    // Null as a goal
    Feature predictionFeature = FeatureFactory.newObjectFeature("x", value);
    Feature goalFeature = FeatureFactory.newObjectFeature("y", null);
    Output predictionOutput = outputFromFeature(predictionFeature);
    Output goalOutput = outputFromFeature(goalFeature);
    double distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.UNDEFINED, goalOutput.getType());
    assertEquals(1.0, distance);
    // Null as a prediction
    predictionFeature = FeatureFactory.newObjectFeature("x", null);
    goalFeature = FeatureFactory.newObjectFeature("y", value);
    predictionOutput = outputFromFeature(predictionFeature);
    goalOutput = outputFromFeature(goalFeature);
    distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.UNDEFINED, predictionOutput.getType());
    assertEquals(1.0, distance);
    // Null as both prediction and goal
    predictionFeature = FeatureFactory.newObjectFeature("x", null);
    goalFeature = FeatureFactory.newObjectFeature("y", null);
    predictionOutput = outputFromFeature(predictionFeature);
    goalOutput = outputFromFeature(goalFeature);
    distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.UNDEFINED, predictionOutput.getType());
}
Also used : Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) ByteBuffer(java.nio.ByteBuffer) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 94 with Output

use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method BooleanDistanceSameValue.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void BooleanDistanceSameValue(int seed) {
    final Random random = new Random(seed);
    final boolean value = random.nextBoolean();
    Feature x = FeatureFactory.newBooleanFeature("x", value);
    Feature y = FeatureFactory.newBooleanFeature("y", value);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    assertEquals(Type.BOOLEAN, ox.getType());
    assertEquals(0.0, distance);
    // Use a random threshold, mustn't make a difference
    distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
    assertEquals(0.0, distance);
}
Also used : Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 95 with Output

use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method binaryDistanceNull.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void binaryDistanceNull(int seed) {
    final Random random = new Random(seed);
    final ByteBuffer value = ByteBuffer.wrap("foo".getBytes());
    // Null as a goal
    Feature predictionFeature = FeatureFactory.newBinaryFeature("x", value);
    Feature goalFeature = FeatureFactory.newBinaryFeature("y", null);
    Output predictionOutput = outputFromFeature(predictionFeature);
    Output goalOutput = outputFromFeature(goalFeature);
    double distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.BINARY, goalOutput.getType());
    assertEquals(1.0, distance);
    // Null as a prediction
    predictionFeature = FeatureFactory.newBinaryFeature("x", null);
    goalFeature = FeatureFactory.newBinaryFeature("y", value);
    predictionOutput = outputFromFeature(predictionFeature);
    goalOutput = outputFromFeature(goalFeature);
    distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.BINARY, predictionOutput.getType());
    assertEquals(1.0, distance);
    // Null as both prediction and goal
    predictionFeature = FeatureFactory.newBinaryFeature("x", null);
    goalFeature = FeatureFactory.newBinaryFeature("y", null);
    predictionOutput = outputFromFeature(predictionFeature);
    goalOutput = outputFromFeature(goalFeature);
    distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.BINARY, predictionOutput.getType());
}
Also used : Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) ByteBuffer(java.nio.ByteBuffer) Feature(org.kie.kogito.explainability.model.Feature) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Output (org.kie.kogito.explainability.model.Output)120 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)109 Feature (org.kie.kogito.explainability.model.Feature)102 Value (org.kie.kogito.explainability.model.Value)63 Random (java.util.Random)61 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)59 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)57 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)52 ArrayList (java.util.ArrayList)47 ValueSource (org.junit.jupiter.params.provider.ValueSource)47 Prediction (org.kie.kogito.explainability.model.Prediction)46 Test (org.junit.jupiter.api.Test)42 List (java.util.List)39 Type (org.kie.kogito.explainability.model.Type)36 LinkedList (java.util.LinkedList)35 CounterfactualEntity (org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity)23 Mockito.mock (org.mockito.Mockito.mock)20 Optional (java.util.Optional)19 ExecutionException (java.util.concurrent.ExecutionException)19 Collectors (java.util.stream.Collectors)18