use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.
the class CounterfactualEntityFactoryTest method testBinaryFactory.
@Test
void testBinaryFactory() {
final ByteBuffer value = ByteBuffer.allocate(256);
Feature feature = FeatureFactory.newBinaryFeature("binary-feature", value);
CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof FixedBinaryEntity);
assertEquals(Type.BINARY, counterfactualEntity.asFeature().getType());
final List<ByteBuffer> categories = Stream.of("bar".getBytes(), "baz".getBytes(), "fun".getBytes()).map(ByteBuffer::wrap).collect(Collectors.toList());
FeatureDomain domain = BinaryFeatureDomain.create(categories);
feature = FeatureFactory.newBinaryFeature("binary-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof BinaryEntity);
assertEquals(domain.getCategories(), ((BinaryEntity) counterfactualEntity).getValueRange());
domain = BinaryFeatureDomain.create(new HashSet<>(categories));
feature = FeatureFactory.newBinaryFeature("binary-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertEquals(domain.getCategories(), ((BinaryEntity) counterfactualEntity).getValueRange());
domain = BinaryFeatureDomain.create(ByteBuffer.wrap("bar".getBytes()), ByteBuffer.wrap("baz".getBytes()), ByteBuffer.wrap("fun".getBytes()));
feature = FeatureFactory.newBinaryFeature("binary-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertEquals(domain.getCategories(), ((BinaryEntity) counterfactualEntity).getValueRange());
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
}
use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.
the class CounterfactualEntityFactoryTest method testTimeFactory.
@Test
void testTimeFactory() {
final LocalTime value = LocalTime.now();
Feature feature = FeatureFactory.newTimeFeature("time-feature", value);
CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof FixedTimeEntity);
assertEquals(Type.TIME, counterfactualEntity.asFeature().getType());
FeatureDomain domain = TimeFeatureDomain.create(value.minusHours(10), value.plusHours(10));
feature = FeatureFactory.newTimeFeature("time-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof TimeEntity);
assertEquals(Type.TIME, counterfactualEntity.asFeature().getType());
assertEquals(value, ((TimeEntity) counterfactualEntity).getProposedValue());
}
use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.
the class CounterfactualEntityFactoryTest method testDurationFactory.
@Test
void testDurationFactory() {
final Duration value = Duration.ofDays(1);
Feature feature = FeatureFactory.newDurationFeature("duration-feature", value);
CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof FixedDurationEntity);
assertEquals(Type.DURATION, counterfactualEntity.asFeature().getType());
FeatureDomain domain = DurationFeatureDomain.create(0, 60, ChronoUnit.SECONDS);
feature = FeatureFactory.newDurationFeature("duration-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof DurationEntity);
assertEquals(Type.DURATION, counterfactualEntity.asFeature().getType());
assertFalse(counterfactualEntity.isConstrained());
CounterfactualEntity entity = DurationEntity.from(feature, Duration.ZERO, Duration.ofDays(2));
assertEquals(0, entity.distance());
assertTrue(((DurationEntity) entity).getValueRange().contains(1e5));
assertFalse(((DurationEntity) entity).getValueRange().contains(2e5));
assertFalse(entity.isConstrained());
entity = DurationEntity.from(feature, Duration.ZERO, Duration.ofDays(2), false);
assertEquals(0, entity.distance());
assertFalse(entity.isConstrained());
FeatureDistribution distribution = new NumericFeatureDistribution(feature, new Random().doubles(10).toArray());
entity = DurationEntity.from(feature, Duration.ZERO, Duration.ofDays(2), distribution);
assertEquals(0, entity.distance());
assertFalse(entity.isConstrained());
}
use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity 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());
}
use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testFinalUniqueIds.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testFinalUniqueIds(int seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
random.setSeed(seed);
final List<Output> goal = new ArrayList<>();
List<Feature> features = List.of(FeatureFactory.newNumericalFeature("f-num1", 10.0, NumericalFeatureDomain.create(0, 20)));
PredictionProvider model = TestUtils.getFeaturePassModel(0);
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(100_000L);
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed((long) seed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
final List<UUID> intermediateIds = new ArrayList<>();
final List<UUID> executionIds = new ArrayList<>();
final Consumer<CounterfactualResult> captureIntermediateIds = counterfactual -> {
intermediateIds.add(counterfactual.getSolutionId());
};
final Consumer<CounterfactualResult> captureExecutionIds = counterfactual -> {
executionIds.add(counterfactual.getExecutionId());
};
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig);
solverConfig.withEasyScoreCalculatorClass(MockCounterFactualScoreCalculator.class);
final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
PredictionInput input = new PredictionInput(features);
PredictionOutput output = new PredictionOutput(goal);
final UUID executionId = UUID.randomUUID();
Prediction prediction = new CounterfactualPrediction(input, output, null, executionId, null);
final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model, captureIntermediateIds.andThen(captureExecutionIds)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
for (CounterfactualEntity entity : counterfactualResult.getEntities()) {
logger.debug("Entity: {}", entity);
}
// All intermediate ids should be unique
assertEquals((int) intermediateIds.stream().distinct().count(), intermediateIds.size());
// There should be at least one intermediate id
assertTrue(intermediateIds.size() > 0);
// There should be at least one execution id
assertTrue(executionIds.size() > 0);
// We should have the same number of execution ids as intermediate ids (captured from intermediate results)
assertEquals(executionIds.size(), intermediateIds.size());
// All execution ids should be the same
assertEquals(1, (int) executionIds.stream().distinct().count());
// The last intermediate id must be different from the final result id
assertNotEquals(intermediateIds.get(intermediateIds.size() - 1), counterfactualResult.getSolutionId());
// Captured execution ids should be the same as the one provided
assertEquals(executionIds.get(0), executionId);
}
Aggregations