@Test public void testEncodersOnNulls() { final NumericValueSchema numericValueSchema = new NumericValueSchema(true); final EncodingHelper.SerializableEncoder numericEncoder = EncodingHelper.encoderForField(numericValueSchema); assertThat(numericEncoder.apply(null)) .as("The result of applying the numeric encoder to a null") .isEqualTo(Double.NaN); final StringValueSchema stringValueSchema = new StringValueSchema(true); final EncodingHelper.SerializableEncoder stringEncoder = EncodingHelper.encoderForField(stringValueSchema); assertThat(stringEncoder.apply(null)) .as("The result of applying the string encoder to a null") .isEqualTo(null); final CategoricalValueSchema categoricalSchema = new CategoricalValueSchema(true, ImmutableSet.of()); final EncodingHelper.SerializableEncoder categoricalEncoder = EncodingHelper.encoderForField(categoricalSchema); assertThat(categoricalEncoder.apply(null)) .as("The result of applying the categorical encoder to a null") .isEqualTo(Double.NaN); }
public static SerializableEncoder encoderForField(final AbstractValueSchema abstractValueSchema) { if (abstractValueSchema instanceof NumericValueSchema) { final SerializableEncoder function = value -> Double.valueOf(value.toString()); return value -> (Double) handleNullOrFailed(value, function, DEFAULT_NUMERIC_VALUE); } else if (abstractValueSchema instanceof CategoricalValueSchema) { final CategoricalValueSchema asCategorical = (CategoricalValueSchema) abstractValueSchema; final Map<String, Double> conversionMap = conversionMap(asCategorical, index -> (double) index); final SerializableEncoder function = value -> conversionMap.getOrDefault(value.toString(), DEFAULT_CATEGORICAL_VALUE); return value -> (Double) handleNullOrFailed(value, function, DEFAULT_CATEGORICAL_VALUE); } else { final SerializableEncoder function = s -> s; return value -> (String) handleNullOrFailed(value, function, DEFAULT_STRING_VALUE); } }
EncodingHelper implements Serializable { public static SerializableEncoder encoderForField(final AbstractValueSchema abstractValueSchema) { if (abstractValueSchema instanceof NumericValueSchema) { final SerializableEncoder function = value -> Double.valueOf(value.toString()); return value -> (Double) handleNullOrFailed(value, function, DEFAULT_NUMERIC_VALUE); } else if (abstractValueSchema instanceof CategoricalValueSchema) { final CategoricalValueSchema asCategorical = (CategoricalValueSchema) abstractValueSchema; final Map<String, Double> conversionMap = conversionMap(asCategorical, index -> (double) index); final SerializableEncoder function = value -> conversionMap.getOrDefault(value.toString(), DEFAULT_CATEGORICAL_VALUE); return value -> (Double) handleNullOrFailed(value, function, DEFAULT_CATEGORICAL_VALUE); } else { final SerializableEncoder function = s -> s; return value -> (String) handleNullOrFailed(value, function, DEFAULT_STRING_VALUE); } } }
EncodingHelper implements Serializable { public static SerializableEncoder encoderForField(final AbstractValueSchema abstractValueSchema) { if (abstractValueSchema instanceof NumericValueSchema) { final SerializableEncoder function = value -> Double.valueOf(value.toString()); return value -> (Double) handleNullOrFailed(value, function, DEFAULT_NUMERIC_VALUE); } else if (abstractValueSchema instanceof CategoricalValueSchema) { final CategoricalValueSchema asCategorical = (CategoricalValueSchema) abstractValueSchema; final Map<String, Double> conversionMap = conversionMap(asCategorical, index -> (double) index); final SerializableEncoder function = value -> conversionMap.getOrDefault(value.toString(), DEFAULT_CATEGORICAL_VALUE); return value -> (Double) handleNullOrFailed(value, function, DEFAULT_CATEGORICAL_VALUE); } else { final SerializableEncoder function = s -> s; return value -> (String) handleNullOrFailed(value, function, DEFAULT_STRING_VALUE); } } EncodingHelper(final DatasetSchema schema); }
EncodingHelper implements Serializable { public static SerializableEncoder encoderForField(final AbstractValueSchema abstractValueSchema) { if (abstractValueSchema instanceof NumericValueSchema) { final SerializableEncoder function = value -> Double.valueOf(value.toString()); return value -> (Double) handleNullOrFailed(value, function, DEFAULT_NUMERIC_VALUE); } else if (abstractValueSchema instanceof CategoricalValueSchema) { final CategoricalValueSchema asCategorical = (CategoricalValueSchema) abstractValueSchema; final Map<String, Double> conversionMap = conversionMap(asCategorical, index -> (double) index); final SerializableEncoder function = value -> conversionMap.getOrDefault(value.toString(), DEFAULT_CATEGORICAL_VALUE); return value -> (Double) handleNullOrFailed(value, function, DEFAULT_CATEGORICAL_VALUE); } else { final SerializableEncoder function = s -> s; return value -> (String) handleNullOrFailed(value, function, DEFAULT_STRING_VALUE); } } EncodingHelper(final DatasetSchema schema); static SerializableEncoder encoderForField(final AbstractValueSchema abstractValueSchema); static String decodeDoubleToCategory(final double doubleValue,
                                                final CategoricalValueSchema categoricalValueSchema); static Function<Serializable, Integer> classToIndexConverter(final CategoricalValueSchema targetVariableSchema); Serializable encode(final Serializable value, final int index); int numberFields(); }
EncodingHelper implements Serializable { public static SerializableEncoder encoderForField(final AbstractValueSchema abstractValueSchema) { if (abstractValueSchema instanceof NumericValueSchema) { final SerializableEncoder function = value -> Double.valueOf(value.toString()); return value -> (Double) handleNullOrFailed(value, function, DEFAULT_NUMERIC_VALUE); } else if (abstractValueSchema instanceof CategoricalValueSchema) { final CategoricalValueSchema asCategorical = (CategoricalValueSchema) abstractValueSchema; final Map<String, Double> conversionMap = conversionMap(asCategorical, index -> (double) index); final SerializableEncoder function = value -> conversionMap.getOrDefault(value.toString(), DEFAULT_CATEGORICAL_VALUE); return value -> (Double) handleNullOrFailed(value, function, DEFAULT_CATEGORICAL_VALUE); } else { final SerializableEncoder function = s -> s; return value -> (String) handleNullOrFailed(value, function, DEFAULT_STRING_VALUE); } } EncodingHelper(final DatasetSchema schema); static SerializableEncoder encoderForField(final AbstractValueSchema abstractValueSchema); static String decodeDoubleToCategory(final double doubleValue,
                                                final CategoricalValueSchema categoricalValueSchema); static Function<Serializable, Integer> classToIndexConverter(final CategoricalValueSchema targetVariableSchema); Serializable encode(final Serializable value, final int index); int numberFields();  }