|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.ml.inference.trainedmodel.ensemble; |
| 20 | + |
| 21 | +import org.elasticsearch.client.ml.inference.NamedXContentObjectHelper; |
| 22 | +import org.elasticsearch.client.ml.inference.trainedmodel.TargetType; |
| 23 | +import org.elasticsearch.client.ml.inference.trainedmodel.TrainedModel; |
| 24 | +import org.elasticsearch.common.Nullable; |
| 25 | +import org.elasticsearch.common.ParseField; |
| 26 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 27 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 28 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 29 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Objects; |
| 35 | + |
| 36 | +public class Ensemble implements TrainedModel { |
| 37 | + |
| 38 | + public static final String NAME = "ensemble"; |
| 39 | + public static final ParseField FEATURE_NAMES = new ParseField("feature_names"); |
| 40 | + public static final ParseField TRAINED_MODELS = new ParseField("trained_models"); |
| 41 | + public static final ParseField AGGREGATE_OUTPUT = new ParseField("aggregate_output"); |
| 42 | + public static final ParseField TARGET_TYPE = new ParseField("target_type"); |
| 43 | + public static final ParseField CLASSIFICATION_LABELS = new ParseField("classification_labels"); |
| 44 | + |
| 45 | + private static final ObjectParser<Builder, Void> PARSER = new ObjectParser<>( |
| 46 | + NAME, |
| 47 | + true, |
| 48 | + Ensemble.Builder::new); |
| 49 | + |
| 50 | + static { |
| 51 | + PARSER.declareStringArray(Ensemble.Builder::setFeatureNames, FEATURE_NAMES); |
| 52 | + PARSER.declareNamedObjects(Ensemble.Builder::setTrainedModels, |
| 53 | + (p, c, n) -> |
| 54 | + p.namedObject(TrainedModel.class, n, null), |
| 55 | + (ensembleBuilder) -> { /* Noop does not matter client side */ }, |
| 56 | + TRAINED_MODELS); |
| 57 | + PARSER.declareNamedObjects(Ensemble.Builder::setOutputAggregatorFromParser, |
| 58 | + (p, c, n) -> p.namedObject(OutputAggregator.class, n, null), |
| 59 | + (ensembleBuilder) -> { /* Noop does not matter client side */ }, |
| 60 | + AGGREGATE_OUTPUT); |
| 61 | + PARSER.declareString(Ensemble.Builder::setTargetType, TARGET_TYPE); |
| 62 | + PARSER.declareStringArray(Ensemble.Builder::setClassificationLabels, CLASSIFICATION_LABELS); |
| 63 | + } |
| 64 | + |
| 65 | + public static Ensemble fromXContent(XContentParser parser) { |
| 66 | + return PARSER.apply(parser, null).build(); |
| 67 | + } |
| 68 | + |
| 69 | + private final List<String> featureNames; |
| 70 | + private final List<TrainedModel> models; |
| 71 | + private final OutputAggregator outputAggregator; |
| 72 | + private final TargetType targetType; |
| 73 | + private final List<String> classificationLabels; |
| 74 | + |
| 75 | + Ensemble(List<String> featureNames, |
| 76 | + List<TrainedModel> models, |
| 77 | + @Nullable OutputAggregator outputAggregator, |
| 78 | + TargetType targetType, |
| 79 | + @Nullable List<String> classificationLabels) { |
| 80 | + this.featureNames = featureNames; |
| 81 | + this.models = models; |
| 82 | + this.outputAggregator = outputAggregator; |
| 83 | + this.targetType = targetType; |
| 84 | + this.classificationLabels = classificationLabels; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public List<String> getFeatureNames() { |
| 89 | + return featureNames; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String getName() { |
| 94 | + return NAME; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { |
| 99 | + builder.startObject(); |
| 100 | + if (featureNames != null) { |
| 101 | + builder.field(FEATURE_NAMES.getPreferredName(), featureNames); |
| 102 | + } |
| 103 | + if (models != null) { |
| 104 | + NamedXContentObjectHelper.writeNamedObjects(builder, params, true, TRAINED_MODELS.getPreferredName(), models); |
| 105 | + } |
| 106 | + if (outputAggregator != null) { |
| 107 | + NamedXContentObjectHelper.writeNamedObjects(builder, |
| 108 | + params, |
| 109 | + false, |
| 110 | + AGGREGATE_OUTPUT.getPreferredName(), |
| 111 | + Collections.singletonList(outputAggregator)); |
| 112 | + } |
| 113 | + if (targetType != null) { |
| 114 | + builder.field(TARGET_TYPE.getPreferredName(), targetType); |
| 115 | + } |
| 116 | + if (classificationLabels != null) { |
| 117 | + builder.field(CLASSIFICATION_LABELS.getPreferredName(), classificationLabels); |
| 118 | + } |
| 119 | + builder.endObject(); |
| 120 | + return builder; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean equals(Object o) { |
| 125 | + if (this == o) return true; |
| 126 | + if (o == null || getClass() != o.getClass()) return false; |
| 127 | + Ensemble that = (Ensemble) o; |
| 128 | + return Objects.equals(featureNames, that.featureNames) |
| 129 | + && Objects.equals(models, that.models) |
| 130 | + && Objects.equals(targetType, that.targetType) |
| 131 | + && Objects.equals(classificationLabels, that.classificationLabels) |
| 132 | + && Objects.equals(outputAggregator, that.outputAggregator); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public int hashCode() { |
| 137 | + return Objects.hash(featureNames, models, outputAggregator, classificationLabels, targetType); |
| 138 | + } |
| 139 | + |
| 140 | + public static Builder builder() { |
| 141 | + return new Builder(); |
| 142 | + } |
| 143 | + |
| 144 | + public static class Builder { |
| 145 | + private List<String> featureNames; |
| 146 | + private List<TrainedModel> trainedModels; |
| 147 | + private OutputAggregator outputAggregator; |
| 148 | + private TargetType targetType; |
| 149 | + private List<String> classificationLabels; |
| 150 | + |
| 151 | + public Builder setFeatureNames(List<String> featureNames) { |
| 152 | + this.featureNames = featureNames; |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | + public Builder setTrainedModels(List<TrainedModel> trainedModels) { |
| 157 | + this.trainedModels = trainedModels; |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + public Builder setOutputAggregator(OutputAggregator outputAggregator) { |
| 162 | + this.outputAggregator = outputAggregator; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + public Builder setTargetType(TargetType targetType) { |
| 167 | + this.targetType = targetType; |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + public Builder setClassificationLabels(List<String> classificationLabels) { |
| 172 | + this.classificationLabels = classificationLabels; |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + private void setOutputAggregatorFromParser(List<OutputAggregator> outputAggregators) { |
| 177 | + this.setOutputAggregator(outputAggregators.get(0)); |
| 178 | + } |
| 179 | + |
| 180 | + private void setTargetType(String targetType) { |
| 181 | + this.targetType = TargetType.fromString(targetType); |
| 182 | + } |
| 183 | + |
| 184 | + public Ensemble build() { |
| 185 | + return new Ensemble(featureNames, trainedModels, outputAggregator, targetType, classificationLabels); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments