Search in sources :

Example 96 with Feature

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

the class ComplexEligibilityDmnCounterfactualExplainerTest method testDMNInvalidCounterfactualExplanation.

@Test
void testDMNInvalidCounterfactualExplanation() throws ExecutionException, InterruptedException, TimeoutException {
    PredictionProvider model = getModel();
    final List<Output> goal = generateGoal(true, true, 0.6);
    List<Feature> features = new LinkedList<>();
    // DMN model does not allow loans for age >= 60, so no CF will be possible
    features.add(FeatureFactory.newNumericalFeature("age", 61));
    features.add(FeatureFactory.newBooleanFeature("hasReferral", true));
    features.add(FeatureFactory.newNumericalFeature("monthlySalary", 500, NumericalFeatureDomain.create(10, 10_000)));
    final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(10_000L);
    // for the purpose of this test, only a few steps are necessary
    final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
    solverConfig.setRandomSeed((long) 23);
    solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
    final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig).withGoalThreshold(0.01);
    final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
    PredictionInput input = new PredictionInput(features);
    PredictionOutput output = new PredictionOutput(goal);
    Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), 60L);
    final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    assertFalse(counterfactualResult.isValid());
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) Prediction(org.kie.kogito.explainability.model.Prediction) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) LinkedList(java.util.LinkedList) CounterfactualResult(org.kie.kogito.explainability.local.counterfactual.CounterfactualResult) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) TerminationConfig(org.optaplanner.core.config.solver.termination.TerminationConfig) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) CounterfactualConfig(org.kie.kogito.explainability.local.counterfactual.CounterfactualConfig) CounterfactualExplainer(org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer) SolverConfig(org.optaplanner.core.config.solver.SolverConfig) Test(org.junit.jupiter.api.Test)

Example 97 with Feature

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

the class ComplexEligibilityDmnCounterfactualExplainerTest method testDMNValidCounterfactualExplanation.

@Test
void testDMNValidCounterfactualExplanation() throws ExecutionException, InterruptedException, TimeoutException {
    PredictionProvider model = getModel();
    final List<Output> goal = generateGoal(true, true, 0.6);
    List<Feature> features = new LinkedList<>();
    features.add(FeatureFactory.newNumericalFeature("age", 40));
    features.add(FeatureFactory.newBooleanFeature("hasReferral", true));
    features.add(FeatureFactory.newNumericalFeature("monthlySalary", 500, NumericalFeatureDomain.create(10, 10_000)));
    final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(10_000L);
    // for the purpose of this test, only a few steps are necessary
    final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
    solverConfig.setRandomSeed((long) 23);
    solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
    final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig).withGoalThreshold(0.01);
    final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
    PredictionInput input = new PredictionInput(features);
    PredictionOutput output = new PredictionOutput(goal);
    Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), 60L);
    final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    List<Output> cfOutputs = counterfactualResult.getOutput().get(0).getOutputs();
    assertTrue(counterfactualResult.isValid());
    assertEquals("inputsAreValid", cfOutputs.get(0).getName());
    assertTrue((Boolean) cfOutputs.get(0).getValue().getUnderlyingObject());
    assertEquals("canRequestLoan", cfOutputs.get(1).getName());
    assertTrue((Boolean) cfOutputs.get(1).getValue().getUnderlyingObject());
    assertEquals("my-scoring-function", cfOutputs.get(2).getName());
    assertEquals(0.6, ((BigDecimal) cfOutputs.get(2).getValue().getUnderlyingObject()).doubleValue(), 0.05);
    List<CounterfactualEntity> entities = counterfactualResult.getEntities();
    assertEquals("age", entities.get(0).asFeature().getName());
    assertEquals(40, entities.get(0).asFeature().getValue().asNumber());
    assertEquals("hasReferral", entities.get(1).asFeature().getName());
    assertTrue((Boolean) entities.get(1).asFeature().getValue().getUnderlyingObject());
    assertEquals("monthlySalary", entities.get(2).asFeature().getName());
    assertTrue(entities.get(2).asFeature().getValue().asNumber() > 6000);
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) Prediction(org.kie.kogito.explainability.model.Prediction) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) LinkedList(java.util.LinkedList) CounterfactualResult(org.kie.kogito.explainability.local.counterfactual.CounterfactualResult) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) TerminationConfig(org.optaplanner.core.config.solver.termination.TerminationConfig) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) CounterfactualConfig(org.kie.kogito.explainability.local.counterfactual.CounterfactualConfig) CounterfactualExplainer(org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer) SolverConfig(org.optaplanner.core.config.solver.SolverConfig) Test(org.junit.jupiter.api.Test)

Example 98 with Feature

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

the class DecisionModelWrapper method toMap.

@SuppressWarnings("unchecked")
private Map<String, Object> toMap(List<Feature> features) {
    Map<String, Object> map = new HashMap<>();
    for (Feature f : features) {
        if (Type.COMPOSITE.equals(f.getType())) {
            List<Feature> compositeFeatures = (List<Feature>) f.getValue().getUnderlyingObject();
            boolean isList = compositeFeatures.stream().allMatch(feature -> feature.getName().startsWith(f.getName() + "_"));
            if (isList) {
                List<Object> objects = new ArrayList<>(compositeFeatures.size());
                for (Feature fs : compositeFeatures) {
                    try {
                        objects.add(toMap((List<Feature>) fs.getValue().getUnderlyingObject()));
                    } catch (ClassCastException cce) {
                        objects.add(fs.getValue().getUnderlyingObject());
                    }
                }
                map.put(f.getName(), objects);
            } else {
                Map<String, Object> maps = new HashMap<>();
                for (Feature cf : compositeFeatures) {
                    Map<String, Object> compositeFeatureMap = toMap(List.of(cf));
                    maps.putAll(compositeFeatureMap);
                }
                map.put(f.getName(), maps);
            }
        } else {
            if (Type.UNDEFINED.equals(f.getType())) {
                Feature underlyingFeature = (Feature) f.getValue().getUnderlyingObject();
                map.put(f.getName(), toMap(List.of(underlyingFeature)));
            } else {
                Object underlyingObject = f.getValue().getUnderlyingObject();
                map.put(f.getName(), underlyingObject);
            }
        }
    }
    if (map.containsKey("context")) {
        map = (Map<String, Object>) map.get("context");
    }
    return map;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Feature(org.kie.kogito.explainability.model.Feature)

Example 99 with Feature

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

the class LimeImpactScoreCalculatorTest method testNonZeroScore.

@Test
void testNonZeroScore() throws ExecutionException, InterruptedException, TimeoutException {
    PredictionProvider model = TestUtils.getDummyTextClassifier();
    LimeImpactScoreCalculator scoreCalculator = new LimeImpactScoreCalculator();
    LimeConfig config = new LimeConfig();
    List<Feature> features = List.of(FeatureFactory.newFulltextFeature("text", "money so they say is the root of all evil today"));
    PredictionInput input = new PredictionInput(features);
    List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(input)).get(Config.DEFAULT_ASYNC_TIMEOUT, Config.DEFAULT_ASYNC_TIMEUNIT);
    assertThat(predictionOutputs).isNotNull();
    assertThat(predictionOutputs.size()).isEqualTo(1);
    PredictionOutput output = predictionOutputs.get(0);
    Prediction prediction = new SimplePrediction(input, output);
    List<Prediction> predictions = List.of(prediction);
    List<LimeConfigEntity> entities = LimeConfigEntityFactory.createEncodingEntities(config);
    LimeConfigSolution solution = new LimeConfigSolution(config, predictions, entities, model);
    SimpleBigDecimalScore score = scoreCalculator.calculateScore(solution);
    assertThat(score).isNotNull();
    assertThat(score.getScore()).isNotNull().isNotEqualTo(BigDecimal.valueOf(0));
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) Prediction(org.kie.kogito.explainability.model.Prediction) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) LimeConfig(org.kie.kogito.explainability.local.lime.LimeConfig) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) SimpleBigDecimalScore(org.optaplanner.core.api.score.buildin.simplebigdecimal.SimpleBigDecimalScore) Test(org.junit.jupiter.api.Test)

Example 100 with Feature

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

the class RecordingLimeExplainerTest method testExplainNonOptimized.

@Test
void testExplainNonOptimized() throws ExecutionException, InterruptedException, TimeoutException {
    RecordingLimeExplainer limeExplainer = new RecordingLimeExplainer(10);
    List<Feature> features = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        features.add(TestUtils.getMockedNumericFeature(i));
    }
    PredictionInput input = new PredictionInput(features);
    PredictionProvider model = TestUtils.getSumSkipModel(0);
    PredictionOutput output = model.predictAsync(List.of(input)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit()).get(0);
    Prediction prediction = new SimplePrediction(input, output);
    Map<String, Saliency> saliencyMap = limeExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    assertNotNull(saliencyMap);
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) Prediction(org.kie.kogito.explainability.model.Prediction) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) ArrayList(java.util.ArrayList) Saliency(org.kie.kogito.explainability.model.Saliency) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Feature (org.kie.kogito.explainability.model.Feature)233 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)118 Test (org.junit.jupiter.api.Test)107 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)107 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)104 Output (org.kie.kogito.explainability.model.Output)102 ArrayList (java.util.ArrayList)97 Random (java.util.Random)92 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)78 Value (org.kie.kogito.explainability.model.Value)74 LinkedList (java.util.LinkedList)72 ValueSource (org.junit.jupiter.params.provider.ValueSource)71 Prediction (org.kie.kogito.explainability.model.Prediction)67 List (java.util.List)51 CounterfactualEntity (org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity)46 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)42 Type (org.kie.kogito.explainability.model.Type)39 NumericalFeatureDomain (org.kie.kogito.explainability.model.domain.NumericalFeatureDomain)37 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)35 FeatureDomain (org.kie.kogito.explainability.model.domain.FeatureDomain)33