Skip to content

[ML][Inference] fix support for nested fields #50258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
import org.elasticsearch.xpack.core.ml.utils.MapHelper;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -103,7 +104,7 @@ public String getName() {

@Override
public void process(Map<String, Object> fields) {
Object value = fields.get(field);
Object value = MapHelper.dig(field, fields);
if (value == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
import org.elasticsearch.xpack.core.ml.utils.MapHelper;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -86,7 +87,7 @@ public String getName() {

@Override
public void process(Map<String, Object> fields) {
Object value = fields.get(field);
Object value = MapHelper.dig(field, fields);
if (value == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
import org.elasticsearch.xpack.core.ml.utils.MapHelper;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -114,7 +115,7 @@ public String getName() {

@Override
public void process(Map<String, Object> fields) {
Object value = fields.get(field);
Object value = MapHelper.dig(field, fields);
if (value == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.xpack.core.ml.inference.trainedmodel.StrictlyParsedTrainedModel;
import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
import org.elasticsearch.xpack.core.ml.utils.MapHelper;

import java.io.IOException;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -129,7 +130,9 @@ public InferenceResults infer(Map<String, Object> fields, InferenceConfig config
"Cannot infer using configuration for [{}] when model target_type is [{}]", config.getName(), targetType.toString());
}

List<Double> features = featureNames.stream().map(f -> InferenceHelpers.toDouble(fields.get(f))).collect(Collectors.toList());
List<Double> features = featureNames.stream()
.map(f -> InferenceHelpers.toDouble(MapHelper.dig(f, fields)))
.collect(Collectors.toList());
return infer(features, config);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.core.ml.utils;

import org.elasticsearch.common.Nullable;

import java.util.Arrays;
import java.util.Map;

public final class MapHelper {

private MapHelper() {}

/**
* This eagerly digs through the map by tokenizing the provided path on '.'.
*
* Only fully nested or fully collapsed fields are searched.
* Examples
*
* {
* "a.b.c.d" : 2
* }
* {
* "a" :{"b": {"c": {"d" : 2}}}
* }
*
* It is possible for ES _source docs to have "mixed" path formats.
*
* Meaning the following _sources (along with the above) are indexed the same:
* {
* "a": {"b.c": {"d": 2}}
* }
* {
* "a.b.c": {"d": 2}
* }
*
* Exhaustively exploring all the combinations of nesting fields via '.' (field name delimiter) or ':' (nested objects)
* would result in 2^n-1 total possible paths, where {@code n = path.split("\\.").length}.
*
* This would result in an exponential runtime algorithm.
*
* NOTE: The default maximum field depth is 20. Meaning 524288 different ways of
* nesting the same fields.
*
*
* @param path Dot delimited path containing the field desired
* @param map The {@link Map} map to dig
* @return The found object. Returns {@code null} if not found
*/
@Nullable
public static Object dig(String path, Map<String, Object> map) {
// short cut before search
if (map.keySet().contains(path)) {
return map.get(path);
}
String[] fields = path.split("\\.");
if (Arrays.stream(fields).anyMatch(String::isEmpty)) {
throw new IllegalArgumentException("Empty path detected. Invalid field name");
}
return explore(fields, map);
}

@SuppressWarnings("unchecked")
private static Object explore(String[] path, Map<String, Object> map) {
for(int i = 0; i < path.length - 1; i++) {
if (map.get(path[i]) instanceof Map<?, ?> == false) {
return null;
}
map = (Map<String, Object>) map.get(path[i]);
}
return map.get(path[path.length - 1]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,22 @@ public void testProcessWithFieldPresent() {
testProcess(encoding, fieldValues, matchers);
}

public void testProcessWithNestedField() {
String field = "categorical.child";
List<Object> values = Arrays.asList("foo", "bar", "foobar", "baz", "farequote", 1.5);
Map<String, Double> valueMap = values.stream().collect(Collectors.toMap(Object::toString,
v -> randomDoubleBetween(0.0, 1.0, false)));
String encodedFeatureName = "encoded";
FrequencyEncoding encoding = new FrequencyEncoding(field, encodedFeatureName, valueMap);

Map<String, Object> fieldValues = new HashMap<>() {{
put("categorical", new HashMap<>(){{
put("child", "farequote");
}});
}};

encoding.process(fieldValues);
assertThat(fieldValues.get("encoded"), equalTo(valueMap.get("farequote")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,19 @@ public void testProcessWithFieldPresent() {
testProcess(encoding, fieldValues, matchers);
}

public void testProcessWithNestedField() {
String field = "categorical.child";
List<Object> values = Arrays.asList("foo", "bar", "foobar", "baz", "farequote", 1.5);
Map<String, String> valueMap = values.stream().collect(Collectors.toMap(Object::toString, v -> "Column_" + v.toString()));
OneHotEncoding encoding = new OneHotEncoding(field, valueMap);
Map<String, Object> fieldValues = new HashMap<>() {{
put("categorical", new HashMap<>(){{
put("child", "farequote");
}});
}};

encoding.process(fieldValues);
assertThat(fieldValues.get("Column_farequote"), equalTo(1));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,24 @@ public void testProcessWithFieldPresent() {
testProcess(encoding, fieldValues, matchers);
}

public void testProcessWithNestedField() {
String field = "categorical.child";
List<Object> values = Arrays.asList("foo", "bar", "foobar", "baz", "farequote", 1.5);
Map<String, Double> valueMap = values.stream().collect(Collectors.toMap(Object::toString,
v -> randomDoubleBetween(0.0, 1.0, false)));
String encodedFeatureName = "encoded";
Double defaultvalue = randomDouble();
TargetMeanEncoding encoding = new TargetMeanEncoding(field, encodedFeatureName, valueMap, defaultvalue);

Map<String, Object> fieldValues = new HashMap<>() {{
put("categorical", new HashMap<>(){{
put("child", "farequote");
}});
}};

encoding.process(fieldValues);

assertThat(fieldValues.get("encoded"), equalTo(valueMap.get("farequote")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,63 @@ public void testRegressionInference() {
closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS)).value(), 0.00001));
}

public void testInferNestedFields() {
List<String> featureNames = Arrays.asList("foo.baz", "bar.biz");
Tree tree1 = Tree.builder()
.setFeatureNames(featureNames)
.setRoot(TreeNode.builder(0)
.setLeftChild(1)
.setRightChild(2)
.setSplitFeature(0)
.setThreshold(0.5))
.addNode(TreeNode.builder(1).setLeafValue(0.3))
.addNode(TreeNode.builder(2)
.setThreshold(0.8)
.setSplitFeature(1)
.setLeftChild(3)
.setRightChild(4))
.addNode(TreeNode.builder(3).setLeafValue(0.1))
.addNode(TreeNode.builder(4).setLeafValue(0.2)).build();
Tree tree2 = Tree.builder()
.setFeatureNames(featureNames)
.setRoot(TreeNode.builder(0)
.setLeftChild(1)
.setRightChild(2)
.setSplitFeature(0)
.setThreshold(0.5))
.addNode(TreeNode.builder(1).setLeafValue(1.5))
.addNode(TreeNode.builder(2).setLeafValue(0.9))
.build();
Ensemble ensemble = Ensemble.builder()
.setTargetType(TargetType.REGRESSION)
.setFeatureNames(featureNames)
.setTrainedModels(Arrays.asList(tree1, tree2))
.setOutputAggregator(new WeightedSum(new double[]{0.5, 0.5}))
.build();

Map<String, Object> featureMap = new HashMap<>() {{
put("foo", new HashMap<>(){{
put("baz", 0.4);
}});
put("bar", new HashMap<>(){{
put("biz", 0.0);
}});
}};
assertThat(0.9,
closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS)).value(), 0.00001));

featureMap = new HashMap<>() {{
put("foo", new HashMap<>(){{
put("baz", 2.0);
}});
put("bar", new HashMap<>(){{
put("biz", 0.7);
}});
}};
assertThat(0.5,
closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS)).value(), 0.00001));
}

public void testOperationsEstimations() {
Tree tree1 = TreeTests.buildRandomTree(Arrays.asList("foo", "bar"), 2);
Tree tree2 = TreeTests.buildRandomTree(Arrays.asList("foo", "bar", "baz"), 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,58 @@ public void testInfer() {
closeTo(((SingleValueInferenceResults)tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS)).value(), 0.00001));
}

public void testInferNestedFields() {
// Build a tree with 2 nodes and 3 leaves using 2 features
// The leaves have unique values 0.1, 0.2, 0.3
Tree.Builder builder = Tree.builder().setTargetType(TargetType.REGRESSION);
TreeNode.Builder rootNode = builder.addJunction(0, 0, true, 0.5);
builder.addLeaf(rootNode.getRightChild(), 0.3);
TreeNode.Builder leftChildNode = builder.addJunction(rootNode.getLeftChild(), 1, true, 0.8);
builder.addLeaf(leftChildNode.getLeftChild(), 0.1);
builder.addLeaf(leftChildNode.getRightChild(), 0.2);

List<String> featureNames = Arrays.asList("foo.baz", "bar.biz");
Tree tree = builder.setFeatureNames(featureNames).build();

// This feature vector should hit the right child of the root node
Map<String, Object> featureMap = new HashMap<>() {{
put("foo", new HashMap<>(){{
put("baz", 0.6);
}});
put("bar", new HashMap<>(){{
put("biz", 0.0);
}});
}};
assertThat(0.3,
closeTo(((SingleValueInferenceResults)tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS)).value(), 0.00001));

// This should hit the left child of the left child of the root node
// i.e. it takes the path left, left
featureMap = new HashMap<>() {{
put("foo", new HashMap<>(){{
put("baz", 0.3);
}});
put("bar", new HashMap<>(){{
put("biz", 0.7);
}});
}};
assertThat(0.1,
closeTo(((SingleValueInferenceResults)tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS)).value(), 0.00001));

// This should hit the right child of the left child of the root node
// i.e. it takes the path left, right
featureMap = new HashMap<>() {{
put("foo", new HashMap<>(){{
put("baz", 0.3);
}});
put("bar", new HashMap<>(){{
put("biz", 0.9);
}});
}};
assertThat(0.2,
closeTo(((SingleValueInferenceResults)tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS)).value(), 0.00001));
}

public void testTreeClassificationProbability() {
// Build a tree with 2 nodes and 3 leaves using 2 features
// The leaves have unique values 0.1, 0.2, 0.3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.core.ml.utils;

import org.elasticsearch.test.ESTestCase;

import java.util.Collections;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

public class MapHelperTests extends ESTestCase {

public void testAbsolutePathStringAsKey() {
String path = "a.b.c.d";
Map<String, Object> map = Collections.singletonMap(path, 2);
assertThat(MapHelper.dig(path, map), equalTo(2));
assertThat(MapHelper.dig(path, Collections.emptyMap()), is(nullValue()));
}

public void testSimplePath() {
String path = "a.b.c.d";
Map<String, Object> map = Collections.singletonMap("a",
Collections.singletonMap("b",
Collections.singletonMap("c",
Collections.singletonMap("d", 2))));
assertThat(MapHelper.dig(path, map), equalTo(2));

map = Collections.singletonMap("a",
Collections.singletonMap("b",
Collections.singletonMap("e", // Not part of path
Collections.singletonMap("d", 2))));
assertThat(MapHelper.dig(path, map), is(nullValue()));
}

public void testSimplePathReturningMap() {
String path = "a.b.c";
Map<String, Object> map = Collections.singletonMap("a",
Collections.singletonMap("b",
Collections.singletonMap("c",
Collections.singletonMap("d", 2))));
assertThat(MapHelper.dig(path, map), equalTo(Collections.singletonMap("d", 2)));
}

}
Loading