Search in sources :

Example 86 with Output

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

the class CounterfactualScoreCalculatorTest method testGoalSizeLarger.

/**
 * Using a larger number of features in the goals (3) than the model's output (2) should
 * throw an {@link IllegalArgumentException} with the appropriate message.
 */
@Test
void testGoalSizeLarger() throws ExecutionException, InterruptedException {
    final CounterFactualScoreCalculator scoreCalculator = new CounterFactualScoreCalculator();
    PredictionProvider model = TestUtils.getFeatureSkipModel(0);
    List<Feature> features = new ArrayList<>();
    List<FeatureDomain> featureDomains = new ArrayList<>();
    List<Boolean> constraints = new ArrayList<>();
    // f-1
    features.add(FeatureFactory.newNumericalFeature("f-1", 1.0));
    featureDomains.add(NumericalFeatureDomain.create(0.0, 10.0));
    constraints.add(false);
    // f-2
    features.add(FeatureFactory.newNumericalFeature("f-2", 2.0));
    featureDomains.add(NumericalFeatureDomain.create(0.0, 10.0));
    constraints.add(false);
    // f-3
    features.add(FeatureFactory.newBooleanFeature("f-3", true));
    featureDomains.add(EmptyFeatureDomain.create());
    constraints.add(false);
    PredictionInput input = new PredictionInput(features);
    PredictionFeatureDomain domains = new PredictionFeatureDomain(featureDomains);
    List<CounterfactualEntity> entities = CounterfactualEntityFactory.createEntities(input);
    List<Output> goal = new ArrayList<>();
    goal.add(new Output("f-1", Type.NUMBER, new Value(1.0), 0.0));
    goal.add(new Output("f-2", Type.NUMBER, new Value(2.0), 0.0));
    goal.add(new Output("f-3", Type.BOOLEAN, new Value(true), 0.0));
    List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(input)).get();
    assertEquals(3, goal.size());
    // A single prediction is expected
    assertEquals(1, predictionOutputs.size());
    // Single prediction with two features
    assertEquals(2, predictionOutputs.get(0).getOutputs().size());
    final CounterfactualSolution solution = new CounterfactualSolution(entities, features, model, goal, UUID.randomUUID(), UUID.randomUUID(), 0.0);
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
        scoreCalculator.calculateScore(solution);
    });
    assertEquals("Prediction size must be equal to goal size", exception.getMessage());
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) ArrayList(java.util.ArrayList) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) PredictionFeatureDomain(org.kie.kogito.explainability.model.PredictionFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) PredictionFeatureDomain(org.kie.kogito.explainability.model.PredictionFeatureDomain) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Value(org.kie.kogito.explainability.model.Value) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 87 with Output

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

the class CounterfactualScoreCalculatorTest method durationDistanceNull.

@Test
void durationDistanceNull() {
    final Duration value = Duration.ofHours(72L);
    // Null as a goal
    Feature predictionFeature = FeatureFactory.newDurationFeature("x", value);
    Feature goalFeature = FeatureFactory.newDurationFeature("y", null);
    Output predictionOutput = outputFromFeature(predictionFeature);
    Output goalOutput = outputFromFeature(goalFeature);
    double distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.DURATION, goalOutput.getType());
    assertEquals(1.0, distance);
    // Null as a prediction
    predictionFeature = FeatureFactory.newDurationFeature("x", null);
    goalFeature = FeatureFactory.newDurationFeature("y", value);
    predictionOutput = outputFromFeature(predictionFeature);
    goalOutput = outputFromFeature(goalFeature);
    distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.DURATION, predictionOutput.getType());
    assertEquals(1.0, distance);
    // Null as both prediction and goal
    predictionFeature = FeatureFactory.newDurationFeature("x", null);
    goalFeature = FeatureFactory.newDurationFeature("y", null);
    predictionOutput = outputFromFeature(predictionFeature);
    goalOutput = outputFromFeature(goalFeature);
    distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.DURATION, predictionOutput.getType());
    System.out.println(distance);
}
Also used : PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Duration(java.time.Duration) Feature(org.kie.kogito.explainability.model.Feature) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 88 with Output

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

the class CounterfactualScoreCalculatorTest method CategoricalDistanceDifferentValue.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void CategoricalDistanceDifferentValue(int seed) {
    final Random random = new Random(seed);
    Feature x = FeatureFactory.newCategoricalFeature("x", UUID.randomUUID().toString());
    Feature y = FeatureFactory.newCategoricalFeature("y", UUID.randomUUID().toString());
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    assertEquals(Type.CATEGORICAL, ox.getType());
    assertEquals(Type.CATEGORICAL, oy.getType());
    assertEquals(1.0, distance);
    // Use a random threshold, mustn't make a difference
    distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
    assertEquals(1.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 89 with Output

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

the class CounterfactualScoreCalculatorTest method DoubleDistanceZero.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void DoubleDistanceZero(int seed) {
    final Random random = new Random(seed);
    Feature x = FeatureFactory.newNumericalFeature("x", 0.0);
    Feature y = FeatureFactory.newNumericalFeature("y", 1.0);
    Output ox = outputFromFeature(x);
    Output oy = outputFromFeature(y);
    double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    assertEquals(Type.NUMBER, ox.getType());
    assertEquals(Type.NUMBER, oy.getType());
    assertEquals(1, distance);
    y = FeatureFactory.newNumericalFeature("y", -1.0);
    oy = outputFromFeature(y);
    distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
    assertEquals(1, 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 90 with Output

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

the class CounterfactualScoreCalculatorTest method CategoricalDistanceNull.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void CategoricalDistanceNull(int seed) {
    final Random random = new Random(seed);
    final String value = UUID.randomUUID().toString();
    // Null as a goal
    Feature predictionFeature = FeatureFactory.newCategoricalFeature("x", value);
    Feature goalFeature = FeatureFactory.newCategoricalFeature("y", null);
    Output predictionOutput = outputFromFeature(predictionFeature);
    Output goalOutput = outputFromFeature(goalFeature);
    double distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.CATEGORICAL, goalOutput.getType());
    assertEquals(1.0, distance);
    // Null as a prediction
    predictionFeature = FeatureFactory.newCategoricalFeature("x", null);
    goalFeature = FeatureFactory.newCategoricalFeature("y", value);
    predictionOutput = outputFromFeature(predictionFeature);
    goalOutput = outputFromFeature(goalFeature);
    distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.CATEGORICAL, predictionOutput.getType());
    assertEquals(1.0, distance);
    // Null as both prediction and goal
    predictionFeature = FeatureFactory.newCategoricalFeature("x", null);
    goalFeature = FeatureFactory.newCategoricalFeature("y", null);
    predictionOutput = outputFromFeature(predictionFeature);
    goalOutput = outputFromFeature(goalFeature);
    distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
    assertEquals(Type.CATEGORICAL, predictionOutput.getType());
}
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)

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