Skip to content

Commit e6edcd5

Browse files
committed
Checkstyle fixes
1 parent b25709c commit e6edcd5

File tree

8 files changed

+228
-92
lines changed

8 files changed

+228
-92
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearning.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,14 @@
195195
import org.elasticsearch.xpack.ml.dataframe.persistence.DataFrameAnalyticsConfigProvider;
196196
import org.elasticsearch.xpack.ml.dataframe.process.AnalyticsProcessFactory;
197197
import org.elasticsearch.xpack.ml.dataframe.process.AnalyticsProcessManager;
198-
import org.elasticsearch.xpack.ml.dataframe.process.results.AnalyticsResult;
199198
import org.elasticsearch.xpack.ml.dataframe.process.MemoryUsageEstimationProcessManager;
200-
import org.elasticsearch.xpack.ml.dataframe.process.results.MemoryUsageEstimationResult;
201-
import org.elasticsearch.xpack.ml.dataframe.process.NativeMemoryUsageEstimationProcessFactory;
202199
import org.elasticsearch.xpack.ml.dataframe.process.NativeAnalyticsProcessFactory;
200+
import org.elasticsearch.xpack.ml.dataframe.process.NativeMemoryUsageEstimationProcessFactory;
201+
import org.elasticsearch.xpack.ml.dataframe.process.results.AnalyticsResult;
202+
import org.elasticsearch.xpack.ml.dataframe.process.results.MemoryUsageEstimationResult;
203203
import org.elasticsearch.xpack.ml.inference.InferenceProcessor;
204204
import org.elasticsearch.xpack.ml.inference.ModelLoader;
205205
import org.elasticsearch.xpack.ml.inference.sillymodel.SillyModelLoader;
206-
import org.elasticsearch.xpack.ml.inference.tree.Tree;
207206
import org.elasticsearch.xpack.ml.inference.tree.TreeModelLoader;
208207
import org.elasticsearch.xpack.ml.job.JobManager;
209208
import org.elasticsearch.xpack.ml.job.JobManagerHolder;

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/tree/Tree.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
17
package org.elasticsearch.xpack.ml.inference.tree;
28

39
import java.util.ArrayList;
@@ -105,7 +111,7 @@ public static class Node {
105111
this(-1, -1, -1, false, value);
106112
}
107113

108-
boolean isLeaf() {
114+
public boolean isLeaf() {
109115
return leftChild < 1;
110116
}
111117

@@ -122,10 +128,22 @@ boolean isMissing(Double feature) {
122128
return feature == null;
123129
}
124130

125-
Double value() {
131+
public Double value() {
126132
return thresholdValue;
127133
}
128134

135+
public int getFeatureIndex() {
136+
return featureIndex;
137+
}
138+
139+
public int getLeftChild() {
140+
return leftChild;
141+
}
142+
143+
public int getRightChild() {
144+
return rightChild;
145+
}
146+
129147
@Override
130148
public String toString() {
131149
StringBuilder builder = new StringBuilder("{\n");

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/tree/TreeEnsembleModel.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
17
package org.elasticsearch.xpack.ml.inference.tree;
28

39
import org.apache.log4j.LogManager;

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/tree/TreeModel.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
17
package org.elasticsearch.xpack.ml.inference.tree;
28

39
import org.elasticsearch.ingest.IngestDocument;
410
import org.elasticsearch.xpack.ml.inference.Model;
511

12+
import java.util.function.BiConsumer;
13+
614
public class TreeModel implements Model {
715

816
private String targetFieldName;
@@ -15,9 +23,9 @@ public class TreeModel implements Model {
1523
}
1624

1725
@Override
18-
public IngestDocument infer(IngestDocument document) {
26+
public void infer(IngestDocument document, BiConsumer<IngestDocument, Exception> handler) {
1927
Double prediction = ensemble.predictFromDoc(document.getSourceAndMetadata());
2028
document.setFieldValue(targetFieldName, prediction);
21-
return document;
29+
handler.accept(document, null);
2230
}
2331
}

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/tree/TreeModelLoader.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
17
package org.elasticsearch.xpack.ml.inference.tree;
28

39
import org.apache.log4j.LogManager;
@@ -46,7 +52,7 @@ public TreeModel load(String modelId, String processorTag, boolean ignoreMissing
4652
String index = ConfigurationUtils.readStringProperty(InferenceProcessor.TYPE, processorTag, config, INDEX);
4753
String targetField = ConfigurationUtils.readStringProperty(InferenceProcessor.TYPE, processorTag, config, TARGET_FIELD);
4854

49-
load(modelId, index, listener);
55+
load(modelId, index, config, listener);
5056
latch.await();
5157
if (exception.get() != null) {
5258
throw exception.get();
@@ -59,11 +65,17 @@ public TreeModel load(String modelId, String processorTag, boolean ignoreMissing
5965
return model;
6066
}
6167

62-
public void load(String id, String index, ActionListener<TreeEnsembleModel> listener) {
68+
@Override
69+
public void consumeConfiguration(String processorTag, Map<String, Object> config) {
70+
ConfigurationUtils.readStringProperty(InferenceProcessor.TYPE, processorTag, config, INDEX);
71+
ConfigurationUtils.readStringProperty(InferenceProcessor.TYPE, processorTag, config, TARGET_FIELD);
72+
}
73+
74+
public void load(String id, String index, Map<String, Object> config, ActionListener<TreeEnsembleModel> listener) {
6375
client.prepareGet(index, null, id).execute(ActionListener.wrap(
6476
response -> {
6577
if (response.isExists()) {
66-
listener.onResponse(createEnsemble(response.getSourceAsMap()));
78+
listener.onResponse(createEnsemble(response.getSourceAsMap(), featureMapFromConfig(config)));
6779
} else {
6880
listener.onFailure(new ResourceNotFoundException("missing model [{}], [{}]", id, index));
6981
}
@@ -72,25 +84,23 @@ public void load(String id, String index, ActionListener<TreeEnsembleModel> list
7284
));
7385
}
7486

75-
@SuppressWarnings("unchecked")
76-
private TreeEnsembleModel createEnsemble(Map<String, Object> source) throws IOException {
87+
private Map<String, Integer> featureMapFromConfig(Map<String, Object> config) {
88+
// TODO, this was hard coded for a demo
7789
Map<String, Integer> featureMap = new HashMap<>();
7890
featureMap.put("f0", 0);
7991
featureMap.put("f1", 1);
8092
featureMap.put("f2", 2);
8193
featureMap.put("f3", 3);
94+
return featureMap;
95+
}
8296

97+
@SuppressWarnings("unchecked")
98+
private TreeEnsembleModel createEnsemble(Map<String, Object> source, Map<String, Integer> featureMap) throws IOException {
8399
List<Map<String, Object>> trees = (List<Map<String, Object>>) source.get("ensemble");
84100
if (trees == null) {
85101
throw new IllegalStateException("missing trees");
86102
}
87103

88104
return XgBoostJsonParser.parse(trees, featureMap);
89105
}
90-
91-
@Override
92-
public void readConfiguration(String processorTag, Map<String, Object> config) {
93-
ConfigurationUtils.readStringProperty(InferenceProcessor.TYPE, processorTag, config, INDEX);
94-
ConfigurationUtils.readStringProperty(InferenceProcessor.TYPE, processorTag, config, TARGET_FIELD);
95-
}
96106
}

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/tree/xgboost/XgBoostJsonParser.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
18
package org.elasticsearch.xpack.ml.inference.tree.xgboost;
29

310
import org.elasticsearch.xpack.ml.inference.tree.Tree;
@@ -14,8 +21,6 @@ public class XgBoostJsonParser {
1421
private static String SPLIT_CONDITION = "split_condition";
1522
private static String CHILDREN = "children";
1623

17-
// private static Logger logger = LogManager.getLogger(XgBoostJsonParser.class);
18-
1924

2025
public static TreeEnsembleModel parse(List<Map<String, Object>> json, Map<String, Integer> featureMap) throws IOException {
2126

@@ -69,6 +74,12 @@ private static void parseTree(Map<String, Object> treeMap, Map<String, Integer>
6974
throw new IOException("field [SPLIT_CONDITION] in node [" + nodeId + "] is not a double");
7075
}
7176

77+
// Fortunately XGBoost numbers the nodes the same way we do (breath first)
78+
// Assuming a 1 based index a non-leaf node at index X will have its child nodes
79+
// at indices 2X and 2X +1 (in practice we are using a zero based index so adjust
80+
// accordingly). XGBoost uses the same numbering system so we don't have to point
81+
// the child indices somewhere else on the new node, the child nodes will
82+
// automatically slot in the in right place.
7283
treeBuilder.addJunction(nodeId, featureIndex, true, threshold);
7384

7485
for (Map<String, Object> child : children) {

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/InferenceProcessorTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class InferenceProcessorTests extends ESTestCase {
2424
public void testFactory() throws Exception {
2525
String processorTag = randomAlphaOfLength(10);
2626
String modelType = "test";
27-
2827
Model mockModel = mock(Model.class);
2928
ModelLoader mockLoader = mock(ModelLoader.class);
3029
when(mockLoader.load(eq("k2"), eq(processorTag), eq(false), any())).thenReturn(mockModel);

0 commit comments

Comments
 (0)