use of org.kie.kogito.explainability.model.domain.FeatureDomain in project kogito-apps by kiegroup.
the class CounterfactualEntityFactoryTest method testCurrencyFactory.
@Test
void testCurrencyFactory() {
final Currency value = Currency.getInstance(Locale.ITALY);
Feature feature = FeatureFactory.newCurrencyFeature("currrency-feature", value);
CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof FixedCurrencyEntity);
assertEquals(Type.CURRENCY, counterfactualEntity.asFeature().getType());
final Feature fixedFeature = FeatureFactory.newCurrencyFeature("currrency-feature", value);
FeatureDomain domain = CurrencyFeatureDomain.create(Currency.getAvailableCurrencies());
feature = FeatureFactory.newCurrencyFeature("currrency-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof CurrencyEntity);
assertEquals(domain.getCategories(), ((CurrencyEntity) counterfactualEntity).getValueRange());
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
domain = CurrencyFeatureDomain.create(new ArrayList<>(Currency.getAvailableCurrencies()));
feature = FeatureFactory.newCurrencyFeature("currrency-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof CurrencyEntity);
assertEquals(domain.getCategories(), ((CurrencyEntity) counterfactualEntity).getValueRange());
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
Currency[] currencies = List.of(Locale.ITALY, Locale.UK, Locale.US).stream().map(Currency::getInstance).collect(Collectors.toList()).toArray(new Currency[0]);
domain = CurrencyFeatureDomain.create(currencies);
feature = FeatureFactory.newCurrencyFeature("currrency-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof CurrencyEntity);
assertEquals(currencies.length, ((CurrencyEntity) counterfactualEntity).getValueRange().size());
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
}
use of org.kie.kogito.explainability.model.domain.FeatureDomain in project kogito-apps by kiegroup.
the class CounterfactualEntityFactoryTest method testObjectFactory.
@Test
void testObjectFactory() {
final URI value = URI.create("./");
Feature feature = FeatureFactory.newObjectFeature("f", value);
CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof FixedObjectEntity);
assertEquals(Type.UNDEFINED, counterfactualEntity.asFeature().getType());
FeatureDomain domain = ObjectFeatureDomain.create("test", 45L);
feature = FeatureFactory.newObjectFeature("uri-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof ObjectEntity);
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
domain = ObjectFeatureDomain.create(List.of("test", 45L));
feature = FeatureFactory.newObjectFeature("uri-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof ObjectEntity);
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
domain = ObjectFeatureDomain.create(Set.of("test", 45L));
feature = FeatureFactory.newObjectFeature("uri-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof ObjectEntity);
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
}
use of org.kie.kogito.explainability.model.domain.FeatureDomain in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testNoCounterfactualPossible.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testNoCounterfactualPossible(long seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
final PerturbationContext perturbationContext = new PerturbationContext(seed, random, 4);
final List<Output> goal = List.of(new Output("inside", Type.BOOLEAN, new Value(true), 0.0));
List<Feature> features = new LinkedList<>();
List<FeatureDomain> featureBoundaries = new LinkedList<>();
List<Boolean> constraints = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("f-num1", 1.0));
constraints.add(true);
featureBoundaries.add(EmptyFeatureDomain.create());
features.add(FeatureFactory.newNumericalFeature("f-num2", 1.0));
constraints.add(false);
featureBoundaries.add(NumericalFeatureDomain.create(0.0, 2.0));
features.add(FeatureFactory.newNumericalFeature("f-num3", 1.0));
constraints.add(false);
featureBoundaries.add(NumericalFeatureDomain.create(0.0, 2.0));
features.add(FeatureFactory.newNumericalFeature("f-num4", 1.0));
constraints.add(true);
featureBoundaries.add(EmptyFeatureDomain.create());
final DataDomain dataDomain = new DataDomain(featureBoundaries);
final double center = 500.0;
final double epsilon = 1.0;
List<Feature> perturbedFeatures = DataUtils.perturbFeatures(features, perturbationContext);
final CounterfactualResult result = runCounterfactualSearch((long) seed, goal, perturbedFeatures, TestUtils.getSumThresholdModel(center, epsilon), DEFAULT_GOAL_THRESHOLD);
assertFalse(result.isValid());
}
use of org.kie.kogito.explainability.model.domain.FeatureDomain in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method testNullBooleanInput.
/**
* Null values for input Boolean features should be accepted as valid
*/
@Test
void testNullBooleanInput() 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.newBooleanFeature("f-2", null));
featureDomains.add(EmptyFeatureDomain.create());
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.BOOLEAN, new Value(null), 0.0));
goal.add(new Output("f-3", Type.BOOLEAN, new Value(true), 0.0));
final CounterfactualSolution solution = new CounterfactualSolution(entities, features, model, goal, UUID.randomUUID(), UUID.randomUUID(), 0.0);
BendableBigDecimalScore score = scoreCalculator.calculateScore(solution);
List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(input)).get();
assertTrue(score.isFeasible());
assertEquals(2, goal.size());
// A single prediction is expected
assertEquals(1, predictionOutputs.size());
// Single prediction with two features
assertEquals(2, predictionOutputs.get(0).getOutputs().size());
assertEquals(0, score.getHardScore(0).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getHardScore(1).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getHardScore(2).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getSoftScore(0).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getSoftScore(1).compareTo(BigDecimal.ZERO));
assertEquals(3, score.getHardLevelsSize());
assertEquals(2, score.getSoftLevelsSize());
}
use of org.kie.kogito.explainability.model.domain.FeatureDomain in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method testGoalSizeLarger.
/**
* Using a larger number of features in the goals (3) than the model's output (2) should
* throw an {@link IllegalArgumentException} with the appropriate message.
*/
@Test
void testGoalSizeLarger() 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-1", Type.NUMBER, new Value(1.0), 0.0));
goal.add(new Output("f-2", Type.NUMBER, new Value(2.0), 0.0));
goal.add(new Output("f-3", Type.BOOLEAN, new Value(true), 0.0));
List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(input)).get();
assertEquals(3, 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());
}
Aggregations