use of tech.tablesaw.selection.BitmapBackedSelection in project symja_android_library by axkr.
the class Rows method tail.
public static void tail(int rowsToInclude, Table oldTable, Table newTable) {
int oldTableSize = oldTable.rowCount();
int start = oldTableSize - rowsToInclude;
Selection rows = new BitmapBackedSelection(rowsToInclude);
for (int i = start; i < oldTableSize; i++) {
rows.add(i);
}
copyRowsToTable(rows, oldTable, newTable);
}
use of tech.tablesaw.selection.BitmapBackedSelection in project symja_android_library by axkr.
the class StandardTableSliceGroup method splitOn.
/**
* Splits the sourceTable table into sub-tables, grouping on the columns whose names are given in
* splitColumnNames
*/
private void splitOn(String... splitColumnNames) {
Map<ByteArray, Selection> selectionMap = new LinkedHashMap<>();
Map<ByteArray, String> sliceNameMap = new HashMap<>();
List<Column<?>> splitColumns = getSourceTable().columns(splitColumnNames);
if (containsTextColumn(splitColumns)) {
for (int i = 0; i < getSourceTable().rowCount(); i++) {
ByteArrayList byteArrayList = new ByteArrayList();
StringBuilder stringKey = new StringBuilder();
int count = 0;
for (Column<?> col : splitColumns) {
stringKey.append(col.getString(i));
if (count < splitColumns.size() - 1) {
stringKey.append(SPLIT_STRING);
}
byteArrayList.addElements(byteArrayList.size(), col.asBytes(i));
count++;
}
// Add to the matching selection.
ByteArray byteArray = new ByteArray(byteArrayList.toByteArray());
Selection selection = selectionMap.getOrDefault(byteArray, new BitmapBackedSelection());
selection.add(i);
selectionMap.put(byteArray, selection);
sliceNameMap.put(byteArray, stringKey.toString());
}
} else {
// handle the case where split is on non-text-columns
int byteSize = getByteSize(splitColumns);
for (int i = 0; i < getSourceTable().rowCount(); i++) {
StringBuilder stringKey = new StringBuilder();
ByteBuffer byteBuffer = ByteBuffer.allocate(byteSize);
int count = 0;
for (Column<?> col : splitColumns) {
stringKey.append(col.getString(i));
if (count < splitColumns.size() - 1) {
stringKey.append(SPLIT_STRING);
}
byteBuffer.put(col.asBytes(i));
count++;
}
// Add to the matching selection.
ByteArray byteArray = new ByteArray(byteBuffer.array());
Selection selection = selectionMap.getOrDefault(byteArray, new BitmapBackedSelection());
selection.add(i);
selectionMap.put(byteArray, selection);
sliceNameMap.put(byteArray, stringKey.toString());
}
}
// Construct slices for all the values in our maps
for (Entry<ByteArray, Selection> entry : selectionMap.entrySet()) {
TableSlice slice = new TableSlice(getSourceTable(), entry.getValue());
slice.setName(sliceNameMap.get(entry.getKey()));
addSlice(slice);
}
}
use of tech.tablesaw.selection.BitmapBackedSelection in project symja_android_library by axkr.
the class RollingColumn method calc.
@SuppressWarnings({ "unchecked" })
public <INCOL extends Column<?>, OUT> Column<?> calc(AggregateFunction<INCOL, OUT> function) {
// TODO: the subset operation copies the array. creating a view would likely be more efficient
Column<?> result = function.returnType().create(generateNewColumnName(function));
for (int i = 0; i < window - 1; i++) {
result.appendMissing();
}
for (int origColIndex = 0; origColIndex < column.size() - window + 1; origColIndex++) {
Selection selection = new BitmapBackedSelection();
selection.addRange(origColIndex, origColIndex + window);
INCOL subsetCol = (INCOL) column.subset(selection.toArray());
OUT answer = function.summarize(subsetCol);
if (answer instanceof Number) {
Number number = (Number) answer;
((DoubleColumn) result).append(number.doubleValue());
} else {
result.appendObj(answer);
}
}
return result;
}
use of tech.tablesaw.selection.BitmapBackedSelection in project symja_android_library by axkr.
the class Table method dropWhere.
public Table dropWhere(Selection selection) {
Selection opposite = new BitmapBackedSelection();
opposite.addRange(0, rowCount());
opposite.andNot(selection);
Table newTable = this.emptyCopy(opposite.size());
Rows.copyRowsToTable(opposite, this, newTable);
return newTable;
}
use of tech.tablesaw.selection.BitmapBackedSelection in project symja_android_library by axkr.
the class Table method sampleSplit.
/**
* Splits the table into two, randomly assigning records to each according to the proportion given
* in trainingProportion
*
* @param table1Proportion The proportion to go in the first table
* @return An array two tables, with the first table having the proportion specified in the method
* parameter, and the second table having the balance of the rows
*/
public Table[] sampleSplit(double table1Proportion) {
Table[] tables = new Table[2];
int table1Count = (int) Math.round(rowCount() * table1Proportion);
Selection table2Selection = new BitmapBackedSelection();
for (int i = 0; i < rowCount(); i++) {
table2Selection.add(i);
}
Selection table1Selection = new BitmapBackedSelection();
Selection table1Records = selectNRowsAtRandom(table1Count, rowCount());
for (int table1Record : table1Records) {
table1Selection.add(table1Record);
}
table2Selection.andNot(table1Selection);
tables[0] = where(table1Selection);
tables[1] = where(table2Selection);
return tables;
}
Aggregations