Search in sources :

Example 1 with SaliencyResponse

use of org.kie.kogito.trusty.service.common.responses.SaliencyResponse 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 SaliencyResponse

use of org.kie.kogito.trusty.service.common.responses.SaliencyResponse in project kogito-apps by kiegroup.

the class JITDMNServiceImpl method buildSalienciesResponse.

private List<SaliencyResponse> buildSalienciesResponse(DMNModel dmnModel, Map<String, Saliency> saliencyMap) {
    List<SaliencyResponse> saliencyModelResponse = new ArrayList<>();
    for (Map.Entry<String, Saliency> entry : saliencyMap.entrySet()) {
        DecisionNode decisionByName = dmnModel.getDecisionByName(entry.getKey());
        saliencyModelResponse.add(new SaliencyResponse(decisionByName.getId(), decisionByName.getName(), entry.getValue().getPerFeatureImportance().stream().map(JITDMNServiceImpl::featureImportanceModelToResponse).filter(Objects::nonNull).collect(Collectors.toList())));
    }
    return saliencyModelResponse;
}
Also used : SaliencyResponse(org.kie.kogito.trusty.service.common.responses.SaliencyResponse) ArrayList(java.util.ArrayList) Saliency(org.kie.kogito.explainability.model.Saliency) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) Map(java.util.Map)

Example 3 with SaliencyResponse

use of org.kie.kogito.trusty.service.common.responses.SaliencyResponse in project kogito-apps by kiegroup.

the class JITDMNServiceImpl method evaluateModelAndExplain.

public DMNResultWithExplanation evaluateModelAndExplain(DMNEvaluator dmnEvaluator, Map<String, Object> context) {
    LocalDMNPredictionProvider localDMNPredictionProvider = new LocalDMNPredictionProvider(dmnEvaluator);
    DMNResult dmnResult = dmnEvaluator.evaluate(context);
    Prediction prediction = new SimplePrediction(LocalDMNPredictionProvider.toPredictionInput(context), LocalDMNPredictionProvider.toPredictionOutput(dmnResult));
    LimeConfig limeConfig = new LimeConfig().withSamples(explainabilityLimeSampleSize).withPerturbationContext(new PerturbationContext(new Random(), explainabilityLimeNoOfPerturbation));
    LimeExplainer limeExplainer = new LimeExplainer(limeConfig);
    Map<String, Saliency> saliencyMap;
    try {
        saliencyMap = limeExplainer.explainAsync(prediction, localDMNPredictionProvider).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    } catch (TimeoutException | InterruptedException | ExecutionException e) {
        if (e instanceof InterruptedException) {
            LOGGER.error("Critical InterruptedException occurred", e);
            Thread.currentThread().interrupt();
        }
        return new DMNResultWithExplanation(new JITDMNResult(dmnEvaluator.getNamespace(), dmnEvaluator.getName(), dmnResult), new SalienciesResponse(EXPLAINABILITY_FAILED, EXPLAINABILITY_FAILED_MESSAGE, null));
    }
    List<SaliencyResponse> saliencyModelResponse = buildSalienciesResponse(dmnEvaluator.getDmnModel(), saliencyMap);
    return new DMNResultWithExplanation(new JITDMNResult(dmnEvaluator.getNamespace(), dmnEvaluator.getName(), dmnResult), new SalienciesResponse(EXPLAINABILITY_SUCCEEDED, null, saliencyModelResponse));
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) SalienciesResponse(org.kie.kogito.trusty.service.common.responses.SalienciesResponse) DMNResult(org.kie.dmn.api.core.DMNResult) JITDMNResult(org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult) PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) SaliencyResponse(org.kie.kogito.trusty.service.common.responses.SaliencyResponse) LimeExplainer(org.kie.kogito.explainability.local.lime.LimeExplainer) Prediction(org.kie.kogito.explainability.model.Prediction) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) DMNResultWithExplanation(org.kie.kogito.jitexecutor.dmn.responses.DMNResultWithExplanation) JITDMNResult(org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult) Saliency(org.kie.kogito.explainability.model.Saliency) LimeConfig(org.kie.kogito.explainability.local.lime.LimeConfig) Random(java.util.Random) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with SaliencyResponse

use of org.kie.kogito.trusty.service.common.responses.SaliencyResponse in project kogito-apps by kiegroup.

the class LIMESaliencyConverterTest method testFromResult_DecisionExists_WhenOutcomeNameNotFound.

@Test
public void testFromResult_DecisionExists_WhenOutcomeNameNotFound() {
    LIMEExplainabilityResult result = LIMEExplainabilityResult.buildSucceeded(EXECUTION_ID, List.of(new SaliencyModel("outcomeName1", List.of(new FeatureImportanceModel("feature1", 1.0))), new SaliencyModel("outcomeName2", List.of(new FeatureImportanceModel("feature2", 2.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", "outcomeNameX", 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(1, response.getSaliencies().size());
    List<SaliencyResponse> saliencyResponses = new ArrayList<>(response.getSaliencies());
    SaliencyResponse saliencyResponse1 = saliencyResponses.get(0);
    assertEquals("outcomeId1", saliencyResponse1.getOutcomeId());
    assertEquals("outcomeName1", saliencyResponse1.getOutcomeName());
    assertEquals(1, saliencyResponse1.getFeatureImportance().size());
    Optional<FeatureImportanceModel> oFeatureImportance1Model1 = saliencyResponse1.getFeatureImportance().stream().filter(fim -> fim.getFeatureName().equals("feature1")).findFirst();
    assertTrue(oFeatureImportance1Model1.isPresent());
    assertEquals(1.0, oFeatureImportance1Model1.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)

Aggregations

SaliencyResponse (org.kie.kogito.trusty.service.common.responses.SaliencyResponse)4 ArrayList (java.util.ArrayList)3 SalienciesResponse (org.kie.kogito.trusty.service.common.responses.SalienciesResponse)3 IntNode (com.fasterxml.jackson.databind.node.IntNode)2 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)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 Test (org.junit.jupiter.api.Test)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 ExplainabilityStatus (org.kie.kogito.explainability.api.ExplainabilityStatus)2 FeatureImportanceModel (org.kie.kogito.explainability.api.FeatureImportanceModel)2 LIMEExplainabilityResult (org.kie.kogito.explainability.api.LIMEExplainabilityResult)2 SaliencyModel (org.kie.kogito.explainability.api.SaliencyModel)2 Saliency (org.kie.kogito.explainability.model.Saliency)2 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)2