use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.
the class RemotePredictionProviderTest method toMap.
@Test
void toMap() {
// simple test
Feature simple = new Feature("simple", Type.NUMBER, new Value(10));
Feature undefined = new Feature("undefined", Type.UNDEFINED, new Value(simple));
Feature composite = new Feature("composite", Type.COMPOSITE, new Value(asList(simple, undefined)));
List<Feature> features1 = asList(simple, undefined, composite);
Map<String, Object> result1 = predictionProvider.toMap(features1);
assertNotNull(result1);
assertEquals(features1.size(), result1.size());
assertTrue(result1.containsKey("simple"));
assertEquals(10, result1.get("simple"));
assertTrue(result1.containsKey("undefined"));
assertTrue(result1.containsKey("composite"));
assertTrue(result1.get("composite") instanceof Map);
// context test
Feature context = new Feature("context", Type.COMPOSITE, new Value(singletonList(simple)));
Feature simple2 = new Feature("simple2", Type.BOOLEAN, new Value(true));
List<Feature> features2 = asList(simple2, context);
Map<String, Object> result2 = predictionProvider.toMap(features2);
assertNotNull(result2);
assertEquals(1, result2.size());
assertTrue(result2.containsKey("simple"));
assertFalse(result2.containsKey("simple2"));
// multiple nesting test
Feature nestedComposite = new Feature("nestedComposite", Type.COMPOSITE, new Value(asList(simple, composite)));
List<Feature> features3 = asList(simple, nestedComposite);
Map<String, Object> result3 = predictionProvider.toMap(features3);
assertNotNull(result3);
assertEquals(features3.size(), result3.size());
assertTrue(result3.containsKey("simple"));
assertEquals(10, result3.get("simple"));
assertTrue(result3.containsKey("nestedComposite"));
assertTrue(result3.get("nestedComposite") instanceof Map);
@SuppressWarnings("unchecked") Map<String, Object> nestedCompositeMap = (Map<String, Object>) result3.get("nestedComposite");
assertEquals(2, nestedCompositeMap.size());
assertTrue(nestedCompositeMap.containsKey("simple"));
assertEquals(10, nestedCompositeMap.get("simple"));
assertTrue(nestedCompositeMap.containsKey("composite"));
assertTrue(nestedCompositeMap.get("composite") instanceof Map);
}
use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.
the class CounterfactualExplainerServiceHandlerTest method testCreateIntermediateResult.
@Test
public void testCreateIntermediateResult() {
CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, COUNTERFACTUAL_ID, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), MAX_RUNNING_TIME_SECONDS);
List<CounterfactualEntity> entities = List.of(DoubleEntity.from(new Feature("input1", Type.NUMBER, new Value(123.0d)), 0, 1000));
CounterfactualResult counterfactuals = new CounterfactualResult(entities, entities.stream().map(CounterfactualEntity::asFeature).collect(Collectors.toList()), List.of(new PredictionOutput(List.of(new Output("output1", Type.NUMBER, new Value(555.0d), 1.0)))), true, UUID.fromString(SOLUTION_ID), UUID.fromString(EXECUTION_ID), 0);
BaseExplainabilityResult base = handler.createIntermediateResult(request, counterfactuals);
assertTrue(base instanceof CounterfactualExplainabilityResult);
CounterfactualExplainabilityResult result = (CounterfactualExplainabilityResult) base;
assertEquals(ExplainabilityStatus.SUCCEEDED, result.getStatus());
assertEquals(CounterfactualExplainabilityResult.Stage.INTERMEDIATE, result.getStage());
assertEquals(EXECUTION_ID, result.getExecutionId());
assertEquals(COUNTERFACTUAL_ID, result.getCounterfactualId());
assertEquals(1, result.getInputs().size());
assertTrue(result.getInputs().stream().anyMatch(i -> i.getName().equals("input1")));
NamedTypedValue input1 = result.getInputs().iterator().next();
assertEquals(Double.class.getSimpleName(), input1.getValue().getType());
assertEquals(TypedValue.Kind.UNIT, input1.getValue().getKind());
assertEquals(123.0, input1.getValue().toUnit().getValue().asDouble());
assertEquals(1, result.getOutputs().size());
assertTrue(result.getOutputs().stream().anyMatch(o -> o.getName().equals("output1")));
NamedTypedValue output1 = result.getOutputs().iterator().next();
assertEquals(Double.class.getSimpleName(), output1.getValue().getType());
assertEquals(TypedValue.Kind.UNIT, output1.getValue().getKind());
assertEquals(555.0, output1.getValue().toUnit().getValue().asDouble());
}
use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.
the class CounterfactualExplainerServiceHandlerTest method testCreateSucceededResult.
@Test
public void testCreateSucceededResult() {
CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, COUNTERFACTUAL_ID, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), MAX_RUNNING_TIME_SECONDS);
List<CounterfactualEntity> entities = List.of(DoubleEntity.from(new Feature("input1", Type.NUMBER, new Value(123.0d)), 0, 1000));
CounterfactualResult counterfactuals = new CounterfactualResult(entities, entities.stream().map(CounterfactualEntity::asFeature).collect(Collectors.toList()), List.of(new PredictionOutput(List.of(new Output("output1", Type.NUMBER, new Value(555.0d), 1.0)))), true, UUID.fromString(SOLUTION_ID), UUID.fromString(EXECUTION_ID), 0);
BaseExplainabilityResult base = handler.createSucceededResult(request, counterfactuals);
assertTrue(base instanceof CounterfactualExplainabilityResult);
CounterfactualExplainabilityResult result = (CounterfactualExplainabilityResult) base;
assertEquals(ExplainabilityStatus.SUCCEEDED, result.getStatus());
assertEquals(CounterfactualExplainabilityResult.Stage.FINAL, result.getStage());
assertEquals(EXECUTION_ID, result.getExecutionId());
assertEquals(COUNTERFACTUAL_ID, result.getCounterfactualId());
assertEquals(1, result.getInputs().size());
assertTrue(result.getInputs().stream().anyMatch(i -> i.getName().equals("input1")));
NamedTypedValue input1 = result.getInputs().iterator().next();
assertEquals(Double.class.getSimpleName(), input1.getValue().getType());
assertEquals(TypedValue.Kind.UNIT, input1.getValue().getKind());
assertEquals(123.0, input1.getValue().toUnit().getValue().asDouble());
assertEquals(1, result.getOutputs().size());
assertTrue(result.getOutputs().stream().anyMatch(o -> o.getName().equals("output1")));
NamedTypedValue output1 = result.getOutputs().iterator().next();
assertEquals(Double.class.getSimpleName(), output1.getValue().getType());
assertEquals(TypedValue.Kind.UNIT, output1.getValue().getKind());
assertEquals(555.0, output1.getValue().toUnit().getValue().asDouble());
}
use of org.kie.kogito.explainability.model.Feature in project kogito-apps by kiegroup.
the class LimeExplainerServiceHandlerTest method testCreateSucceededResult.
@Test
public void testCreateSucceededResult() {
LIMEExplainabilityRequest request = new LIMEExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, Collections.emptyList(), Collections.emptyList());
Map<String, Saliency> saliencies = Map.of("s1", new Saliency(new Output("salary", Type.NUMBER), List.of(new FeatureImportance(new Feature("age", Type.NUMBER, new Value(25.0)), 5.0), new FeatureImportance(new Feature("dependents", Type.NUMBER, new Value(2)), -11.0))));
BaseExplainabilityResult base = handler.createSucceededResult(request, saliencies);
assertTrue(base instanceof LIMEExplainabilityResult);
LIMEExplainabilityResult result = (LIMEExplainabilityResult) base;
assertEquals(ExplainabilityStatus.SUCCEEDED, result.getStatus());
assertEquals(EXECUTION_ID, result.getExecutionId());
assertEquals(1, result.getSaliencies().size());
SaliencyModel saliencyModel = result.getSaliencies().iterator().next();
assertEquals(2, saliencyModel.getFeatureImportance().size());
assertEquals("age", saliencyModel.getFeatureImportance().get(0).getFeatureName());
assertEquals(5.0, saliencyModel.getFeatureImportance().get(0).getFeatureScore());
assertEquals("dependents", saliencyModel.getFeatureImportance().get(1).getFeatureName());
assertEquals(-11.0, saliencyModel.getFeatureImportance().get(1).getFeatureScore());
}
use of org.kie.kogito.explainability.model.Feature 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());
}
}
Aggregations