Search in sources :

Example 36 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class ASTDataset method newListOfAssociations.

/**
 * Create a <code>Dataset</code> object from a <code>List(...)</code> of associations. Each
 * association represents a row in the <code>Dataset</code>. The left-hand-side of each singular
 * rule in an association was assumed to be the name of the resulting dataset columns. Identical
 * names maps the right-hand-side values of the rule to the same columns in the resulting <code>
 * Dataset
 * </code>.
 *
 * @param listOfAssociations
 * @return {@link F#NIL} if the <code>Dataset</code> cannot be created
 */
public static IExpr newListOfAssociations(IAST listOfAssociations) {
    // 1. phase: build up column names
    List<String> colNames = new ArrayList<String>();
    Set<String> colNamesSet = new HashSet<String>();
    for (int i = 1; i < listOfAssociations.size(); i++) {
        IAssociation assoc = (IAssociation) listOfAssociations.get(i);
        for (int j = 1; j < assoc.size(); j++) {
            IAST rule = assoc.getRule(j);
            String columnName = rule.first().toString();
            if (!colNamesSet.contains(columnName)) {
                colNamesSet.add(columnName);
                colNames.add(columnName);
            }
        }
    }
    if (colNames.size() > 0) {
        // 2. phase: define the columns
        Table table = Table.create();
        Column<?>[] cols = new Column<?>[colNames.size()];
        for (int i = 0; i < colNames.size(); i++) {
            cols[i] = ExprColumn.create(colNames.get(i));
        }
        table.addColumns(cols);
        // 3. phase: add the values
        for (int i = 1; i < listOfAssociations.size(); i++) {
            IAssociation assoc = (IAssociation) listOfAssociations.get(i);
            Row row = table.appendRow();
            for (int j = 1; j < assoc.size(); j++) {
                IAST rule = assoc.getRule(j);
                String columnName = rule.first().toString();
                IExpr value = rule.second();
                row.setExpr(columnName, value);
            }
        }
        return newTablesawTable(table);
    }
    return F.NIL;
}
Also used : IAssociation(org.matheclipse.core.interfaces.IAssociation) Table(tech.tablesaw.api.Table) ArrayList(java.util.ArrayList) Column(tech.tablesaw.columns.Column) ExprColumn(tech.tablesaw.api.ExprColumn) IAST(org.matheclipse.core.interfaces.IAST) Row(tech.tablesaw.api.Row) IExpr(org.matheclipse.core.interfaces.IExpr) HashSet(java.util.HashSet)

Example 37 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class ASTDataset method select.

@Override
public IExpr select(IAST ast) {
    IExpr row = ast.arg1();
    IExpr column = ast.arg2();
    IExpr[] part = new IExpr[ast.size() - 3];
    IExpr result = select(row, column);
    if (part.length == 0) {
        return result;
    }
    if (result.isDataset()) {
        for (int i = 0; i < part.length; i++) {
            part[i] = ast.get(i + 3);
        }
        EvalEngine engine = EvalEngine.get();
        ASTDataset dataset = (ASTDataset) result;
        Table table = dataset.fTable;
        final List<String> names = table.columnNames();
        if (names.size() > 0) {
            Table resultTable = Table.create();
            Column<?>[] cols = new Column<?>[names.size()];
            for (int i = 0; i < names.size(); i++) {
                cols[i] = ExprColumn.create(names.get(i));
            }
            resultTable.addColumns(cols);
            for (int i = 0; i < table.rowCount(); i++) {
                Row currentRow = table.row(i);
                Row resultRow = resultTable.appendRow();
                for (int j = 0; j < table.columnCount(); j++) {
                    String columnName = names.get(j);
                    ColumnType t = currentRow.getColumnType(columnName);
                    IExpr arg = dataToExpr(table.get(i, j), t);
                    IExpr value = S.Part.of1(engine, arg, part);
                    if (value.isAST(S.Part) || !value.isPresent()) {
                        IASTAppendable missing = F.ast(S.Missing);
                        missing.append(F.$str("PartAbsent"));
                        missing.appendAll(part, 0, part.length);
                        value = missing;
                    }
                    resultRow.setExpr(columnName, value);
                }
            }
            return ASTDataset.newTablesawTable(resultTable);
        }
    }
    return F.NIL;
}
Also used : IASTDataset(org.matheclipse.core.interfaces.IASTDataset) Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) Column(tech.tablesaw.columns.Column) ExprColumn(tech.tablesaw.api.ExprColumn) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) Row(tech.tablesaw.api.Row)

Example 38 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class SemanticImport method readFile.

private static IExpr readFile(String fileName, EvalEngine engine) {
    try {
        File file = new File(fileName);
        if (file.exists()) {
            Table table = Table.read().csv(file);
            return ASTDataset.newTablesawTable(table);
        }
        LOGGER.log(engine.getLogLevel(), "file {} does not exist!", fileName);
    } catch (IOException ioe) {
        LOGGER.log(engine.getLogLevel(), "file {} not found!", fileName);
    } catch (RuntimeException rex) {
        LOGGER.log(engine.getLogLevel(), "file {}", fileName, rex);
    }
    return F.NIL;
}
Also used : Table(tech.tablesaw.api.Table) IOException(java.io.IOException) File(java.io.File)

Example 39 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class Summarizer method combineTables.

private Table combineTables(List<Table> tables) {
    Preconditions.checkArgument(!tables.isEmpty());
    Table result = tables.get(0);
    for (int i = 1; i < tables.size(); i++) {
        Table table = tables.get(i);
        for (Column<?> column : table.columns()) {
            if (tableDoesNotContain(column.name(), result)) {
                result.addColumns(column);
            }
        }
    }
    return result;
}
Also used : Table(tech.tablesaw.api.Table)

Example 40 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class Summarizer method apply.

/**
 * Returns the result of applying to the functions to all the values in the appropriate column
 * TODO add a test that uses a non numeric return type with apply
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Table apply() {
    if (groupColumnNames.length > 0) {
        TableSliceGroup group = StandardTableSliceGroup.create(temp, groupColumnNames);
        return summarize(group);
    } else {
        List<Table> results = new ArrayList<>();
        ArrayListMultimap<String, AggregateFunction<?, ?>> reductionMultimap = getAggregateFunctionMultimap();
        for (String name : reductionMultimap.keys()) {
            List<AggregateFunction<?, ?>> reductions = reductionMultimap.get(name);
            Table table = TableSliceGroup.summaryTableName(temp);
            for (AggregateFunction function : reductions) {
                Column column = temp.column(name);
                Object result = function.summarize(column);
                ColumnType type = function.returnType();
                Column newColumn = type.create(TableSliceGroup.aggregateColumnName(name, function.functionName()));
                if (result instanceof Number) {
                    Number number = (Number) result;
                    newColumn.append(number.doubleValue());
                } else {
                    newColumn.append(result);
                }
                table.addColumns(newColumn);
            }
            results.add(table);
        }
        return (combineTables(results));
    }
}
Also used : Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) QuerySupport.numberColumn(tech.tablesaw.api.QuerySupport.numberColumn) Column(tech.tablesaw.columns.Column) CategoricalColumn(tech.tablesaw.api.CategoricalColumn) IntColumn(tech.tablesaw.api.IntColumn) ArrayList(java.util.ArrayList) StandardTableSliceGroup(tech.tablesaw.table.StandardTableSliceGroup) TableSliceGroup(tech.tablesaw.table.TableSliceGroup)

Aggregations

Table (tech.tablesaw.api.Table)42 StringColumn (tech.tablesaw.api.StringColumn)10 ArrayList (java.util.ArrayList)9 DoubleColumn (tech.tablesaw.api.DoubleColumn)8 IntColumn (tech.tablesaw.api.IntColumn)8 ColumnType (tech.tablesaw.api.ColumnType)7 Column (tech.tablesaw.columns.Column)7 TreeBasedTable (com.google.common.collect.TreeBasedTable)5 Map (java.util.Map)5 List (java.util.List)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 ExprColumn (tech.tablesaw.api.ExprColumn)3 Row (tech.tablesaw.api.Row)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 IAST (org.matheclipse.core.interfaces.IAST)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1