From 77c2cba06445f2cc216095ebf4c22e6dbaf0e8b2 Mon Sep 17 00:00:00 2001 From: Larry White Date: Mon, 11 Jul 2016 03:48:31 -0400 Subject: [PATCH] Replaced reduce function in table with the use of SummaryFunction --- .../com/github/lwhite1/tablesaw/api/Table.java | 7 ------- .../reducing/functions/SummaryFunction.java | 2 +- .../lwhite1/tablesaw/examples/ServiceExample.java | 8 +++----- .../lwhite1/tablesaw/examples/TornadoExample.java | 9 +++++---- .../tablesaw/integration/AirlineDelays2.java | 14 +++++++------- 5 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/github/lwhite1/tablesaw/api/Table.java b/src/main/java/com/github/lwhite1/tablesaw/api/Table.java index b6779e179..9a89795c8 100644 --- a/src/main/java/com/github/lwhite1/tablesaw/api/Table.java +++ b/src/main/java/com/github/lwhite1/tablesaw/api/Table.java @@ -21,7 +21,6 @@ import com.github.lwhite1.tablesaw.table.Projection; import com.github.lwhite1.tablesaw.table.Relation; import com.github.lwhite1.tablesaw.table.Rows; -import com.github.lwhite1.tablesaw.table.ViewGroup; import com.github.lwhite1.tablesaw.util.IntComparatorChain; import com.github.lwhite1.tablesaw.util.ReversingIntComparator; import com.github.lwhite1.tablesaw.util.Selection; @@ -677,15 +676,9 @@ public static Table readTable(String tableNameAndPath) { */ public double reduce(String numericColumnName, NumericReduceFunction function) { Column column = column(numericColumnName); - return function.reduce(column.toDoubleArray()); } - public Table reduce(String numericColumnName, NumericReduceFunction function, String... groupColumnName) { - ViewGroup tableGroup = ViewGroup.create(this, groupColumnName); - return tableGroup.reduce(numericColumnName, function); - } - /** * Returns a new table constructed from a character delimited (aka CSV) text file *

diff --git a/src/main/java/com/github/lwhite1/tablesaw/reducing/functions/SummaryFunction.java b/src/main/java/com/github/lwhite1/tablesaw/reducing/functions/SummaryFunction.java index 643eab5d6..fdabc0da2 100644 --- a/src/main/java/com/github/lwhite1/tablesaw/reducing/functions/SummaryFunction.java +++ b/src/main/java/com/github/lwhite1/tablesaw/reducing/functions/SummaryFunction.java @@ -31,7 +31,7 @@ public Table by(String... columnNames) { } /** - * Returns the result of applying to the function once to all the values in the original column + * Returns the result of applying to the function to all the values in the appropriate column */ public double get() { return original.reduce(summarizedColumnName, function()); diff --git a/src/test/java/com/github/lwhite1/tablesaw/examples/ServiceExample.java b/src/test/java/com/github/lwhite1/tablesaw/examples/ServiceExample.java index 50ac31e02..b9a9ab38d 100644 --- a/src/test/java/com/github/lwhite1/tablesaw/examples/ServiceExample.java +++ b/src/test/java/com/github/lwhite1/tablesaw/examples/ServiceExample.java @@ -1,13 +1,11 @@ package com.github.lwhite1.tablesaw.examples; -import com.github.lwhite1.tablesaw.api.Table; import com.github.lwhite1.tablesaw.api.DateTimeColumn; import com.github.lwhite1.tablesaw.api.LongColumn; +import com.github.lwhite1.tablesaw.api.Table; import it.unimi.dsi.fastutil.floats.FloatArrayList; -import static com.github.lwhite1.tablesaw.reducing.NumericReduceUtils.median; -import static com.github.lwhite1.tablesaw.api.QueryHelper.allOf; -import static com.github.lwhite1.tablesaw.api.QueryHelper.column; +import static com.github.lwhite1.tablesaw.api.QueryHelper.*; /** * Usage example using a Tornado dataset @@ -44,7 +42,7 @@ public static void main(String[] args) throws Exception { (column("SKU").startsWith("429")), (column("Operation").isEqualTo("Assembly")))); - Table durationByFacilityAndShift = q2_429_assembly.reduce("Duration", median, "Facility", "Shift"); + Table durationByFacilityAndShift = q2_429_assembly.median("Duration").by("Facility", "Shift"); // TODO(lwhite): We need a top() method that can be used to return the top table rows FloatArrayList tops = durationByFacilityAndShift.floatColumn("Median").top(5); diff --git a/src/test/java/com/github/lwhite1/tablesaw/examples/TornadoExample.java b/src/test/java/com/github/lwhite1/tablesaw/examples/TornadoExample.java index 6c87ebee2..8fdcc11d4 100644 --- a/src/test/java/com/github/lwhite1/tablesaw/examples/TornadoExample.java +++ b/src/test/java/com/github/lwhite1/tablesaw/examples/TornadoExample.java @@ -1,7 +1,8 @@ package com.github.lwhite1.tablesaw.examples; -import com.github.lwhite1.tablesaw.reducing.NumericReduceUtils; -import com.github.lwhite1.tablesaw.api.*; +import com.github.lwhite1.tablesaw.api.CategoryColumn; +import com.github.lwhite1.tablesaw.api.ColumnType; +import com.github.lwhite1.tablesaw.api.Table; import static com.github.lwhite1.tablesaw.api.ColumnType.*; import static com.github.lwhite1.tablesaw.api.QueryHelper.column; @@ -80,13 +81,13 @@ public static void main(String[] args) throws Exception { //TODO(lwhite): Provide a param for title of the new table (or auto-generate a better one). - Table injuriesByScale = tornadoes.reduce("Injuries", NumericReduceUtils.median, "Scale"); + Table injuriesByScale = tornadoes.median("Injuries").by("Scale"); injuriesByScale.setName("Median injuries by Tornado Scale"); out(injuriesByScale.print()); //TODO(lwhite): Provide a param for title of the new table (or auto-generate a better one). - Table injuriesByScaleState = tornadoes.reduce("Injuries", NumericReduceUtils.median, "Scale", "State"); + Table injuriesByScaleState = tornadoes.median("Injuries").by("Scale", "State"); injuriesByScaleState.setName("Median injuries by Tornado Scale and State"); out(injuriesByScaleState.print()); diff --git a/src/test/java/com/github/lwhite1/tablesaw/integration/AirlineDelays2.java b/src/test/java/com/github/lwhite1/tablesaw/integration/AirlineDelays2.java index dee2643a6..f3f862873 100644 --- a/src/test/java/com/github/lwhite1/tablesaw/integration/AirlineDelays2.java +++ b/src/test/java/com/github/lwhite1/tablesaw/integration/AirlineDelays2.java @@ -1,12 +1,12 @@ package com.github.lwhite1.tablesaw.integration; -import com.github.lwhite1.tablesaw.api.*; - +import com.github.lwhite1.tablesaw.api.BooleanColumn; +import com.github.lwhite1.tablesaw.api.ColumnType; +import com.github.lwhite1.tablesaw.api.Table; import com.google.common.base.Stopwatch; import java.util.concurrent.TimeUnit; -import static com.github.lwhite1.tablesaw.reducing.NumericReduceUtils.mean; import static com.github.lwhite1.tablesaw.api.ColumnType.*; import static com.github.lwhite1.tablesaw.api.QueryHelper.*; import static java.lang.System.out; @@ -79,22 +79,22 @@ private AirlineDelays2() throws Exception { // Compute average number of delayed flights per month - Table monthGroup = ord.reduce("DepDelay", mean, "Month"); + Table monthGroup = ord.mean("DepDelay").by("Month"); out(monthGroup.print()); //TODO Plot - Table dayOfWeekGroup = ord.reduce("DepDelay", mean, "DayOfWeek"); + Table dayOfWeekGroup = ord.mean("DepDelay").by("DayOfWeek"); out(dayOfWeekGroup.print()); //TODO Plot ord.addColumn(ord.timeColumn("CRSDepTime").hour()); System.out.println(ord.columnNames()); - Table hourGroup = ord.reduce("DepDelay", mean, "CRSDepTime[hour]"); + Table hourGroup = ord.mean("DepDelay").by("CRSDepTime[hour]"); out(hourGroup.print()); //TODO Plot // Compute average number of delayed flights per carrier - Table carrierGroup = ord.reduce("DepDelay", mean, "UniqueCarrier"); + Table carrierGroup = ord.mean("DepDelay").by("UniqueCarrier"); carrierGroup = carrierGroup.sortDescendingOn("Mean"); out(carrierGroup.print()); }