use of org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer in project kogito-apps by kiegroup.
the class LocalExplainerServiceHandlerRegistryTest method setup.
@BeforeEach
@SuppressWarnings("unchecked")
public void setup() {
LimeExplainer limeExplainer = mock(LimeExplainer.class);
CounterfactualExplainer counterfactualExplainer = mock(CounterfactualExplainer.class);
PredictionProviderFactory predictionProviderFactory = mock(PredictionProviderFactory.class);
limeExplainerServiceHandler = spy(new LimeExplainerServiceHandler(limeExplainer, predictionProviderFactory));
counterfactualExplainerServiceHandler = spy(new CounterfactualExplainerServiceHandler(counterfactualExplainer, predictionProviderFactory, MAX_RUNNING_TIME_SECONDS));
predictionProvider = mock(PredictionProvider.class);
callback = mock(Consumer.class);
when(predictionProviderFactory.createPredictionProvider(any(), any(), any())).thenReturn(predictionProvider);
Instance<LocalExplainerServiceHandler<?, ?>> explanationHandlers = mock(Instance.class);
when(explanationHandlers.stream()).thenReturn(Stream.of(limeExplainerServiceHandler, counterfactualExplainerServiceHandler));
registry = new LocalExplainerServiceHandlerRegistry(explanationHandlers);
}
use of org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer in project kogito-apps by kiegroup.
the class CounterfactualExplainerProducer method produce.
@Produces
public CounterfactualExplainer produce() {
LOG.debug("CounterfactualExplainer created");
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withGoalThreshold(this.goalThreshold).withExecutor(executor);
return new CounterfactualExplainer(counterfactualConfig);
}
use of org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer in project kogito-apps by kiegroup.
the class PrequalificationDmnCounterfactualExplainerTest method testValidCounterfactual.
@Test
void testValidCounterfactual() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
final List<Output> goal = List.of(new Output("Qualified?", Type.BOOLEAN, new Value(true), 0.0d));
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(steps);
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed(randomSeed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
CounterfactualConfig config = new CounterfactualConfig().withGoalThreshold(0.1);
config.withSolverConfig(solverConfig);
final CounterfactualExplainer explainer = new CounterfactualExplainer(config);
PredictionInput input = getTestInputVariable();
PredictionOutput output = new PredictionOutput(goal);
// test model
List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(getTestInputFixed())).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
final Output predictionOutput = predictionOutputs.get(0).getOutputs().get(0);
assertEquals("Qualified?", predictionOutput.getName());
assertFalse((Boolean) predictionOutput.getValue().getUnderlyingObject());
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), null);
CounterfactualResult counterfactualResult = explainer.explainAsync(prediction, model).get();
List<Feature> cfFeatures = counterfactualResult.getEntities().stream().map(CounterfactualEntity::asFeature).collect(Collectors.toList());
List<Feature> unflattened = CompositeFeatureUtils.unflattenFeatures(cfFeatures, input.getFeatures());
List<PredictionOutput> outputs = model.predictAsync(List.of(new PredictionInput(unflattened))).get();
assertTrue(counterfactualResult.isValid());
final Output decideOutput = outputs.get(0).getOutputs().get(0);
assertEquals("Qualified?", decideOutput.getName());
assertTrue((Boolean) decideOutput.getValue().getUnderlyingObject());
}
Aggregations