Search in sources :

Example 6 with FeatureDomain

use of org.kie.kogito.explainability.model.domain.FeatureDomain in project kogito-apps by kiegroup.

the class CounterfactualEntityFactoryTest method testDoubleFactory.

@Test
void testDoubleFactory() {
    final double value = 5.5;
    final FeatureDomain domain = NumericalFeatureDomain.create(0.0, 10.0);
    final Feature feature = FeatureFactory.newNumericalFeature("double-feature", value, domain);
    final CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertTrue(counterfactualEntity instanceof DoubleEntity);
    assertEquals(value, counterfactualEntity.asFeature().getValue().asNumber());
}
Also used : CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) FixedDoubleEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedDoubleEntity) DoubleEntity(org.kie.kogito.explainability.local.counterfactual.entities.DoubleEntity) ObjectFeatureDomain(org.kie.kogito.explainability.model.domain.ObjectFeatureDomain) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) CurrencyFeatureDomain(org.kie.kogito.explainability.model.domain.CurrencyFeatureDomain) URIFeatureDomain(org.kie.kogito.explainability.model.domain.URIFeatureDomain) DurationFeatureDomain(org.kie.kogito.explainability.model.domain.DurationFeatureDomain) TimeFeatureDomain(org.kie.kogito.explainability.model.domain.TimeFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) BinaryFeatureDomain(org.kie.kogito.explainability.model.domain.BinaryFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) Feature(org.kie.kogito.explainability.model.Feature) Test(org.junit.jupiter.api.Test)

Example 7 with FeatureDomain

use of org.kie.kogito.explainability.model.domain.FeatureDomain in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method testNullIntegerInput.

/**
 * Null values for input Integer features should not be accepted as valid
 */
@Test
void testNullIntegerInput() throws ExecutionException, InterruptedException {
    List<Feature> features = new ArrayList<>();
    List<FeatureDomain> featureDomains = new ArrayList<>();
    List<Boolean> constraints = new ArrayList<>();
    // f-1
    features.add(FeatureFactory.newNumericalFeature("f-1", 1.0));
    featureDomains.add(NumericalFeatureDomain.create(0.0, 10.0));
    constraints.add(false);
    // f-2
    features.add(FeatureFactory.newNumericalFeature("f-2", null));
    featureDomains.add(NumericalFeatureDomain.create(0, 10));
    constraints.add(false);
    // f-3
    features.add(FeatureFactory.newBooleanFeature("f-3", true));
    featureDomains.add(EmptyFeatureDomain.create());
    constraints.add(false);
    PredictionInput input = new PredictionInput(features);
    PredictionFeatureDomain domains = new PredictionFeatureDomain(featureDomains);
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
        CounterfactualEntityFactory.createEntities(input);
    });
    assertEquals("Null numeric features are not supported in counterfactuals", exception.getMessage());
}
Also used : PredictionFeatureDomain(org.kie.kogito.explainability.model.PredictionFeatureDomain) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) ArrayList(java.util.ArrayList) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) PredictionFeatureDomain(org.kie.kogito.explainability.model.PredictionFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) Feature(org.kie.kogito.explainability.model.Feature) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with FeatureDomain

use of org.kie.kogito.explainability.model.domain.FeatureDomain in project kogito-apps by kiegroup.

the class CounterfactualScoreCalculatorTest method testGoalSizeSmaller.

/**
 * Using a smaller number of features in the goals (1) than the model's output (2) should
 * throw an {@link IllegalArgumentException} with the appropriate message.
 */
@Test
void testGoalSizeSmaller() throws ExecutionException, InterruptedException {
    final CounterFactualScoreCalculator scoreCalculator = new CounterFactualScoreCalculator();
    PredictionProvider model = TestUtils.getFeatureSkipModel(0);
    List<Feature> features = new ArrayList<>();
    List<FeatureDomain> featureDomains = new ArrayList<>();
    List<Boolean> constraints = new ArrayList<>();
    // f-1
    features.add(FeatureFactory.newNumericalFeature("f-1", 1.0));
    featureDomains.add(NumericalFeatureDomain.create(0.0, 10.0));
    constraints.add(false);
    // f-2
    features.add(FeatureFactory.newNumericalFeature("f-2", 2.0));
    featureDomains.add(NumericalFeatureDomain.create(0.0, 10.0));
    constraints.add(false);
    // f-3
    features.add(FeatureFactory.newBooleanFeature("f-3", true));
    featureDomains.add(EmptyFeatureDomain.create());
    constraints.add(false);
    PredictionInput input = new PredictionInput(features);
    PredictionFeatureDomain domains = new PredictionFeatureDomain(featureDomains);
    List<CounterfactualEntity> entities = CounterfactualEntityFactory.createEntities(input);
    List<Output> goal = new ArrayList<>();
    goal.add(new Output("f-2", Type.NUMBER, new Value(2.0), 0.0));
    List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(input)).get();
    assertEquals(1, goal.size());
    // A single prediction is expected
    assertEquals(1, predictionOutputs.size());
    // Single prediction with two features
    assertEquals(2, predictionOutputs.get(0).getOutputs().size());
    final CounterfactualSolution solution = new CounterfactualSolution(entities, features, model, goal, UUID.randomUUID(), UUID.randomUUID(), 0.0);
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
        scoreCalculator.calculateScore(solution);
    });
    assertEquals("Prediction size must be equal to goal size", exception.getMessage());
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) ArrayList(java.util.ArrayList) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) PredictionFeatureDomain(org.kie.kogito.explainability.model.PredictionFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) PredictionFeatureDomain(org.kie.kogito.explainability.model.PredictionFeatureDomain) 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) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with FeatureDomain

use of org.kie.kogito.explainability.model.domain.FeatureDomain in project kogito-apps by kiegroup.

the class ConversionUtilsTest method testToFeatureDomain_UnitFixedNumber.

@Test
void testToFeatureDomain_UnitFixedNumber() {
    FeatureDomain featureDomain = ConversionUtils.toFeatureDomain(new CounterfactualSearchDomainUnitValue("integer", "integer", true, null));
    assertTrue(featureDomain instanceof EmptyFeatureDomain);
}
Also used : EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) 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) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) Test(org.junit.jupiter.api.Test)

Example 10 with FeatureDomain

use of org.kie.kogito.explainability.model.domain.FeatureDomain in project kogito-apps by kiegroup.

the class ConversionUtilsTest method testToFeatureDomain_UnitRangeDouble.

@Test
void testToFeatureDomain_UnitRangeDouble() {
    FeatureDomain featureDomain = ConversionUtils.toFeatureDomain(new CounterfactualSearchDomainUnitValue("double", "double", true, new CounterfactualDomainRange(DoubleNode.valueOf(-273.15), DoubleNode.valueOf(Double.MAX_VALUE))));
    assertTrue(featureDomain instanceof NumericalFeatureDomain);
    NumericalFeatureDomain numericalFeatureDomain = (NumericalFeatureDomain) featureDomain;
    assertEquals(-273.15, numericalFeatureDomain.getLowerBound());
    assertEquals(Double.MAX_VALUE, numericalFeatureDomain.getUpperBound());
    assertNull(numericalFeatureDomain.getCategories());
}
Also used : CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) 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) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) Test(org.junit.jupiter.api.Test)

Aggregations

FeatureDomain (org.kie.kogito.explainability.model.domain.FeatureDomain)39 NumericalFeatureDomain (org.kie.kogito.explainability.model.domain.NumericalFeatureDomain)38 Test (org.junit.jupiter.api.Test)32 EmptyFeatureDomain (org.kie.kogito.explainability.model.domain.EmptyFeatureDomain)31 Feature (org.kie.kogito.explainability.model.Feature)29 CategoricalFeatureDomain (org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain)25 CounterfactualEntity (org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 BinaryFeatureDomain (org.kie.kogito.explainability.model.domain.BinaryFeatureDomain)13 CurrencyFeatureDomain (org.kie.kogito.explainability.model.domain.CurrencyFeatureDomain)13 DurationFeatureDomain (org.kie.kogito.explainability.model.domain.DurationFeatureDomain)13 ObjectFeatureDomain (org.kie.kogito.explainability.model.domain.ObjectFeatureDomain)13 URIFeatureDomain (org.kie.kogito.explainability.model.domain.URIFeatureDomain)13 TimeFeatureDomain (org.kie.kogito.explainability.model.domain.TimeFeatureDomain)12 ArrayList (java.util.ArrayList)10 Output (org.kie.kogito.explainability.model.Output)8 Random (java.util.Random)7 PredictionFeatureDomain (org.kie.kogito.explainability.model.PredictionFeatureDomain)7 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)7 Value (org.kie.kogito.explainability.model.Value)7