Search in sources :

Example 1 with CounterfactualExplainabilityRequest

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

the class ExplainabilityApiV1Test method testRequestCounterfactualsWhenExecutionDoesExist.

@Test
public void testRequestCounterfactualsWhenExecutionDoesExist() {
    when(trustyService.requestCounterfactuals(anyString(), any(), any())).thenReturn(new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, new ModelIdentifier("resourceType", "resourceIdentifier"), COUNTERFACTUAL_ID, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), MAX_RUNNING_TIME_SECONDS));
    org.kie.kogito.trusty.service.common.requests.CounterfactualRequest request = new org.kie.kogito.trusty.service.common.requests.CounterfactualRequest(Collections.emptyList(), Collections.emptyList());
    Response response = explainabilityEndpoint.requestCounterfactuals(EXECUTION_ID, request);
    assertNotNull(response);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    Object entity = response.getEntity();
    assertNotNull(entity);
    assertTrue(entity instanceof CounterfactualRequestResponse);
    CounterfactualRequestResponse counterfactualRequestResponse = (CounterfactualRequestResponse) entity;
    assertEquals(EXECUTION_ID, counterfactualRequestResponse.getExecutionId());
    assertEquals(COUNTERFACTUAL_ID, counterfactualRequestResponse.getCounterfactualId());
    assertEquals(MAX_RUNNING_TIME_SECONDS, counterfactualRequestResponse.getMaxRunningTimeSeconds());
}
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) ModelIdentifier(org.kie.kogito.explainability.api.ModelIdentifier) CounterfactualRequestResponse(org.kie.kogito.trusty.service.common.responses.CounterfactualRequestResponse) Test(org.junit.jupiter.api.Test)

Example 2 with CounterfactualExplainabilityRequest

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

the class CounterfactualExplainabilityRequestMarshallerTest method testWriteAndRead.

@Test
public void testWriteAndRead() throws IOException {
    ModelIdentifier modelIdentifier = new ModelIdentifier("resourceType", "resourceId");
    List<NamedTypedValue> originalInputs = Collections.singletonList(new NamedTypedValue("unitIn", new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(10))));
    List<NamedTypedValue> goals = Collections.singletonList(new NamedTypedValue("unitIn", new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(10))));
    List<CounterfactualSearchDomain> searchDomains = Collections.singletonList(new CounterfactualSearchDomain("age", new CounterfactualSearchDomainUnitValue("integer", "integer", Boolean.TRUE, new CounterfactualDomainRange(JsonNodeFactory.instance.numberNode(0), JsonNodeFactory.instance.numberNode(10)))));
    CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest("executionId", "serviceUrl", modelIdentifier, "counterfactualId", originalInputs, goals, searchDomains, 60L);
    CounterfactualExplainabilityRequestMarshaller marshaller = new CounterfactualExplainabilityRequestMarshaller(new ObjectMapper());
    marshaller.writeTo(writer, request);
    CounterfactualExplainabilityRequest retrieved = marshaller.readFrom(reader);
    Assertions.assertEquals(request.getExecutionId(), retrieved.getExecutionId());
    Assertions.assertEquals(request.getCounterfactualId(), retrieved.getCounterfactualId());
    Assertions.assertEquals(goals.get(0).getName(), retrieved.getGoals().stream().findFirst().get().getName());
    Assertions.assertEquals(searchDomains.get(0).getName(), retrieved.getSearchDomains().stream().findFirst().get().getName());
    Assertions.assertEquals(0, ((CounterfactualDomainRange) retrieved.getSearchDomains().stream().findFirst().get().getValue().toUnit().getDomain()).getLowerBound().asInt());
    Assertions.assertEquals(60L, request.getMaxRunningTimeSeconds());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) ModelIdentifier(org.kie.kogito.explainability.api.ModelIdentifier) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Example 3 with CounterfactualExplainabilityRequest

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

the class CounterfactualExplainerServiceHandlerTest method testCreateIntermediateResult.

@Test
public void testCreateIntermediateResult() {
    CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, COUNTERFACTUAL_ID, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), MAX_RUNNING_TIME_SECONDS);
    List<CounterfactualEntity> entities = List.of(DoubleEntity.from(new Feature("input1", Type.NUMBER, new Value(123.0d)), 0, 1000));
    CounterfactualResult counterfactuals = new CounterfactualResult(entities, entities.stream().map(CounterfactualEntity::asFeature).collect(Collectors.toList()), List.of(new PredictionOutput(List.of(new Output("output1", Type.NUMBER, new Value(555.0d), 1.0)))), true, UUID.fromString(SOLUTION_ID), UUID.fromString(EXECUTION_ID), 0);
    BaseExplainabilityResult base = handler.createIntermediateResult(request, counterfactuals);
    assertTrue(base instanceof CounterfactualExplainabilityResult);
    CounterfactualExplainabilityResult result = (CounterfactualExplainabilityResult) base;
    assertEquals(ExplainabilityStatus.SUCCEEDED, result.getStatus());
    assertEquals(CounterfactualExplainabilityResult.Stage.INTERMEDIATE, result.getStage());
    assertEquals(EXECUTION_ID, result.getExecutionId());
    assertEquals(COUNTERFACTUAL_ID, result.getCounterfactualId());
    assertEquals(1, result.getInputs().size());
    assertTrue(result.getInputs().stream().anyMatch(i -> i.getName().equals("input1")));
    NamedTypedValue input1 = result.getInputs().iterator().next();
    assertEquals(Double.class.getSimpleName(), input1.getValue().getType());
    assertEquals(TypedValue.Kind.UNIT, input1.getValue().getKind());
    assertEquals(123.0, input1.getValue().toUnit().getValue().asDouble());
    assertEquals(1, result.getOutputs().size());
    assertTrue(result.getOutputs().stream().anyMatch(o -> o.getName().equals("output1")));
    NamedTypedValue output1 = result.getOutputs().iterator().next();
    assertEquals(Double.class.getSimpleName(), output1.getValue().getType());
    assertEquals(TypedValue.Kind.UNIT, output1.getValue().getKind());
    assertEquals(555.0, output1.getValue().toUnit().getValue().asDouble());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) BeforeEach(org.junit.jupiter.api.BeforeEach) BaseExplainabilityRequest(org.kie.kogito.explainability.api.BaseExplainabilityRequest) Feature(org.kie.kogito.explainability.model.Feature) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) Value(org.kie.kogito.explainability.model.Value) CounterfactualResult(org.kie.kogito.explainability.local.counterfactual.CounterfactualResult) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) ExplainabilityStatus(org.kie.kogito.explainability.api.ExplainabilityStatus) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Test(org.junit.jupiter.api.Test) List(java.util.List) CollectionValue(org.kie.kogito.tracing.typedvalue.CollectionValue) Output(org.kie.kogito.explainability.model.Output) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntNode(com.fasterxml.jackson.databind.node.IntNode) Prediction(org.kie.kogito.explainability.model.Prediction) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) PredictionProviderFactory(org.kie.kogito.explainability.PredictionProviderFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CounterfactualSearchDomainCollectionValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainCollectionValue) CounterfactualExplainer(org.kie.kogito.explainability.local.counterfactual.CounterfactualExplainer) Type(org.kie.kogito.explainability.model.Type) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) DoubleEntity(org.kie.kogito.explainability.local.counterfactual.entities.DoubleEntity) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) BooleanNode(com.fasterxml.jackson.databind.node.BooleanNode) Collections(java.util.Collections) ModelIdentifier(org.kie.kogito.explainability.api.ModelIdentifier) CounterfactualExplainabilityResult(org.kie.kogito.explainability.api.CounterfactualExplainabilityResult) Feature(org.kie.kogito.explainability.model.Feature) CounterfactualResult(org.kie.kogito.explainability.local.counterfactual.CounterfactualResult) CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) BaseExplainabilityResult(org.kie.kogito.explainability.api.BaseExplainabilityResult) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Value(org.kie.kogito.explainability.model.Value) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CollectionValue(org.kie.kogito.tracing.typedvalue.CollectionValue) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue) CounterfactualSearchDomainCollectionValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainCollectionValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) Test(org.junit.jupiter.api.Test)

Example 4 with CounterfactualExplainabilityRequest

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

the class CounterfactualExplainerServiceHandlerTest method testGetPredictionWithStructuredSearchDomains.

@Test
public void testGetPredictionWithStructuredSearchDomains() {
    CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, COUNTERFACTUAL_ID, Collections.emptyList(), Collections.emptyList(), List.of(new CounterfactualSearchDomain("input1", new CounterfactualSearchDomainStructureValue("number", Map.of("input2b", new CounterfactualSearchDomainUnitValue("number", "number", true, new CounterfactualDomainRange(new IntNode(10), new IntNode(20))))))), MAX_RUNNING_TIME_SECONDS);
    assertThrows(IllegalArgumentException.class, () -> handler.getPrediction(request));
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) IntNode(com.fasterxml.jackson.databind.node.IntNode) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue) Test(org.junit.jupiter.api.Test)

Example 5 with CounterfactualExplainabilityRequest

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

the class CounterfactualExplainerServiceHandlerTest method testGetPredictionWithStructuredInputModel.

@Test
public void testGetPredictionWithStructuredInputModel() {
    CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, COUNTERFACTUAL_ID, List.of(new NamedTypedValue("input1", new StructureValue("number", Map.of("input2b", new UnitValue("number", new IntNode(55)))))), Collections.emptyList(), Collections.emptyList(), MAX_RUNNING_TIME_SECONDS);
    assertThrows(IllegalArgumentException.class, () -> handler.getPrediction(request));
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) IntNode(com.fasterxml.jackson.databind.node.IntNode) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) CounterfactualSearchDomainStructureValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue) Test(org.junit.jupiter.api.Test)

Aggregations

CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)41 Test (org.junit.jupiter.api.Test)37 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)23 IntNode (com.fasterxml.jackson.databind.node.IntNode)22 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)21 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)18 CounterfactualDomainRange (org.kie.kogito.explainability.api.CounterfactualDomainRange)17 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)17 List (java.util.List)11 CounterfactualSearchDomainStructureValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue)11 StructureValue (org.kie.kogito.tracing.typedvalue.StructureValue)10 ArrayList (java.util.ArrayList)9 ModelIdentifier (org.kie.kogito.explainability.api.ModelIdentifier)9 TextNode (com.fasterxml.jackson.databind.node.TextNode)8 CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)8 CounterfactualSearchDomainCollectionValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainCollectionValue)8 CounterfactualPrediction (org.kie.kogito.explainability.model.CounterfactualPrediction)8 Prediction (org.kie.kogito.explainability.model.Prediction)8 CollectionValue (org.kie.kogito.tracing.typedvalue.CollectionValue)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7