Search in sources :

Example 11 with CounterfactualSearchDomain

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

the class CounterfactualExplainerServiceHandlerTest method testGetPredictionWithCollectionSearchDomains.

@Test
public void testGetPredictionWithCollectionSearchDomains() {
    CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, COUNTERFACTUAL_ID, Collections.emptyList(), Collections.emptyList(), List.of(new CounterfactualSearchDomain("input1", new CounterfactualSearchDomainCollectionValue("number", List.of(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) CounterfactualSearchDomainCollectionValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainCollectionValue) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Example 12 with CounterfactualSearchDomain

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

the class CounterfactualExplainerServiceHandlerTest method testGetPredictionWithFlatSearchDomainsNotFixed.

@Test
public void testGetPredictionWithFlatSearchDomainsNotFixed() {
    CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, COUNTERFACTUAL_ID, List.of(new NamedTypedValue("output1", new UnitValue("number", new IntNode(25)))), Collections.emptyList(), List.of(new CounterfactualSearchDomain("output1", new CounterfactualSearchDomainUnitValue("number", "number", false, new CounterfactualDomainRange(new IntNode(10), new IntNode(20))))), MAX_RUNNING_TIME_SECONDS);
    Prediction prediction = handler.getPrediction(request);
    assertTrue(prediction instanceof CounterfactualPrediction);
    CounterfactualPrediction counterfactualPrediction = (CounterfactualPrediction) prediction;
    assertEquals(1, counterfactualPrediction.getInput().getFeatures().size());
    Feature feature1 = counterfactualPrediction.getInput().getFeatures().get(0);
    assertTrue(feature1.getDomain() instanceof NumericalFeatureDomain);
    final NumericalFeatureDomain domain = (NumericalFeatureDomain) feature1.getDomain();
    assertEquals(10, domain.getLowerBound());
    assertEquals(20, domain.getUpperBound());
    assertEquals(counterfactualPrediction.getMaxRunningTimeSeconds(), request.getMaxRunningTimeSeconds());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) IntNode(com.fasterxml.jackson.databind.node.IntNode) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) Prediction(org.kie.kogito.explainability.model.Prediction) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) Feature(org.kie.kogito.explainability.model.Feature) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) Test(org.junit.jupiter.api.Test)

Example 13 with CounterfactualSearchDomain

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

the class CounterfactualExplainerServiceHandlerTest method testGetPredictionWithFlatSearchDomainsFixed.

@Test
public void testGetPredictionWithFlatSearchDomainsFixed() {
    CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, COUNTERFACTUAL_ID, List.of(new NamedTypedValue("output1", new UnitValue("number", new IntNode(25)))), Collections.emptyList(), List.of(new CounterfactualSearchDomain("output1", new CounterfactualSearchDomainUnitValue("number", "number", true, new CounterfactualDomainRange(new IntNode(10), new IntNode(20))))), MAX_RUNNING_TIME_SECONDS);
    Prediction prediction = handler.getPrediction(request);
    assertTrue(prediction instanceof CounterfactualPrediction);
    CounterfactualPrediction counterfactualPrediction = (CounterfactualPrediction) prediction;
    assertEquals(1, counterfactualPrediction.getInput().getFeatures().size());
    Feature feature1 = counterfactualPrediction.getInput().getFeatures().get(0);
    assertTrue(feature1.getDomain() instanceof EmptyFeatureDomain);
    assertEquals(counterfactualPrediction.getMaxRunningTimeSeconds(), request.getMaxRunningTimeSeconds());
}
Also used : CounterfactualExplainabilityRequest(org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) IntNode(com.fasterxml.jackson.databind.node.IntNode) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) Prediction(org.kie.kogito.explainability.model.Prediction) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) Feature(org.kie.kogito.explainability.model.Feature) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) Test(org.junit.jupiter.api.Test)

Example 14 with CounterfactualSearchDomain

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

the class ConversionUtilsTest method getDoubleSearchDomain.

private static CounterfactualSearchDomain getDoubleSearchDomain(String name, double lowerBound, double upperBound) {
    final CounterfactualDomainRange range = new CounterfactualDomainRange(DoubleNode.valueOf(lowerBound), DoubleNode.valueOf(upperBound));
    CounterfactualSearchDomainUnitValue searchDomain = new CounterfactualSearchDomainUnitValue("double", "double", Boolean.FALSE, range);
    return new CounterfactualSearchDomain(name, searchDomain);
}
Also used : CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain)

Example 15 with CounterfactualSearchDomain

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

the class ConversionUtilsTest method toFeatureDomainsConstraintsMultiElement.

@Test
void toFeatureDomainsConstraintsMultiElement() {
    final Random random = new Random();
    List<NamedTypedValue> values = IntStream.range(0, 10).mapToObj(i -> getDoubleUnit("f-" + i, random.nextDouble())).collect(Collectors.toList());
    List<CounterfactualSearchDomain> domains = IntStream.range(0, 10).mapToObj(i -> getDoubleSearchDomain("f-" + i, -1, 1)).collect(Collectors.toList());
    final List<Feature> features = ConversionUtils.toFeatureList(values, domains);
    assertEquals(10, features.size());
    assertTrue(features.stream().allMatch(f -> f.getType() == Type.NUMBER));
    assertTrue(features.stream().noneMatch(Feature::isConstrained));
    assertTrue(features.stream().map(Feature::getDomain).noneMatch(FeatureDomain::isEmpty));
    assertTrue(features.stream().map(Feature::getDomain).map(FeatureDomain::getLowerBound).allMatch(lb -> lb == -1.0));
    assertTrue(features.stream().map(Feature::getDomain).map(FeatureDomain::getUpperBound).allMatch(ub -> ub == 1.0));
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IntNode(com.fasterxml.jackson.databind.node.IntNode) Feature(org.kie.kogito.explainability.model.Feature) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Random(java.util.Random) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) Value(org.kie.kogito.explainability.model.Value) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Pair(org.apache.commons.lang3.tuple.Pair) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonObject(io.vertx.core.json.JsonObject) Collections.singletonMap(java.util.Collections.singletonMap) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CounterfactualDomainCategorical(org.kie.kogito.explainability.api.CounterfactualDomainCategorical) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Type(org.kie.kogito.explainability.model.Type) TextNode(com.fasterxml.jackson.databind.node.TextNode) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Test(org.junit.jupiter.api.Test) List(java.util.List) CounterfactualDomain(org.kie.kogito.explainability.api.CounterfactualDomain) CollectionValue(org.kie.kogito.tracing.typedvalue.CollectionValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Output(org.kie.kogito.explainability.model.Output) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) BooleanNode(com.fasterxml.jackson.databind.node.BooleanNode) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) Random(java.util.Random) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) Feature(org.kie.kogito.explainability.model.Feature) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Test(org.junit.jupiter.api.Test)

Aggregations

CounterfactualSearchDomain (org.kie.kogito.explainability.api.CounterfactualSearchDomain)28 Test (org.junit.jupiter.api.Test)22 CounterfactualDomainRange (org.kie.kogito.explainability.api.CounterfactualDomainRange)19 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)19 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)17 IntNode (com.fasterxml.jackson.databind.node.IntNode)16 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)12 TextNode (com.fasterxml.jackson.databind.node.TextNode)11 ArrayList (java.util.ArrayList)9 CounterfactualDomainCategorical (org.kie.kogito.explainability.api.CounterfactualDomainCategorical)9 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)9 List (java.util.List)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Map (java.util.Map)4 CounterfactualDomain (org.kie.kogito.explainability.api.CounterfactualDomain)4 Feature (org.kie.kogito.explainability.model.Feature)4 StructureValue (org.kie.kogito.tracing.typedvalue.StructureValue)4 CounterfactualRequestResponse (org.kie.kogito.trusty.service.common.responses.CounterfactualRequestResponse)4 Decision (org.kie.kogito.trusty.storage.api.model.decision.Decision)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4