forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Data frame analytics analysis stats (elastic#53788)
Adds parsing and indexing of analysis instrumentation stats. The latest one is also returned from the get-stats API. Note that we chose to duplicate objects even where they are currently similar. There are already ideas on how these will diverge in the future and while the duplication looks ugly at the moment, it is the option that offers the highest flexibility.
- Loading branch information
1 parent
07eefa9
commit 04aee0a
Showing
72 changed files
with
4,972 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
29 changes: 29 additions & 0 deletions
29
...t-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/AnalysisStats.java
This file contains 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,29 @@ | ||
/* | ||
* 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.dataframe.stats; | ||
|
||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
|
||
/** | ||
* Statistics for the data frame analysis | ||
*/ | ||
public interface AnalysisStats extends ToXContentObject { | ||
|
||
String getName(); | ||
} |
52 changes: 52 additions & 0 deletions
52
.../java/org/elasticsearch/client/ml/dataframe/stats/AnalysisStatsNamedXContentProvider.java
This file contains 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,52 @@ | ||
/* | ||
* 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.dataframe.stats; | ||
|
||
import org.elasticsearch.client.ml.dataframe.stats.classification.ClassificationStats; | ||
import org.elasticsearch.client.ml.dataframe.stats.outlierdetection.OutlierDetectionStats; | ||
import org.elasticsearch.client.ml.dataframe.stats.regression.RegressionStats; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.plugins.spi.NamedXContentProvider; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class AnalysisStatsNamedXContentProvider implements NamedXContentProvider { | ||
|
||
@Override | ||
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() { | ||
return Arrays.asList( | ||
new NamedXContentRegistry.Entry( | ||
AnalysisStats.class, | ||
ClassificationStats.NAME, | ||
(p, c) -> ClassificationStats.PARSER.apply(p, null) | ||
), | ||
new NamedXContentRegistry.Entry( | ||
AnalysisStats.class, | ||
OutlierDetectionStats.NAME, | ||
(p, c) -> OutlierDetectionStats.PARSER.apply(p, null) | ||
), | ||
new NamedXContentRegistry.Entry( | ||
AnalysisStats.class, | ||
RegressionStats.NAME, | ||
(p, c) -> RegressionStats.PARSER.apply(p, null) | ||
) | ||
); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
.../java/org/elasticsearch/client/ml/dataframe/stats/classification/ClassificationStats.java
This file contains 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,135 @@ | ||
/* | ||
* 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.dataframe.stats.classification; | ||
|
||
import org.elasticsearch.client.common.TimeUtil; | ||
import org.elasticsearch.client.ml.dataframe.stats.AnalysisStats; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.Objects; | ||
|
||
public class ClassificationStats implements AnalysisStats { | ||
|
||
public static final ParseField NAME = new ParseField("classification_stats"); | ||
|
||
public static final ParseField TIMESTAMP = new ParseField("timestamp"); | ||
public static final ParseField ITERATION = new ParseField("iteration"); | ||
public static final ParseField HYPERPARAMETERS = new ParseField("hyperparameters"); | ||
public static final ParseField TIMING_STATS = new ParseField("timing_stats"); | ||
public static final ParseField VALIDATION_LOSS = new ParseField("validation_loss"); | ||
|
||
public static final ConstructingObjectParser<ClassificationStats, Void> PARSER = new ConstructingObjectParser<>(NAME.getPreferredName(), | ||
true, | ||
a -> new ClassificationStats( | ||
(Instant) a[0], | ||
(Integer) a[1], | ||
(Hyperparameters) a[2], | ||
(TimingStats) a[3], | ||
(ValidationLoss) a[4] | ||
) | ||
); | ||
|
||
static { | ||
PARSER.declareField(ConstructingObjectParser.constructorArg(), | ||
p -> TimeUtil.parseTimeFieldToInstant(p, TIMESTAMP.getPreferredName()), | ||
TIMESTAMP, | ||
ObjectParser.ValueType.VALUE); | ||
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), ITERATION); | ||
PARSER.declareObject(ConstructingObjectParser.constructorArg(), Hyperparameters.PARSER, HYPERPARAMETERS); | ||
PARSER.declareObject(ConstructingObjectParser.constructorArg(), TimingStats.PARSER, TIMING_STATS); | ||
PARSER.declareObject(ConstructingObjectParser.constructorArg(), ValidationLoss.PARSER, VALIDATION_LOSS); | ||
} | ||
|
||
private final Instant timestamp; | ||
private final Integer iteration; | ||
private final Hyperparameters hyperparameters; | ||
private final TimingStats timingStats; | ||
private final ValidationLoss validationLoss; | ||
|
||
public ClassificationStats(Instant timestamp, Integer iteration, Hyperparameters hyperparameters, TimingStats timingStats, | ||
ValidationLoss validationLoss) { | ||
this.timestamp = Instant.ofEpochMilli(Objects.requireNonNull(timestamp).toEpochMilli()); | ||
this.iteration = iteration; | ||
this.hyperparameters = Objects.requireNonNull(hyperparameters); | ||
this.timingStats = Objects.requireNonNull(timingStats); | ||
this.validationLoss = Objects.requireNonNull(validationLoss); | ||
} | ||
|
||
public Instant getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public Integer getIteration() { | ||
return iteration; | ||
} | ||
|
||
public Hyperparameters getHyperparameters() { | ||
return hyperparameters; | ||
} | ||
|
||
public TimingStats getTimingStats() { | ||
return timingStats; | ||
} | ||
|
||
public ValidationLoss getValidationLoss() { | ||
return validationLoss; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
builder.timeField(TIMESTAMP.getPreferredName(), TIMESTAMP.getPreferredName() + "_string", timestamp.toEpochMilli()); | ||
if (iteration != null) { | ||
builder.field(ITERATION.getPreferredName(), iteration); | ||
} | ||
builder.field(HYPERPARAMETERS.getPreferredName(), hyperparameters); | ||
builder.field(TIMING_STATS.getPreferredName(), timingStats); | ||
builder.field(VALIDATION_LOSS.getPreferredName(), validationLoss); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
ClassificationStats that = (ClassificationStats) o; | ||
return Objects.equals(timestamp, that.timestamp) | ||
&& Objects.equals(iteration, that.iteration) | ||
&& Objects.equals(hyperparameters, that.hyperparameters) | ||
&& Objects.equals(timingStats, that.timingStats) | ||
&& Objects.equals(validationLoss, that.validationLoss); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(timestamp, iteration, hyperparameters, timingStats, validationLoss); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME.getPreferredName(); | ||
} | ||
} |
Oops, something went wrong.