use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method objectDistanceSameValue.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void objectDistanceSameValue(int seed) {
final Random random = new Random(seed);
final ByteBuffer value = ByteBuffer.wrap("foo".getBytes());
Feature x = FeatureFactory.newObjectFeature("x", value);
Feature y = FeatureFactory.newObjectFeature("y", value);
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
assertEquals(Type.UNDEFINED, ox.getType());
assertEquals(0.0, distance);
// Use a random threshold, mustn't make a difference
distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
assertEquals(0.0, distance);
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method durationDistanceDifferentValue.
@Test
void durationDistanceDifferentValue() {
final double SECONDS = 120L;
Feature x = FeatureFactory.newDurationFeature("x", Duration.ZERO);
Feature y = FeatureFactory.newDurationFeature("y", Duration.ofSeconds((long) SECONDS));
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
assertEquals(Type.DURATION, ox.getType());
assertEquals(Type.DURATION, oy.getType());
assertEquals(SECONDS, distance);
x = FeatureFactory.newDurationFeature("x", Duration.ofSeconds((long) SECONDS));
y = FeatureFactory.newDurationFeature("y", Duration.ofDays(1L));
ox = outputFromFeature(x);
oy = outputFromFeature(y);
distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
assertEquals(0.9986, distance, 0.01);
x = FeatureFactory.newDurationFeature("x", Duration.ofDays(2L));
y = FeatureFactory.newDurationFeature("y", Duration.ofDays(1L));
ox = outputFromFeature(x);
oy = outputFromFeature(y);
distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
System.out.println(distance);
assertEquals(0.5, distance, 1e-4);
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method CategoricalDistanceSameValue.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void CategoricalDistanceSameValue(int seed) {
final Random random = new Random(seed);
final String value = UUID.randomUUID().toString();
Feature x = FeatureFactory.newCategoricalFeature("x", value);
Feature y = FeatureFactory.newCategoricalFeature("y", value);
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
assertEquals(Type.CATEGORICAL, ox.getType());
assertEquals(0.0, distance);
// Use a random threshold, mustn't make a difference
distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
assertEquals(0.0, distance);
}
use of org.kie.kogito.explainability.model.Output 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());
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method BooleanDistanceDifferentValueThreshold.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void BooleanDistanceDifferentValueThreshold(int seed) {
final Random random = new Random(seed);
boolean value = random.nextBoolean();
Feature x = FeatureFactory.newBooleanFeature("x", value);
Feature y = FeatureFactory.newBooleanFeature("y", !value);
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
double distance = CounterFactualScoreCalculator.outputDistance(ox, oy, 0.25);
assertEquals(Type.BOOLEAN, ox.getType());
assertEquals(Type.BOOLEAN, oy.getType());
assertEquals(1.0, distance);
}
Aggregations