use of org.diirt.vtype.VTable in project yamcs-studio by yamcs.
the class VTableFactory method union.
public static VTable union(VString extraColumnName, final VStringArray extraColumnData, final VTable... tables) {
// Prune nulls
final List<String> extraColumnDataPruned = new ArrayList<>();
final List<VTable> tablesPruned = new ArrayList<>();
for (int i = 0; i < tables.length; i++) {
VTable table = tables[i];
if (table != null) {
extraColumnDataPruned.add(extraColumnData.getData().get(i));
tablesPruned.add(table);
}
}
if (tablesPruned.isEmpty()) {
return null;
}
List<String> columnNames = new ArrayList<>();
List<Class<?>> columnTypes = new ArrayList<>();
List<Map<String, VColumn>> tableColumns = new ArrayList<>();
if (extraColumnName != null) {
columnNames.add(extraColumnName.getValue());
columnTypes.add(String.class);
}
int[] tableOffsets = new int[tablesPruned.size()];
int currentOffset = 0;
for (int k = 0; k < tablesPruned.size(); k++) {
VTable table = tablesPruned.get(k);
if (table == null) {
continue;
}
tableOffsets[k] = currentOffset;
currentOffset += table.getRowCount();
tableColumns.add(VColumn.columnMap(table));
for (int i = 0; i < table.getColumnCount(); i++) {
String name = table.getColumnName(i);
if (!columnNames.contains(name)) {
columnNames.add(name);
columnTypes.add(table.getColumnType(i));
}
}
}
final int rowCount = currentOffset;
final ListInt offsets = new ArrayInt(tableOffsets);
List<Object> columnData = new ArrayList<>();
if (extraColumnName != null) {
columnData.add(new AbstractList<String>() {
@Override
public String get(int index) {
int nTable = ListNumbers.binarySearchValueOrLower(offsets, index);
return extraColumnDataPruned.get(nTable);
}
@Override
public int size() {
return rowCount;
}
});
}
for (int i = 1; i < columnNames.size(); i++) {
String columnName = columnNames.get(i);
List<VColumn> columns = new ArrayList<>();
for (int j = 0; j < tableColumns.size(); j++) {
columns.add(tableColumns.get(j).get(columnName));
}
columnData.add(VColumn.combineData(columnTypes.get(i), rowCount, offsets, columns));
}
return ValueFactory.newVTable(columnTypes, columnNames, columnData);
}
use of org.diirt.vtype.VTable in project yamcs-studio by yamcs.
the class Histogram2DOfFormulaFunction method calculate.
@Override
public Object calculate(List<Object> args) {
if (NullUtils.containsNull(args)) {
return null;
}
VTable table = (VTable) args.get(0);
VString yColumnName = (VString) args.get(1);
VString xColumnName = (VString) args.get(2);
ListNumber yData = ValueUtil.numericColumnOf(table, yColumnName.getValue());
ListNumber xData = ValueUtil.numericColumnOf(table, xColumnName.getValue());
int nPoints = Math.min(yData.size(), xData.size());
Statistics xStats = StatisticsUtil.statisticsOf(xData);
Statistics yStats = StatisticsUtil.statisticsOf(yData);
int nXBins = 20;
int nYBins = 20;
Range aggregatedXRange = Ranges.aggregateRange(xStats.getRange(), previousXRange);
Range aggregatedYRange = Ranges.aggregateRange(yStats.getRange(), previousYRange);
Range xRange;
Range yRange;
if (Ranges.overlap(aggregatedXRange, xStats.getRange()) >= 0.90) {
xRange = aggregatedXRange;
} else {
xRange = xStats.getRange();
}
if (Ranges.overlap(aggregatedYRange, yStats.getRange()) >= 0.90) {
yRange = aggregatedYRange;
} else {
yRange = yStats.getRange();
}
double minXValueRange = xRange.getMinimum();
double maxXValueRange = xRange.getMaximum();
double minYValueRange = yRange.getMinimum();
double maxYValueRange = yRange.getMaximum();
ListNumber xBoundaries = ListNumbers.linearListFromRange(minXValueRange, maxXValueRange, nXBins + 1);
ListNumber yBoundaries = ListNumbers.linearListFromRange(minYValueRange, maxYValueRange, nYBins + 1);
int[] binData = new int[nXBins * nYBins];
double maxCount = 0;
for (int i = 0; i < nPoints; i++) {
double xValue = xData.getDouble(i);
double yValue = yData.getDouble(i);
// Check value in range
if (xRange.contains(xValue)) {
if (yRange.contains(yValue)) {
int xBin = (int) Math.floor(xRange.normalize(xValue) * nXBins);
int yBin = (int) Math.floor(yRange.normalize(yValue) * nYBins);
if (xBin == nXBins) {
xBin--;
}
if (yBin == nYBins) {
yBin--;
}
int binIndex = yBin * nXBins + xBin;
binData[binIndex]++;
if (binData[binIndex] > maxCount) {
maxCount = binData[binIndex];
}
}
}
}
// time based "forget"
if (previousMaxCount > maxCount && (previousMaxCount < maxCount * 2.0 || maxCount < 9)) {
maxCount = previousMaxCount;
}
previousMaxCount = maxCount;
previousXRange = xRange;
previousXData = xData;
previousYData = yData;
previousResult = newVNumberArray(new ArrayInt(binData), new ArrayInt(nYBins, nXBins), Arrays.asList(newDisplay(yBoundaries, ""), newDisplay(xBoundaries, "")), alarmNone(), timeNow(), newDisplay(0.0, 0.0, 0.0, "count", NumberFormats.format(0), maxCount, maxCount, maxCount, Double.NaN, Double.NaN));
return previousResult;
}
use of org.diirt.vtype.VTable in project yamcs-studio by yamcs.
the class TableRangeArrayFilterFunction method calculate.
@Override
public Object calculate(final List<Object> args) {
if (NullUtils.containsNull(args)) {
return null;
}
VTable table = (VTable) args.get(0);
VString columnName = (VString) args.get(1);
VNumberArray range = (VNumberArray) args.get(2);
if (range.getData().size() != 2) {
throw new IllegalArgumentException("Range array must be of 2 elements");
}
VTable result = VTableFactory.tableRangeFilter(table, columnName.getValue(), ValueFactory.newVDouble(range.getData().getDouble(0)), ValueFactory.newVDouble(range.getData().getDouble(1)));
return result;
}
use of org.diirt.vtype.VTable in project yamcs-studio by yamcs.
the class TableValueFilterFunction method calculate.
@Override
public Object calculate(final List<Object> args) {
VTable table = (VTable) args.get(0);
VString columnName = (VString) args.get(1);
VType value = (VType) args.get(2);
if (columnName == null || columnName.getValue() == null || table == null || value == null) {
return null;
}
VTable result = VTableFactory.tableValueFilter(table, columnName.getValue(), value);
return result;
}
use of org.diirt.vtype.VTable in project yamcs-studio by yamcs.
the class VTableFactory method join.
public static VTable join(VTable... tables) {
if (tables.length == 0) {
return null;
}
if (tables.length == 1) {
return tables[0];
}
// Find columns to join
Map<String, int[]> commonColumnsIndexes = null;
for (int nTable = 0; nTable < tables.length; nTable++) {
VTable vTable = tables[nTable];
if (commonColumnsIndexes == null) {
commonColumnsIndexes = new HashMap<>();
for (int i = 0; i < vTable.getColumnCount(); i++) {
int[] indexes = new int[tables.length];
indexes[0] = i;
commonColumnsIndexes.put(vTable.getColumnName(i), indexes);
}
} else {
commonColumnsIndexes.keySet().retainAll(columnNames(vTable));
for (int i = 0; i < vTable.getColumnCount(); i++) {
if (commonColumnsIndexes.keySet().contains(vTable.getColumnName(i))) {
commonColumnsIndexes.get(vTable.getColumnName(i))[nTable] = i;
}
}
}
}
if (commonColumnsIndexes.isEmpty()) {
throw new UnsupportedOperationException("Case not implemented yet");
}
List<EqualValueFilter> filters = new ArrayList<>();
for (Map.Entry<String, int[]> entry : commonColumnsIndexes.entrySet()) {
int[] indexes = entry.getValue();
filters.add(new EqualValueFilter(Arrays.asList(tables), indexes));
}
// Find rows
boolean done = false;
List<BufferInt> rowIndexes = new ArrayList<>();
for (int i = 0; i < tables.length; i++) {
rowIndexes.add(new BufferInt());
if (tables[i].getRowCount() == 0) {
done = true;
}
}
int[] currentIndexes = new int[tables.length];
while (!done) {
boolean match = true;
for (EqualValueFilter filter : filters) {
match = match && filter.filterRow(currentIndexes);
}
if (match) {
for (int i = 0; i < currentIndexes.length; i++) {
rowIndexes.get(i).addInt(currentIndexes[i]);
}
}
boolean needsIncrement = true;
int offset = currentIndexes.length - 1;
while (needsIncrement) {
currentIndexes[offset]++;
if (currentIndexes[offset] == tables[offset].getRowCount()) {
currentIndexes[offset] = 0;
offset--;
if (offset == -1) {
done = true;
needsIncrement = false;
}
} else {
needsIncrement = false;
}
}
}
List<String> columnNames = new ArrayList<>();
List<Class<?>> columnTypes = new ArrayList<>();
List<Object> columnData = new ArrayList<>();
for (int nColumn = 0; nColumn < tables[0].getColumnCount(); nColumn++) {
columnNames.add(tables[0].getColumnName(nColumn));
Class<?> type = tables[0].getColumnType(nColumn);
if (type.isPrimitive()) {
columnTypes.add(type);
columnData.add(ListNumbers.listView((ListNumber) tables[0].getColumnData(nColumn), rowIndexes.get(0)));
} else {
columnTypes.add(type);
columnData.add(createView((List<?>) tables[0].getColumnData(nColumn), rowIndexes.get(0)));
}
}
for (int i = 1; i < tables.length; i++) {
VTable vTable = tables[i];
for (int nColumn = 0; nColumn < vTable.getColumnCount(); nColumn++) {
if (!commonColumnsIndexes.containsKey(vTable.getColumnName(nColumn))) {
columnNames.add(vTable.getColumnName(nColumn));
Class<?> type = vTable.getColumnType(nColumn);
if (type.isPrimitive()) {
columnTypes.add(type);
columnData.add(ListNumbers.listView((ListNumber) vTable.getColumnData(nColumn), rowIndexes.get(i)));
} else {
columnTypes.add(type);
columnData.add(createView((List<?>) vTable.getColumnData(nColumn), rowIndexes.get(i)));
}
}
}
}
return ValueFactory.newVTable(columnTypes, columnNames, columnData);
}
Aggregations