Search in sources :

Example 1 with COUNTERFACTUAL_RESULT

use of org.kie.kogito.explainability.TestUtils.COUNTERFACTUAL_RESULT 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)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 Consumer (java.util.function.Consumer)1 Stream (java.util.stream.Stream)1 Instance (javax.enterprise.inject.Instance)1 Assertions.assertDoesNotThrow (org.junit.jupiter.api.Assertions.assertDoesNotThrow)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)1 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)1 Assertions.assertSame (org.junit.jupiter.api.Assertions.assertSame)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 ThrowingSupplier (org.junit.jupiter.api.function.ThrowingSupplier)1 COUNTERFACTUAL_ID (org.kie.kogito.explainability.TestUtils.COUNTERFACTUAL_ID)1 COUNTERFACTUAL_REQUEST (org.kie.kogito.explainability.TestUtils.COUNTERFACTUAL_REQUEST)1 COUNTERFACTUAL_RESULT (org.kie.kogito.explainability.TestUtils.COUNTERFACTUAL_RESULT)1 EXECUTION_ID (org.kie.kogito.explainability.TestUtils.EXECUTION_ID)1 FEATURE_IMPORTANCE_1 (org.kie.kogito.explainability.TestUtils.FEATURE_IMPORTANCE_1)1 LIME_REQUEST (org.kie.kogito.explainability.TestUtils.LIME_REQUEST)1