use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class OpenNLPPDPExplainerTest method testOpenNLPLangDetect.
@Test
void testOpenNLPLangDetect() throws Exception {
PartialDependencePlotExplainer partialDependencePlotExplainer = new PartialDependencePlotExplainer();
InputStream is = getClass().getResourceAsStream("/opennlp/langdetect-183.bin");
LanguageDetectorModel languageDetectorModel = new LanguageDetectorModel(is);
LanguageDetector languageDetector = new LanguageDetectorME(languageDetectorModel);
PredictionProvider model = inputs -> CompletableFuture.supplyAsync(() -> {
List<PredictionOutput> results = new ArrayList<>();
for (PredictionInput predictionInput : inputs) {
StringBuilder builder = new StringBuilder();
for (Feature f : predictionInput.getFeatures()) {
if (builder.length() > 0) {
builder.append(' ');
}
builder.append(f.getValue().asString());
}
Language language = languageDetector.predictLanguage(builder.toString());
PredictionOutput predictionOutput = new PredictionOutput(List.of(new Output("lang", Type.TEXT, new Value(language.getLang()), language.getConfidence())));
results.add(predictionOutput);
}
return results;
});
List<String> texts = List.of("we want your money", "please reply quickly", "you are the lucky winner", "italiani, spaghetti pizza mandolino", "guten tag", "allez les bleus", "daje roma");
List<Prediction> predictions = new ArrayList<>();
for (String text : texts) {
List<Feature> features = new ArrayList<>();
features.add(FeatureFactory.newFulltextFeature("text", text));
PredictionInput predictionInput = new PredictionInput(features);
PredictionOutput predictionOutput = model.predictAsync(List.of(predictionInput)).get().get(0);
predictions.add(new SimplePrediction(predictionInput, predictionOutput));
}
List<PartialDependenceGraph> pdps = partialDependencePlotExplainer.explainFromPredictions(model, predictions);
assertThat(pdps).isNotEmpty();
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class PmmlCompoundScorecardLimeExplainerTest method getModel.
private PredictionProvider getModel() {
return inputs -> CompletableFuture.supplyAsync(() -> {
List<PredictionOutput> outputs = new ArrayList<>(inputs.size());
for (PredictionInput predictionInput : inputs) {
List<Feature> inputFeatures = predictionInput.getFeatures();
CompoundNestedPredicateScorecardExecutor pmmlModel = new CompoundNestedPredicateScorecardExecutor(inputFeatures.get(0).getValue().asNumber(), inputFeatures.get(1).getValue().asString());
PMML4Result result = pmmlModel.execute(compoundScoreCardRuntime);
Map<String, Object> resultVariables = result.getResultVariables();
String score = "" + resultVariables.get(CompoundNestedPredicateScorecardExecutor.TARGET_FIELD);
String reason1 = "" + resultVariables.get(CompoundNestedPredicateScorecardExecutor.REASON_CODE1_FIELD);
PredictionOutput predictionOutput = new PredictionOutput(List.of(new Output("score", Type.TEXT, new Value(score), 1d), new Output("reason1", Type.TEXT, new Value(reason1), 1d)));
outputs.add(predictionOutput);
}
return outputs;
});
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method TextDistanceSameValue.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void TextDistanceSameValue(int seed) {
final String value = UUID.randomUUID().toString();
Feature x = FeatureFactory.newTextFeature("x", value);
Feature y = FeatureFactory.newTextFeature("y", value);
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
final double distance = CounterFactualScoreCalculator.outputDistance(ox, oy);
assertEquals(Type.TEXT, ox.getType());
assertEquals(0.0, distance);
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method testGoalSizeMatch.
/**
* If the goal and the model's output is the same, the distances should all be zero.
*/
@Test
void testGoalSizeMatch() 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));
goal.add(new Output("f-3", Type.BOOLEAN, new Value(true), 0.0));
final CounterfactualSolution solution = new CounterfactualSolution(entities, features, model, goal, UUID.randomUUID(), UUID.randomUUID(), 0.0);
BendableBigDecimalScore score = scoreCalculator.calculateScore(solution);
List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(input)).get();
assertTrue(score.isFeasible());
assertEquals(2, goal.size());
// A single prediction is expected
assertEquals(1, predictionOutputs.size());
// Single prediction with two features
assertEquals(2, predictionOutputs.get(0).getOutputs().size());
assertEquals(0, score.getHardScore(0).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getHardScore(1).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getHardScore(2).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getSoftScore(0).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getSoftScore(1).compareTo(BigDecimal.ZERO));
assertEquals(3, score.getHardLevelsSize());
assertEquals(2, score.getSoftLevelsSize());
}
use of org.kie.kogito.explainability.model.Output in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method DoubleDistanceSameValue.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void DoubleDistanceSameValue(int seed) {
final Random random = new Random(seed);
final double value = random.nextDouble();
Feature x = FeatureFactory.newNumericalFeature("x", value);
Feature y = FeatureFactory.newNumericalFeature("y", value);
Output ox = outputFromFeature(x);
Output oy = outputFromFeature(y);
// Use a random threshold, mustn't make a difference
final double distance = CounterFactualScoreCalculator.outputDistance(ox, oy, random.nextDouble());
assertEquals(Type.NUMBER, ox.getType());
assertEquals(0.0, distance);
}
Aggregations