Search in sources :

Example 1 with KeyV3

use of water.api.schemas3.KeyV3 in project h2o-3 by h2oai.

the class SplitFrameHandler method run.

public SplitFrameV3 run(int version, SplitFrameV3 sf) {
    SplitFrame splitFrame = sf.createAndFillImpl();
    Job job = splitFrame.exec();
    SplitFrameV3 spv3 = new SplitFrameV3(splitFrame);
    spv3.key = new KeyV3.JobKeyV3(job._key);
    return spv3;
}
Also used : SplitFrameV3(water.api.schemas3.SplitFrameV3) SplitFrame(hex.SplitFrame) Job(water.Job) KeyV3(water.api.schemas3.KeyV3)

Example 2 with KeyV3

use of water.api.schemas3.KeyV3 in project h2o-3 by h2oai.

the class AssemblyHandler method fit.

public AssemblyV99 fit(int version, AssemblyV99 ass) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    if (ass == null)
        return null;
    if (ass.steps == null)
        return ass;
    // process assembly:
    //   of the form [name__class__ast__inplace__names, name__class__ast__inplace__names, ...]
    // s[0] : stepName
    // s[1] : transform class
    // s[2] : ast (can be noop)
    // s[3] : inplace
    // s[4] : names
    ArrayList<Transform> steps = new ArrayList<>();
    for (String step : ass.steps) {
        String[] s = step.split("__");
        Class transformClass = Class.forName("water.rapids.transforms." + s[1]);
        Class[] constructorTypes = new Class[] { String.class, /*name*/
        String.class, /*ast*/
        boolean.class, /*inplace*/
        String[].class };
        Object[] constructorArgs = new Object[] { s[0], s[2], Boolean.valueOf(s[3]), s[4].equals("|") ? null : s[4].split("\\|") };
        steps.add((Transform) transformClass.getConstructor(constructorTypes).newInstance(constructorArgs));
    }
    Assembly assembly = new Assembly(Key.make("assembly_" + Key.make().toString()), steps.toArray(new Transform[steps.size()]));
    ass.result = new KeyV3.FrameKeyV3(assembly.fit((Frame) DKV.getGet(ass.frame.key()))._key);
    ass.assembly = new KeyV3.AssemblyKeyV3(assembly._key);
    DKV.put(assembly);
    return ass;
}
Also used : Frame(water.fvec.Frame) ArrayList(java.util.ArrayList) KeyV3(water.api.schemas3.KeyV3) Transform(water.rapids.transforms.Transform) Assembly(water.rapids.Assembly)

Example 3 with KeyV3

use of water.api.schemas3.KeyV3 in project h2o-3 by h2oai.

the class PojoUtils method copyProperties.

/**
   * Copy properties "of the same name" from one POJO to the other.  If the fields are
   * named consistently (both sides have fields named "_foo" and/or "bar") this acts like
   * Apache Commons PojoUtils.copyProperties(). If one side has leading underscores and
   * the other does not then the names are conformed according to the field_naming
   * parameter.
   *
   * @param dest Destination POJO
   * @param origin Origin POJO
   * @param field_naming Are the fields named consistently, or does one side have underscores?
   * @param skip_fields Array of origin or destination field names to skip
   * @param only_fields Array of origin or destination field names to include; ones not in this list will be skipped
   */
public static void copyProperties(Object dest, Object origin, FieldNaming field_naming, String[] skip_fields, String[] only_fields) {
    if (null == dest || null == origin)
        return;
    Field[] dest_fields = Weaver.getWovenFields(dest.getClass());
    Field[] orig_fields = Weaver.getWovenFields(origin.getClass());
    for (Field orig_field : orig_fields) {
        String origin_name = orig_field.getName();
        String dest_name = field_naming.toDest(origin_name);
        if (skip_fields != null && (ArrayUtils.contains(skip_fields, origin_name) || ArrayUtils.contains(skip_fields, dest_name)))
            continue;
        if (only_fields != null && !(ArrayUtils.contains(only_fields, origin_name) || ArrayUtils.contains(only_fields, dest_name)))
            continue;
        try {
            Field dest_field = null;
            for (Field fd : dest_fields) {
                if (fd.getName().equals(dest_name)) {
                    dest_field = fd;
                    break;
                }
            }
            if (dest_field != null) {
                dest_field.setAccessible(true);
                orig_field.setAccessible(true);
                // Log.info("PojoUtils.copyProperties, origin field: " + orig_field + "; destination field: " + dest_field);
                if (null == orig_field.get(origin)) {
                    //
                    // Assigning null to dest.
                    //
                    dest_field.set(dest, null);
                } else if (dest_field.getType().isArray() && orig_field.getType().isArray() && (dest_field.getType().getComponentType() != orig_field.getType().getComponentType())) {
                    // TODO: other types of arrays. . .
                    if (dest_field.getType().getComponentType() == double.class && orig_field.getType().getComponentType() == Double.class) {
                        //
                        // Assigning an Double[] to an double[]
                        //
                        double[] copy = (double[]) orig_field.get(origin);
                        dest_field.set(dest, copy);
                    } else if (dest_field.getType().getComponentType() == Double.class && orig_field.getType().getComponentType() == double.class) {
                        //
                        // Assigning an double[] to an Double[]
                        //
                        Double[] copy = (Double[]) orig_field.get(origin);
                        dest_field.set(dest, copy);
                    } else if (dest_field.getType().getComponentType() == int.class && orig_field.getType().getComponentType() == Integer.class) {
                        //
                        // Assigning an Integer[] to an int[]
                        //
                        int[] copy = (int[]) orig_field.get(origin);
                        dest_field.set(dest, copy);
                    } else if (dest_field.getType().getComponentType() == Integer.class && orig_field.getType().getComponentType() == int.class) {
                        //
                        // Assigning an int[] to an Integer[]
                        //
                        Integer[] copy = (Integer[]) orig_field.get(origin);
                        dest_field.set(dest, copy);
                    } else if (Schema.class.isAssignableFrom(dest_field.getType().getComponentType()) && (Schema.getImplClass((Class<? extends Schema>) dest_field.getType().getComponentType())).isAssignableFrom(orig_field.getType().getComponentType())) {
                        //
                        // Assigning an array of impl fields to an array of schema fields, e.g. a DeepLearningParameters[] into a DeepLearningParametersV2[]
                        //
                        Class dest_component_class = dest_field.getType().getComponentType();
                        // NOTE: there can be a race on the source array, so shallow copy it.
                        // If it has shrunk the elements might have dangling references.
                        Iced[] orig_array = (Iced[]) orig_field.get(origin);
                        int length = orig_array.length;
                        // Will null pad if it has shrunk since calling length
                        Iced[] orig_array_copy = Arrays.copyOf(orig_array, length);
                        Schema[] translation = (Schema[]) Array.newInstance(dest_component_class, length);
                        int version = ((Schema) dest).getSchemaVersion();
                        // Look up the schema for each element of the array; if not found fall back to the schema for the base class.
                        for (int i = 0; i < length; i++) {
                            Iced impl = orig_array_copy[i];
                            if (null == impl) {
                                // also can happen if the array shrank between .length and the copy
                                translation[i++] = null;
                            } else {
                                Schema s = null;
                                try {
                                    s = SchemaServer.schema(version, impl);
                                } catch (H2ONotFoundArgumentException e) {
                                    s = ((Schema) dest_field.getType().getComponentType().newInstance());
                                }
                                translation[i] = s.fillFromImpl(impl);
                            }
                        }
                        dest_field.set(dest, translation);
                    } else if (Schema.class.isAssignableFrom(orig_field.getType().getComponentType()) && Iced.class.isAssignableFrom(dest_field.getType().getComponentType())) {
                        //
                        // Assigning an array of schema fields to an array of impl fields, e.g. a DeepLearningParametersV2[] into a DeepLearningParameters[]
                        //
                        // We can't check against the actual impl class I, because we can't instantiate the schema base classes to get the impl class from an instance:
                        // dest_field.getType().getComponentType().isAssignableFrom(((Schema)f.getType().getComponentType().newInstance()).getImplClass())) {
                        Class dest_component_class = dest_field.getType().getComponentType();
                        Schema[] orig_array = (Schema[]) orig_field.get(origin);
                        int length = orig_array.length;
                        Schema[] orig_array_copy = Arrays.copyOf(orig_array, length);
                        Iced[] translation = (Iced[]) Array.newInstance(dest_component_class, length);
                        for (int i = 0; i < length; i++) {
                            Schema s = orig_array_copy[i];
                            translation[i] = s == null ? null : s.createAndFillImpl();
                        }
                        dest_field.set(dest, translation);
                    } else {
                        throw H2O.fail("Don't know how to cast an array of: " + orig_field.getType().getComponentType() + " to an array of: " + dest_field.getType().getComponentType());
                    }
                // end of array handling
                } else if (dest_field.getType() == Key.class && Keyed.class.isAssignableFrom(orig_field.getType())) {
                    //
                    // Assigning a Keyed (e.g., a Frame or Model) to a Key.
                    //
                    dest_field.set(dest, ((Keyed) orig_field.get(origin))._key);
                } else if (orig_field.getType() == Key.class && Keyed.class.isAssignableFrom(dest_field.getType())) {
                    //
                    // Assigning a Key (for e.g., a Frame or Model) to a Keyed (e.g., a Frame or Model).
                    //
                    Value v = DKV.get((Key) orig_field.get(origin));
                    dest_field.set(dest, (null == v ? null : v.get()));
                } else if (KeyV3.class.isAssignableFrom(dest_field.getType()) && Keyed.class.isAssignableFrom(orig_field.getType())) {
                    //
                    // Assigning a Keyed (e.g., a Frame or Model) to a KeyV1.
                    //
                    dest_field.set(dest, KeyV3.make(((Class<? extends KeyV3>) dest_field.getType()), ((Keyed) orig_field.get(origin))._key));
                } else if (KeyV3.class.isAssignableFrom(orig_field.getType()) && Keyed.class.isAssignableFrom(dest_field.getType())) {
                    //
                    // Assigning a KeyV1 (for e.g., a Frame or Model) to a Keyed (e.g., a Frame or Model).
                    //
                    KeyV3 k = (KeyV3) orig_field.get(origin);
                    Value v = DKV.get(Key.make(k.name));
                    dest_field.set(dest, (null == v ? null : v.get()));
                } else if (KeyV3.class.isAssignableFrom(dest_field.getType()) && Key.class.isAssignableFrom(orig_field.getType())) {
                    //
                    // Assigning a Key to a KeyV1.
                    //
                    dest_field.set(dest, KeyV3.make(((Class<? extends KeyV3>) dest_field.getType()), (Key) orig_field.get(origin)));
                } else if (KeyV3.class.isAssignableFrom(orig_field.getType()) && Key.class.isAssignableFrom(dest_field.getType())) {
                    //
                    // Assigning a KeyV1 to a Key.
                    //
                    KeyV3 k = (KeyV3) orig_field.get(origin);
                    dest_field.set(dest, (null == k.name ? null : Key.make(k.name)));
                } else if (dest_field.getType() == Pattern.class && String.class.isAssignableFrom(orig_field.getType())) {
                    //
                    // Assigning a String to a Pattern.
                    //
                    dest_field.set(dest, Pattern.compile((String) orig_field.get(origin)));
                } else if (orig_field.getType() == Pattern.class && String.class.isAssignableFrom(dest_field.getType())) {
                    //
                    // We are assigning a Pattern to a String.
                    //
                    dest_field.set(dest, orig_field.get(origin).toString());
                } else if (dest_field.getType() == FrameV3.ColSpecifierV3.class && String.class.isAssignableFrom(orig_field.getType())) {
                    //
                    // Assigning a String to a ColSpecifier.  Note that we currently support only the colname, not a frame name too.
                    //
                    dest_field.set(dest, new FrameV3.ColSpecifierV3((String) orig_field.get(origin)));
                } else if (orig_field.getType() == FrameV3.ColSpecifierV3.class && String.class.isAssignableFrom(dest_field.getType())) {
                    //
                    // We are assigning a ColSpecifierV2 to a String.  The column_name gets copied.
                    //
                    dest_field.set(dest, ((FrameV3.ColSpecifierV3) orig_field.get(origin)).column_name);
                } else if (Enum.class.isAssignableFrom(dest_field.getType()) && String.class.isAssignableFrom(orig_field.getType())) {
                    //
                    // Assigning a String into an enum field.
                    //
                    Class<Enum> dest_class = (Class<Enum>) dest_field.getType();
                    dest_field.set(dest, Enum.valueOf(dest_class, (String) orig_field.get(origin)));
                } else if (Enum.class.isAssignableFrom(orig_field.getType()) && String.class.isAssignableFrom(dest_field.getType())) {
                    //
                    // Assigning an enum field into a String.
                    //
                    Object o = orig_field.get(origin);
                    dest_field.set(dest, (o == null ? null : o.toString()));
                } else if (Schema.class.isAssignableFrom(dest_field.getType()) && Schema.getImplClass((Class<? extends Schema>) dest_field.getType()).isAssignableFrom(orig_field.getType())) {
                    //
                    // Assigning an impl field into a schema field, e.g. a DeepLearningParameters into a DeepLearningParametersV2.
                    //
                    dest_field.set(dest, SchemaServer.schema(/* ((Schema)dest).getSchemaVersion() TODO: remove HACK!! */
                    3, (Class<? extends Iced>) orig_field.get(origin).getClass()).fillFromImpl((Iced) orig_field.get(origin)));
                } else if (Schema.class.isAssignableFrom(orig_field.getType()) && Schema.getImplClass((Class<? extends Schema>) orig_field.getType()).isAssignableFrom(dest_field.getType())) {
                    //
                    // Assigning a schema field into an impl field, e.g. a DeepLearningParametersV2 into a DeepLearningParameters.
                    //
                    Schema s = ((Schema) orig_field.get(origin));
                    dest_field.set(dest, s.fillImpl(s.createImpl()));
                } else if ((Schema.class.isAssignableFrom(dest_field.getType()) && Key.class.isAssignableFrom(orig_field.getType()))) {
                    //
                    // Assigning an impl field fetched via a Key into a schema field, e.g. a DeepLearningParameters into a DeepLearningParametersV2.
                    // Note that unlike the cases above we don't know the type of the impl class until we fetch in the body of the if.
                    //
                    Key origin_key = (Key) orig_field.get(origin);
                    Value v = DKV.get(origin_key);
                    if (null == v || null == v.get()) {
                        dest_field.set(dest, null);
                    } else {
                        if (((Schema) dest_field.get(dest)).getImplClass().isAssignableFrom(v.get().getClass())) {
                            Schema s = ((Schema) dest_field.get(dest));
                            dest_field.set(dest, SchemaServer.schema(s.getSchemaVersion(), s.getImplClass()).fillFromImpl(v.get()));
                        } else {
                            Log.err("Can't fill Schema of type: " + dest_field.getType() + " with value of type: " + v.getClass() + " fetched from Key: " + origin_key);
                            dest_field.set(dest, null);
                        }
                    }
                } else if (Schema.class.isAssignableFrom(orig_field.getType()) && Keyed.class.isAssignableFrom(dest_field.getType())) {
                    //
                    // Assigning a schema field into a Key field, e.g. a DeepLearningV2 into a (DeepLearningParameters) key.
                    //
                    Schema s = ((Schema) orig_field.get(origin));
                    dest_field.set(dest, ((Keyed) s.fillImpl(s.createImpl()))._key);
                } else {
                    //
                    // Normal case: not doing any type conversion.
                    //
                    dest_field.set(dest, orig_field.get(origin));
                }
            }
        } catch (IllegalAccessException e) {
            Log.err("Illegal access exception trying to copy field: " + origin_name + " of class: " + origin.getClass() + " to field: " + dest_name + " of class: " + dest.getClass());
        } catch (InstantiationException e) {
            Log.err("Instantiation exception trying to copy field: " + origin_name + " of class: " + origin.getClass() + " to field: " + dest_name + " of class: " + dest.getClass());
        } catch (Exception e) {
            Log.err(e.getClass().getCanonicalName() + " Exception: " + origin_name + " of class: " + origin.getClass() + " to field: " + dest_name + " of class: " + dest.getClass());
            throw e;
        }
    }
}
Also used : Schema(water.api.Schema) KeyV3(water.api.schemas3.KeyV3) Field(java.lang.reflect.Field) Pattern(java.util.regex.Pattern) FrameV3(water.api.schemas3.FrameV3) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) H2ONotFoundArgumentException(water.exceptions.H2ONotFoundArgumentException) H2ONotFoundArgumentException(water.exceptions.H2ONotFoundArgumentException)

Example 4 with KeyV3

use of water.api.schemas3.KeyV3 in project h2o-3 by h2oai.

the class MakeGLMModelHandler method getDataInfoFrame.

// instead of adding a new endpoint, just put this stupid test functionality here
/** Get the expanded (interactions + offsets) dataset. Careful printing! Test only
  */
public DataInfoFrameV3 getDataInfoFrame(int version, DataInfoFrameV3 args) {
    Frame fr = DKV.getGet(args.frame.key());
    if (null == fr)
        throw new IllegalArgumentException("no frame found");
    args.result = new KeyV3.FrameKeyV3(oneHot(fr, args.interactions, args.use_all, args.standardize, args.interactions_only, true)._key);
    return args;
}
Also used : ValFrame(water.rapids.vals.ValFrame) KeyV3(water.api.schemas3.KeyV3)

Example 5 with KeyV3

use of water.api.schemas3.KeyV3 in project h2o-3 by h2oai.

the class MakeGLMModelHandler method computeGram.

public GramV3 computeGram(int v, GramV3 input) {
    if (DKV.get(input.X.key()) == null)
        throw new IllegalArgumentException("Frame " + input.X.key() + " does not exist.");
    Frame fr = input.X.key().get();
    Frame frcpy = new Frame(fr._names.clone(), fr.vecs().clone());
    String wname = null;
    Vec weight = null;
    if (input.W != null && !input.W.column_name.isEmpty()) {
        wname = input.W.column_name;
        if (fr.find(wname) == -1)
            throw new IllegalArgumentException("Did not find weight vector " + wname);
        weight = frcpy.remove(wname);
    }
    DataInfo dinfo = new DataInfo(frcpy, null, 0, input.use_all_factor_levels, input.standardize ? TransformType.STANDARDIZE : TransformType.NONE, TransformType.NONE, input.skip_missing, false, !input.skip_missing, /* weight */
    false, /* offset */
    false, /* fold */
    false, /* intercept */
    true);
    DKV.put(dinfo);
    if (weight != null)
        dinfo.setWeights(wname, weight);
    Gram.GramTask gt = new Gram.GramTask(null, dinfo, false, true).doAll(dinfo._adaptedFrame);
    double[][] gram = gt._gram.getXX();
    dinfo.remove();
    String[] names = water.util.ArrayUtils.append(dinfo.coefNames(), "Intercept");
    Vec[] vecs = new Vec[gram.length];
    Key[] keys = new VectorGroup().addVecs(vecs.length);
    for (int i = 0; i < vecs.length; ++i) vecs[i] = Vec.makeVec(gram[i], keys[i]);
    input.destination_frame = new KeyV3.FrameKeyV3();
    String keyname = input.X.key().toString();
    if (keyname.endsWith(".hex"))
        keyname = keyname.substring(0, keyname.lastIndexOf("."));
    keyname = keyname + "_gram";
    if (weight != null)
        keyname = keyname + "_" + wname;
    Key k = Key.make(keyname);
    if (DKV.get(k) != null) {
        int cnt = 0;
        while (cnt < 1000 && DKV.get(k = Key.make(keyname + "_" + cnt)) != null) cnt++;
        if (cnt == 1000)
            throw new IllegalArgumentException("unable to make unique key");
    }
    input.destination_frame.fillFromImpl(k);
    DKV.put(new Frame(k, names, vecs));
    return input;
}
Also used : DataInfo(hex.DataInfo) ValFrame(water.rapids.vals.ValFrame) KeyV3(water.api.schemas3.KeyV3) Gram(hex.gram.Gram) VectorGroup(water.fvec.Vec.VectorGroup) Key(water.Key)

Aggregations

KeyV3 (water.api.schemas3.KeyV3)10 H2OIllegalArgumentException (water.exceptions.H2OIllegalArgumentException)4 FrameV3 (water.api.schemas3.FrameV3)3 Frame (water.fvec.Frame)3 Field (java.lang.reflect.Field)2 Map (java.util.Map)2 JobV3 (water.api.schemas3.JobV3)2 ValFrame (water.rapids.vals.ValFrame)2 CreateFrame (hex.CreateFrame)1 DataInfo (hex.DataInfo)1 SplitFrame (hex.SplitFrame)1 Gram (hex.gram.Gram)1 Grid (hex.grid.Grid)1 Word2VecModel (hex.word2vec.Word2VecModel)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Properties (java.util.Properties)1