Search in sources :

Example 16 with CounterfactualExplainabilityResult

use of org.kie.kogito.explainability.api.CounterfactualExplainabilityResult 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 17 with CounterfactualExplainabilityResult

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

the class ExplainabilityMessagingHandlerCounterfactualsIT method mockExplainAsyncInvocationWithIntermediateResults.

@Override
protected void mockExplainAsyncInvocationWithIntermediateResults(Consumer<BaseExplainabilityResult> callback) {
    CounterfactualExplainabilityResult intermediateResult = CounterfactualExplainabilityResult.buildSucceeded(EXECUTION_ID, COUNTERFACTUAL_ID, SOLUTION_ID, 0L, Boolean.TRUE, CounterfactualExplainabilityResult.Stage.INTERMEDIATE, Collections.emptyList(), Collections.emptyList());
    callback.accept(intermediateResult);
}
Also used : CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)

Example 18 with CounterfactualExplainabilityResult

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

the class ExplainabilityApiV1Test method testGetCounterfactualResultsWhenExecutionDoesExistAndResultsHaveBeenCreated.

@Test
public void testGetCounterfactualResultsWhenExecutionDoesExistAndResultsHaveBeenCreated() {
    NamedTypedValue goal = buildGoalUnit("unit", "string", new TextNode("hello"));
    CounterfactualSearchDomain searchDomain = buildSearchDomainUnit("unit", "string", new CounterfactualDomainCategorical(List.of(new TextNode("hello"), new TextNode("goodbye"))));
    CounterfactualExplainabilityResult solution1 = new CounterfactualExplainabilityResult(EXECUTION_ID, COUNTERFACTUAL_ID, "solution1", 0L, ExplainabilityStatus.SUCCEEDED, "", true, CounterfactualExplainabilityResult.Stage.INTERMEDIATE, Collections.emptyList(), Collections.emptyList());
    CounterfactualExplainabilityResult solution2 = new CounterfactualExplainabilityResult(EXECUTION_ID, COUNTERFACTUAL_ID, "solution2", 1L, ExplainabilityStatus.SUCCEEDED, "", true, CounterfactualExplainabilityResult.Stage.FINAL, Collections.emptyList(), Collections.emptyList());
    when(trustyService.getCounterfactualRequest(anyString(), anyString())).thenReturn(new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, new ModelIdentifier("resourceType", "resourceIdentifier"), COUNTERFACTUAL_ID, Collections.emptyList(), List.of(goal), List.of(searchDomain), MAX_RUNNING_TIME_SECONDS));
    when(trustyService.getCounterfactualResults(anyString(), anyString())).thenReturn(List.of(solution1, solution2));
    Response response = explainabilityEndpoint.getCounterfactualDetails(EXECUTION_ID, COUNTERFACTUAL_ID);
    assertNotNull(response);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    Object entity = response.getEntity();
    assertNotNull(entity);
    assertTrue(entity instanceof CounterfactualResultsResponse);
    CounterfactualResultsResponse resultsResponse = (CounterfactualResultsResponse) entity;
    assertEquals(EXECUTION_ID, resultsResponse.getExecutionId());
    assertEquals(COUNTERFACTUAL_ID, resultsResponse.getCounterfactualId());
    assertEquals(MAX_RUNNING_TIME_SECONDS, resultsResponse.getMaxRunningTimeSeconds());
    assertEquals(1, resultsResponse.getGoals().size());
    assertEquals(goal, resultsResponse.getGoals().iterator().next());
    assertEquals(1, resultsResponse.getSearchDomains().size());
    assertEquals(searchDomain, resultsResponse.getSearchDomains().iterator().next());
    assertEquals(2, resultsResponse.getSolutions().size());
    assertEquals(solution1, resultsResponse.getSolutions().get(0));
    assertEquals(solution2, resultsResponse.getSolutions().get(1));
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) CounterfactualRequestResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualRequestResponse) Response(javax.ws.rs.core.Response) CounterfactualResultsResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualResultsResponse) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) ModelIdentifier(org.kie.kogito.explainability.api.ModelIdentifier) TextNode(com.fasterxml.jackson.databind.node.TextNode) CounterfactualDomainCategorical(org.kie.kogito.explainability.api.CounterfactualDomainCategorical) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) CounterfactualResultsResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualResultsResponse) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Example 19 with CounterfactualExplainabilityResult

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

the class CounterfactualExplainabilityResultsManagerDuplicatesTest method testPurgeWhenResultSetSizeIsGreaterThanMinimum_WithoutDuplicateInputs.

@Test
public void testPurgeWhenResultSetSizeIsGreaterThanMinimum_WithoutDuplicateInputs() {
    CounterfactualExplainabilityResult result0 = makeResult(0, List.of(makeCounterfactualInput("0")), Collections.emptyList());
    CounterfactualExplainabilityResult result1 = makeResult(1, List.of(makeCounterfactualInput("1")), Collections.emptyList());
    CounterfactualExplainabilityResult result2 = makeResult(2, List.of(makeCounterfactualInput("2")), Collections.emptyList());
    CounterfactualExplainabilityResult result3 = makeResult(3, List.of(makeCounterfactualInput("3")), Collections.emptyList());
    when(query.execute()).thenReturn(List.of(result0, result1, result2, result3));
    manager.purge(COUNTERFACTUAL_ID, storage);
    verify(storage, never()).remove(anyString());
}
Also used : CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) Test(org.junit.jupiter.api.Test)

Example 20 with CounterfactualExplainabilityResult

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

the class CounterfactualExplainabilityResultsManagerDuplicatesTest method testPurgeWhenResultSetSizeIsGreaterThanMinimum_WithDuplicateInputs_FinalLast.

@Test
public void testPurgeWhenResultSetSizeIsGreaterThanMinimum_WithDuplicateInputs_FinalLast() {
    CounterfactualExplainabilityResult result0 = makeResult(0, Collections.emptyList(), Collections.emptyList());
    CounterfactualExplainabilityResult result1 = makeResult(1, Collections.emptyList(), Collections.emptyList());
    CounterfactualExplainabilityResult result2 = makeResult(2, List.of(makeCounterfactualInput("a")), Collections.emptyList());
    CounterfactualExplainabilityResult result3 = makeResult(3, CounterfactualExplainabilityResult.Stage.FINAL, List.of(makeCounterfactualInput("a")), Collections.emptyList());
    when(query.execute()).thenReturn(List.of(result0, result1, result2, result3));
    manager.purge(COUNTERFACTUAL_ID, storage);
    verify(storage).remove(anyString());
    verify(storage).remove(result2.getSolutionId());
}
Also used : CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) Test(org.junit.jupiter.api.Test)

Aggregations

CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)29 Test (org.junit.jupiter.api.Test)26 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)8 BaseExplainabilityResult (org.kie.kogito.explainability.api.BaseExplainabilityResult)6 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)6 IntNode (com.fasterxml.jackson.databind.node.IntNode)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Consumer (java.util.function.Consumer)4 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)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 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)3 ExplainabilityStatus (org.kie.kogito.explainability.api.ExplainabilityStatus)3 ModelIdentifier (org.kie.kogito.explainability.api.ModelIdentifier)3 Prediction (org.kie.kogito.explainability.model.Prediction)3 Query (org.kie.kogito.persistence.api.query.Query)3 BooleanNode (com.fasterxml.jackson.databind.node.BooleanNode)2