All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.whylogs.core.ModelProfile Maven / Gradle / Ivy

There is a newer version: 0.1.8-b0
Show newest version
package com.whylogs.core;

import com.google.common.collect.Sets;
import com.whylogs.core.message.ModelProfileMessage;
import com.whylogs.core.metrics.ModelMetrics;
import java.util.Map;
import java.util.Set;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.val;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ModelProfile {
  private final Set outputFields;
  @Getter private final ModelMetrics metrics;

  public ModelProfile(
      String prediction, String target, String score, Iterable additionalOutputFields) {
    this.outputFields = Sets.newHashSet(additionalOutputFields);
    this.outputFields.add(prediction);
    this.metrics = new ModelMetrics(prediction, target, score);
  }

  public ModelProfile(String prediction, String target, Iterable additionalOutputFields) {
    this.outputFields = Sets.newHashSet(additionalOutputFields);
    this.outputFields.add(prediction);
    this.metrics = new ModelMetrics(prediction, target);
  }

  public ModelProfileMessage.Builder toProtobuf() {
    val builder = ModelProfileMessage.newBuilder();
    builder.addAllOutputFields(outputFields);
    if (metrics != null) {
      builder.setMetrics(metrics.toProtobuf());
    }
    return builder;
  }

  public static ModelProfile fromProtobuf(ModelProfileMessage message) {
    if (message == null || message.getSerializedSize() == 0) {
      return null;
    }

    val metrics = ModelMetrics.fromProtobuf(message.getMetrics());
    val outputFields = Sets.newHashSet(message.getOutputFieldsList());

    return new ModelProfile(outputFields, metrics);
  }

  public ModelProfile merge(ModelProfile other) {
    if (other == null) {
      return new ModelProfile(Sets.newHashSet(outputFields), metrics.copy());
    }
    return new ModelProfile(Sets.newHashSet(outputFields), metrics.merge(other.metrics));
  }

  public ModelProfile copy() {
    return merge(null);
  }

  public void track(Map columns) {
    this.metrics.track(columns);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy