Search in sources :

Example 1 with Row

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

the class ASTDataset method newAssociationOfAssociations.

/**
 * Create a <code>Dataset</code> object from a (head-)association <code>&lt;|...|&gt;</code> of
 * (sub-)associations. Each key in the (head-)association is used in the first column the
 * (sub-)association represents the other columns of a row in the <code>Dataset</code>. The
 * left-hand-side of each singular rule in a (sub-)association was assumed to be the name of the
 * resulting <code>Dataset</code> columns. Identical names maps the right-hand-side values of the
 * rule to the same columns in the resulting dataset.
 *
 * @param assocOfAssociations
 * @return {@link F#NIL} if the <code>Dataset</code> cannot be created
 */
public static IExpr newAssociationOfAssociations(IAssociation assocOfAssociations) {
    // 1. phase: build up column names; reserve 1 column for header assoc
    List<String> colNames = new ArrayList<String>();
    Set<String> colNamesSet = new HashSet<String>();
    colNamesSet.add("");
    colNames.add("");
    for (int i = 1; i < assocOfAssociations.size(); i++) {
        IAssociation assoc = (IAssociation) assocOfAssociations.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 < assocOfAssociations.size(); i++) {
            IExpr rule = assocOfAssociations.getRule(i);
            IAssociation assoc = (IAssociation) rule.second();
            Row row = table.appendRow();
            row.setExpr("", rule.first());
            for (int j = 1; j < assoc.size(); j++) {
                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) IExpr(org.matheclipse.core.interfaces.IExpr) Row(tech.tablesaw.api.Row) HashSet(java.util.HashSet)

Example 2 with Row

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

the class Summarizer method assignToGroupsByStep.

private IntColumn assignToGroupsByStep(int step) {
    IntColumn groupColumn = IntColumn.create(GROUP_COL_TEMP_NAME, temp.rowCount());
    temp.addColumns(groupColumn);
    int groupId = 1;
    int withinGroupCount = 0;
    Row row = new Row(temp);
    while (row.hasNext()) {
        row.next();
        if (withinGroupCount < step) {
            withinGroupCount++;
            groupColumn.set(row.getRowNumber(), groupId);
        } else {
            groupId++;
            groupColumn.set(row.getRowNumber(), groupId);
            withinGroupCount = 1;
        }
    }
    int lastGroupSize = temp.where(numberColumn(GROUP_COL_TEMP_NAME).isEqualTo(groupId)).rowCount();
    if (lastGroupSize < step) {
        temp = temp.dropWhere(numberColumn(GROUP_COL_TEMP_NAME).isEqualTo(groupId));
    }
    temp.addColumns(IntColumn.indexColumn("index", temp.rowCount(), 1));
    return groupColumn;
}
Also used : Row(tech.tablesaw.api.Row) IntColumn(tech.tablesaw.api.IntColumn)

Example 3 with Row

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

the class AnalyticQueryEngine method processNumberingFunctions.

/**
 * Execute all numbering functions for the given slice setting values in the appropriate
 * destination column.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void processNumberingFunctions(TableSlice slice) {
    for (String toColumn : query.getArgumentList().getNumberingFunctions().keySet()) {
        if (rowComparator == null) {
            throw new IllegalArgumentException("Cannot use Numbering Function without OrderBy");
        }
        FunctionCall<NumberingFunctions> functionCall = query.getArgumentList().getNumberingFunctions().get(toColumn);
        NumberingFunctions numberingFunctions = functionCall.getFunction();
        NumberingFunction function = numberingFunctions.getImplementation();
        Column<Integer> destinationColumn = (Column<Integer>) destination.column(functionCall.getDestinationColumnName());
        int prevRowNumber = -1;
        // Slice has already been ordered.
        for (Row row : slice) {
            if (row.getRowNumber() == 0) {
                function.addNextRow();
            } else {
                // Consecutive rows are equal.
                if (rowComparator.compare(slice.mappedRowNumber(prevRowNumber), slice.mappedRowNumber(row.getRowNumber())) == 0) {
                    function.addEqualRow();
                } else {
                    // Consecutive rows are not equal.
                    function.addNextRow();
                }
            }
            prevRowNumber = row.getRowNumber();
            // Set the row number in the destination that corresponds to the row in the view.
            destinationColumn.set(slice.mappedRowNumber(row.getRowNumber()), function.getValue());
        }
    }
}
Also used : Column(tech.tablesaw.columns.Column) Row(tech.tablesaw.api.Row)

Example 4 with Row

use of tech.tablesaw.api.Row 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 5 with Row

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

the class ASTDataset method normal.

@Override
public IASTAppendable normal(boolean nilIfUnevaluated) {
    Cache<IAST, IAST> cache = CacheBuilder.newBuilder().maximumSize(500).build();
    final List<String> names = fTable.columnNames();
    List<IStringX> namesStr = new ArrayList<IStringX>(names.size());
    for (int i = 0; i < names.size(); i++) {
        namesStr.add(F.stringx(names.get(i)));
    }
    if (names.size() == 1) {
        Column<?> column = fTable.column(names.get(0));
        ColumnType t = column.type();
        IASTAppendable resultList = F.ListAlloc(column.size());
        for (int j = 0; j < column.size(); j++) {
            Object obj = column.get(j);
            IExpr expr = F.NIL;
            expr = dataToExpr(obj, t);
            resultList.append(expr);
        }
        return resultList;
    }
    IASTAppendable list = F.ListAlloc(names.size());
    int size = fTable.rowCount();
    for (int k = 0; k < size; k++) {
        Row row = fTable.row(k);
        IAssociation assoc = F.assoc();
        for (int j = 0; j < row.columnCount(); j++) {
            String columnName = names.get(j);
            IStringX colName = namesStr.get(j);
            ColumnType t = row.getColumnType(columnName);
            Object obj = row.getObject(j);
            if (t.equals(ColumnType.EXPR)) {
                IExpr expr = (IExpr) obj;
                ruleCache(cache, assoc, F.Rule(colName, expr));
            } else if (t.equals(ColumnType.BOOLEAN)) {
                Boolean b = row.getBoolean(j);
                if (b) {
                    ruleCache(cache, assoc, F.Rule(colName, S.True));
                } else {
                    ruleCache(cache, assoc, F.Rule(colName, S.False));
                }
            } else if (t.equals(ColumnType.SHORT)) {
                short sValue = row.getShort(j);
                ruleCache(cache, assoc, F.Rule(colName, F.ZZ(sValue)));
            } else if (t.equals(ColumnType.INTEGER)) {
                int iValue = row.getInt(j);
                ruleCache(cache, assoc, F.Rule(colName, F.ZZ(iValue)));
            } else if (t.equals(ColumnType.LONG)) {
                long lValue = row.getLong(j);
                ruleCache(cache, assoc, F.Rule(colName, F.ZZ(lValue)));
            } else if (t.equals(ColumnType.FLOAT)) {
                float fValue = row.getFloat(j);
                ruleCache(cache, assoc, F.Rule(colName, F.num(fValue)));
            } else if (t.equals(ColumnType.DOUBLE)) {
                double dValue = row.getDouble(j);
                ruleCache(cache, assoc, F.Rule(colName, F.num(dValue)));
            } else if (t.equals(ColumnType.STRING)) {
                ruleCache(cache, assoc, F.Rule(colName, F.stringx(row.getString(j))));
            } else if (t.equals(ColumnType.LOCAL_DATE_TIME)) {
                LocalDateTime lDate = row.getDateTime(j);
                ruleCache(cache, assoc, F.Rule(colName, DateObjectExpr.newInstance(lDate)));
            } else if (t.equals(ColumnType.LOCAL_DATE)) {
                LocalDate lDate = row.getDate(j);
                ruleCache(cache, assoc, F.Rule(colName, DateObjectExpr.newInstance(lDate.atStartOfDay())));
            } else if (t.equals(ColumnType.LOCAL_TIME)) {
                LocalTime lTime = row.getTime(j);
                ruleCache(cache, assoc, F.Rule(colName, TimeObjectExpr.newInstance(lTime)));
            } else if (t.equals(ColumnType.SKIP)) {
                // ruleCache(cache, assoc, F.Rule(colName, F.Missing));
                ruleCache(cache, assoc, F.Rule(colName, F.Missing(S.NotAvailable)));
            } else {
                IExpr valueStr = F.stringx(obj.toString());
                ruleCache(cache, assoc, F.Rule(colName, valueStr));
            }
        }
        if (size == 1) {
            return assoc;
        }
        list.append(assoc);
    }
    return list;
}
Also used : LocalDateTime(java.time.LocalDateTime) IAssociation(org.matheclipse.core.interfaces.IAssociation) ColumnType(tech.tablesaw.api.ColumnType) LocalTime(java.time.LocalTime) ArrayList(java.util.ArrayList) LocalDate(java.time.LocalDate) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) IStringX(org.matheclipse.core.interfaces.IStringX) IExpr(org.matheclipse.core.interfaces.IExpr) Row(tech.tablesaw.api.Row)

Aggregations

Row (tech.tablesaw.api.Row)6 IExpr (org.matheclipse.core.interfaces.IExpr)4 Column (tech.tablesaw.columns.Column)4 ArrayList (java.util.ArrayList)3 IAST (org.matheclipse.core.interfaces.IAST)3 IAssociation (org.matheclipse.core.interfaces.IAssociation)3 ExprColumn (tech.tablesaw.api.ExprColumn)3 Table (tech.tablesaw.api.Table)3 HashSet (java.util.HashSet)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 ColumnType (tech.tablesaw.api.ColumnType)2 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 IASTDataset (org.matheclipse.core.interfaces.IASTDataset)1 IStringX (org.matheclipse.core.interfaces.IStringX)1 IntColumn (tech.tablesaw.api.IntColumn)1