Search in sources :

Example 11 with BaseExplainabilityResult

use of org.kie.kogito.explainability.api.BaseExplainabilityResult in project kogito-apps by kiegroup.

the class ExplanationServiceImplTest method testCounterfactualsExplainAsyncSuccess.

@SuppressWarnings("unchecked")
void testCounterfactualsExplainAsyncSuccess(ThrowingSupplier<BaseExplainabilityResult> invocation) {
    when(instance.stream()).thenReturn(Stream.of(cfExplainerServiceHandlerMock));
    when(cfExplainerMock.explainAsync(any(Prediction.class), eq(predictionProviderMock), any(Consumer.class))).thenReturn(CompletableFuture.completedFuture(COUNTERFACTUAL_RESULT));
    BaseExplainabilityResult result = assertDoesNotThrow(invocation);
    assertNotNull(result);
    assertTrue(result instanceof CounterfactualExplainabilityResult);
    CounterfactualExplainabilityResult counterfactualResult = (CounterfactualExplainabilityResult) result;
    assertEquals(EXECUTION_ID, counterfactualResult.getExecutionId());
    assertEquals(COUNTERFACTUAL_ID, counterfactualResult.getCounterfactualId());
    assertSame(ExplainabilityStatus.SUCCEEDED, counterfactualResult.getStatus());
    assertNull(counterfactualResult.getStatusDetails());
    assertEquals(COUNTERFACTUAL_RESULT.getEntities().size(), counterfactualResult.getInputs().size());
    assertEquals(COUNTERFACTUAL_RESULT.getOutput().size(), counterfactualResult.getOutputs().size());
    assertTrue(counterfactualResult.getOutputs().stream().anyMatch(o -> o.getName().equals("output1")));
    NamedTypedValue value = counterfactualResult.getOutputs().iterator().next();
    assertTrue(value.getValue().isUnit());
    assertEquals(Double.class.getSimpleName(), value.getValue().toUnit().getType());
    assertEquals(555.0, value.getValue().toUnit().getValue().asDouble());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) SALIENCY_MAP(org.kie.kogito.explainability.TestUtils.SALIENCY_MAP) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) LimeExplainerServiceHandler(org.kie.kogito.explainability.handlers.LimeExplainerServiceHandler) FeatureImportanceModel(org.kie.kogito.explainability.api.FeatureImportanceModel) COUNTERFACTUAL_ID(org.kie.kogito.explainability.TestUtils.COUNTERFACTUAL_ID) Prediction(org.kie.kogito.explainability.model.Prediction) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) FEATURE_IMPORTANCE_1(org.kie.kogito.explainability.TestUtils.FEATURE_IMPORTANCE_1) Mockito.doThrow(org.mockito.Mockito.doThrow) CounterfactualExplainerServiceHandler(org.kie.kogito.explainability.handlers.CounterfactualExplainerServiceHandler) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) SALIENCY(org.kie.kogito.explainability.TestUtils.SALIENCY) Instance(javax.enterprise.inject.Instance) LocalExplainerServiceHandlerRegistry(org.kie.kogito.explainability.handlers.LocalExplainerServiceHandlerRegistry) EXECUTION_ID(org.kie.kogito.explainability.TestUtils.EXECUTION_ID) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) LimeExplainer(org.kie.kogito.explainability.local.lime.LimeExplainer) CounterfactualExplainer(org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer) ExplainabilityStatus(org.kie.kogito.explainability.api.ExplainabilityStatus) Mockito.when(org.mockito.Mockito.when) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) COUNTERFACTUAL_RESULT(org.kie.kogito.explainability.TestUtils.COUNTERFACTUAL_RESULT) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) COUNTERFACTUAL_REQUEST(org.kie.kogito.explainability.TestUtils.COUNTERFACTUAL_REQUEST) LIME_REQUEST(org.kie.kogito.explainability.TestUtils.LIME_REQUEST) Stream(java.util.stream.Stream) ThrowingSupplier(org.junit.jupiter.api.function.ThrowingSupplier) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) SaliencyModel(org.kie.kogito.explainability.api.SaliencyModel) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) Mockito.mock(org.mockito.Mockito.mock) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) Consumer(java.util.function.Consumer) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) Prediction(org.kie.kogito.explainability.model.Prediction) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)

Example 12 with BaseExplainabilityResult

use of org.kie.kogito.explainability.api.BaseExplainabilityResult in project kogito-apps by kiegroup.

the class ExplanationServiceImplTest method testLIMEExplainAsyncFailed.

@Test
@SuppressWarnings("unchecked")
void testLIMEExplainAsyncFailed() {
    String errorMessage = "Something bad happened";
    RuntimeException exception = new RuntimeException(errorMessage);
    when(instance.stream()).thenReturn(Stream.of(limeExplainerServiceHandlerMock));
    when(limeExplainerMock.explainAsync(any(Prediction.class), eq(predictionProviderMock), any(Consumer.class))).thenThrow(exception);
    BaseExplainabilityResult result = assertDoesNotThrow(() -> explanationService.explainAsync(LIME_REQUEST, callbackMock).toCompletableFuture().get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit()));
    assertNotNull(result);
    assertTrue(result instanceof LIMEExplainabilityResult);
    LIMEExplainabilityResult exceptionResult = (LIMEExplainabilityResult) result;
    assertEquals(EXECUTION_ID, exceptionResult.getExecutionId());
    assertSame(ExplainabilityStatus.FAILED, exceptionResult.getStatus());
    assertEquals(errorMessage, exceptionResult.getStatusDetails());
}
Also used : LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) Consumer(java.util.function.Consumer) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) Prediction(org.kie.kogito.explainability.model.Prediction) Test(org.junit.jupiter.api.Test)

Example 13 with BaseExplainabilityResult

use of org.kie.kogito.explainability.api.BaseExplainabilityResult in project kogito-apps by kiegroup.

the class LocalExplainerServiceHandler method explainAsyncWithResults.

/**
 * Requests calculation of an explanation decorated with both "success" and "failure" result handlers.
 * See:
 * - {@link LocalExplainer#explainAsync}
 * - {@link LocalExplainerServiceHandler#createSucceededResult(BaseExplainabilityRequest, Object)}
 * - {@link LocalExplainerServiceHandler#createFailedResult(BaseExplainabilityRequest, Throwable)}
 *
 * @param request The explanation request.
 * @param intermediateResultsConsumer A consumer for intermediate results provided by the explainer.
 * @return
 */
default CompletableFuture<BaseExplainabilityResult> explainAsyncWithResults(R request, Consumer<BaseExplainabilityResult> intermediateResultsConsumer) {
    Prediction prediction = getPrediction(request);
    PredictionProvider predictionProvider = getPredictionProvider(request);
    return explainAsync(prediction, predictionProvider, s -> intermediateResultsConsumer.accept(createIntermediateResult(request, s))).thenApply(input -> createSucceededResult(request, input)).exceptionally(e -> createFailedResult(request, e));
}
Also used : Consumer(java.util.function.Consumer) BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) LocalExplainer(org.kie.kogito.explainability.local.LocalExplainer) Prediction(org.kie.kogito.explainability.model.Prediction) CompletableFuture(java.util.concurrent.CompletableFuture) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Prediction(org.kie.kogito.explainability.model.Prediction) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider)

Example 14 with BaseExplainabilityResult

use of org.kie.kogito.explainability.api.BaseExplainabilityResult in project kogito-apps by kiegroup.

the class BaseExplainabilityMessagingHandlerIT method explainabilityRequestIsProcessedAndAResultMessageIsSent.

@Test
void explainabilityRequestIsProcessedAndAResultMessageIsSent() throws Exception {
    BaseExplainabilityRequest request = buildRequest();
    BaseExplainabilityResult result = buildResult();
    when(explanationService.explainAsync(any(BaseExplainabilityRequest.class), any())).thenReturn(CompletableFuture.completedFuture(result));
    kafkaClient.produce(ExplainabilityCloudEventBuilder.buildCloudEventJsonString(request), TOPIC_REQUEST);
    verify(explanationService, timeout(2000).times(1)).explainAsync(any(BaseExplainabilityRequest.class), any());
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    kafkaClient.consume(TOPIC_RESULT, s -> {
        LOGGER.info("Received from kafka: {}", s);
        CloudEventUtils.decode(s).ifPresent((CloudEvent cloudEvent) -> {
            try {
                BaseExplainabilityResult event = objectMapper.readValue(cloudEvent.getData().toBytes(), BaseExplainabilityResult.class);
                assertNotNull(event);
                assertResult(event);
                countDownLatch.countDown();
            } catch (IOException e) {
                LOGGER.error("Error parsing {}", s, e);
                throw new RuntimeException(e);
            }
        });
    });
    assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));
    kafkaClient.shutdown();
}
Also used : BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) CloudEvent(io.cloudevents.CloudEvent) Test(org.junit.jupiter.api.Test)

Example 15 with BaseExplainabilityResult

use of org.kie.kogito.explainability.api.BaseExplainabilityResult in project kogito-apps by kiegroup.

the class ExplainabilityApiV1IT method testEndpointWithRequest.

@Test
void testEndpointWithRequest() throws JsonProcessingException {
    ModelIdentifier modelIdentifier = new ModelIdentifier("dmn", "namespace:name");
    String body = MAPPER.writeValueAsString(new LIMEExplainabilityRequest(executionId, serviceUrl, modelIdentifier, Collections.emptyList(), Collections.emptyList()));
    BaseExplainabilityResult result = given().contentType(ContentType.JSON).body(body).when().post("/v1/explain").as(BaseExplainabilityResult.class);
    assertEquals(executionId, result.getExecutionId());
}
Also used : LIMEExplainabilityRequest(org.kie.kogito.explainability.api.LIMEExplainabilityRequest) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) ModelIdentifier(org.kie.kogito.explainability.api.ModelIdentifier) Test(org.junit.jupiter.api.Test) QuarkusTest(io.quarkus.test.junit.QuarkusTest)

Aggregations

BaseExplainabilityResult (org.kie.kogito.explainability.api.BaseExplainabilityResult)16 Test (org.junit.jupiter.api.Test)14 Consumer (java.util.function.Consumer)8 Prediction (org.kie.kogito.explainability.model.Prediction)7 CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)6 LIMEExplainabilityResult (org.kie.kogito.explainability.api.LIMEExplainabilityResult)6 BaseExplainabilityRequest (org.kie.kogito.explainability.api.BaseExplainabilityRequest)5 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)4 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)3 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)3 ExplainabilityStatus (org.kie.kogito.explainability.api.ExplainabilityStatus)3 LIMEExplainabilityRequest (org.kie.kogito.explainability.api.LIMEExplainabilityRequest)3 ModelIdentifier (org.kie.kogito.explainability.api.ModelIdentifier)3 SaliencyModel (org.kie.kogito.explainability.api.SaliencyModel)3 Feature (org.kie.kogito.explainability.model.Feature)3 Output (org.kie.kogito.explainability.model.Output)3 Value (org.kie.kogito.explainability.model.Value)3