use of org.kie.kogito.explainability.local.lime.LimeExplainer in project kogito-apps by kiegroup.
the class DefaultLimeOptimizationServiceTest method testNullConfig.
@Test
void testNullConfig() {
LimeConfigOptimizer optimizer = new LimeConfigOptimizer();
int max = 1;
LimeOptimizationService service = new DefaultLimeOptimizationService(optimizer, max);
assertThat(service.getBestConfigFor(new LimeExplainer())).isNull();
}
use of org.kie.kogito.explainability.local.lime.LimeExplainer in project kogito-apps by kiegroup.
the class LimeStabilityScoreCalculator method getStabilityScore.
private BigDecimal getStabilityScore(PredictionProvider model, LimeConfig config, List<Prediction> predictions) {
double succeededEvaluations = 0;
int topK = 2;
BigDecimal stabilityScore = BigDecimal.ZERO;
LimeExplainer limeExplainer = new LimeExplainer(config);
for (Prediction prediction : predictions) {
try {
LocalSaliencyStability stability = ExplainabilityMetrics.getLocalSaliencyStability(model, prediction, limeExplainer, topK, NUM_RUNS);
for (String decision : stability.getDecisions()) {
BigDecimal decisionMarginalScore = getDecisionMarginalScore(stability, decision, topK);
stabilityScore = stabilityScore.add(decisionMarginalScore);
succeededEvaluations++;
}
} catch (ExecutionException e) {
LOGGER.error("Saliency stability calculation returned an error {}", e.getMessage());
} catch (InterruptedException e) {
LOGGER.error("Interrupted while waiting for saliency stability calculation {}", e.getMessage());
Thread.currentThread().interrupt();
} catch (TimeoutException e) {
LOGGER.error("Timed out while waiting for saliency stability calculation", e);
}
}
if (succeededEvaluations > 0) {
stabilityScore = stabilityScore.divide(BigDecimal.valueOf(succeededEvaluations), RoundingMode.CEILING);
}
return stabilityScore;
}
use of org.kie.kogito.explainability.local.lime.LimeExplainer in project kogito-apps by kiegroup.
the class LimeExplainerProducerTest method produce.
@Test
void produce() {
LimeExplainerProducer producer = new LimeExplainerProducer(1, 2, 10);
LimeExplainer limeExplainer = producer.produce();
assertNotNull(limeExplainer);
assertEquals(1, limeExplainer.getLimeConfig().getNoOfSamples());
assertEquals(2, limeExplainer.getLimeConfig().getPerturbationContext().getNoOfPerturbations());
assertEquals(LimeConfig.DEFAULT_NO_OF_RETRIES, limeExplainer.getLimeConfig().getNoOfRetries());
}
use of org.kie.kogito.explainability.local.lime.LimeExplainer 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.lime.LimeExplainer in project kogito-apps by kiegroup.
the class TrafficViolationDmnLimeExplainerTest method testTrafficViolationDMNExplanation.
@Test
void testTrafficViolationDMNExplanation() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
PredictionInput predictionInput = getTestInput();
List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(predictionInput)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
Prediction prediction = new SimplePrediction(predictionInput, predictionOutputs.get(0));
Random random = new Random();
PerturbationContext perturbationContext = new PerturbationContext(0L, random, 1);
LimeConfig limeConfig = new LimeConfig().withSamples(10).withPerturbationContext(perturbationContext);
LimeExplainer limeExplainer = new LimeExplainer(limeConfig);
Map<String, Saliency> saliencyMap = limeExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
for (Saliency saliency : saliencyMap.values()) {
assertNotNull(saliency);
List<String> strings = saliency.getTopFeatures(3).stream().map(f -> f.getFeature().getName()).collect(Collectors.toList());
assertTrue(strings.contains("Actual Speed") || strings.contains("Speed Limit"));
}
assertDoesNotThrow(() -> ValidationUtils.validateLocalSaliencyStability(model, prediction, limeExplainer, 1, 0.3, 0.3));
String decision = "Fine";
List<PredictionInput> inputs = new ArrayList<>();
for (int n = 0; n < 10; n++) {
inputs.add(new PredictionInput(DataUtils.perturbFeatures(predictionInput.getFeatures(), perturbationContext)));
}
DataDistribution distribution = new PredictionInputsDataDistribution(inputs);
int k = 2;
int chunkSize = 5;
double f1 = ExplainabilityMetrics.getLocalSaliencyF1(decision, model, limeExplainer, distribution, k, chunkSize);
AssertionsForClassTypes.assertThat(f1).isBetween(0.5d, 1d);
}
Aggregations