Search in sources :

Example 21 with DataDistribution

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

the class DummyModelsLimeExplainerTest method testMapOneFeatureToOutputRegression.

@ParameterizedTest
@ValueSource(longs = { 0 })
void testMapOneFeatureToOutputRegression(long seed) throws Exception {
    Random random = new Random();
    int idx = 1;
    List<Feature> features = new LinkedList<>();
    features.add(TestUtils.getMockedNumericFeature(100));
    features.add(TestUtils.getMockedNumericFeature(20));
    features.add(TestUtils.getMockedNumericFeature(0.1));
    PredictionInput input = new PredictionInput(features);
    PredictionProvider model = TestUtils.getFeaturePassModel(idx);
    List<PredictionOutput> outputs = model.predictAsync(List.of(input)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    Prediction prediction = new SimplePrediction(input, outputs.get(0));
    LimeConfig limeConfig = new LimeConfig().withSamples(100).withPerturbationContext(new PerturbationContext(seed, random, 1));
    LimeExplainer limeExplainer = new LimeExplainer(limeConfig);
    Map<String, Saliency> saliencyMap = limeExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    for (Saliency saliency : saliencyMap.values()) {
        assertNotNull(saliency);
        List<FeatureImportance> topFeatures = saliency.getTopFeatures(3);
        assertEquals(3, topFeatures.size());
        assertEquals(1d, ExplainabilityMetrics.impactScore(model, prediction, topFeatures));
    }
    int topK = 1;
    double minimumPositiveStabilityRate = 0.5;
    double minimumNegativeStabilityRate = 0.5;
    TestUtils.assertLimeStability(model, prediction, limeExplainer, topK, minimumPositiveStabilityRate, minimumNegativeStabilityRate);
    List<PredictionInput> inputs = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        List<Feature> fs = new LinkedList<>();
        fs.add(TestUtils.getMockedNumericFeature());
        fs.add(TestUtils.getMockedNumericFeature());
        fs.add(TestUtils.getMockedNumericFeature());
        inputs.add(new PredictionInput(fs));
    }
    DataDistribution distribution = new PredictionInputsDataDistribution(inputs);
    int k = 2;
    int chunkSize = 10;
    String decision = "feature-" + idx;
    double precision = ExplainabilityMetrics.getLocalSaliencyPrecision(decision, model, limeExplainer, distribution, k, chunkSize);
    assertThat(precision).isZero();
    double recall = ExplainabilityMetrics.getLocalSaliencyRecall(decision, model, limeExplainer, distribution, k, chunkSize);
    assertThat(recall).isEqualTo(1);
    double f1 = ExplainabilityMetrics.getLocalSaliencyF1(decision, model, limeExplainer, distribution, k, chunkSize);
    assertThat(f1).isZero();
}
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) LinkedList(java.util.LinkedList) Random(java.util.Random) FeatureImportance(org.kie.kogito.explainability.model.FeatureImportance) PredictionInputsDataDistribution(org.kie.kogito.explainability.model.PredictionInputsDataDistribution) DataDistribution(org.kie.kogito.explainability.model.DataDistribution) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictionInputsDataDistribution(org.kie.kogito.explainability.model.PredictionInputsDataDistribution) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with DataDistribution

use of org.kie.kogito.explainability.model.DataDistribution 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 23 with DataDistribution

use of org.kie.kogito.explainability.model.DataDistribution 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 24 with DataDistribution

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

the class DataUtilsTest method testRandomDistributionGeneration.

@Test
void testRandomDistributionGeneration() {
    DataDistribution dataDistribution = DataUtils.generateRandomDataDistribution(10, 10, random);
    assertNotNull(dataDistribution);
    assertNotNull(dataDistribution.asFeatureDistributions());
    for (FeatureDistribution featureDistribution : dataDistribution.asFeatureDistributions()) {
        assertNotNull(featureDistribution);
    }
}
Also used : 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) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 25 with DataDistribution

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

the class LimeConfigOptimizerTest method testSameConfig.

@Test
void testSameConfig() throws ExecutionException, InterruptedException {
    long seed = 0;
    List<LimeConfig> optimizedConfigs = new ArrayList<>();
    PredictionProvider model = TestUtils.getSumSkipModel(1);
    DataDistribution dataDistribution = DataUtils.generateRandomDataDistribution(5, 100, new Random());
    List<PredictionInput> samples = dataDistribution.sample(3);
    List<PredictionOutput> predictionOutputs = model.predictAsync(samples).get();
    List<Prediction> predictions = DataUtils.getPredictions(samples, predictionOutputs);
    for (int i = 0; i < 2; i++) {
        Random random = new Random();
        LimeConfig initialConfig = new LimeConfig().withSamples(10).withPerturbationContext(new PerturbationContext(seed, random, 1));
        LimeConfigOptimizer limeConfigOptimizer = new LimeConfigOptimizer().withDeterministicExecution(true).withStepCountLimit(10).withTimeLimit(10);
        LimeConfig optimizedConfig = limeConfigOptimizer.optimize(initialConfig, predictions, model);
        optimizedConfigs.add(optimizedConfig);
    }
    LimeConfig first = optimizedConfigs.get(0);
    LimeConfig second = optimizedConfigs.get(1);
    assertThat(first.getNoOfRetries()).isEqualTo(second.getNoOfRetries());
    assertThat(first.getNoOfSamples()).isEqualTo(second.getNoOfSamples());
    assertThat(first.getProximityFilteredDatasetMinimum()).isEqualTo(second.getProximityFilteredDatasetMinimum());
    assertThat(first.getProximityKernelWidth()).isEqualTo(second.getProximityKernelWidth());
    assertThat(first.getProximityThreshold()).isEqualTo(second.getProximityThreshold());
    assertThat(first.isProximityFilter()).isEqualTo(second.isProximityFilter());
    assertThat(first.isAdaptDatasetVariance()).isEqualTo(second.isAdaptDatasetVariance());
    assertThat(first.isPenalizeBalanceSparse()).isEqualTo(second.isPenalizeBalanceSparse());
    assertThat(first.getEncodingParams().getNumericTypeClusterGaussianFilterWidth()).isEqualTo(second.getEncodingParams().getNumericTypeClusterGaussianFilterWidth());
    assertThat(first.getEncodingParams().getNumericTypeClusterThreshold()).isEqualTo(second.getEncodingParams().getNumericTypeClusterThreshold());
    assertThat(first.getSeparableDatasetRatio()).isEqualTo(second.getSeparableDatasetRatio());
    assertThat(first.getPerturbationContext().getNoOfPerturbations()).isEqualTo(second.getPerturbationContext().getNoOfPerturbations());
}
Also used : PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) Prediction(org.kie.kogito.explainability.model.Prediction) ArrayList(java.util.ArrayList) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) LimeConfig(org.kie.kogito.explainability.local.lime.LimeConfig) DataDistribution(org.kie.kogito.explainability.model.DataDistribution) Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Test(org.junit.jupiter.api.Test)

Aggregations

DataDistribution (org.kie.kogito.explainability.model.DataDistribution)32 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)27 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)25 Prediction (org.kie.kogito.explainability.model.Prediction)25 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)25 ArrayList (java.util.ArrayList)24 Random (java.util.Random)24 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)24 PredictionInputsDataDistribution (org.kie.kogito.explainability.model.PredictionInputsDataDistribution)21 Saliency (org.kie.kogito.explainability.model.Saliency)20 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)20 Test (org.junit.jupiter.api.Test)19 Feature (org.kie.kogito.explainability.model.Feature)18 LimeConfig (org.kie.kogito.explainability.local.lime.LimeConfig)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 LimeExplainer (org.kie.kogito.explainability.local.lime.LimeExplainer)12 FeatureImportance (org.kie.kogito.explainability.model.FeatureImportance)11 LinkedList (java.util.LinkedList)9 ValueSource (org.junit.jupiter.params.provider.ValueSource)8 FeatureDistribution (org.kie.kogito.explainability.model.FeatureDistribution)8