use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class TestUtils method getEvenFeatureModel.
public static PredictionProvider getEvenFeatureModel(int featureIndex) {
return inputs -> supplyAsync(() -> {
List<PredictionOutput> predictionOutputs = new LinkedList<>();
for (PredictionInput predictionInput : inputs) {
List<Feature> features = predictionInput.getFeatures();
Feature feature = features.get(featureIndex);
double v = feature.getValue().asNumber();
PredictionOutput predictionOutput = new PredictionOutput(List.of(new Output("feature-" + featureIndex, Type.BOOLEAN, new Value(v % 2 == 0), 1d)));
predictionOutputs.add(predictionOutput);
}
return predictionOutputs;
});
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class PartialDependencePlotExplainerTest method getMetadata.
private PredictionProviderMetadata getMetadata(Random random) {
return new PredictionProviderMetadata() {
@Override
public DataDistribution getDataDistribution() {
return DataUtils.generateRandomDataDistribution(3, 100, random);
}
@Override
public PredictionInput getInputShape() {
List<Feature> features = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("f0", 0));
features.add(FeatureFactory.newNumericalFeature("f1", 0));
features.add(FeatureFactory.newNumericalFeature("f2", 0));
return new PredictionInput(features);
}
@Override
public PredictionOutput getOutputShape() {
List<Output> outputs = new LinkedList<>();
outputs.add(new Output("sum-but0", Type.BOOLEAN, new Value(false), 0d));
return new PredictionOutput(outputs);
}
};
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testConsumers.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testConsumers(int seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
random.setSeed(seed);
final List<Output> goal = List.of(new Output("inside", Type.BOOLEAN, new Value(true), 0.0));
List<Feature> features = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("f-num1", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num2", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num3", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num4", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
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) seed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
@SuppressWarnings("unchecked") final Consumer<CounterfactualResult> assertIntermediateCounterfactualNotNull = mock(Consumer.class);
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig).withGoalThreshold(0.01);
final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
PredictionInput input = new PredictionInput(features);
final double center = 500.0;
final double epsilon = 10.0;
final PredictionProvider model = TestUtils.getSumThresholdModel(center, epsilon);
PredictionOutput output = new PredictionOutput(goal);
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), null);
final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model, assertIntermediateCounterfactualNotNull).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
for (CounterfactualEntity entity : counterfactualResult.getEntities()) {
logger.debug("Entity: {}", entity);
}
logger.debug("Outputs: {}", counterfactualResult.getOutput().get(0).getOutputs());
// At least one intermediate result is generated
verify(assertIntermediateCounterfactualNotNull, atLeast(1)).accept(any());
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testNonEmptyInput.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testNonEmptyInput(int seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
random.setSeed(seed);
final List<Output> goal = List.of(new Output("class", Type.NUMBER, new Value(10.0), 0.0d));
List<Feature> features = new LinkedList<>();
for (int i = 0; i < 4; i++) {
features.add(FeatureFactory.newNumericalFeature("f-" + i, random.nextDouble(), NumericalFeatureDomain.create(0.0, 1000.0)));
}
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(10L);
// for the purpose of this test, only a few steps are necessary
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed((long) seed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig);
final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
PredictionProvider model = TestUtils.getSumSkipModel(0);
PredictionInput input = new PredictionInput(features);
PredictionOutput output = new PredictionOutput(goal);
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), null);
final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
for (CounterfactualEntity entity : counterfactualResult.getEntities()) {
logger.debug("Entity: {}", entity);
}
logger.debug("Outputs: {}", counterfactualResult.getOutput().get(0).getOutputs());
assertNotNull(counterfactualResult);
assertNotNull(counterfactualResult.getEntities());
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testCounterfactualCategoricalStrictFail.
/**
* Search for a counterfactual using categorical features with the Symbolic arithmetic model.
* The outcome match is strict (goal threshold of zero).
* The CF should be invalid with this number of iterations.
*
* @param seed
* @throws ExecutionException
* @throws InterruptedException
* @throws TimeoutException
*/
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testCounterfactualCategoricalStrictFail(int seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
random.setSeed(seed);
final List<Output> goal = List.of(new Output("result", Type.NUMBER, new Value(25.0), 0.0d));
List<Feature> features = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("x-1", 5.0, NumericalFeatureDomain.create(0.0, 100.0)));
features.add(FeatureFactory.newNumericalFeature("x-2", 40.0, NumericalFeatureDomain.create(0.0, 100.0)));
features.add(FeatureFactory.newCategoricalFeature("operand", "*", CategoricalFeatureDomain.create("+", "-", "/", "*")));
final CounterfactualResult result = runCounterfactualSearch((long) seed, goal, features, TestUtils.getSymbolicArithmeticModel(), 0.0);
final List<CounterfactualEntity> counterfactualEntities = result.getEntities();
Stream<Feature> counterfactualFeatures = counterfactualEntities.stream().map(CounterfactualEntity::asFeature);
String operand = counterfactualFeatures.filter(feature -> feature.getName().equals("operand")).findFirst().get().getValue().asString();
List<Feature> numericalFeatures = counterfactualEntities.stream().map(CounterfactualEntity::asFeature).filter(feature -> !feature.getName().equals("operand")).collect(Collectors.toList());
double opResult = 0.0;
for (Feature feature : numericalFeatures) {
switch(operand) {
case "+":
opResult += feature.getValue().asNumber();
break;
case "-":
opResult -= feature.getValue().asNumber();
break;
case "*":
opResult *= feature.getValue().asNumber();
break;
case "/":
opResult /= feature.getValue().asNumber();
break;
}
}
final double epsilon = 0.1;
assertFalse(result.isValid());
assertTrue(opResult <= 25.0 + epsilon);
assertTrue(opResult >= 25.0 - epsilon);
}
Aggregations