|
| 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 | + |
| 20 | +package org.elasticsearch.client.ml.inference.trainedmodel.metadata; |
| 21 | + |
| 22 | +import org.elasticsearch.common.Nullable; |
| 23 | +import org.elasticsearch.common.ParseField; |
| 24 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | +import org.elasticsearch.common.xcontent.XContentParseException; |
| 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 TotalFeatureImportance implements ToXContentObject { |
| 37 | + |
| 38 | + private static final String NAME = "total_feature_importance"; |
| 39 | + public static final ParseField FEATURE_NAME = new ParseField("feature_name"); |
| 40 | + public static final ParseField IMPORTANCE = new ParseField("importance"); |
| 41 | + public static final ParseField CLASSES = new ParseField("classes"); |
| 42 | + public static final ParseField MEAN_MAGNITUDE = new ParseField("mean_magnitude"); |
| 43 | + public static final ParseField MIN = new ParseField("min"); |
| 44 | + public static final ParseField MAX = new ParseField("max"); |
| 45 | + |
| 46 | + @SuppressWarnings("unchecked") |
| 47 | + public static final ConstructingObjectParser<TotalFeatureImportance, Void> PARSER = new ConstructingObjectParser<>(NAME, |
| 48 | + true, |
| 49 | + a -> new TotalFeatureImportance((String)a[0], (Importance)a[1], (List<ClassImportance>)a[2])); |
| 50 | + |
| 51 | + static { |
| 52 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), FEATURE_NAME); |
| 53 | + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), Importance.PARSER, IMPORTANCE); |
| 54 | + PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), ClassImportance.PARSER, CLASSES); |
| 55 | + } |
| 56 | + |
| 57 | + public static TotalFeatureImportance fromXContent(XContentParser parser) { |
| 58 | + return PARSER.apply(parser, null); |
| 59 | + } |
| 60 | + |
| 61 | + public final String featureName; |
| 62 | + public final Importance importance; |
| 63 | + public final List<ClassImportance> classImportances; |
| 64 | + |
| 65 | + TotalFeatureImportance(String featureName, @Nullable Importance importance, @Nullable List<ClassImportance> classImportances) { |
| 66 | + this.featureName = featureName; |
| 67 | + this.importance = importance; |
| 68 | + this.classImportances = classImportances == null ? Collections.emptyList() : classImportances; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 73 | + builder.startObject(); |
| 74 | + builder.field(FEATURE_NAME.getPreferredName(), featureName); |
| 75 | + if (importance != null) { |
| 76 | + builder.field(IMPORTANCE.getPreferredName(), importance); |
| 77 | + } |
| 78 | + if (classImportances.isEmpty() == false) { |
| 79 | + builder.field(CLASSES.getPreferredName(), classImportances); |
| 80 | + } |
| 81 | + builder.endObject(); |
| 82 | + return builder; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean equals(Object o) { |
| 87 | + if (this == o) return true; |
| 88 | + if (o == null || getClass() != o.getClass()) return false; |
| 89 | + TotalFeatureImportance that = (TotalFeatureImportance) o; |
| 90 | + return Objects.equals(that.importance, importance) |
| 91 | + && Objects.equals(featureName, that.featureName) |
| 92 | + && Objects.equals(classImportances, that.classImportances); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int hashCode() { |
| 97 | + return Objects.hash(featureName, importance, classImportances); |
| 98 | + } |
| 99 | + |
| 100 | + public static class Importance implements ToXContentObject { |
| 101 | + private static final String NAME = "importance"; |
| 102 | + |
| 103 | + public static final ConstructingObjectParser<Importance, Void> PARSER = new ConstructingObjectParser<>(NAME, |
| 104 | + true, |
| 105 | + a -> new Importance((double)a[0], (double)a[1], (double)a[2])); |
| 106 | + |
| 107 | + static { |
| 108 | + PARSER.declareDouble(ConstructingObjectParser.constructorArg(), MEAN_MAGNITUDE); |
| 109 | + PARSER.declareDouble(ConstructingObjectParser.constructorArg(), MIN); |
| 110 | + PARSER.declareDouble(ConstructingObjectParser.constructorArg(), MAX); |
| 111 | + } |
| 112 | + |
| 113 | + private final double meanMagnitude; |
| 114 | + private final double min; |
| 115 | + private final double max; |
| 116 | + |
| 117 | + public Importance(double meanMagnitude, double min, double max) { |
| 118 | + this.meanMagnitude = meanMagnitude; |
| 119 | + this.min = min; |
| 120 | + this.max = max; |
| 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 | + Importance that = (Importance) o; |
| 128 | + return Double.compare(that.meanMagnitude, meanMagnitude) == 0 && |
| 129 | + Double.compare(that.min, min) == 0 && |
| 130 | + Double.compare(that.max, max) == 0; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public int hashCode() { |
| 135 | + return Objects.hash(meanMagnitude, min, max); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 140 | + builder.startObject(); |
| 141 | + builder.field(MEAN_MAGNITUDE.getPreferredName(), meanMagnitude); |
| 142 | + builder.field(MIN.getPreferredName(), min); |
| 143 | + builder.field(MAX.getPreferredName(), max); |
| 144 | + builder.endObject(); |
| 145 | + return builder; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public static class ClassImportance implements ToXContentObject { |
| 150 | + private static final String NAME = "total_class_importance"; |
| 151 | + |
| 152 | + public static final ParseField CLASS_NAME = new ParseField("class_name"); |
| 153 | + public static final ParseField IMPORTANCE = new ParseField("importance"); |
| 154 | + |
| 155 | + public static final ConstructingObjectParser<ClassImportance, Void> PARSER = new ConstructingObjectParser<>(NAME, |
| 156 | + true, |
| 157 | + a -> new ClassImportance(a[0], (Importance)a[1])); |
| 158 | + |
| 159 | + static { |
| 160 | + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> { |
| 161 | + if (p.currentToken() == XContentParser.Token.VALUE_STRING) { |
| 162 | + return p.text(); |
| 163 | + } else if (p.currentToken() == XContentParser.Token.VALUE_NUMBER) { |
| 164 | + return p.numberValue(); |
| 165 | + } else if (p.currentToken() == XContentParser.Token.VALUE_BOOLEAN) { |
| 166 | + return p.booleanValue(); |
| 167 | + } |
| 168 | + throw new XContentParseException("Unsupported token [" + p.currentToken() + "]"); |
| 169 | + }, CLASS_NAME, ObjectParser.ValueType.VALUE); |
| 170 | + PARSER.declareObject(ConstructingObjectParser.constructorArg(), Importance.PARSER, IMPORTANCE); |
| 171 | + } |
| 172 | + |
| 173 | + public static ClassImportance fromXContent(XContentParser parser) { |
| 174 | + return PARSER.apply(parser, null); |
| 175 | + } |
| 176 | + |
| 177 | + public final Object className; |
| 178 | + public final Importance importance; |
| 179 | + |
| 180 | + ClassImportance(Object className, Importance importance) { |
| 181 | + this.className = className; |
| 182 | + this.importance = importance; |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 187 | + builder.startObject(); |
| 188 | + builder.field(CLASS_NAME.getPreferredName(), className); |
| 189 | + builder.field(IMPORTANCE.getPreferredName(), importance); |
| 190 | + builder.endObject(); |
| 191 | + return builder; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public boolean equals(Object o) { |
| 196 | + if (this == o) return true; |
| 197 | + if (o == null || getClass() != o.getClass()) return false; |
| 198 | + ClassImportance that = (ClassImportance) o; |
| 199 | + return Objects.equals(that.importance, importance) && Objects.equals(className, that.className); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public int hashCode() { |
| 204 | + return Objects.hash(className, importance); |
| 205 | + } |
| 206 | + |
| 207 | + } |
| 208 | +} |
0 commit comments