Search in sources :

Example 41 with Value

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

the class DataUtilsTest method testDropLinearizedFeature.

@Test
void testDropLinearizedFeature() {
    for (Type t : Type.values()) {
        Feature target = TestUtils.getMockedFeature(t, new Value(1d));
        List<Feature> features = new LinkedList<>();
        features.add(TestUtils.getMockedNumericFeature());
        features.add(target);
        features.add(TestUtils.getMockedTextFeature("foo bar"));
        features.add(TestUtils.getMockedNumericFeature());
        Feature source = FeatureFactory.newCompositeFeature("composite", features);
        Feature newFeature = DataUtils.dropOnLinearizedFeatures(target, source);
        assertNotEquals(source, newFeature);
    }
}
Also used : Type(org.kie.kogito.explainability.model.Type) Value(org.kie.kogito.explainability.model.Value) Feature(org.kie.kogito.explainability.model.Feature) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 42 with Value

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

the class PmmlScorecardCategoricalLimeExplainerTest method getModel.

private PredictionProvider getModel() {
    return inputs -> CompletableFuture.supplyAsync(() -> {
        List<PredictionOutput> outputs = new ArrayList<>();
        for (PredictionInput input1 : inputs) {
            List<Feature> features1 = input1.getFeatures();
            SimpleScorecardCategoricalExecutor pmmlModel = new SimpleScorecardCategoricalExecutor(features1.get(0).getValue().asString(), features1.get(1).getValue().asString());
            PMML4Result result = pmmlModel.execute(scorecardCategoricalRuntime);
            String score = "" + result.getResultVariables().get(SimpleScorecardCategoricalExecutor.TARGET_FIELD);
            String reason1 = "" + result.getResultVariables().get(SimpleScorecardCategoricalExecutor.REASON_CODE1_FIELD);
            String reason2 = "" + result.getResultVariables().get(SimpleScorecardCategoricalExecutor.REASON_CODE2_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), new Output("reason2", Type.TEXT, new Value(reason2), 1d)));
            outputs.add(predictionOutput);
        }
        return outputs;
    });
}
Also used : FeatureFactory(org.kie.kogito.explainability.model.FeatureFactory) PredictionInputsDataDistribution(org.kie.kogito.explainability.model.PredictionInputsDataDistribution) PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) PMMLRuntime(org.kie.pmml.api.runtime.PMMLRuntime) Feature(org.kie.kogito.explainability.model.Feature) Prediction(org.kie.kogito.explainability.model.Prediction) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssertionsForClassTypes(org.assertj.core.api.AssertionsForClassTypes) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) PMML4Result(org.kie.api.pmml.PMML4Result) Value(org.kie.kogito.explainability.model.Value) DataDistribution(org.kie.kogito.explainability.model.DataDistribution) Saliency(org.kie.kogito.explainability.model.Saliency) PMMLRuntimeFactoryInternal.getPMMLRuntime(org.kie.pmml.evaluator.assembler.factories.PMMLRuntimeFactoryInternal.getPMMLRuntime) ArrayList(java.util.ArrayList) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) LimeConfig(org.kie.kogito.explainability.local.lime.LimeConfig) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) LimeConfigOptimizer(org.kie.kogito.explainability.local.lime.optim.LimeConfigOptimizer) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) LimeExplainer(org.kie.kogito.explainability.local.lime.LimeExplainer) DataUtils(org.kie.kogito.explainability.utils.DataUtils) Type(org.kie.kogito.explainability.model.Type) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) List(java.util.List) ExplainabilityMetrics(org.kie.kogito.explainability.utils.ExplainabilityMetrics) Output(org.kie.kogito.explainability.model.Output) ValidationUtils(org.kie.kogito.explainability.utils.ValidationUtils) Config(org.kie.kogito.explainability.Config) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) PMML4Result(org.kie.api.pmml.PMML4Result) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) ArrayList(java.util.ArrayList) Value(org.kie.kogito.explainability.model.Value) Feature(org.kie.kogito.explainability.model.Feature)

Example 43 with Value

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

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

the class TestUtils method getFixedOutputClassifier.

public static PredictionProvider getFixedOutputClassifier() {
    return inputs -> supplyAsync(() -> {
        List<PredictionOutput> outputs = new LinkedList<>();
        for (PredictionInput ignored : inputs) {
            Output output = new Output("class", Type.BOOLEAN, new Value(false), 1d);
            outputs.add(new PredictionOutput(List.of(output)));
        }
        return outputs;
    });
}
Also used : Arrays(java.util.Arrays) LimeExplainer(org.kie.kogito.explainability.local.lime.LimeExplainer) Feature(org.kie.kogito.explainability.model.Feature) Prediction(org.kie.kogito.explainability.model.Prediction) Random(java.util.Random) Mockito.when(org.mockito.Mockito.when) Value(org.kie.kogito.explainability.model.Value) Type(org.kie.kogito.explainability.model.Type) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) ArrayList(java.util.ArrayList) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) List(java.util.List) Pair(org.apache.commons.lang3.tuple.Pair) Output(org.kie.kogito.explainability.model.Output) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) Optional(java.util.Optional) ValidationUtils(org.kie.kogito.explainability.utils.ValidationUtils) LinkedList(java.util.LinkedList) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Mockito.mock(org.mockito.Mockito.mock) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Value(org.kie.kogito.explainability.model.Value) LinkedList(java.util.LinkedList)

Example 45 with Value

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

the class TestUtils method getMockedTextFeature.

public static Feature getMockedTextFeature(String s) {
    Feature f = mock(Feature.class);
    when(f.getType()).thenReturn(Type.TEXT);
    when(f.getName()).thenReturn("f-text");
    Value value = mock(Value.class);
    when(value.getUnderlyingObject()).thenReturn(s);
    when(value.asNumber()).thenReturn(Double.NaN);
    when(value.asString()).thenReturn(s);
    when(f.getValue()).thenReturn(value);
    return f;
}
Also used : Value(org.kie.kogito.explainability.model.Value) Feature(org.kie.kogito.explainability.model.Feature)

Aggregations

Value (org.kie.kogito.explainability.model.Value)80 Feature (org.kie.kogito.explainability.model.Feature)69 Output (org.kie.kogito.explainability.model.Output)59 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)54 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)49 ArrayList (java.util.ArrayList)42 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)42 LinkedList (java.util.LinkedList)36 Type (org.kie.kogito.explainability.model.Type)36 Test (org.junit.jupiter.api.Test)35 List (java.util.List)33 Prediction (org.kie.kogito.explainability.model.Prediction)33 Random (java.util.Random)31 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)23 Arrays (java.util.Arrays)16 Map (java.util.Map)16 Optional (java.util.Optional)16 CounterfactualEntity (org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity)16 FeatureFactory (org.kie.kogito.explainability.model.FeatureFactory)16 Collectors (java.util.stream.Collectors)15