Search in sources :

Example 16 with H2OIllegalArgumentException

use of water.exceptions.H2OIllegalArgumentException 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 17 with H2OIllegalArgumentException

use of water.exceptions.H2OIllegalArgumentException 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 18 with H2OIllegalArgumentException

use of water.exceptions.H2OIllegalArgumentException 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 19 with H2OIllegalArgumentException

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

the class PojoUtils method filterFields.

/**
   * Null out fields in this schema and its children as specified by parameters __exclude_fields and __include_fields.
   * <b>NOTE: modifies the scheme tree in place.</b>
   */
public static void filterFields(Object o, String includes, String excludes) {
    if (null == o)
        return;
    if (null == excludes || "".equals(excludes))
        return;
    if (// not yet implemented
    null != includes)
        throw new H2OIllegalArgumentException("_include_fields", "filterFields", includes);
    String[] exclude_paths = excludes.split(",");
    for (String path : exclude_paths) {
        // for each path. . .
        int slash = path.indexOf("/");
        if (-1 == slash || slash == path.length()) {
            // NOTE: handles trailing slash
            // we've hit the end: null the field, if it exists
            Field f = ReflectionUtils.findNamedField(o, path);
            if (null == f)
                throw new H2OIllegalArgumentException("_exclude_fields", "filterFields", path);
            try {
                f.set(o, null);
            } catch (IllegalAccessException e) {
                throw new H2OIllegalArgumentException("_exclude_fields", "filterFields", path);
            }
        } else // hit the end of the path
        {
            String first = path.substring(0, slash);
            String rest = path.substring(slash + 1);
            Field f = ReflectionUtils.findNamedField(o, first);
            if (null == f)
                throw new H2OIllegalArgumentException("_exclude_fields", "filterFields", path);
            if (f.getType().isArray() && Object.class.isAssignableFrom(f.getType().getComponentType())) {
                // recurse into the children with the "rest" of the path
                try {
                    Object[] field_value = (Object[]) f.get(o);
                    for (Object child : field_value) {
                        filterFields(child, null, rest);
                    }
                } catch (IllegalAccessException e) {
                    throw new H2OIllegalArgumentException("_exclude_fields", "filterFields", path);
                }
            } else if (Object.class.isAssignableFrom(f.getType())) {
                // recurse into the child with the "rest" of the path
                try {
                    Object field_value = f.get(o);
                    filterFields(field_value, null, rest);
                } catch (IllegalAccessException e) {
                    throw new H2OIllegalArgumentException("_exclude_fields", "filterFields", path);
                }
            } else {
                throw new H2OIllegalArgumentException("_exclude_fields", "filterFields", path);
            }
        }
    // need to recurse
    }
// foreach exclude_paths
}
Also used : Field(java.lang.reflect.Field) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException)

Example 20 with H2OIllegalArgumentException

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

the class RapidsHandler method exec.

public RapidsSchemaV3 exec(int version, RapidsSchemaV3 rapids) {
    if (rapids == null)
        return null;
    if (!StringUtils.isNullOrEmpty(rapids.id))
        throw new H2OIllegalArgumentException("Field RapidsSchemaV3.id is deprecated and should not be set " + rapids.id);
    if (StringUtils.isNullOrEmpty(rapids.ast))
        return rapids;
    if (StringUtils.isNullOrEmpty(rapids.session_id))
        rapids.session_id = "_specialSess";
    Session ses = RapidsHandler.SESSIONS.get(rapids.session_id);
    if (ses == null) {
        ses = new Session(rapids.session_id);
        RapidsHandler.SESSIONS.put(rapids.session_id, ses);
    }
    Val val;
    try {
        // This call is synchronized on the session instance
        val = Rapids.exec(rapids.ast, ses);
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (Throwable e) {
        Log.err(e);
        e.printStackTrace();
        throw e;
    }
    switch(val.type()) {
        case Val.NUM:
            return new RapidsNumberV3(val.getNum());
        case Val.NUMS:
            return new RapidsNumbersV3(val.getNums());
        case Val.ROW:
            return new RapidsNumbersV3(val.getRow());
        case Val.STR:
            return new RapidsStringV3(val.getStr());
        case Val.STRS:
            return new RapidsStringsV3(val.getStrs());
        case Val.FRM:
            return new RapidsFrameV3(val.getFrame());
        case Val.FUN:
            return new RapidsFunctionV3(val.getFun().toString());
        default:
            throw H2O.fail();
    }
}
Also used : Val(water.rapids.Val) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) Session(water.rapids.Session)

Aggregations

H2OIllegalArgumentException (water.exceptions.H2OIllegalArgumentException)43 Frame (water.fvec.Frame)16 Key (water.Key)6 H2OKeyNotFoundArgumentException (water.exceptions.H2OKeyNotFoundArgumentException)6 Vec (water.fvec.Vec)6 Field (java.lang.reflect.Field)3 Chunk (water.fvec.Chunk)3 NewChunk (water.fvec.NewChunk)3 DeepLearningParameters (hex.deeplearning.DeepLearningModel.DeepLearningParameters)2 GLMModel (hex.glm.GLMModel)2 DRFModel (hex.tree.drf.DRFModel)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Iced (water.Iced)2 FrameV3 (water.api.schemas3.FrameV3)2 JobV3 (water.api.schemas3.JobV3)2 KeyV3 (water.api.schemas3.KeyV3)2 ByteVec (water.fvec.ByteVec)2 C0DChunk (water.fvec.C0DChunk)2