use of water.api.schemas3.FrameV3 in project h2o-3 by h2oai.
the class FindHandler method find.
// called through reflection by RequestServer
@SuppressWarnings("unused")
public FindV3 find(int version, FindV3 find) {
Frame frame = find.key._fr;
// Peel out an optional column; restrict to this column
if (find.column != null) {
Vec vec = frame.vec(find.column);
if (vec == null)
throw new H2OColumnNotFoundArgumentException("column", frame, find.column);
find.key = new FrameV3(new Frame(new String[] { find.column }, new Vec[] { vec }));
}
// Convert the search string into a column-specific flavor
Vec[] vecs = frame.vecs();
double[] ds = new double[vecs.length];
for (int i = 0; i < vecs.length; i++) {
if (vecs[i].isCategorical()) {
int idx = ArrayUtils.find(vecs[i].domain(), find.match);
if (idx == -1 && vecs.length == 1)
throw new H2OCategoricalLevelNotFoundArgumentException("match", find.match, frame._key.toString(), frame.name(i));
ds[i] = idx;
} else if (vecs[i].isUUID()) {
throw H2O.unimpl();
} else if (vecs[i].isString()) {
throw H2O.unimpl();
} else if (vecs[i].isTime()) {
throw H2O.unimpl();
} else {
try {
ds[i] = find.match == null ? Double.NaN : Double.parseDouble(find.match);
} catch (NumberFormatException e) {
if (vecs.length == 1) {
// There's only one Vec and it's a numeric Vec and our search string isn't a number
IcedHashMapGeneric.IcedHashMapStringObject values = new IcedHashMapGeneric.IcedHashMapStringObject();
String msg = "Frame: " + frame._key.toString() + " as only one column, it is numeric, and the find pattern is not numeric: " + find.match;
values.put("frame_name", frame._key.toString());
values.put("column_name", frame.name(i));
values.put("pattern", find.match);
throw new H2OIllegalArgumentException(msg, msg, values);
}
// Do not match
ds[i] = Double.longBitsToDouble(0xcafebabe);
}
}
}
Find f = new Find(find.row, ds).doAll(frame);
find.prev = f._prev;
find.next = f._next == Long.MAX_VALUE ? -1 : f._next;
return find;
}
use of water.api.schemas3.FrameV3 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;
}
}
}
use of water.api.schemas3.FrameV3 in project h2o-3 by h2oai.
the class PojoUtils method fillFromMap.
/**
* Fill the fields of an Object from the corresponding fields in a Map.
* @see #fillFromJson(Object, String)
*/
private static Object fillFromMap(Object o, Map<String, Object> setFields) {
for (String key : setFields.keySet()) {
// TODO: doesn't handle arrays yet!
Object value = setFields.get(key);
if (value instanceof Map) {
// handle nested objects
try {
Field f = PojoUtils.getFieldEvenInherited(o, key);
f.setAccessible(true);
// In some cases, the target object has children already (e.g., defaults), while in other cases it doesn't.
if (null == f.get(o))
f.set(o, f.getType().newInstance());
fillFromMap(f.get(o), (Map<String, Object>) value);
} catch (NoSuchFieldException e) {
throw new IllegalArgumentException("Field not found: '" + key + "' on object " + o);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Cannot get value of the field: '" + key + "' on object " + o);
} catch (InstantiationException e) {
try {
throw new IllegalArgumentException("Cannot create new child object of type: " + PojoUtils.getFieldEvenInherited(o, key).getClass().getCanonicalName() + " for field: '" + key + "' on object " + o);
} catch (NoSuchFieldException ee) {
// Can't happen: we've already checked for this.
throw new IllegalArgumentException("Cannot create new child object of type for field: '" + key + "' on object " + o);
}
}
} else {
// this now-redundant code:
try {
Field f = PojoUtils.getFieldEvenInherited(o, key);
f.setAccessible(true);
if (f.getType().isAssignableFrom(FrameV3.ColSpecifierV3.class)) {
setField(o, key, new FrameV3.ColSpecifierV3((String) value));
} else if (KeyV3.class.isAssignableFrom(f.getType())) {
setField(o, key, KeyV3.make((Class<? extends KeyV3>) f.getType(), Key.make((String) value)));
} else {
setField(o, key, value);
}
} catch (NoSuchFieldException e) {
throw new IllegalArgumentException("Field not found: '" + key + "' on object " + o);
}
}
// else not a nested object
}
// for all fields in the map
return o;
}
use of water.api.schemas3.FrameV3 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());
}
use of water.api.schemas3.FrameV3 in project h2o-3 by h2oai.
the class FrameV3 method fillFromImpl.
public FrameV3 fillFromImpl(Frame f, long row_offset, int row_count, int column_offset, int column_count) {
// 100 rows by default
if (row_count == 0)
row_count = 100;
// full width by default
if (column_count == 0)
column_count = f.numCols() - column_offset;
row_count = (int) Math.min(row_count, row_offset + f.numRows());
column_count = Math.min(column_count, column_offset + f.numCols());
this.frame_id = new FrameKeyV3(f._key);
this.checksum = f.checksum();
this.byte_size = f.byteSize();
this.row_offset = row_offset;
this.rows = f.numRows();
this.num_columns = f.numCols();
this.row_count = row_count;
this.total_column_count = f.numCols();
this.column_offset = column_offset;
this.column_count = column_count;
this.columns = new ColV3[column_count];
Vec[] vecs = f.vecs();
Futures fs = new Futures();
// NOTE: SKIP deleted Vecs! The columns entry will be null for deleted Vecs.
for (int i = 0; i < column_count; i++) if (null == DKV.get(vecs[column_offset + i]._key))
Log.warn("For Frame: " + f._key + ", Vec number: " + (column_offset + i) + " (" + f.name((column_offset + i)) + ") is missing; not returning it.");
else
vecs[column_offset + i].startRollupStats(fs);
for (int i = 0; i < column_count; i++) if (null == DKV.get(vecs[column_offset + i]._key))
Log.warn("For Frame: " + f._key + ", Vec number: " + (column_offset + i) + " (" + f.name((column_offset + i)) + ") is missing; not returning it.");
else
columns[i] = new ColV3(f._names[column_offset + i], vecs[column_offset + i], this.row_offset, this.row_count);
fs.blockForPending();
this.is_text = f.numCols() == 1 && vecs[0] instanceof ByteVec;
this.default_percentiles = Vec.PERCENTILES;
ChunkSummary cs = FrameUtils.chunkSummary(f);
this.chunk_summary = new TwoDimTableV3(cs.toTwoDimTableChunkTypes());
this.distribution_summary = new TwoDimTableV3(cs.toTwoDimTableDistribution());
this._fr = f;
return this;
}
Aggregations