private Action makeLineChart(Field x, Field y) { // If there is close to one data point per x coordinate just use lines if (x.numericProperty("unique") > 0.95 * x.numericProperty("validNumeric")) return get("line").apply(x, y); // Otherwise show lines and points return get("lineWithPoints").apply(x, y); }
private boolean goodForWordle(Field f) { if (!f.preferCategorical()) return false; if (f.numericProperty("unique") > 100 || f.numericProperty("unique") < 7) return false; // Too long names are not good for (Object c : f.categories()) if (c.toString().length() > 20) return false; return true; }
private boolean allShowExtent(Field[] fields) { // Categorical and numeric fields both show elements as extents on the axis for (Field field : fields) { if (field.isNumeric() && !field.isBinned()) return false; } return true; }
private String asString(NumericScale details, Field field) { String s = details.type + " : " + field.format(details.min) + " " + field.format(details.max) + " : "; for (int i = 0; i < details.divisions.length; i++) s += "|" + field.format(details.divisions[i]); return s; }
@Test public void testMakingField() { Field f = Data.makeColumnField("a", "label", new Object[] {"1", "2", "a", "2", null, 1}); assertEquals("a", f.name); assertEquals("label", f.label); assertEquals(6, f.rowCount()); assertEquals(5, f.valid()); }
private void setLocations( ElementDefinition.ElementDimensionDefinition dim, String dimName, Field[] fields, boolean categorical) { String scaleName = "scale_" + dimName; if (fields.length == 0) { // There are no fields -- we have a notional [0,1] extent, so use the center of that dim.center = "function() { return " + scaleName + "(0.5) }"; dim.left = "function() { return " + scaleName + "(0) }"; dim.right = "function() { return " + scaleName + "(1) }"; } else if (fields.length == 1) { Field field = fields[0]; // The single field String dataFunction = D3Util.writeCall(field); // A call to that field using the datum 'd' if (isRange(field)) { // This is a range field, but we have not been asked to show both ends, // so we use the difference between the top and bottom dim.center = "function(d) { return " + scaleName + "(" + dataFunction + ".extent()) }"; // Left and Right are not defined } else if (field.isBinned() && !categorical) { // A Binned value on a non-categorical axes dim.center = "function(d) { return " + scaleName + "(" + dataFunction + ".mid) }"; dim.left = "function(d) { return " + scaleName + "(" + dataFunction + ".low) }"; dim.right = "function(d) { return " + scaleName + "(" + dataFunction + ".high) }"; } else { // Nothing unusual -- just define the center dim.center = "function(d) { return " + scaleName + "(" + dataFunction + ") }"; } } else { // The dimension contains two fields: a range String lowDataFunc = D3Util.writeCall(fields[0]); // A call to the low field using the datum 'd' String highDataFunc = D3Util.writeCall(fields[1]); // A call to the high field using the datum 'd' // When one of the fields is a range, use the outermost value of that if (isRange(fields[0])) lowDataFunc += ".low"; if (isRange(fields[1])) highDataFunc += ".high"; dim.left = "function(d) { return " + scaleName + "(" + lowDataFunc + ") }"; dim.right = "function(d) { return " + scaleName + "(" + highDataFunc + ") }"; dim.center = "function(d) { return " + scaleName + "( (" + highDataFunc + " + " + lowDataFunc + " )/2) }"; } }
@Test public void testDateConversionForBadDates() { Object[] data = new Object[1000]; for (int i = 0; i < data.length; i++) data[i] = i % 10 == 0 ? "name" : "-"; Field f = Data.makeColumnField("a", "label", data); Field g = Data.toDate(f); assertNotEquals(f, g); assertEquals("a", g.name); assertEquals("label", g.label); assertEquals(1000, g.rowCount()); assertEquals(0, g.valid()); }
private void setProperties(String f, Field to, Field src) { // Nothing to set for a list if (f.equals("list")) { // Need to keep the date format to.copyProperties(src, "dateFormat"); to.set("list", true); } else if (f.equals("count") || f.equals("percent") || f.equals("valid") || f.equals("unique")) // These are numeric, but do not preserve properties to.setNumeric(); else // All properties copy over Fields.copyBaseProperties(src, to); }
@Test public void testNumericConversion() { Field f = Data.makeColumnField("a", "label", new Object[] {"1", "2", "a", "2", null, 1}); Field g = Data.toNumeric(f); assertNotEquals(f, g); assertEquals("a", g.name); assertEquals("label", g.label); assertEquals(6, g.rowCount()); assertEquals(4, g.valid()); // Should not change assertEquals(g, Data.toNumeric(g)); }
@Test public void testTiedValues() { Field f = Data.makeColumnField("a", "label", new Object[] {200, 100, 400, 500, 100}); Field g = Data.makeColumnField("a", "label", new Object[] {200, 100, 400, 500, 600}); Field h = Data.makeColumnField("a", "label", new Object[] {200, 100, 400, 100, 200}); Field i = Data.makeColumnField("a", "label", new Object[] {100, 100, 100, 500, 500}); Field j = Data.makeColumnField("a", "label", new Object[] {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 3, 4}); assertEquals(100, f.numericProperty("mode"), 0.01); assertEquals(400, g.numericProperty("mode"), 0.01); assertEquals(100, h.numericProperty("mode"), 0.01); assertEquals(100, i.numericProperty("mode"), 0.01); assertEquals(3, j.numericProperty("mode"), 0.01); }
@Test public void testPadding() { Field a = Data.toNumeric(Data.makeColumnField("a", "label", new Object[] {100, 200, 120, 200, 3100})); a.set("transform", "linear"); Assert.assertEquals( "linear : 70 3130 : |500|1000|1500|2000|2500|3000", asString( NumericScale.makeLinearScale(a, false, 0.0, new double[] {0.01, 0.01}, 5, false), a)); // Does not pad past zero Assert.assertEquals( "linear : -200 3400 : |0|1000|2000|3000", asString( NumericScale.makeLinearScale(a, false, 0.0, new double[] {0.1, 0.1}, 5, false), a)); }
@Test public void testProperties() { Field f = Data.makeColumnField("a", "label", new Object[0]); assertEquals(null, f.property("xyz")); f.set("xyz", "12"); assertEquals("12", f.stringProperty("xyz")); assertEquals(12.00001, f.numericProperty("xyz"), 0.001); f.set("abc", false); assertEquals(Boolean.FALSE, f.property("abc")); f.set("abc", true); assertEquals(Boolean.TRUE, f.property("abc")); }
@Test public void testPermute() { Field base = Data.makeIndexingField("a", "b", 10); Field a = Data.permute(base, new int[] {0, 0, 0, 1, 1, 2, 2, 3}, false); assertEquals(true, a.hasProperty("numeric")); assertEquals("a", a.name); assertEquals("b", a.label); assertEquals(8, a.rowCount()); assertEquals(8, a.getNumericProperty("valid"), 0.001); assertEquals(8, a.getNumericProperty("validNumeric"), 0.001); assertEquals(4, a.getNumericProperty("unique"), 0.001); assertEquals((1 + 1 + 1 + 2 + 2 + 3 + 3 + 4) / 8.0, a.getNumericProperty("mean"), 0.001); assertEquals(1, a.value(1)); assertEquals(1, a.value(2)); assertEquals(4, a.value(7)); assertEquals("1, 2, 3, 4", Data.join(a.categories())); }
Field(String name, String label, Provider provider, Field base) { this.name = name; this.label = label == null ? name : label; this.provider = provider; // Information is provided in the base field if (base != null) { if (provider == null) { // Ensure that the base field has everything calculated because we cannot calculate lazily // later base.makeNominalStats(); base.makeNumericStats(); base.makeDateStats(); } copyPropertiesFrom(base); } }
private Action chooseBivariate(Field x, Field y) { // Dates need line charts if (x.isDate() && !y.isDate() && !y.preferCategorical()) return makeLineChart(x, y); if (y.isDate() && !x.isDate() && !x.preferCategorical()) return makeLineChart(y, x); // Heatmaps if either is categorical if (x.preferCategorical() || y.preferCategorical()) return get("heatmap").apply(orderByCoarseness(x, y)); // Default to scatter return get("scatter").apply(orderByCoarseness(x, y)); }
@Test @JSTranslation(ignore = true) public void testJavaDates() { long feb_1_1970 = 60907593600000L; Field f = Data.makeColumnField( "test", "dates", new Object[] { new Date(feb_1_1970), new Date(feb_1_1970 + 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 2 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 3 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 4 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 5 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 6 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 7 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 8 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 9 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 10 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 20 * 1000 * 60 * 60 * 24) }); f.set("numeric", true); f.set("date", true); assertTrue(f.isDate()); assertEquals("Feb 1 00:00", f.valueFormatted(0)); assertEquals("Feb 2 00:00", f.valueFormatted(1)); assertEquals("Feb 3 00:00", f.valueFormatted(2)); assertEquals("Feb 11 00:00", f.valueFormatted(10)); assertEquals("Feb 21 00:00", f.valueFormatted(11)); }
@Test public void testDateConversion() { Field f = Data.makeColumnField( "a", "label", new Object[] {"Jan 4, 1980", "Jan 4, 1988", "a", "2", null, "Jan 9, 1978"}); Field g = Data.toDate(f, null); assertNotEquals(f, g); assertEquals("a", g.name); assertEquals("label", g.label); assertEquals(6, g.rowCount()); assertEquals(3, g.valid()); // Should not change assertEquals(g, Data.toDate(g, null)); }
@Test public void testLinearScale() { double[] pad = {0, 0}; Field a = Data.makeColumnField("a", "label", new Object[] {2, 4, 7, 120, 45, 120, 200, 3345}); a = Data.toNumeric(a); a.set("transform", "linear"); Assert.assertEquals( "linear : 2 3345 : |500|1000|1500|2000|2500|3000", asString(NumericScale.makeLinearScale(a, false, 0.0, pad, 5, false), a)); Assert.assertEquals( "linear : 2 3345 : |200|400|600|800|1000|1200|1400|1600|1800|2000|2200|2400|2600|2800|3000|3200", asString(NumericScale.makeLinearScale(a, false, 0.0, pad, 15, false), a)); Assert.assertEquals( "linear : 2 3345 : |1000|2000|3000", asString(NumericScale.makeLinearScale(a, false, 0.0, pad, 2, false), a)); Assert.assertEquals( "linear : 2 3345 : |2000", asString(NumericScale.makeLinearScale(a, false, 0.0, pad, 1, false), a)); Assert.assertEquals( "linear : 0 3345 : |0|500|1000|1500|2000|2500|3000", asString(NumericScale.makeLinearScale(a, false, 0.01, pad, 7, false), a)); Assert.assertEquals( "linear : 0 3500 : |0|500|1000|1500|2000|2500|3000|3500", asString(NumericScale.makeLinearScale(a, true, 0.0, pad, 7, false), a)); a = Data.makeColumnField("a", "label", new Object[] {-22.2, -22.201, -22.9, -22.7}); a = Data.toNumeric(a); Assert.assertEquals( "linear : -23 -22 : |-23|-22.8|-22.6|-22.4|-22.2|-22", asString(Auto.makeNumericScale(a, true, pad, 0.0, 5, false), a)); Assert.assertEquals( "linear : -22.9 -22.2 : |-22.8|-22.6|-22.4|-22.2", asString(NumericScale.makeLinearScale(a, false, 0.0, pad, 4, false), a)); Assert.assertEquals( "linear : -23 -22 : |-23|-22.5|-22", asString(Auto.makeNumericScale(a, true, pad, 0.0, 4, false), a)); Assert.assertEquals( "linear : -23 -22 : |-23|-22.5|-22", asString(Auto.makeNumericScale(a, true, pad, 0.95, 4, false), a)); Assert.assertEquals( "linear : -30 0 : |-30|-20|-10|0", asString(NumericScale.makeLinearScale(a, true, 0.99, pad, 4, false), a)); }
@Test public void testIndexing() { Field a = Data.makeIndexingField("a", "b", 30); assertEquals("a", a.name); assertEquals("b", a.label); assertEquals(30, a.rowCount()); assertEquals(30, a.getNumericProperty("valid"), 0.001); assertEquals(30, a.getNumericProperty("validNumeric"), 0.001); assertEquals(30, a.getNumericProperty("unique"), 0.001); assertEquals(15.5, a.getNumericProperty("mean"), 0.001); assertEquals(3, a.value(2)); assertEquals(8, a.value(7)); assertEquals(true, a.hasProperty("numeric")); }
@Test public void testConstant() { Field a = Data.makeConstantField("a", "b", "foo", 50); assertEquals("a", a.name); assertEquals("b", a.label); assertEquals(50, a.rowCount()); assertEquals(50, a.getNumericProperty("valid"), 0.001); assertEquals(0, a.getNumericProperty("validNumeric"), 0.001); assertEquals(1, a.getNumericProperty("unique"), 0.001); assertEquals("foo", a.value(2)); assertEquals(false, a.hasProperty("numeric")); }
@Test public void testDateScaleHoursOrLess() { Field a; double[] pad = {0, 0}; a = Data.toDate(Data.makeColumnField("a", "label", new Object[] {"09:45:22", "09:45:24"})); Assert.assertEquals( "date : 09:45:22 09:45:24 : |09:45:22|09:45:23|09:45:24", asString(NumericScale.makeDateScale(a, false, pad, 5), a)); a = Data.toDate(Data.makeColumnField("a", "label", new Object[] {"09:45:22", "09:45:54"})); assertEquals(0.40650462963, a.min(), 1e-9); assertEquals(0.406875, a.max(), 1e-9); Assert.assertEquals( "date : 09:45:22 09:45:54 : |09:45:30|09:45:40|09:45:50", asString(NumericScale.makeDateScale(a, false, pad, 5), a)); a = Data.toDate(Data.makeColumnField("a", "label", new Object[] {"09:45:22", "09:45:54"})); Assert.assertEquals( "date : 09:45:20 09:46:00 : |09:45:20|09:45:30|09:45:40|09:45:50|09:46:00", asString(NumericScale.makeDateScale(a, true, pad, 5), a)); a = Data.toDate(Data.makeColumnField("a", "label", new Object[] {"09:45:22", "10:01:54"})); Assert.assertEquals( "date : 09:45 10:05 : |09:45|09:50|09:55|10:00|10:05", asString(NumericScale.makeDateScale(a, true, pad, 5), a)); a = Data.toDate(Data.makeColumnField("a", "label", new Object[] {"09:45:22", "12:01:54"})); Assert.assertEquals( "date : 09:30 12:30 : |09:30|10:00|10:30|11:00|11:30|12:00|12:30", asString(NumericScale.makeDateScale(a, true, pad, 5), a)); a = Data.toDate(Data.makeColumnField("a", "label", new Object[] {"09:45:22", "23:01:54"})); Assert.assertEquals( "date : 09:00 00:00 : |09:00|12:00|15:00|18:00|21:00|00:00", asString(NumericScale.makeDateScale(a, true, pad, 5), a)); }
@Test public void testDate() { Field a; a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 1, 1970", "January 1, 1970 00:00:01"})); assertEquals(0.0, a.min(), 1e-9); assertEquals(1.0 / 3600 / 24, a.max(), 1e-9); assertEquals(DateUnit.second, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 1, 1970", "January 1, 1970 00:04:00"})); assertEquals(0.0, a.min(), 1e-9); assertEquals(4.0 / 60 / 24, a.max(), 1e-9); assertEquals(DateUnit.minute, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 1, 1970", "January 1, 1970 00:06:00"})); assertEquals(0.0, a.min(), 1e-9); assertEquals(6.0 / 60 / 24, a.max(), 1e-9); assertEquals(DateUnit.minute, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 1, 1970", "January 1, 1970 00:45:00"})); assertEquals(0.0, a.min(), 1e-9); assertEquals(45.0 / 60 / 24, a.max(), 1e-9); assertEquals(DateUnit.minute, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 1, 1970", "January 1, 1970 04:59:00"})); assertEquals(0.0, a.min(), 1e-9); assertEquals(DateUnit.hour, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 1, 1970", "January 1, 1970 05:00:00"})); assertEquals(0.0, a.min(), 1e-9); assertEquals(5.0 / 24, a.max(), 1e-9); assertEquals(DateUnit.hour, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 1, 1970", "January 4, 1970"})); assertEquals(0.0, a.min(), 1e-9); assertEquals(3.0, a.max(), 1e-9); assertEquals(DateUnit.day, a.property("dateUnit")); // Note that only three days are needed to get days as a unit -- this an exception to the usual // "5 ticks" rule a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 10, 1970", "January 2, 1970"})); assertEquals(1.0, a.min(), 1e-9); assertEquals(9.0, a.max(), 1e-9); assertEquals(DateUnit.day, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField("a", "label", new Object[] {"January 10, 1970", "March 2, 1970"})); assertEquals(9.0, a.min(), 1e-9); assertEquals(60.0, a.max(), 1e-9); assertEquals(DateUnit.week, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 10, 1970", "December 31, 1970"})); assertEquals(9.0, a.min(), 1e-9); assertEquals(364.0, a.max(), 1e-9); assertEquals(DateUnit.quarter, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 10, 1970", "January 10, 1972"})); assertEquals(9.0, a.min(), 1e-9); assertEquals(2 * 365 + 9, a.max(), 1e-9); assertEquals(DateUnit.quarter, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 10, 1970", "January 10, 1974"})); assertEquals(9.0, a.min(), 1e-9); assertEquals(4 * 365 + 1 + 9, a.max(), 1e-9); assertEquals(DateUnit.year, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 10, 1970", "January 10, 1976"})); assertEquals(9.0, a.min(), 1e-9); assertEquals(6 * 365 + 1 + 9, a.max(), 1e-9); assertEquals(DateUnit.year, a.property("dateUnit")); a = Data.toDate( Data.makeColumnField( "a", "label", new Object[] {"January 10, 1970", "January 10, 2030"})); assertEquals(9.0, a.min(), 1e-9); assertEquals(60 * 365 + 15 + 9, a.max(), 1e-9); assertEquals(DateUnit.decade, a.property("dateUnit")); a = Data.toDate( Data.toDate( Data.makeColumnField("a", "label", new Object[] {"09:45:22", "09:45:24"}), null)); assertEquals((9 + 45 / 60.0 + 22 / 3600.0) / 24.0, a.min(), 1e-9); assertEquals((9 + 45 / 60.0 + 24 / 3600.0) / 24.0, a.max(), 1e-9); assertEquals(DateUnit.second, a.property("dateUnit")); }
@Test public void testChooseTransform() { Field a = Data.makeColumnField("a", "label", new Object[] {100, 200, 300, 400, 500, 600}); Field b = Data.makeColumnField("b", "label", new Object[] {"a", "b", "c"}); Field c = Data.makeColumnField("d", "label", new Object[] {1, 2, 2, 2, 2, 2, 2, 3}); Field d = Data.makeColumnField("d", "label", new Object[] {1, 1, 1, 1, 1, 2, 2, 2, 5, 10}); Field e = Data.makeColumnField("e", "label", new Object[] {1, 1, 1, 1, 1, 2, 2, 2, 5, 10, 100, 1000}); Field f = Data.makeColumnField("f", "label", new Object[] {0, 1, 1, 1, 1, 2, 2, 2, 5, 10, 100, 1000}); Field g = Data.makeColumnField( "g", "label", new Object[] {-1, 1, 1, 1, 1, 2, 2, 2, 5, 10, 100, 1000}); Auto.setTransform(a); Auto.setTransform(b); Auto.setTransform(c); Auto.setTransform(d); Auto.setTransform(e); Auto.setTransform(f); Auto.setTransform(g); Assert.assertEquals("linear", a.stringProperty("transform")); Assert.assertEquals("linear", b.stringProperty("transform")); Assert.assertEquals("linear", c.stringProperty("transform")); Assert.assertEquals("root", d.stringProperty("transform")); Assert.assertEquals("log", e.stringProperty("transform")); Assert.assertEquals("root", f.stringProperty("transform")); Assert.assertEquals("linear", g.stringProperty("transform")); }
@Test public void testFieldStats() { Field uniform = Data.makeColumnField("a", "label", new Object[] {100, 200, 300, 400, 500, 600}); Field uniformWithMissing = Data.makeColumnField("a", "label", new Object[] {100, 200, null, 300, "a", 400, 500, 600}); Field peak = Data.makeColumnField("b", "label", new Object[] {1, 2, 2, 2, 2, 2, 2, 3}); Field skew = Data.makeColumnField("c", "label", new Object[] {1, 1, 1, 1, 1, 2, 2, 2, 5, 10}); Field a = Data.makeColumnField("f", "label", new Object[] {0, 1, 1, 1, 1, 2, 2, 2, 5, 10, 100, 1000}); Field b = Data.makeColumnField("f", "label", new Object[] {10, 20, 30, 40, 22, 50, 60}); uniform = uniform.dropData(); uniformWithMissing = uniformWithMissing.dropData(); peak = peak.dropData(); skew = skew.dropData(); a = a.dropData(); b = b.dropData(); // Basics assertEquals(6, uniform.rowCount()); assertEquals(8, uniformWithMissing.rowCount()); assertEquals(350, uniform.numericProperty("median"), 0.01); assertEquals(350, uniformWithMissing.numericProperty("median"), 0.01); assertEquals(2, peak.numericProperty("median"), 0.01); assertEquals(1.5, skew.numericProperty("median"), 0.01); assertEquals(2, a.numericProperty("median"), 0.01); assertEquals(30, b.numericProperty("median"), 0.01); assertEquals(200, uniform.numericProperty("q1"), 0.01); assertEquals(200, uniformWithMissing.numericProperty("q1"), 0.01); assertEquals(2, peak.numericProperty("q1"), 0.01); assertEquals(1, skew.numericProperty("q1"), 0.01); assertEquals(1, a.numericProperty("q1"), 0.01); assertEquals(21, b.numericProperty("q1"), 0.01); assertEquals(500, uniform.numericProperty("q3"), 0.01); assertEquals(500, uniformWithMissing.numericProperty("q3"), 0.01); assertEquals(2, peak.numericProperty("q3"), 0.01); assertEquals(2, skew.numericProperty("q3"), 0.01); assertEquals(7.5, a.numericProperty("q3"), 0.01); assertEquals(45, b.numericProperty("q3"), 0.01); assertEquals(100, uniform.numericProperty("granularity"), 0.01); assertEquals(100, uniformWithMissing.numericProperty("granularity"), 0.01); assertEquals(1, peak.numericProperty("granularity"), 0.01); assertEquals(1, skew.numericProperty("granularity"), 0.01); assertEquals(1, a.numericProperty("granularity"), 0.01); assertEquals(2, b.numericProperty("granularity"), 0.01); }
@Test public void testBasicFieldStats() { Field f = Data.makeColumnField("a", "label", new Object[] {"1", "2", "a", "2", null, 0}); Field g = Data.makeColumnField("a", "label", new Object[] {100, 200, 400, 500, 600}); Field h = Data.makeColumnField("a", "label", new Object[] {"a", "b", "c", "c"}); Field i = Data.makeIndexingField("a", "label", 20); Field c = Data.makeConstantField("a", "label", 5.0, 2000); assertEquals(0, f.min(), 0.01); assertEquals(2, f.max(), 0.01); assertEquals(100, g.min(), 0.01); assertEquals(600, g.max(), 0.01); assertEquals(null, h.min()); assertEquals(null, h.max()); assertEquals(1, i.min(), 0.01); assertEquals(20, i.max(), 0.01); assertEquals(5.0, c.min(), 0.01); assertEquals(5.0, c.max(), 0.01); int[] counts = (int[]) h.property("categoryCounts"); Assert.assertEquals(1, counts[0]); Assert.assertEquals(1, counts[1]); Assert.assertEquals(2, counts[2]); assertEquals(4, f.uniqueValuesCount()); assertEquals(5, g.uniqueValuesCount()); assertEquals(3, h.uniqueValuesCount()); assertEquals(20, i.uniqueValuesCount()); assertEquals(1, c.uniqueValuesCount()); }
@Test public void testAutoConvertDate() { // Make some dates Date d1 = Data.asDate("1971-1-3"); Date d2 = Data.asDate("1971-1-9"); Date d3 = Data.asDate("1971-1-31"); Field a = Data.makeColumnField("field", null, new Object[] {d1, d2, d3, null}); Field b = Auto.convert(a); Assert.assertEquals(true, b.isNumeric()); Assert.assertEquals(true, b.isDate()); // Should ignore one error a = Data.makeColumnField("field", null, new Object[] {d1, d2, "oops", d3, null, null}); b = Auto.convert(a); Assert.assertEquals(true, b.isNumeric()); Assert.assertEquals(true, b.isDate()); assertEquals(3, b.valid()); a = Data.makeColumnField("field", null, new Object[] {"1971-1-3", "1971-1-12"}); b = Auto.convert(a); Assert.assertEquals(true, b.isNumeric()); Assert.assertEquals(true, b.isDate()); assertEquals(2, b.valid()); assertEquals(9.0, b.max() - b.min(), 0.001); a = Data.makeColumnField("field", null, new Object[] {1970, 1972, 1978}); b = Auto.convert(a); Assert.assertEquals(true, b.isNumeric()); Assert.assertEquals(true, b.isDate()); assertEquals(3, b.valid()); assertEquals(8 * 365 + 2, b.max() - b.min(), 0.001); a = Data.makeColumnField("field", null, new Object[] {1970, 1971.5, 1978}); b = Auto.convert(a); Assert.assertEquals(true, b.isNumeric()); Assert.assertEquals(false, b.isDate()); assertEquals(3, b.valid()); assertEquals(8, b.max() - b.min(), 0.001); }
@Test public void testEmptyField() { Field f = Data.makeColumnField("a", "label", new Object[0]); assertEquals(0, f.rowCount()); }
private boolean isRange(Field field) { String s = field.stringProperty("summary"); return s != null && (s.equals("iqr") || s.equals("range")); }
@Test public void testMomentFieldStats() { Field uniform = Data.makeColumnField("a", "label", new Object[] {100, 200, 300, 400, 500, 600}); Field uniformWithMissing = Data.makeColumnField("a", "label", new Object[] {100, 200, null, 300, "a", 400, 500, 600}); Field peak = Data.makeColumnField("b", "label", new Object[] {1, 2, 2, 2, 2, 2, 2, 3}); Field skew = Data.makeColumnField("c", "label", new Object[] {1, 1, 1, 1, 1, 2, 2, 2, 5, 10}); assertEquals(350, uniform.numericProperty("mean"), 0.01); assertEquals(350, uniformWithMissing.numericProperty("mean"), 0.01); assertEquals(2, peak.numericProperty("mean"), 0.01); assertEquals(2.6, skew.numericProperty("mean"), 0.01); assertEquals(187.08, uniform.numericProperty("stddev"), 0.01); assertEquals(187.08, uniformWithMissing.numericProperty("stddev"), 0.01); assertEquals(0.534, peak.numericProperty("stddev"), 0.01); assertEquals(2.875, skew.numericProperty("stddev"), 0.01); assertEquals(0, uniform.numericProperty("skew"), 0.01); assertEquals(0, uniformWithMissing.numericProperty("skew"), 0.01); assertEquals(0, peak.numericProperty("skew"), 0.01); assertEquals(1.86, skew.numericProperty("skew"), 0.01); assertEquals(-1.557, uniform.numericProperty("kurtosis"), 0.01); assertEquals(-1.557, uniformWithMissing.numericProperty("kurtosis"), 0.01); assertEquals(0.5, peak.numericProperty("kurtosis"), 0.01); assertEquals(1.983, skew.numericProperty("kurtosis"), 0.01); }
@Test public void testAutoConvertNumeric() { Field a = Data.makeColumnField("field", null, new Object[] {1, 2, 3, 4}); Field b = Auto.convert(a); assertEquals(a, b); Assert.assertEquals(true, b.isNumeric()); Assert.assertEquals(false, b.isDate()); Assert.assertEquals(2.5, b.numericProperty("mean"), 0.001); a = Data.makeColumnField("field", null, new Object[] {"1", "2", "3", "4"}); b = Auto.convert(a); Assert.assertEquals(false, a == b); Assert.assertEquals(true, b.isNumeric()); Assert.assertEquals(false, b.isDate()); Assert.assertEquals(2.5, b.numericProperty("mean"), 0.001); a = Data.makeColumnField("field", null, new Object[] {"a", "2", "3", "4"}); b = Auto.convert(a); Assert.assertEquals(false, a == b); Assert.assertEquals(true, b.isNumeric()); Assert.assertEquals(false, b.isDate()); Assert.assertEquals(3.0, b.numericProperty("mean"), 0.001); a = Data.makeColumnField("field", null, new Object[] {"a", "2", "c", "4"}); b = Auto.convert(a); Assert.assertEquals(true, a == b); Assert.assertEquals(false, b.isNumeric()); Assert.assertEquals(false, b.isDate()); }