use of org.diirt.util.array.ArrayInt 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.util.array.ArrayInt 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.util.array.ArrayInt in project yamcs-studio by yamcs.
the class Sine2DWaveform method nextValue.
@Override
VDoubleArray nextValue() {
if (initialReference == null) {
initialReference = lastTime;
}
double t = initialReference.until(lastTime, ChronoUnit.SECONDS);
double omega = 2 * Math.PI / periodInSeconds;
double k = 2 * Math.PI / wavelengthInSamples;
double min = -1.0;
double max = 1.0;
double range = 0.0;
return (VDoubleArray) ValueFactory.newVNumberArray(generateNewValue(omega, t, k), new ArrayInt(ySamples, xSamples), ValueUtil.defaultArrayDisplay(new ArrayInt(ySamples, xSamples)), alarmNone(), newTime(lastTime), newDisplay(min, min + range * 0.1, min + range * 0.2, "", Constants.DOUBLE_FORMAT, min + range * 0.8, min + range * 0.9, max, min, max));
}
use of org.diirt.util.array.ArrayInt in project org.csstudio.display.builder by kasemir.
the class FormatOptionHandlerTest method testString.
@Test
public void testString() throws Exception {
// Actual String
VType value = ValueFactory.newVString("Test1", ValueFactory.alarmNone(), ValueFactory.timeNow());
String text = FormatOptionHandler.format(value, FormatOption.DEFAULT, -1, true);
System.out.println(text);
assertThat(text, equalTo("Test1"));
text = FormatOptionHandler.format(value, FormatOption.STRING, -1, true);
System.out.println(text);
assertThat(text, equalTo("Test1"));
text = FormatOptionHandler.format(value, FormatOption.EXPONENTIAL, -1, true);
System.out.println(text);
assertThat(text, equalTo("Test1"));
// Number interpreted as char
value = ValueFactory.newVDouble(65.0, display);
text = FormatOptionHandler.format(value, FormatOption.STRING, -1, true);
System.out.println(text);
assertThat(text, equalTo("A V"));
// Number array interpreted as long string
// UTF-8 for 'Hello'
ListNumber data = new ArrayInt(72, 101, 108, 108, 111);
value = ValueFactory.newVNumberArray(data, ValueFactory.alarmNone(), ValueFactory.timeNow(), display);
System.out.println(value);
text = FormatOptionHandler.format(value, FormatOption.STRING, -1, true);
System.out.println(text);
assertThat(text, equalTo("Hello"));
data = new ArrayInt(/* Dollar */
0x24, /* Euro */
0xE2, 0x82, 0xAC);
value = ValueFactory.newVNumberArray(data, ValueFactory.alarmNone(), ValueFactory.timeNow(), display);
System.out.println(value);
text = FormatOptionHandler.format(value, FormatOption.STRING, -1, true);
System.out.println(text);
// For this to work, Eclipse IDE Preferences -> General -> Workspace -> Text file encoding
// must be set to UTF-8, which is the default on Linux but not necessarily Windows
assertThat(text, equalTo("$€"));
}
use of org.diirt.util.array.ArrayInt in project org.csstudio.display.builder by kasemir.
the class ImagePlot method setCrosshairLocation.
/**
* Set location of crosshair
* @param x_val
* @param y_val
*/
public void setCrosshairLocation(final double x_val, final double y_val, final RTImagePlotListener listener) {
if (Double.isNaN(x_val) || Double.isNaN(y_val)) {
if (crosshair_position == null)
return;
crosshair_position = null;
if (listener != null)
listener.changedCursorInfo(Double.NaN, Double.NaN, -1, -1, Double.NaN);
requestRedraw();
return;
}
final Point2D pos = new Point2D(x_val, y_val);
if (pos.equals(crosshair_position))
return;
crosshair_position = pos;
if (listener != null)
listener.changedCrosshair(x_val, y_val);
// Location as coordinate in image
// No "+0.5" rounding! Truncate to get full pixel offsets,
// don't jump to next pixel when mouse moves beyond 'half' of the current pixel.
// Use -1 to mark location outside of data width resp. height.
int image_x = (int) (data_width * (x_val - min_x) / (max_x - min_x));
if (image_x < 0)
image_x = -1;
else if (image_x >= data_width)
image_x = -1;
// Mouse and image coords for Y go 'down'
int image_y = (int) (data_height * (max_y - y_val) / (max_y - min_y));
if (image_y < 0)
image_y = -1;
else if (image_y >= data_height)
image_y = -1;
final ListNumber data = image_data;
double pixel = Double.NaN;
if (data != null && image_x >= 0 && image_y >= 0) {
final int offset = image_x + image_y * data_width;
try {
if (unsigned_data) {
if (data instanceof ArrayByte)
pixel = Byte.toUnsignedInt(data.getByte(offset));
else if (data instanceof ArrayShort)
pixel = Short.toUnsignedInt(data.getShort(offset));
else if (data instanceof ArrayInt)
pixel = Integer.toUnsignedLong(data.getInt(offset));
else
pixel = data.getDouble(offset);
} else
pixel = data.getDouble(offset);
} catch (Throwable ex) {
// Catch ArrayIndexOutOfBoundsException or other internal errors of ListNumber
logger.log(Level.WARNING, "Error accessing pixel " + image_x + ", " + image_y + " of data with size " + data.size());
// leave pixel == Double.NaN;
}
}
if (listener != null)
listener.changedCursorInfo(x_val, y_val, image_x, image_y, pixel);
requestRedraw();
}
Aggregations