@Test public void testTarget() throws Exception { Map<String, Object> mb = ImmutableMap.<String, Object>builder() .put("inbound.column-names", ImmutableList.of("a", "e", "c")) .put("inbound.categorical-columns", ImmutableList.of("a", "c")) .put("inbound.target-column", "c") .build(); Config conf = overlayConfigOnDefault(mb); InboundSettings settings = InboundSettings.create(conf); assertEquals(2, settings.getTargetColumn().intValue()); }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { RDFGenerationManager generationManager = getGenerationManager(); Generation generation = generationManager.getCurrentGeneration(); if (generation == null) { response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); return; } InboundSettings inboundSettings = getInboundSettings(); TreeBasedClassifier forest = generation.getForest(); Map<Integer, BiMap<String, Integer>> columnToCategoryNameToIDMapping = generation.getColumnToCategoryNameToIDMapping(); int totalColumns = getTotalColumns(); for (CharSequence line : CharStreams.readLines(request.getReader())) { generationManager.append(line); String[] tokens = DelimitedDataUtils.decode(line); if (tokens.length != totalColumns) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Wrong column count"); return; } Feature target = null; Feature[] features = new Feature[totalColumns]; // Too big by 1 but makes math easier try { for (int col = 0; col < features.length; col++) { if (col == inboundSettings.getTargetColumn()) { target = buildFeature(col, tokens[col], columnToCategoryNameToIDMapping); features[col] = IgnoredFeature.INSTANCE; } else { features[col] = buildFeature(col, tokens[col], columnToCategoryNameToIDMapping); } } } catch (IllegalArgumentException iae) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad input line"); return; } Preconditions.checkNotNull(target); Example example = new Example(target, features); forest.update(example); } }
private Feature buildFeature( int columnNumber, String token, Map<Integer, BiMap<String, Integer>> columnToCategoryNameToIDMapping) { InboundSettings inboundSettings = getInboundSettings(); if (inboundSettings.isNumeric(columnNumber)) { return NumericFeature.forValue(Float.parseFloat(token)); } if (inboundSettings.isCategorical(columnNumber)) { return CategoricalFeature.forValue( ClassifyServlet.categoricalFromString( columnNumber, token, columnToCategoryNameToIDMapping)); } return IgnoredFeature.INSTANCE; }
@Test public void testContinuousSettings() throws Exception { Map<String, Object> mb = ImmutableMap.<String, Object>builder() .put("inbound.column-names", ImmutableList.of("a", "b", "c", "d", "e")) .put("inbound.id-columns", ImmutableList.of("a")) .put("inbound.numeric-columns", ImmutableList.of(1, "e")) .put("inbound.ignored-columns", ImmutableList.of("c")) .build(); Config conf = overlayConfigOnDefault(mb); InboundSettings settings = InboundSettings.create(conf); assertEquals(ImmutableList.of(0), ImmutableList.copyOf(settings.getIdColumns())); assertEquals(ImmutableList.of(3), ImmutableList.copyOf(settings.getCategoricalColumns())); assertEquals(ImmutableList.of(1, 4), ImmutableList.copyOf(settings.getNumericColumns())); assertEquals(ImmutableList.of(2), ImmutableList.copyOf(settings.getIgnoredColumns())); assertNull(settings.getTargetColumn()); /* Spec spec = settings.getSpec(); Assert.assertEquals(5, spec.size()); Assert.assertEquals(DataType.STRING, spec.getField(0).spec().getDataType()); Assert.assertEquals(DataType.DOUBLE, spec.getField(1).spec().getDataType()); Assert.assertEquals(DataType.STRING, spec.getField(2).spec().getDataType()); */ }
@Test public void testDefaults() throws Exception { Map<String, Object> mb = ImmutableMap.<String, Object>builder() .put("inbound.categorical-columns", ImmutableList.of()) .build(); Config conf = overlayConfigOnDefault(mb); InboundSettings settings = InboundSettings.create(conf); assertTrue(settings.getIdColumns().isEmpty()); assertTrue(settings.getIgnoredColumns().isEmpty()); // assertNull(settings.getSpec()); assertTrue(settings.getCategoricalColumns().isEmpty()); assertTrue(settings.getNumericColumns().isEmpty()); assertEquals(Integer.valueOf(1729), settings.getLookupFunction().apply(1729)); assertNull(settings.getTargetColumn()); }
@Test(expected = IllegalArgumentException.class) public void testUnknownColumn() { InboundSettings settings = InboundSettings.create(ConfigUtils.getDefaultConfig()); settings.getLookupFunction().apply("foo"); }
@Test(expected = IllegalArgumentException.class) public void testNoSetting() { InboundSettings.create(ConfigUtils.getDefaultConfig()); }