Skip to content

Commit

Permalink
Replaced reduce function in table with the use of SummaryFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
lwhite1 committed Jul 11, 2016
1 parent 9db0a7b commit 77c2cba
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
7 changes: 0 additions & 7 deletions src/main/java/com/github/lwhite1/tablesaw/api/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 77c2cba

Please sign in to comment.