use of org.kie.kogito.explainability.model.PerturbationContext 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);
}
use of org.kie.kogito.explainability.model.PerturbationContext 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);
}
use of org.kie.kogito.explainability.model.PerturbationContext in project kogito-apps by kiegroup.
the class DataUtilsTest method assertPerturbDropString.
private void assertPerturbDropString(PredictionInput input, int noOfPerturbations) {
List<Feature> newFeatures = DataUtils.perturbFeatures(input.getFeatures(), new PerturbationContext(random, noOfPerturbations));
int changedFeatures = 0;
for (int i = 0; i < input.getFeatures().size(); i++) {
String v = input.getFeatures().get(i).getValue().asString();
String pv = newFeatures.get(i).getValue().asString();
if (!v.equals(pv)) {
changedFeatures++;
}
}
assertThat(changedFeatures).isBetween((int) Math.min(noOfPerturbations, input.getFeatures().size() * 0.5), (int) Math.max(noOfPerturbations, input.getFeatures().size() * 0.5));
}
use of org.kie.kogito.explainability.model.PerturbationContext 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());
}
use of org.kie.kogito.explainability.model.PerturbationContext in project kogito-apps by kiegroup.
the class LimeExplainerTest method testDeterministic.
@ParameterizedTest
@ValueSource(longs = { 0, 1, 2, 3, 4 })
void testDeterministic(long seed) throws ExecutionException, InterruptedException, TimeoutException {
List<Saliency> saliencies = new ArrayList<>();
for (int j = 0; j < 2; j++) {
Random random = new Random();
LimeConfig limeConfig = new LimeConfig().withPerturbationContext(new PerturbationContext(seed, random, DEFAULT_NO_OF_PERTURBATIONS)).withSamples(10);
LimeExplainer limeExplainer = new LimeExplainer(limeConfig);
List<Feature> features = new ArrayList<>();
for (int i = 0; i < 4; i++) {
features.add(TestUtils.getMockedNumericFeature(i));
}
PredictionInput input = new PredictionInput(features);
PredictionProvider model = TestUtils.getSumSkipModel(0);
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());
saliencies.add(saliencyMap.get("sum-but0"));
}
assertThat(saliencies.get(0).getPerFeatureImportance().stream().map(FeatureImportance::getScore).collect(Collectors.toList())).isEqualTo(saliencies.get(1).getPerFeatureImportance().stream().map(FeatureImportance::getScore).collect(Collectors.toList()));
}
Aggregations