Search in sources :

Example 1 with NamedTypedValue

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

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

the class DecisionMarshallerTest method testWriteAndRead.

@Test
public void testWriteAndRead() throws IOException {
    List<DecisionInput> inputs = Collections.singletonList(new DecisionInput("id", "in", new UnitValue("nameIn", "number", JsonNodeFactory.instance.numberNode(10))));
    List<DecisionOutcome> outcomes = Collections.singletonList(new DecisionOutcome("id", "out", DMNDecisionResult.DecisionEvaluationStatus.SUCCEEDED.toString(), new UnitValue("nameOut", "number", JsonNodeFactory.instance.numberNode(10)), List.of(new NamedTypedValue("nameOut", new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(10)))), new ArrayList<>()));
    Decision decision = new Decision("executionId", "source", "serviceUrl", 0L, true, "executor", "model", "namespace", inputs, outcomes);
    DecisionMarshaller marshaller = new DecisionMarshaller(new ObjectMapper());
    marshaller.writeTo(writer, decision);
    Decision retrieved = marshaller.readFrom(reader);
    Assertions.assertEquals(decision.getExecutionId(), retrieved.getExecutionId());
    Assertions.assertEquals(decision.getSourceUrl(), retrieved.getSourceUrl());
    Assertions.assertEquals(decision.getServiceUrl(), retrieved.getServiceUrl());
    Assertions.assertEquals(decision.getExecutedModelName(), retrieved.getExecutedModelName());
    Assertions.assertEquals(inputs.get(0).getName(), retrieved.getInputs().stream().findFirst().get().getName());
    Assertions.assertEquals(inputs.get(0).getValue().getType(), retrieved.getInputs().stream().findFirst().get().getValue().getType());
    Assertions.assertEquals(inputs.get(0).getValue().toUnit().getBaseType(), retrieved.getInputs().stream().findFirst().get().getValue().toUnit().getBaseType());
    Assertions.assertEquals(outcomes.get(0).getOutcomeId(), retrieved.getOutcomes().stream().findFirst().get().getOutcomeId());
    Assertions.assertTrue(retrieved.getOutcomes().stream().findFirst().get().getMessages().isEmpty());
}
Also used : DecisionInput(org.kie.kogito.trusty.storage.api.model.decision.DecisionInput) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 3 with NamedTypedValue

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

use of org.kie.kogito.explainability.api.NamedTypedValue 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)

Example 5 with NamedTypedValue

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

the class CounterfactualExplainerServiceHandlerTest method testGetPredictionWithFlatOutputModelReordered.

@Test
public void testGetPredictionWithFlatOutputModelReordered() {
    CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, COUNTERFACTUAL_ID, Collections.emptyList(), List.of(new NamedTypedValue("inputsAreValid", new UnitValue("boolean", BooleanNode.FALSE)), new NamedTypedValue("canRequestLoan", new UnitValue("booelan", BooleanNode.TRUE)), new NamedTypedValue("my-scoring-function", new UnitValue("number", new DoubleNode(0.85)))), Collections.emptyList(), MAX_RUNNING_TIME_SECONDS);
    Prediction prediction = handler.getPrediction(request);
    assertTrue(prediction instanceof CounterfactualPrediction);
    CounterfactualPrediction counterfactualPrediction = (CounterfactualPrediction) prediction;
    List<Output> outputs = counterfactualPrediction.getOutput().getOutputs();
    assertEquals(3, outputs.size());
    Output output1 = outputs.get(0);
    assertEquals("my-scoring-function", output1.getName());
    assertEquals(Type.NUMBER, output1.getType());
    assertEquals(0.85, output1.getValue().asNumber());
    Output output2 = outputs.get(1);
    assertEquals("inputsAreValid", output2.getName());
    assertEquals(Type.BOOLEAN, output2.getType());
    assertEquals(Boolean.FALSE, output2.getValue().getUnderlyingObject());
    Output output3 = outputs.get(2);
    assertEquals("canRequestLoan", output3.getName());
    assertEquals(Type.BOOLEAN, output3.getType());
    assertEquals(Boolean.TRUE, output3.getValue().getUnderlyingObject());
    assertTrue(counterfactualPrediction.getInput().getFeatures().isEmpty());
    assertEquals(counterfactualPrediction.getMaxRunningTimeSeconds(), request.getMaxRunningTimeSeconds());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) Prediction(org.kie.kogito.explainability.model.Prediction) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) Test(org.junit.jupiter.api.Test)

Aggregations

NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)33 Test (org.junit.jupiter.api.Test)27 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)22 IntNode (com.fasterxml.jackson.databind.node.IntNode)19 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)19 CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)19 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)16 CounterfactualDomainRange (org.kie.kogito.explainability.api.CounterfactualDomainRange)12 CounterfactualExplainabilityResult (org.kie.kogito.explainability.api.CounterfactualExplainabilityResult)11 StructureValue (org.kie.kogito.tracing.typedvalue.StructureValue)11 List (java.util.List)9 CounterfactualSearchDomainStructureValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainStructureValue)9 Prediction (org.kie.kogito.explainability.model.Prediction)9 CollectionValue (org.kie.kogito.tracing.typedvalue.CollectionValue)9 Map (java.util.Map)8 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)8 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)8 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)8 ModelIdentifier (org.kie.kogito.explainability.api.ModelIdentifier)8 CounterfactualPrediction (org.kie.kogito.explainability.model.CounterfactualPrediction)8