Search in sources :

Example 26 with H2OIllegalArgumentException

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

the class ParseHandler method parse.

// Entry point for parsing.
// called through reflection by RequestServer
@SuppressWarnings("unused")
public ParseV3 parse(int version, ParseV3 parse) {
    ParserInfo parserInfo = ParserService.INSTANCE.getByName(parse.parse_type).info();
    ParseSetup setup = new ParseSetup(parserInfo, parse.separator, parse.single_quotes, parse.check_header, parse.number_columns, delNulls(parse.column_names), ParseSetup.strToColumnTypes(parse.column_types), parse.domains, parse.na_strings, null, new ParseWriter.ParseErr[0], parse.chunk_size);
    if (parse.source_frames == null)
        throw new H2OIllegalArgumentException("Data for Frame '" + parse.destination_frame.name + "' is not available. Please check that the path is valid (for all H2O nodes).'");
    Key[] srcs = new Key[parse.source_frames.length];
    for (int i = 0; i < parse.source_frames.length; i++) srcs[i] = parse.source_frames[i].key();
    parse.job = new JobV3(ParseDataset.parse(parse.destination_frame.key(), srcs, parse.delete_on_done, setup, parse.blocking)._job);
    if (parse.blocking) {
        Frame fr = DKV.getGet(parse.destination_frame.key());
        parse.rows = fr.numRows();
    }
    return parse;
}
Also used : Frame(water.fvec.Frame) ParseSetup(water.parser.ParseSetup) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) ParseWriter(water.parser.ParseWriter) ParserInfo(water.parser.ParserInfo) JobV3(water.api.schemas3.JobV3) Key(water.Key)

Example 27 with H2OIllegalArgumentException

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

the class ParseSetupHandler method guessSetup.

public ParseSetupV3 guessSetup(int version, ParseSetupV3 p) {
    if (p.source_frames == null)
        throw new H2OIllegalArgumentException("No file names given for parsing.");
    Key[] fkeys = new Key[p.source_frames.length];
    for (int i = 0; i < p.source_frames.length; i++) {
        fkeys[i] = p.source_frames[i].key();
        if (DKV.get(fkeys[i]) == null)
            throw new IllegalArgumentException("Key not loaded: " + p.source_frames[i]);
    }
    // corrects for json putting in empty strings in the place of empty sub-arrays
    if (p.na_strings != null)
        for (int i = 0; i < p.na_strings.length; i++) if (p.na_strings[i] != null && p.na_strings[i].length == 0)
            p.na_strings[i] = null;
    ParseSetup ps;
    try {
        ps = ParseSetup.guessSetup(fkeys, new ParseSetup(p));
    } catch (Throwable ex) {
        Throwable ex2 = ex;
        if (ex instanceof DistributedException)
            ex2 = ex.getCause();
        if (ex2 instanceof ParseDataset.H2OParseException)
            throw new H2OIllegalArgumentException(ex2.getMessage());
        throw ex;
    }
    if (ps._errs != null && ps._errs.length > 0) {
        p.warnings = new String[ps._errs.length];
        for (int i = 0; i < ps._errs.length; ++i) p.warnings[i] = ps._errs[i].toString();
    }
    // TODO: ParseSetup throws away the srcs list. . .
    if ((null == p.column_name_filter || "".equals(p.column_name_filter)) && (0 == p.column_offset) && (0 == p.column_count)) {
        // return the entire data preview
        PojoUtils.copyProperties(p, ps, PojoUtils.FieldNaming.ORIGIN_HAS_UNDERSCORES, new String[] { "destination_key", "source_keys", "column_types", "parse_type" });
        p.total_filtered_column_count = p.number_columns;
    } else {
        // have to manually copy the desired parts of p.data to apply either column_name_filter or column pagination or both
        PojoUtils.copyProperties(p, ps, PojoUtils.FieldNaming.ORIGIN_HAS_UNDERSCORES, new String[] { "destination_key", "source_keys", "column_types", "data", "parse_type" });
        String[] all_col_names = ps.getColumnNames();
        String[][] data = ps.getData();
        ArrayList<Integer> keep_indexes = new ArrayList<>();
        if (null != p.column_name_filter && !"".equals(p.column_name_filter)) {
            // filter and then paginate columns
            Pattern pattern = Pattern.compile(p.column_name_filter);
            Matcher m = pattern.matcher("dummy");
            for (int column = 0; column < all_col_names.length; column++) {
                m.reset(all_col_names[column]);
                if (m.matches())
                    keep_indexes.add(column);
            }
        } else {
            // note: we do a little extra work below by treating this like the filter case, but the code is simpler
            for (int column = 0; column < all_col_names.length; column++) {
                keep_indexes.add(column);
            }
        }
        int width_to_return = Math.max(0, keep_indexes.size() - p.column_offset);
        if (p.column_count > 0)
            width_to_return = Math.min(width_to_return, p.column_count);
        String[][] filtered_data = new String[data.length][width_to_return];
        for (int row = 0; row < data.length; row++) {
            int output_column = 0;
            for (int input_column_index = p.column_offset; input_column_index < p.column_offset + width_to_return; input_column_index++) {
                // indirect through keep_indexes
                filtered_data[row][output_column++] = data[row][keep_indexes.get(input_column_index)];
            }
        }
        p.data = filtered_data;
        p.total_filtered_column_count = keep_indexes.size();
    }
    p.destination_frame = ParseSetup.createHexName(p.source_frames[0].toString());
    if (p.check_header == ParseSetup.HAS_HEADER && p.data != null && Arrays.equals(p.column_names, p.data[0]))
        p.data = Arrays.copyOfRange(p.data, 1, p.data.length);
    // Fill in data type names for each column.
    p.column_types = ps.getColumnTypeStrings();
    p.parse_type = ps.getParseType() != null ? ps.getParseType().name() : GUESS_INFO.name();
    return p;
}
Also used : Pattern(java.util.regex.Pattern) ParseSetup(water.parser.ParseSetup) DistributedException(water.util.DistributedException) Matcher(java.util.regex.Matcher) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) ArrayList(java.util.ArrayList) ParseDataset(water.parser.ParseDataset) Key(water.Key) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException)

Example 28 with H2OIllegalArgumentException

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

Example 29 with H2OIllegalArgumentException

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

the class GridSchemaV99 method fillFromImpl.

@Override
public GridSchemaV99 fillFromImpl(Grid grid) {
    Key<Model>[] gridModelKeys = grid.getModelKeys();
    // Return only keys which are referencing to existing objects in DKV
    // However, here is still implicit race, since we are sending
    // keys to client, but referenced models can be deleted in meantime
    // Hence, client has to be responsible for handling this situation
    // - call getModel and check for null model
    // pre-allocate
    List<Key<Model>> modelKeys = new ArrayList<>(gridModelKeys.length);
    for (Key k : gridModelKeys) {
        if (k != null && DKV.get(k) != null) {
            modelKeys.add(k);
        }
    }
    // Default sort order -- TODO: Outsource
    if (sort_by == null && modelKeys.size() > 0 && modelKeys.get(0) != null) {
        Model m = DKV.getGet(modelKeys.get(0));
        if (m != null && m.isSupervised()) {
            if (m._output.nclasses() > 1) {
                sort_by = "logloss";
                decreasing = false;
            } else {
                sort_by = "residual_deviance";
                decreasing = false;
            }
        }
    }
    // If not, show all possible metrics
    if (modelKeys.size() > 0 && sort_by != null) {
        Set<String> possibleMetrics = ModelMetrics.getAllowedMetrics(modelKeys.get(0));
        if (!possibleMetrics.contains(sort_by.toLowerCase())) {
            throw new H2OIllegalArgumentException("Invalid argument for sort_by specified. Must be one of: " + Arrays.toString(possibleMetrics.toArray(new String[0])));
        }
    }
    // Are we sorting by model metrics?
    if (null != sort_by && !sort_by.isEmpty()) {
        // sort the model keys
        modelKeys = ModelMetrics.sortModelsByMetric(sort_by, decreasing, modelKeys);
        // fill the metrics arrays
        training_metrics = new ModelMetricsBaseV3[modelKeys.size()];
        validation_metrics = new ModelMetricsBaseV3[modelKeys.size()];
        cross_validation_metrics = new ModelMetricsBaseV3[modelKeys.size()];
        cross_validation_metrics_summary = new TwoDimTableV3[modelKeys.size()];
        for (int i = 0; i < modelKeys.size(); i++) {
            Model m = DKV.getGet(modelKeys.get(i));
            if (m != null) {
                Model.Output o = m._output;
                if (null != o._training_metrics)
                    training_metrics[i] = (ModelMetricsBaseV3) SchemaServer.schema(3, o._training_metrics).fillFromImpl(o._training_metrics);
                if (null != o._validation_metrics)
                    validation_metrics[i] = (ModelMetricsBaseV3) SchemaServer.schema(3, o._validation_metrics).fillFromImpl(o._validation_metrics);
                if (null != o._cross_validation_metrics)
                    cross_validation_metrics[i] = (ModelMetricsBaseV3) SchemaServer.schema(3, o._cross_validation_metrics).fillFromImpl(o._cross_validation_metrics);
                if (o._cross_validation_metrics_summary != null)
                    cross_validation_metrics_summary[i] = new TwoDimTableV3(o._cross_validation_metrics_summary);
            }
        }
    }
    KeyV3.ModelKeyV3[] modelIds = new KeyV3.ModelKeyV3[modelKeys.size()];
    Key<Model>[] keys = new Key[modelKeys.size()];
    for (int i = 0; i < modelIds.length; i++) {
        modelIds[i] = new KeyV3.ModelKeyV3(modelKeys.get(i));
        keys[i] = modelIds[i].key();
    }
    grid_id = new KeyV3.GridKeyV3(grid._key);
    model_ids = modelIds;
    hyper_names = grid.getHyperNames();
    failed_params = toModelParametersSchema(grid.getFailedParameters());
    failure_details = grid.getFailureDetails();
    failure_stack_traces = grid.getFailureStackTraces();
    failed_raw_params = grid.getFailedRawParameters();
    TwoDimTable t = grid.createSummaryTable(keys, sort_by, decreasing);
    if (t != null)
        summary_table = new TwoDimTableV3().fillFromImpl(t);
    TwoDimTable h = grid.createScoringHistoryTable();
    if (h != null)
        scoring_history = new TwoDimTableV3().fillFromImpl(h);
    return this;
}
Also used : ArrayList(java.util.ArrayList) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) TwoDimTable(water.util.TwoDimTable) Model(hex.Model) Key(water.Key)

Example 30 with H2OIllegalArgumentException

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

the class GridSearchSchema method fillFromParms.

@Override
public S fillFromParms(Properties parms) {
    if (parms.containsKey("hyper_parameters")) {
        Map<String, Object> m = water.util.JSONUtils.parse(parms.getProperty("hyper_parameters"));
        // Convert lists and singletons into arrays
        for (Map.Entry<String, Object> e : m.entrySet()) {
            Object o = e.getValue();
            Object[] o2 = o instanceof List ? ((List) o).toArray() : new Object[] { o };
            hyper_parameters.put(e.getKey(), o2);
        }
        parms.remove("hyper_parameters");
    }
    if (parms.containsKey("search_criteria")) {
        Properties p = water.util.JSONUtils.parseToProperties(parms.getProperty("search_criteria"));
        if (!p.containsKey("strategy")) {
            throw new H2OIllegalArgumentException("search_criteria.strategy", "null");
        }
        // TODO: move this into a factory method in HyperSpaceSearchCriteriaV99
        String strategy = (String) p.get("strategy");
        if ("Cartesian".equals(strategy)) {
            search_criteria = new HyperSpaceSearchCriteriaV99.CartesianSearchCriteriaV99();
        } else if ("RandomDiscrete".equals(strategy)) {
            search_criteria = new HyperSpaceSearchCriteriaV99.RandomDiscreteValueSearchCriteriaV99();
            if (p.containsKey("max_runtime_secs") && Double.parseDouble((String) p.get("max_runtime_secs")) < 0) {
                throw new H2OIllegalArgumentException("max_runtime_secs must be >= 0 (0 for unlimited time)", strategy);
            }
            if (p.containsKey("max_models") && Integer.parseInt((String) p.get("max_models")) < 0) {
                throw new H2OIllegalArgumentException("max_models must be >= 0 (0 for all models)", strategy);
            }
        } else {
            throw new H2OIllegalArgumentException("search_criteria.strategy", strategy);
        }
        search_criteria.fillWithDefaults();
        search_criteria.fillFromParms(p);
        parms.remove("search_criteria");
    } else {
        // Fall back to Cartesian if there's no search_criteria specified.
        search_criteria = new HyperSpaceSearchCriteriaV99.CartesianSearchCriteriaV99();
    }
    if (parms.containsKey("grid_id")) {
        grid_id = new KeyV3.GridKeyV3(Key.<Grid>make(parms.getProperty("grid_id")));
        parms.remove("grid_id");
    }
    // Do not check validity of parameters, GridSearch is tolerant of bad
    // parameters (on purpose, many hyper-param points in the grid might be
    // illegal for whatever reason).
    this.parameters.fillFromParms(parms, false);
    return (S) this;
}
Also used : Grid(hex.grid.Grid) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) Properties(java.util.Properties) KeyV3(water.api.schemas3.KeyV3) List(java.util.List) IcedHashMap(water.util.IcedHashMap) Map(java.util.Map)

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