-
Notifications
You must be signed in to change notification settings - Fork 25.3k
[ML] add new multi custom processor for data frame analytics and model inference #67362
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
benwtrent
merged 4 commits into
elastic:master
from
benwtrent:feature/ml-inference-multi-pre-processor
Jan 15, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e7d9c28
[ML] add new multi custom processor for data frame analytics
benwtrent ed276fb
fixing ngram test generation
benwtrent 0eebe80
Merge branch 'master' into feature/ml-inference-multi-pre-processor
benwtrent a691ca2
addressing PR comments and updating tests
benwtrent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...t-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/Multi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client.ml.inference.preprocessing; | ||
|
||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.elasticsearch.client.ml.inference.NamedXContentObjectHelper; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
/** | ||
* Multi-PreProcessor for chaining together multiple processors | ||
*/ | ||
public class Multi implements PreProcessor { | ||
|
||
public static final String NAME = "multi_encoding"; | ||
public static final ParseField PROCESSORS = new ParseField("processors"); | ||
public static final ParseField CUSTOM = new ParseField("custom"); | ||
|
||
@SuppressWarnings("unchecked") | ||
public static final ConstructingObjectParser<Multi, Void> PARSER = new ConstructingObjectParser<>( | ||
NAME, | ||
true, | ||
a -> new Multi((List<PreProcessor>)a[0], (Boolean)a[1])); | ||
static { | ||
PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), | ||
(p, c, n) -> p.namedObject(PreProcessor.class, n, null), | ||
(_unused) -> {/* Does not matter client side*/ }, | ||
PROCESSORS); | ||
PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), CUSTOM); | ||
} | ||
|
||
public static Multi fromXContent(XContentParser parser) { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
private final List<PreProcessor> processors; | ||
private final Boolean custom; | ||
|
||
Multi(List<PreProcessor> processors, Boolean custom) { | ||
this.processors = Objects.requireNonNull(processors, PROCESSORS.getPreferredName()); | ||
this.custom = custom; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
NamedXContentObjectHelper.writeNamedObjects(builder, params, true, PROCESSORS.getPreferredName(), processors); | ||
if (custom != null) { | ||
builder.field(CUSTOM.getPreferredName(), custom); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Multi multi = (Multi) o; | ||
return Objects.equals(multi.processors, processors) && Objects.equals(custom, multi.custom); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(custom, processors); | ||
} | ||
|
||
public static Builder builder(List<PreProcessor> processors) { | ||
return new Builder(processors); | ||
} | ||
|
||
public static class Builder { | ||
private final List<PreProcessor> processors; | ||
private Boolean custom; | ||
|
||
public Builder(List<PreProcessor> processors) { | ||
this.processors = processors; | ||
} | ||
|
||
public Builder setCustom(boolean custom) { | ||
this.custom = custom; | ||
return this; | ||
} | ||
|
||
public Multi build() { | ||
return new Multi(processors, custom); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...h-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/MultiTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.ml.inference.preprocessing; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.elasticsearch.client.ml.inference.MlInferenceNamedXContentProvider; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.test.AbstractXContentTestCase; | ||
|
||
|
||
public class MultiTests extends AbstractXContentTestCase<Multi> { | ||
|
||
@Override | ||
protected Multi doParseInstance(XContentParser parser) throws IOException { | ||
return Multi.fromXContent(parser); | ||
} | ||
|
||
@Override | ||
protected Predicate<String> getRandomFieldsExcludeFilter() { | ||
return field -> !field.isEmpty(); | ||
} | ||
|
||
@Override | ||
protected NamedXContentRegistry xContentRegistry() { | ||
return new NamedXContentRegistry(new MlInferenceNamedXContentProvider().getNamedXContentParsers()); | ||
} | ||
|
||
@Override | ||
protected boolean supportsUnknownFields() { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected Multi createTestInstance() { | ||
return createRandom(); | ||
} | ||
|
||
public static Multi createRandom() { | ||
final List<PreProcessor> processors; | ||
Boolean isCustom = randomBoolean() ? null : randomBoolean(); | ||
if (isCustom == null || isCustom == false) { | ||
NGram nGram = new NGram(randomAlphaOfLength(10), Arrays.asList(1, 2), 0, 10, isCustom, "f"); | ||
List<PreProcessor> preProcessorList = new ArrayList<>(); | ||
preProcessorList.add(nGram); | ||
Stream.generate(() -> randomFrom( | ||
FrequencyEncodingTests.createRandom(randomFrom(nGram.outputFields())), | ||
TargetMeanEncodingTests.createRandom(randomFrom(nGram.outputFields())), | ||
OneHotEncodingTests.createRandom(randomFrom(nGram.outputFields())) | ||
)).limit(randomIntBetween(1, 10)).forEach(preProcessorList::add); | ||
processors = preProcessorList; | ||
} else { | ||
processors = Stream.generate( | ||
() -> randomFrom( | ||
FrequencyEncodingTests.createRandom(), | ||
TargetMeanEncodingTests.createRandom(), | ||
OneHotEncodingTests.createRandom(), | ||
NGramTests.createRandom() | ||
) | ||
).limit(randomIntBetween(1, 10)).collect(Collectors.toList()); | ||
} | ||
return new Multi(processors, isCustom); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.