use of org.kie.kogito.explainability.model.PredictionInput 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());
}
use of org.kie.kogito.explainability.model.PredictionInput 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);
}
use of org.kie.kogito.explainability.model.PredictionInput in project kogito-apps by kiegroup.
the class DecisionModelWrapper method predictAsync.
@Override
public CompletableFuture<List<PredictionOutput>> predictAsync(List<PredictionInput> inputs) {
List<PredictionOutput> predictionOutputs = new LinkedList<>();
for (PredictionInput input : inputs) {
Map<String, Object> contextVariables = toMap(input.getFeatures());
final DMNContext context = decisionModel.newContext(contextVariables);
DMNResult dmnResult = decisionModel.evaluateAll(context);
List<Output> outputs = new LinkedList<>();
for (DMNDecisionResult decisionResult : dmnResult.getDecisionResults()) {
String decisionName = decisionResult.getDecisionName();
if (!skippedDecisions.contains(decisionName)) {
Object result = decisionResult.getResult();
Value value = new Value(result);
Type type;
if (result == null) {
type = Type.TEXT;
} else {
if (result instanceof Boolean) {
type = Type.BOOLEAN;
} else if (result instanceof String) {
type = Type.TEXT;
} else {
type = Type.NUMBER;
}
}
Output output = new Output(decisionName, type, value, 1d);
outputs.add(output);
}
}
PredictionOutput predictionOutput = new PredictionOutput(outputs);
predictionOutputs.add(predictionOutput);
}
return completedFuture(predictionOutputs);
}
use of org.kie.kogito.explainability.model.PredictionInput in project kogito-apps by kiegroup.
the class LimeConfigOptimizerTest method assertConfigOptimized.
private void assertConfigOptimized(LimeConfigOptimizer limeConfigOptimizer) throws InterruptedException, java.util.concurrent.ExecutionException {
LimeConfig initialConfig = new LimeConfig().withSamples(10);
PredictionProvider model = TestUtils.getSumSkipModel(1);
Random random = new Random();
random.setSeed(4);
DataDistribution dataDistribution = DataUtils.generateRandomDataDistribution(5, 100, random);
List<PredictionInput> samples = dataDistribution.sample(10);
List<PredictionOutput> predictionOutputs = model.predictAsync(samples).get();
List<Prediction> predictions = DataUtils.getPredictions(samples, predictionOutputs);
LimeConfig optimizedConfig = limeConfigOptimizer.optimize(initialConfig, predictions, model);
assertThat(optimizedConfig).isNotNull();
Assertions.assertThat(optimizedConfig).isNotSameAs(initialConfig);
}
use of org.kie.kogito.explainability.model.PredictionInput 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));
}
Aggregations