Search in sources :

Example 1 with HasNameValue

use of org.kie.kogito.explainability.api.HasNameValue in project kogito-apps by kiegroup.

the class ConversionUtils method toFeatureList.

// //////////////////////////
// TO EXPLAINABILITY MODEL
// //////////////////////////
/*
     * ---------------------------------------
     * Feature conversion
     * ---------------------------------------
     */
public static List<Feature> toFeatureList(Collection<? extends HasNameValue<TypedValue>> values, Collection<CounterfactualSearchDomain> searchDomains) {
    if (searchDomains.isEmpty()) {
        return toFeatureList(values);
    } else {
        AtomicInteger index = new AtomicInteger();
        final List<FeatureDomain> featureDomains = toFeatureDomainList(searchDomains);
        final List<Boolean> featureConstraints = toFeatureConstraintList(searchDomains);
        return values.stream().map(hnv -> {
            final String name = hnv.getName();
            final TypedValue value = hnv.getValue();
            final int i = index.getAndIncrement();
            return toFeature(name, value, featureDomains.get(i), featureConstraints.get(i));
        }).collect(Collectors.toList());
    }
}
Also used : FeatureFactory(org.kie.kogito.explainability.model.FeatureFactory) Feature(org.kie.kogito.explainability.model.Feature) BiFunction(java.util.function.BiFunction) CounterfactualSearchDomainValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainValue) CounterfactualDomainRange(org.kie.kogito.explainability.api.CounterfactualDomainRange) Value(org.kie.kogito.explainability.model.Value) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Pair(org.apache.commons.lang3.tuple.Pair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) HasNameValue(org.kie.kogito.explainability.api.HasNameValue) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonObject(io.vertx.core.json.JsonObject) CounterfactualDomainCategorical(org.kie.kogito.explainability.api.CounterfactualDomainCategorical) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Type(org.kie.kogito.explainability.model.Type) TextNode(com.fasterxml.jackson.databind.node.TextNode) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) Objects(java.util.Objects) List(java.util.List) CounterfactualDomain(org.kie.kogito.explainability.api.CounterfactualDomain) CollectionValue(org.kie.kogito.tracing.typedvalue.CollectionValue) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) Output(org.kie.kogito.explainability.model.Output) Optional(java.util.Optional) BooleanNode(com.fasterxml.jackson.databind.node.BooleanNode) CounterfactualSearchDomain(org.kie.kogito.explainability.api.CounterfactualSearchDomain) Collections(java.util.Collections) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue)

Example 2 with HasNameValue

use of org.kie.kogito.explainability.api.HasNameValue in project kogito-apps by kiegroup.

the class RemotePredictionProvider method toPredictionOutput.

protected PredictionOutput toPredictionOutput(JsonObject mainObj) {
    if (mainObj == null || !mainObj.containsKey("result")) {
        LOG.error("Malformed json {}", mainObj);
        return null;
    }
    List<Output> resultOutputs = toOutputList(mainObj.getJsonObject("result"));
    List<String> resultOutputNames = resultOutputs.stream().map(Output::getName).collect(toList());
    Map<String, TypedValue> mappedOutputs = predictionOutputs.stream().collect(Collectors.toMap(HasNameValue::getName, HasNameValue::getValue));
    // It's possible that some outputs are missing in the response from the prediction service
    // (e.g. when the generated perturbed inputs don't make sense and a decision is skipped).
    // The explainer, however, may throw exceptions if it can't find all the inputs that were
    // specified in the execution request.
    // Here we take the outputs received from the prediction service and we fill (only if needed)
    // the missing ones with Output objects containing "null" values of type UNDEFINED, to make
    // the explainer happy.
    List<Output> outputs = Stream.concat(resultOutputs.stream().filter(output -> mappedOutputs.containsKey(output.getName())), mappedOutputs.keySet().stream().filter(key -> !resultOutputNames.contains(key)).map(key -> new Output(key, Type.UNDEFINED, new Value(null), 1d))).collect(toList());
    return new PredictionOutput(outputs);
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) Feature(org.kie.kogito.explainability.model.Feature) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Value(org.kie.kogito.explainability.model.Value) Map(java.util.Map) HasNameValue(org.kie.kogito.explainability.api.HasNameValue) JsonObject(io.vertx.core.json.JsonObject) URI(java.net.URI) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictInput(org.kie.kogito.explainability.models.PredictInput) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) Collection(java.util.Collection) ThreadContext(org.eclipse.microprofile.context.ThreadContext) ConversionUtils.toOutputList(org.kie.kogito.explainability.ConversionUtils.toOutputList) Collectors(java.util.stream.Collectors) Type(org.kie.kogito.explainability.model.Type) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) Objects(java.util.Objects) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Output(org.kie.kogito.explainability.model.Output) Vertx(io.vertx.mutiny.core.Vertx) WebClient(io.vertx.mutiny.ext.web.client.WebClient) ModelIdentifier(org.kie.kogito.explainability.api.ModelIdentifier) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Value(org.kie.kogito.explainability.model.Value) HasNameValue(org.kie.kogito.explainability.api.HasNameValue) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 HasNameValue (org.kie.kogito.explainability.api.HasNameValue)2 Feature (org.kie.kogito.explainability.model.Feature)2 Output (org.kie.kogito.explainability.model.Output)2 Type (org.kie.kogito.explainability.model.Type)2 Value (org.kie.kogito.explainability.model.Value)2 TypedValue (org.kie.kogito.tracing.typedvalue.TypedValue)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BooleanNode (com.fasterxml.jackson.databind.node.BooleanNode)1 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 JsonArray (io.vertx.core.json.JsonArray)1 WebClientOptions (io.vertx.ext.web.client.WebClientOptions)1 Vertx (io.vertx.mutiny.core.Vertx)1 WebClient (io.vertx.mutiny.ext.web.client.WebClient)1