use of org.kie.kogito.explainability.api.FeatureImportanceModel 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());
}
use of org.kie.kogito.explainability.api.FeatureImportanceModel 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);
}
use of org.kie.kogito.explainability.api.FeatureImportanceModel in project kogito-apps by kiegroup.
the class AbstractTrustyServiceIT method testStoreExplainabilityResult_LIME.
@Test
public void testStoreExplainabilityResult_LIME() {
String executionId = "myLIMEExecution1Store";
trustyService.storeExplainabilityResult(executionId, new LIMEExplainabilityResult(executionId, ExplainabilityStatus.SUCCEEDED, "status", List.of(new SaliencyModel("outcomeName", List.of(new FeatureImportanceModel("feature", 1.0))))));
LIMEExplainabilityResult result = trustyService.getExplainabilityResultById(executionId, LIMEExplainabilityResult.class);
assertNotNull(result);
}
use of org.kie.kogito.explainability.api.FeatureImportanceModel in project kogito-apps by kiegroup.
the class LIMEExplainabilityResultMarshallerTest method testWriteAndRead.
@Test
public void testWriteAndRead() throws IOException {
List<FeatureImportanceModel> featureImportanceModels = Collections.singletonList(new FeatureImportanceModel("aFeature", 0d));
List<SaliencyModel> saliencyModels = Collections.singletonList(new SaliencyModel("outcomeName", featureImportanceModels));
LIMEExplainabilityResult limeExplainabilityResult = new LIMEExplainabilityResult("executionId", ExplainabilityStatus.SUCCEEDED, "statusDetail", saliencyModels);
LIMEExplainabilityResultMarshaller marshaller = new LIMEExplainabilityResultMarshaller(new ObjectMapper());
marshaller.writeTo(writer, limeExplainabilityResult);
LIMEExplainabilityResult retrieved = marshaller.readFrom(reader);
Assertions.assertEquals(limeExplainabilityResult.getExecutionId(), retrieved.getExecutionId());
Assertions.assertEquals(limeExplainabilityResult.getStatus(), retrieved.getStatus());
Assertions.assertEquals(limeExplainabilityResult.getStatusDetails(), retrieved.getStatusDetails());
Assertions.assertEquals(saliencyModels.get(0).getOutcomeName(), retrieved.getSaliencies().get(0).getOutcomeName());
Assertions.assertEquals(featureImportanceModels.get(0).getFeatureName(), retrieved.getSaliencies().get(0).getFeatureImportance().get(0).getFeatureName());
Assertions.assertEquals(featureImportanceModels.get(0).getFeatureScore(), retrieved.getSaliencies().get(0).getFeatureImportance().get(0).getFeatureScore());
}
use of org.kie.kogito.explainability.api.FeatureImportanceModel in project kogito-apps by kiegroup.
the class LIMESaliencyConverterTest method testFromResult_DecisionNotExists.
@Test
public void testFromResult_DecisionNotExists() {
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)))));
when(trustyService.getDecisionById(eq(EXECUTION_ID))).thenThrow(new IllegalArgumentException());
assertThrows(IllegalArgumentException.class, () -> converter.fromResult(EXECUTION_ID, result));
}
Aggregations