Search in sources :

Example 1 with IndependentFeaturesDataDistribution

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

the class HighScoreNumericFeatureZonesProviderTest method testEmptyData.

@Test
void testEmptyData() {
    List<Feature> features = new ArrayList<>();
    PredictionProvider predictionProvider = TestUtils.getSumThresholdModel(0.1, 0.1);
    List<FeatureDistribution> featureDistributions = new ArrayList<>();
    DataDistribution dataDistribution = new IndependentFeaturesDataDistribution(featureDistributions);
    Map<String, HighScoreNumericFeatureZones> highScoreFeatureZones = HighScoreNumericFeatureZonesProvider.getHighScoreFeatureZones(dataDistribution, predictionProvider, features, 10);
    assertThat(highScoreFeatureZones).isNotNull();
    assertThat(highScoreFeatureZones.size()).isZero();
}
Also used : GenericFeatureDistribution(org.kie.kogito.explainability.model.GenericFeatureDistribution) FeatureDistribution(org.kie.kogito.explainability.model.FeatureDistribution) IndependentFeaturesDataDistribution(org.kie.kogito.explainability.model.IndependentFeaturesDataDistribution) DataDistribution(org.kie.kogito.explainability.model.DataDistribution) ArrayList(java.util.ArrayList) IndependentFeaturesDataDistribution(org.kie.kogito.explainability.model.IndependentFeaturesDataDistribution) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) Test(org.junit.jupiter.api.Test)

Example 2 with IndependentFeaturesDataDistribution

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

the class LimeExplainerTest method testWithDataDistribution.

@Test
void testWithDataDistribution() throws InterruptedException, ExecutionException, TimeoutException {
    Random random = new Random();
    PerturbationContext perturbationContext = new PerturbationContext(4L, random, 1);
    List<FeatureDistribution> featureDistributions = new ArrayList<>();
    int nf = 4;
    List<Feature> features = new ArrayList<>();
    for (int i = 0; i < nf; i++) {
        Feature numericalFeature = FeatureFactory.newNumericalFeature("f-" + i, Double.NaN);
        features.add(numericalFeature);
        List<Value> values = new ArrayList<>();
        for (int r = 0; r < 4; r++) {
            values.add(Type.NUMBER.randomValue(perturbationContext));
        }
        featureDistributions.add(new GenericFeatureDistribution(numericalFeature, values));
    }
    DataDistribution dataDistribution = new IndependentFeaturesDataDistribution(featureDistributions);
    LimeConfig limeConfig = new LimeConfig().withDataDistribution(dataDistribution).withPerturbationContext(perturbationContext).withSamples(10);
    LimeExplainer limeExplainer = new LimeExplainer(limeConfig);
    PredictionInput input = new PredictionInput(features);
    PredictionProvider model = TestUtils.getSumThresholdModel(random.nextDouble(), random.nextDouble());
    PredictionOutput output = model.predictAsync(List.of(input)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit()).get(0);
    Prediction prediction = new SimplePrediction(input, output);
    Map<String, Saliency> saliencyMap = limeExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    assertThat(saliencyMap).isNotNull();
    String decisionName = "inside";
    Saliency saliency = saliencyMap.get(decisionName);
    assertThat(saliency).isNotNull();
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) Prediction(org.kie.kogito.explainability.model.Prediction) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) ArrayList(java.util.ArrayList) Saliency(org.kie.kogito.explainability.model.Saliency) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) GenericFeatureDistribution(org.kie.kogito.explainability.model.GenericFeatureDistribution) FeatureDistribution(org.kie.kogito.explainability.model.FeatureDistribution) Random(java.util.Random) DataDistribution(org.kie.kogito.explainability.model.DataDistribution) IndependentFeaturesDataDistribution(org.kie.kogito.explainability.model.IndependentFeaturesDataDistribution) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Value(org.kie.kogito.explainability.model.Value) IndependentFeaturesDataDistribution(org.kie.kogito.explainability.model.IndependentFeaturesDataDistribution) GenericFeatureDistribution(org.kie.kogito.explainability.model.GenericFeatureDistribution) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with IndependentFeaturesDataDistribution

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

the class HighScoreNumericFeatureZonesProviderTest method testNonEmptyData.

@Test
void testNonEmptyData() {
    Random random = new Random();
    random.setSeed(0);
    PerturbationContext perturbationContext = new PerturbationContext(random, 1);
    List<Feature> features = new ArrayList<>();
    PredictionProvider predictionProvider = TestUtils.getSumThresholdModel(0.1, 0.1);
    List<FeatureDistribution> featureDistributions = new ArrayList<>();
    int nf = 4;
    for (int i = 0; i < nf; i++) {
        Feature numericalFeature = FeatureFactory.newNumericalFeature("f-" + i, Double.NaN);
        features.add(numericalFeature);
        List<Value> values = new ArrayList<>();
        for (int r = 0; r < 4; r++) {
            values.add(Type.NUMBER.randomValue(perturbationContext));
        }
        featureDistributions.add(new GenericFeatureDistribution(numericalFeature, values));
    }
    DataDistribution dataDistribution = new IndependentFeaturesDataDistribution(featureDistributions);
    Map<String, HighScoreNumericFeatureZones> highScoreFeatureZones = HighScoreNumericFeatureZonesProvider.getHighScoreFeatureZones(dataDistribution, predictionProvider, features, 10);
    assertThat(highScoreFeatureZones).isNotNull();
    assertThat(highScoreFeatureZones.size()).isEqualTo(4);
}
Also used : PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) ArrayList(java.util.ArrayList) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) GenericFeatureDistribution(org.kie.kogito.explainability.model.GenericFeatureDistribution) FeatureDistribution(org.kie.kogito.explainability.model.FeatureDistribution) Random(java.util.Random) IndependentFeaturesDataDistribution(org.kie.kogito.explainability.model.IndependentFeaturesDataDistribution) DataDistribution(org.kie.kogito.explainability.model.DataDistribution) Value(org.kie.kogito.explainability.model.Value) IndependentFeaturesDataDistribution(org.kie.kogito.explainability.model.IndependentFeaturesDataDistribution) GenericFeatureDistribution(org.kie.kogito.explainability.model.GenericFeatureDistribution) Test(org.junit.jupiter.api.Test)

Example 4 with IndependentFeaturesDataDistribution

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

the class DataUtilsTest method testBootstrap.

@Test
void testBootstrap() {
    List<Value> values = new ArrayList<>();
    PerturbationContext perturbationContext = new PerturbationContext(random, 1);
    for (int i = 0; i < 4; i++) {
        values.add(Type.NUMBER.randomValue(perturbationContext));
    }
    Feature mockedNumericFeature = TestUtils.getMockedNumericFeature();
    DataDistribution dataDistribution = new IndependentFeaturesDataDistribution(List.of(new GenericFeatureDistribution(mockedNumericFeature, values)));
    Map<String, FeatureDistribution> featureDistributionMap = DataUtils.boostrapFeatureDistributions(dataDistribution, perturbationContext, 10, 1, 500, new HashMap<>());
    assertThat(featureDistributionMap).isNotNull();
    assertThat(featureDistributionMap).isNotEmpty();
    FeatureDistribution actual = featureDistributionMap.get(mockedNumericFeature.getName());
    assertThat(actual).isNotNull();
    List<Value> allSamples = actual.getAllSamples();
    assertThat(allSamples).isNotNull();
    assertThat(allSamples).hasSize(10);
}
Also used : PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) ArrayList(java.util.ArrayList) Feature(org.kie.kogito.explainability.model.Feature) GenericFeatureDistribution(org.kie.kogito.explainability.model.GenericFeatureDistribution) FeatureDistribution(org.kie.kogito.explainability.model.FeatureDistribution) DataDistribution(org.kie.kogito.explainability.model.DataDistribution) IndependentFeaturesDataDistribution(org.kie.kogito.explainability.model.IndependentFeaturesDataDistribution) Value(org.kie.kogito.explainability.model.Value) IndependentFeaturesDataDistribution(org.kie.kogito.explainability.model.IndependentFeaturesDataDistribution) GenericFeatureDistribution(org.kie.kogito.explainability.model.GenericFeatureDistribution) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with IndependentFeaturesDataDistribution

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

the class DataUtils method generateRandomDataDistribution.

/**
 * Generate a random data distribution.
 *
 * @param noOfFeatures number of features
 * @param distributionSize number of samples for each feature
 * @return a data distribution
 */
public static DataDistribution generateRandomDataDistribution(int noOfFeatures, int distributionSize, Random random) {
    List<FeatureDistribution> featureDistributions = new LinkedList<>();
    for (int i = 0; i < noOfFeatures; i++) {
        double[] doubles = generateData(random.nextDouble(), random.nextDouble(), distributionSize, random);
        Feature feature = FeatureFactory.newNumericalFeature("f_" + i, Double.NaN);
        FeatureDistribution featureDistribution = new NumericFeatureDistribution(feature, doubles);
        featureDistributions.add(featureDistribution);
    }
    return new IndependentFeaturesDataDistribution(featureDistributions);
}
Also used : NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) FeatureDistribution(org.kie.kogito.explainability.model.FeatureDistribution) IndependentFeaturesDataDistribution(org.kie.kogito.explainability.model.IndependentFeaturesDataDistribution) Feature(org.kie.kogito.explainability.model.Feature) NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) LinkedList(java.util.LinkedList)

Aggregations

Feature (org.kie.kogito.explainability.model.Feature)5 FeatureDistribution (org.kie.kogito.explainability.model.FeatureDistribution)5 IndependentFeaturesDataDistribution (org.kie.kogito.explainability.model.IndependentFeaturesDataDistribution)5 ArrayList (java.util.ArrayList)4 Test (org.junit.jupiter.api.Test)4 DataDistribution (org.kie.kogito.explainability.model.DataDistribution)4 GenericFeatureDistribution (org.kie.kogito.explainability.model.GenericFeatureDistribution)4 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)3 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)3 Value (org.kie.kogito.explainability.model.Value)3 Random (java.util.Random)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 LinkedList (java.util.LinkedList)1 NumericFeatureDistribution (org.kie.kogito.explainability.model.NumericFeatureDistribution)1 Prediction (org.kie.kogito.explainability.model.Prediction)1 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)1 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)1 Saliency (org.kie.kogito.explainability.model.Saliency)1 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)1