Search in sources :

Example 1 with LIMEExplainabilityResult

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

the class LIMESaliencyConverterTest method testFromResult_DecisionExists.

@Test
public void testFromResult_DecisionExists() {
    LIMEExplainabilityResult result = LIMEExplainabilityResult.buildSucceeded(EXECUTION_ID, List.of(new SaliencyModel("outcomeName1", List.of(new FeatureImportanceModel("feature1a", 1.0), new FeatureImportanceModel("feature1b", 2.0))), new SaliencyModel("outcomeName2", List.of(new FeatureImportanceModel("feature2", 3.0)))));
    Decision decision = new Decision(EXECUTION_ID, "sourceUrl", "serviceUrl", 0L, true, "executorName", "executorModelName", "executorModelNamespace", new ArrayList<>(), new ArrayList<>());
    decision.getOutcomes().add(new DecisionOutcome("outcomeId1", "outcomeName1", ExplainabilityStatus.SUCCEEDED.name(), new UnitValue("type", new IntNode(1)), Collections.emptyList(), Collections.emptyList()));
    decision.getOutcomes().add(new DecisionOutcome("outcomeId2", "outcomeName2", ExplainabilityStatus.SUCCEEDED.name(), new UnitValue("type2", new IntNode(2)), Collections.emptyList(), Collections.emptyList()));
    when(trustyService.getDecisionById(eq(EXECUTION_ID))).thenReturn(decision);
    SalienciesResponse response = converter.fromResult(EXECUTION_ID, result);
    assertNotNull(response);
    assertEquals(ExplainabilityStatus.SUCCEEDED.name(), response.getStatus());
    assertEquals(2, response.getSaliencies().size());
    List<SaliencyResponse> saliencyResponses = new ArrayList<>(response.getSaliencies());
    SaliencyResponse saliencyResponse1 = saliencyResponses.get(0);
    assertEquals("outcomeId1", saliencyResponse1.getOutcomeId());
    assertEquals("outcomeName1", saliencyResponse1.getOutcomeName());
    assertEquals(2, saliencyResponse1.getFeatureImportance().size());
    Optional<FeatureImportanceModel> oFeatureImportance1Model1 = saliencyResponse1.getFeatureImportance().stream().filter(fim -> fim.getFeatureName().equals("feature1a")).findFirst();
    assertTrue(oFeatureImportance1Model1.isPresent());
    assertEquals(1.0, oFeatureImportance1Model1.get().getFeatureScore());
    Optional<FeatureImportanceModel> oFeatureImportance2Model1 = saliencyResponse1.getFeatureImportance().stream().filter(fim -> fim.getFeatureName().equals("feature1b")).findFirst();
    assertTrue(oFeatureImportance2Model1.isPresent());
    assertEquals(2.0, oFeatureImportance2Model1.get().getFeatureScore());
    SaliencyResponse saliencyResponse2 = saliencyResponses.get(1);
    assertEquals("outcomeId2", saliencyResponse2.getOutcomeId());
    assertEquals("outcomeName2", saliencyResponse2.getOutcomeName());
    assertEquals(1, saliencyResponse2.getFeatureImportance().size());
    Optional<FeatureImportanceModel> oFeatureImportance1Model2 = saliencyResponse2.getFeatureImportance().stream().filter(fim -> fim.getFeatureName().equals("feature2")).findFirst();
    assertTrue(oFeatureImportance1Model2.isPresent());
    assertEquals(3.0, oFeatureImportance1Model2.get().getFeatureScore());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) FeatureImportanceModel(org.kie.kogito.explainability.api.FeatureImportanceModel) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) IntNode(com.fasterxml.jackson.databind.node.IntNode) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) SaliencyResponse(org.kie.kogito.trusty.service.common.responses.SaliencyResponse) ArrayList(java.util.ArrayList) SalienciesResponse(org.kie.kogito.trusty.service.common.responses.SalienciesResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ExplainabilityStatus(org.kie.kogito.explainability.api.ExplainabilityStatus) Mockito.when(org.mockito.Mockito.when) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SaliencyModel(org.kie.kogito.explainability.api.SaliencyModel) Collections(java.util.Collections) TrustyService(org.kie.kogito.trusty.service.common.TrustyService) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) SalienciesResponse(org.kie.kogito.trusty.service.common.responses.SalienciesResponse) LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) SaliencyResponse(org.kie.kogito.trusty.service.common.responses.SaliencyResponse) SaliencyModel(org.kie.kogito.explainability.api.SaliencyModel) DecisionOutcome(org.kie.kogito.trusty.storage.api.model.decision.DecisionOutcome) ArrayList(java.util.ArrayList) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) IntNode(com.fasterxml.jackson.databind.node.IntNode) FeatureImportanceModel(org.kie.kogito.explainability.api.FeatureImportanceModel) Test(org.junit.jupiter.api.Test)

Example 2 with LIMEExplainabilityResult

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

the class ExplanationServiceImplTest method testLIMEExplainAsyncSuccess.

@SuppressWarnings("unchecked")
void testLIMEExplainAsyncSuccess(ThrowingSupplier<BaseExplainabilityResult> invocation) {
    when(instance.stream()).thenReturn(Stream.of(limeExplainerServiceHandlerMock));
    when(limeExplainerMock.explainAsync(any(Prediction.class), eq(predictionProviderMock), any(Consumer.class))).thenReturn(CompletableFuture.completedFuture(SALIENCY_MAP));
    BaseExplainabilityResult result = assertDoesNotThrow(invocation);
    assertNotNull(result);
    assertTrue(result instanceof LIMEExplainabilityResult);
    LIMEExplainabilityResult limeResult = (LIMEExplainabilityResult) result;
    assertEquals(EXECUTION_ID, limeResult.getExecutionId());
    assertSame(ExplainabilityStatus.SUCCEEDED, limeResult.getStatus());
    assertNull(limeResult.getStatusDetails());
    assertEquals(SALIENCY_MAP.size(), limeResult.getSaliencies().size());
    SaliencyModel saliency = limeResult.getSaliencies().iterator().next();
    assertEquals(SALIENCY.getPerFeatureImportance().size(), saliency.getFeatureImportance().size());
    FeatureImportanceModel featureImportance1 = saliency.getFeatureImportance().get(0);
    assertEquals(FEATURE_IMPORTANCE_1.getFeature().getName(), featureImportance1.getFeatureName());
    assertEquals(FEATURE_IMPORTANCE_1.getScore(), featureImportance1.getFeatureScore(), 0.01);
}
Also used : LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) Consumer(java.util.function.Consumer) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) SaliencyModel(org.kie.kogito.explainability.api.SaliencyModel) FeatureImportanceModel(org.kie.kogito.explainability.api.FeatureImportanceModel) Prediction(org.kie.kogito.explainability.model.Prediction)

Example 3 with LIMEExplainabilityResult

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

the class ExplanationServiceImplTest method testServiceHandlerLookupLIME.

@Test
void testServiceHandlerLookupLIME() {
    when(instance.stream()).thenReturn(Stream.of(limeExplainerServiceHandlerMock, cfExplainerServiceHandlerMock));
    when(limeExplainerMock.explainAsync(any(), any(), any())).thenReturn(CompletableFuture.completedFuture(SALIENCY_MAP));
    BaseExplainabilityResult result = assertDoesNotThrow(() -> explanationService.explainAsync(LIME_REQUEST, callbackMock).toCompletableFuture().get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit()));
    assertNotNull(result);
    assertTrue(result instanceof LIMEExplainabilityResult);
}
Also used : LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) Test(org.junit.jupiter.api.Test)

Example 4 with LIMEExplainabilityResult

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

the class LimeExplainerServiceHandlerTest method testCreateSucceededResult.

@Test
public void testCreateSucceededResult() {
    LIMEExplainabilityRequest request = new LIMEExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, Collections.emptyList(), Collections.emptyList());
    Map<String, Saliency> saliencies = Map.of("s1", new Saliency(new Output("salary", Type.NUMBER), List.of(new FeatureImportance(new Feature("age", Type.NUMBER, new Value(25.0)), 5.0), new FeatureImportance(new Feature("dependents", Type.NUMBER, new Value(2)), -11.0))));
    BaseExplainabilityResult base = handler.createSucceededResult(request, saliencies);
    assertTrue(base instanceof LIMEExplainabilityResult);
    LIMEExplainabilityResult result = (LIMEExplainabilityResult) base;
    assertEquals(ExplainabilityStatus.SUCCEEDED, result.getStatus());
    assertEquals(EXECUTION_ID, result.getExecutionId());
    assertEquals(1, result.getSaliencies().size());
    SaliencyModel saliencyModel = result.getSaliencies().iterator().next();
    assertEquals(2, saliencyModel.getFeatureImportance().size());
    assertEquals("age", saliencyModel.getFeatureImportance().get(0).getFeatureName());
    assertEquals(5.0, saliencyModel.getFeatureImportance().get(0).getFeatureScore());
    assertEquals("dependents", saliencyModel.getFeatureImportance().get(1).getFeatureName());
    assertEquals(-11.0, saliencyModel.getFeatureImportance().get(1).getFeatureScore());
}
Also used : LIMEExplainabilityRequest(org.kie.kogito.explainability.api.LIMEExplainabilityRequest) LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) FeatureImportance(org.kie.kogito.explainability.model.FeatureImportance) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) SaliencyModel(org.kie.kogito.explainability.api.SaliencyModel) Output(org.kie.kogito.explainability.model.Output) Value(org.kie.kogito.explainability.model.Value) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CollectionValue(org.kie.kogito.tracing.typedvalue.CollectionValue) Saliency(org.kie.kogito.explainability.model.Saliency) Feature(org.kie.kogito.explainability.model.Feature) Test(org.junit.jupiter.api.Test)

Example 5 with LIMEExplainabilityResult

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

the class LimeExplainerServiceHandlerTest method testCreateFailedResult.

@Test
public void testCreateFailedResult() {
    LIMEExplainabilityRequest request = new LIMEExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, Collections.emptyList(), Collections.emptyList());
    BaseExplainabilityResult base = handler.createFailedResult(request, new NullPointerException("Something went wrong"));
    assertTrue(base instanceof LIMEExplainabilityResult);
    LIMEExplainabilityResult result = (LIMEExplainabilityResult) base;
    assertEquals(ExplainabilityStatus.FAILED, result.getStatus());
    assertEquals("Something went wrong", result.getStatusDetails());
    assertEquals(EXECUTION_ID, result.getExecutionId());
}
Also used : LIMEExplainabilityRequest(org.kie.kogito.explainability.api.LIMEExplainabilityRequest) LIMEExplainabilityResult(org.kie.kogito.explainability.api.LIMEExplainabilityResult) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) Test(org.junit.jupiter.api.Test)

Aggregations

LIMEExplainabilityResult (org.kie.kogito.explainability.api.LIMEExplainabilityResult)14 Test (org.junit.jupiter.api.Test)13 SaliencyModel (org.kie.kogito.explainability.api.SaliencyModel)7 FeatureImportanceModel (org.kie.kogito.explainability.api.FeatureImportanceModel)6 BaseExplainabilityResult (org.kie.kogito.explainability.api.BaseExplainabilityResult)5 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 IntNode (com.fasterxml.jackson.databind.node.IntNode)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)2 Consumer (java.util.function.Consumer)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)2 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 ExplainabilityStatus (org.kie.kogito.explainability.api.ExplainabilityStatus)2