Search in sources :

Example 11 with Feature

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

the class CounterfactualEntityFactoryTest method testCategoricalFactorySet.

@Test
void testCategoricalFactorySet() {
    final String value = "foo";
    final FeatureDomain domain = CategoricalFeatureDomain.create(Set.of("foo", "bar"));
    final Feature feature = FeatureFactory.newCategoricalFeature("categorical-feature", value, domain);
    final CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertTrue(counterfactualEntity instanceof CategoricalEntity);
    assertEquals(domain.getCategories(), ((CategoricalEntity) counterfactualEntity).getValueRange());
    assertEquals(value, counterfactualEntity.asFeature().getValue().toString());
}
Also used : CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) 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) CategoricalEntity(org.kie.kogito.explainability.local.counterfactual.entities.CategoricalEntity) FixedCategoricalEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedCategoricalEntity) Feature(org.kie.kogito.explainability.model.Feature) Test(org.junit.jupiter.api.Test)

Example 12 with Feature

use of org.kie.kogito.explainability.model.Feature 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());
}
Also used : CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) BinaryEntity(org.kie.kogito.explainability.local.counterfactual.entities.BinaryEntity) FixedBinaryEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedBinaryEntity) FixedBinaryEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedBinaryEntity) 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) ByteBuffer(java.nio.ByteBuffer) Feature(org.kie.kogito.explainability.model.Feature) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 13 with Feature

use of org.kie.kogito.explainability.model.Feature 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());
}
Also used : CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) FixedTimeEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedTimeEntity) TimeEntity(org.kie.kogito.explainability.local.counterfactual.entities.TimeEntity) LocalTime(java.time.LocalTime) FixedTimeEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedTimeEntity) 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 14 with Feature

use of org.kie.kogito.explainability.model.Feature 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());
}
Also used : CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) FeatureDistribution(org.kie.kogito.explainability.model.FeatureDistribution) NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) FixedDurationEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedDurationEntity) Random(java.util.Random) 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) Duration(java.time.Duration) Feature(org.kie.kogito.explainability.model.Feature) FixedDurationEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedDurationEntity) DurationEntity(org.kie.kogito.explainability.local.counterfactual.entities.DurationEntity) NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) Test(org.junit.jupiter.api.Test)

Example 15 with Feature

use of org.kie.kogito.explainability.model.Feature 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)

Aggregations

Feature (org.kie.kogito.explainability.model.Feature)233 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)118 Test (org.junit.jupiter.api.Test)107 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)107 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)104 Output (org.kie.kogito.explainability.model.Output)102 ArrayList (java.util.ArrayList)97 Random (java.util.Random)92 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)78 Value (org.kie.kogito.explainability.model.Value)74 LinkedList (java.util.LinkedList)72 ValueSource (org.junit.jupiter.params.provider.ValueSource)71 Prediction (org.kie.kogito.explainability.model.Prediction)67 List (java.util.List)51 CounterfactualEntity (org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity)46 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)42 Type (org.kie.kogito.explainability.model.Type)39 NumericalFeatureDomain (org.kie.kogito.explainability.model.domain.NumericalFeatureDomain)37 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)35 FeatureDomain (org.kie.kogito.explainability.model.domain.FeatureDomain)33