Search in sources :

Example 1 with H2OKeyNotFoundArgumentException

use of water.exceptions.H2OKeyNotFoundArgumentException in project h2o-3 by h2oai.

the class ModelMetricsHandler method predictAsync.

/**
   * Score a frame with the given model and return the metrics AND the prediction frame.
   */
// called through reflection by RequestServer
@SuppressWarnings("unused")
public JobV3 predictAsync(int version, final ModelMetricsListSchemaV3 s) {
    // parameters checking:
    if (null == s.model)
        throw new H2OIllegalArgumentException("model", "predict", s.model);
    if (null == DKV.get(s.model.name))
        throw new H2OKeyNotFoundArgumentException("model", "predict", s.model.name);
    if (null == s.frame)
        throw new H2OIllegalArgumentException("frame", "predict", s.frame);
    if (null == DKV.get(s.frame.name))
        throw new H2OKeyNotFoundArgumentException("frame", "predict", s.frame.name);
    if (s.deviances || null != s.deviances_frame)
        throw new H2OIllegalArgumentException("deviances", "not supported for async", s.deviances_frame);
    final ModelMetricsList parms = s.createAndFillImpl();
    if (s.deep_features_hidden_layer > 0) {
        if (null == parms._predictions_name)
            parms._predictions_name = "deep_features" + Key.make().toString().substring(0, 5) + "_" + parms._model._key.toString() + "_on_" + parms._frame._key.toString();
    } else if (null == parms._predictions_name) {
        if (parms._exemplar_index >= 0) {
            parms._predictions_name = "members_" + parms._model._key.toString() + "_for_exemplar_" + parms._exemplar_index;
        } else {
            parms._predictions_name = "predictions" + Key.make().toString().substring(0, 5) + "_" + parms._model._key.toString() + "_on_" + parms._frame._key.toString();
        }
    }
    final Job<Frame> j = new Job(Key.make(parms._predictions_name), Frame.class.getName(), "prediction");
    H2O.H2OCountedCompleter work = new H2O.H2OCountedCompleter() {

        @Override
        public void compute2() {
            if (s.deep_features_hidden_layer < 0) {
                parms._model.score(parms._frame, parms._predictions_name, j, true);
            } else {
                Frame predictions = ((Model.DeepFeatures) parms._model).scoreDeepFeatures(parms._frame, s.deep_features_hidden_layer, j);
                predictions = new Frame(Key.<Frame>make(parms._predictions_name), predictions.names(), predictions.vecs());
                DKV.put(predictions._key, predictions);
            }
            tryComplete();
        }
    };
    j.start(work, parms._frame.anyVec().nChunks());
    return new JobV3().fillFromImpl(j);
}
Also used : Frame(water.fvec.Frame) H2OKeyNotFoundArgumentException(water.exceptions.H2OKeyNotFoundArgumentException) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException)

Example 2 with H2OKeyNotFoundArgumentException

use of water.exceptions.H2OKeyNotFoundArgumentException in project h2o-3 by h2oai.

the class ModelMetricsHandler method make.

/**
   * Make a model metrics object from actual and predicted values
   */
// called through reflection by RequestServer
@SuppressWarnings("unused")
public ModelMetricsMakerSchemaV3 make(int version, ModelMetricsMakerSchemaV3 s) {
    // parameters checking:
    if (null == s.predictions_frame)
        throw new H2OIllegalArgumentException("predictions_frame", "make", s.predictions_frame);
    Frame pred = DKV.getGet(s.predictions_frame);
    if (null == pred)
        throw new H2OKeyNotFoundArgumentException("predictions_frame", "make", s.predictions_frame);
    if (null == s.actuals_frame)
        throw new H2OIllegalArgumentException("actuals_frame", "make", s.actuals_frame);
    Frame act = DKV.getGet(s.actuals_frame);
    if (null == act)
        throw new H2OKeyNotFoundArgumentException("actuals_frame", "make", s.actuals_frame);
    if (s.domain == null) {
        if (pred.numCols() != 1) {
            throw new H2OIllegalArgumentException("predictions_frame", "make", "For regression problems (domain=null), the predictions_frame must have exactly 1 column.");
        }
        ModelMetricsRegression mm = ModelMetricsRegression.make(pred.anyVec(), act.anyVec(), s.distribution);
        s.model_metrics = new ModelMetricsRegressionV3().fillFromImpl(mm);
    } else if (s.domain.length == 2) {
        if (pred.numCols() != 1) {
            throw new H2OIllegalArgumentException("predictions_frame", "make", "For domains with 2 class labels, the predictions_frame must have exactly one column containing the class-1 probabilities.");
        }
        ModelMetricsBinomial mm = ModelMetricsBinomial.make(pred.anyVec(), act.anyVec(), s.domain);
        s.model_metrics = new ModelMetricsBinomialV3().fillFromImpl(mm);
    } else if (s.domain.length > 2) {
        if (pred.numCols() != s.domain.length) {
            throw new H2OIllegalArgumentException("predictions_frame", "make", "For domains with " + s.domain.length + " class labels, the predictions_frame must have exactly " + s.domain.length + " columns containing the class-probabilities.");
        }
        ModelMetricsMultinomial mm = ModelMetricsMultinomial.make(pred, act.anyVec(), s.domain);
        s.model_metrics = new ModelMetricsMultinomialV3().fillFromImpl(mm);
    } else {
        throw H2O.unimpl();
    }
    return s;
}
Also used : Frame(water.fvec.Frame) H2OKeyNotFoundArgumentException(water.exceptions.H2OKeyNotFoundArgumentException) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException)

Example 3 with H2OKeyNotFoundArgumentException

use of water.exceptions.H2OKeyNotFoundArgumentException in project h2o-3 by h2oai.

the class ModelMetricsHandler method predict.

/**
   * Score a frame with the given model and return the metrics AND the prediction frame.
   */
// called through reflection by RequestServer
@SuppressWarnings("unused")
public ModelMetricsListSchemaV3 predict(int version, ModelMetricsListSchemaV3 s) {
    // parameters checking:
    if (s.model == null)
        throw new H2OIllegalArgumentException("model", "predict", null);
    if (DKV.get(s.model.name) == null)
        throw new H2OKeyNotFoundArgumentException("model", "predict", s.model.name);
    // Aggregator doesn't need a Frame to 'predict'
    if (s.exemplar_index < 0) {
        if (s.frame == null)
            throw new H2OIllegalArgumentException("frame", "predict", null);
        if (DKV.get(s.frame.name) == null)
            throw new H2OKeyNotFoundArgumentException("frame", "predict", s.frame.name);
    }
    ModelMetricsList parms = s.createAndFillImpl();
    Frame predictions;
    Frame deviances = null;
    if (!s.reconstruction_error && !s.reconstruction_error_per_feature && s.deep_features_hidden_layer < 0 && !s.project_archetypes && !s.reconstruct_train && !s.leaf_node_assignment && s.exemplar_index < 0) {
        if (null == parms._predictions_name)
            parms._predictions_name = "predictions" + Key.make().toString().substring(0, 5) + "_" + parms._model._key.toString() + "_on_" + parms._frame._key.toString();
        predictions = parms._model.score(parms._frame, parms._predictions_name);
        if (s.deviances) {
            if (!parms._model.isSupervised())
                throw new H2OIllegalArgumentException("Deviances can only be computed for supervised models.");
            if (null == parms._deviances_name)
                parms._deviances_name = "deviances" + Key.make().toString().substring(0, 5) + "_" + parms._model._key.toString() + "_on_" + parms._frame._key.toString();
            deviances = parms._model.computeDeviances(parms._frame, predictions, parms._deviances_name);
        }
    } else {
        if (s.deviances)
            throw new H2OIllegalArgumentException("Cannot compute deviances in combination with other special predictions.");
        if (Model.DeepFeatures.class.isAssignableFrom(parms._model.getClass())) {
            if (s.reconstruction_error || s.reconstruction_error_per_feature) {
                if (s.deep_features_hidden_layer >= 0)
                    throw new H2OIllegalArgumentException("Can only compute either reconstruction error OR deep features.", "");
                if (null == parms._predictions_name)
                    parms._predictions_name = "reconstruction_error" + Key.make().toString().substring(0, 5) + "_" + parms._model._key.toString() + "_on_" + parms._frame._key.toString();
                predictions = ((Model.DeepFeatures) parms._model).scoreAutoEncoder(parms._frame, Key.make(parms._predictions_name), parms._reconstruction_error_per_feature);
            } else {
                if (s.deep_features_hidden_layer < 0)
                    throw new H2OIllegalArgumentException("Deep features hidden layer index must be >= 0.", "");
                if (null == parms._predictions_name)
                    parms._predictions_name = "deep_features" + Key.make().toString().substring(0, 5) + "_" + parms._model._key.toString() + "_on_" + parms._frame._key.toString();
                predictions = ((Model.DeepFeatures) parms._model).scoreDeepFeatures(parms._frame, s.deep_features_hidden_layer);
            }
            predictions = new Frame(Key.<Frame>make(parms._predictions_name), predictions.names(), predictions.vecs());
            DKV.put(predictions._key, predictions);
        } else if (Model.GLRMArchetypes.class.isAssignableFrom(parms._model.getClass())) {
            if (s.project_archetypes) {
                if (parms._predictions_name == null)
                    parms._predictions_name = "reconstructed_archetypes_" + Key.make().toString().substring(0, 5) + "_" + parms._model._key.toString() + "_of_" + parms._frame._key.toString();
                predictions = ((Model.GLRMArchetypes) parms._model).scoreArchetypes(parms._frame, Key.<Frame>make(parms._predictions_name), s.reverse_transform);
            } else {
                assert s.reconstruct_train;
                if (parms._predictions_name == null)
                    parms._predictions_name = "reconstruction_" + Key.make().toString().substring(0, 5) + "_" + parms._model._key.toString() + "_of_" + parms._frame._key.toString();
                predictions = ((Model.GLRMArchetypes) parms._model).scoreReconstruction(parms._frame, Key.<Frame>make(parms._predictions_name), s.reverse_transform);
            }
        } else if (s.leaf_node_assignment) {
            assert (Model.LeafNodeAssignment.class.isAssignableFrom(parms._model.getClass()));
            if (null == parms._predictions_name)
                parms._predictions_name = "leaf_node_assignment" + Key.make().toString().substring(0, 5) + "_" + parms._model._key.toString() + "_on_" + parms._frame._key.toString();
            predictions = ((Model.LeafNodeAssignment) parms._model).scoreLeafNodeAssignment(parms._frame, Key.<Frame>make(parms._predictions_name));
        } else if (s.exemplar_index >= 0) {
            assert (Model.ExemplarMembers.class.isAssignableFrom(parms._model.getClass()));
            if (null == parms._predictions_name)
                parms._predictions_name = "members_" + parms._model._key.toString() + "_for_exemplar_" + parms._exemplar_index;
            predictions = ((Model.ExemplarMembers) parms._model).scoreExemplarMembers(Key.<Frame>make(parms._predictions_name), parms._exemplar_index);
        } else
            throw new H2OIllegalArgumentException("Requires a Deep Learning, GLRM, DRF or GBM model.", "Model must implement specific methods.");
    }
    ModelMetricsListSchemaV3 mm = this.fetch(version, s);
    // For the others cons one up here to return the predictions frame.
    if (null == mm)
        mm = new ModelMetricsListSchemaV3();
    mm.predictions_frame = new KeyV3.FrameKeyV3(predictions._key);
    if (//don't show metrics in leaf node assignments are made
    parms._leaf_node_assignment)
        mm.model_metrics = null;
    if (deviances != null)
        mm.deviances_frame = new KeyV3.FrameKeyV3(deviances._key);
    if (null == mm.model_metrics || 0 == mm.model_metrics.length) {
    // There was no response in the test set -> cannot make a model_metrics object
    } else {
        // TODO: Should call schema(version)
        mm.model_metrics[0].predictions = new FrameV3(predictions, 0, 100);
    }
    return mm;
}
Also used : Frame(water.fvec.Frame) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) H2OKeyNotFoundArgumentException(water.exceptions.H2OKeyNotFoundArgumentException)

Example 4 with H2OKeyNotFoundArgumentException

use of water.exceptions.H2OKeyNotFoundArgumentException in project h2o-3 by h2oai.

the class Schema method parse.

// URL parameter parse
static <E> Object parse(String field_name, String s, Class fclz, boolean required, Class schemaClass) {
    if (fclz.isPrimitive() || String.class.equals(fclz)) {
        try {
            return parsePrimitve(s, fclz);
        } catch (NumberFormatException ne) {
            String msg = "Illegal argument for field: " + field_name + " of schema: " + schemaClass.getSimpleName() + ": cannot convert \"" + s + "\" to type " + fclz.getSimpleName();
            throw new H2OIllegalArgumentException(msg);
        }
    }
    // An array?
    if (fclz.isArray()) {
        // Get component type
        Class<E> afclz = (Class<E>) fclz.getComponentType();
        // Result
        E[] a = null;
        // Handle simple case with null-array
        if (s.equals("null") || s.length() == 0)
            return null;
        // Splitted values
        // "".split(",") => {""} so handle the empty case explicitly
        String[] splits;
        if (s.startsWith("[") && s.endsWith("]")) {
            // It looks like an array
            read(s, 0, '[', fclz);
            read(s, s.length() - 1, ']', fclz);
            String inside = s.substring(1, s.length() - 1).trim();
            if (inside.length() == 0)
                splits = new String[] {};
            else
                splits = splitArgs(inside);
        } else {
            // Lets try to parse single value as an array!
            // See PUBDEV-1955
            splits = new String[] { s.trim() };
        }
        // Can't cast an int[] to an Object[].  Sigh.
        if (afclz == int.class) {
            // TODO: other primitive types. . .
            a = (E[]) Array.newInstance(Integer.class, splits.length);
        } else if (afclz == double.class) {
            a = (E[]) Array.newInstance(Double.class, splits.length);
        } else if (afclz == float.class) {
            a = (E[]) Array.newInstance(Float.class, splits.length);
        } else {
            // Fails with primitive classes; need the wrapper class.  Thanks, Java.
            a = (E[]) Array.newInstance(afclz, splits.length);
        }
        for (int i = 0; i < splits.length; i++) {
            if (String.class == afclz || KeyV3.class.isAssignableFrom(afclz)) {
                // strip quotes off string values inside array
                String stripped = splits[i].trim();
                if ("null".equals(stripped.toLowerCase()) || "na".equals(stripped.toLowerCase())) {
                    a[i] = null;
                    continue;
                }
                // Quotes are now optional because standard clients will send arrays of length one as just strings.
                if (stripped.startsWith("\"") && stripped.endsWith("\"")) {
                    stripped = stripped.substring(1, stripped.length() - 1);
                }
                a[i] = (E) parse(field_name, stripped, afclz, required, schemaClass);
            } else {
                a[i] = (E) parse(field_name, splits[i].trim(), afclz, required, schemaClass);
            }
        }
        return a;
    }
    // Are we parsing an object from a string? NOTE: we might want to make this check more restrictive.
    if (!fclz.isAssignableFrom(Schema.class) && s != null && s.startsWith("{") && s.endsWith("}")) {
        return gson.fromJson(s, fclz);
    }
    if (fclz.equals(Key.class))
        if ((s == null || s.length() == 0) && required)
            throw new H2OKeyNotFoundArgumentException(field_name, s);
        else if (!required && (s == null || s.length() == 0))
            return null;
        else
            // If the key name is in an array we need to trim surrounding quotes.
            return Key.make(s.startsWith("\"") ? s.substring(1, s.length() - 1) : s);
    if (KeyV3.class.isAssignableFrom(fclz)) {
        if ((s == null || s.length() == 0) && required)
            throw new H2OKeyNotFoundArgumentException(field_name, s);
        if (!required && (s == null || s.length() == 0))
            return null;
        // If the key name is in an array we need to trim surrounding quotes.
        return KeyV3.make(fclz, Key.make(s.startsWith("\"") ? s.substring(1, s.length() - 1) : s));
    }
    if (Enum.class.isAssignableFrom(fclz)) {
        return EnumUtils.valueOf(fclz, s);
    }
    // TODO: these can be refactored into a single case using the facilities in Schema:
    if (FrameV3.class.isAssignableFrom(fclz)) {
        if ((s == null || s.length() == 0) && required)
            throw new H2OKeyNotFoundArgumentException(field_name, s);
        else if (!required && (s == null || s.length() == 0))
            return null;
        else {
            Value v = DKV.get(s);
            // not required
            if (null == v)
                return null;
            if (!v.isFrame())
                throw H2OIllegalArgumentException.wrongKeyType(field_name, s, "Frame", v.get().getClass());
            // TODO: version!
            return new FrameV3((Frame) v.get());
        }
    }
    if (JobV3.class.isAssignableFrom(fclz)) {
        if ((s == null || s.length() == 0) && required)
            throw new H2OKeyNotFoundArgumentException(s);
        else if (!required && (s == null || s.length() == 0))
            return null;
        else {
            Value v = DKV.get(s);
            // not required
            if (null == v)
                return null;
            if (!v.isJob())
                throw H2OIllegalArgumentException.wrongKeyType(field_name, s, "Job", v.get().getClass());
            // TODO: version!
            return new JobV3().fillFromImpl((Job) v.get());
        }
    }
    // where the frame name is also specified.
    if (FrameV3.ColSpecifierV3.class.isAssignableFrom(fclz)) {
        return new FrameV3.ColSpecifierV3(s);
    }
    if (ModelSchemaV3.class.isAssignableFrom(fclz))
        throw H2O.fail("Can't yet take ModelSchemaV3 as input.");
    /*
      if( (s==null || s.length()==0) && required ) throw new IllegalArgumentException("Missing key");
      else if (!required && (s == null || s.length() == 0)) return null;
      else {
      Value v = DKV.get(s);
      if (null == v) return null; // not required
      if (! v.isModel()) throw new IllegalArgumentException("Model argument points to a non-model object.");
      return v.get();
      }
    */
    throw H2O.fail("Unimplemented schema fill from " + fclz.getSimpleName());
}
Also used : Frame(water.fvec.Frame) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) KeyV3(water.api.schemas3.KeyV3) FrameV3(water.api.schemas3.FrameV3) H2OKeyNotFoundArgumentException(water.exceptions.H2OKeyNotFoundArgumentException) JobV3(water.api.schemas3.JobV3)

Example 5 with H2OKeyNotFoundArgumentException

use of water.exceptions.H2OKeyNotFoundArgumentException in project h2o-3 by h2oai.

the class ModelMetricsHandler method score.

/**
   * Score a frame with the given model and return just the metrics.
   * <p>
   * NOTE: ModelMetrics are now always being created by model.score. . .
   */
// called through reflection by RequestServer
@SuppressWarnings("unused")
public ModelMetricsListSchemaV3 score(int version, ModelMetricsListSchemaV3 s) {
    // parameters checking:
    if (null == s.model)
        throw new H2OIllegalArgumentException("model", "predict", s.model);
    if (null == DKV.get(s.model.name))
        throw new H2OKeyNotFoundArgumentException("model", "predict", s.model.name);
    if (null == s.frame)
        throw new H2OIllegalArgumentException("frame", "predict", s.frame);
    if (null == DKV.get(s.frame.name))
        throw new H2OKeyNotFoundArgumentException("frame", "predict", s.frame.name);
    ModelMetricsList parms = s.createAndFillImpl();
    // throw away predictions, keep metrics as a side-effect
    parms._model.score(parms._frame, parms._predictions_name).remove();
    ModelMetricsListSchemaV3 mm = this.fetch(version, s);
    // For the others cons one up here to return the predictions frame.
    if (null == mm)
        mm = new ModelMetricsListSchemaV3();
    if (null == mm.model_metrics || 0 == mm.model_metrics.length) {
        Log.warn("Score() did not return a ModelMetrics for model: " + s.model + " on frame: " + s.frame);
    }
    return mm;
}
Also used : H2OKeyNotFoundArgumentException(water.exceptions.H2OKeyNotFoundArgumentException) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException)

Aggregations

H2OIllegalArgumentException (water.exceptions.H2OIllegalArgumentException)5 H2OKeyNotFoundArgumentException (water.exceptions.H2OKeyNotFoundArgumentException)5 Frame (water.fvec.Frame)4 FrameV3 (water.api.schemas3.FrameV3)1 JobV3 (water.api.schemas3.JobV3)1 KeyV3 (water.api.schemas3.KeyV3)1