use of org.gephi.datalab.api.AttributeColumnsController in project gephi by gephi.
the class AttributeColumnsMergeStrategiesControllerImpl method mergeNumericColumnsToTimeInterval.
@Override
public Column mergeNumericColumnsToTimeInterval(Table table, Column startColumn, Column endColumn, double defaultStart, double defaultEnd) {
checkTableAndOneColumn(table, startColumn, endColumn);
Column timeIntervalColumn = getTimeIntervalColumn(table);
final int startColumnIndex = startColumn != null ? startColumn.getIndex() : -1;
final int endColumnIndex = endColumn != null ? endColumn.getIndex() : -1;
final boolean isStartColumnNumeric = startColumn != null ? (!AttributeUtils.isDynamicType(startColumn.getTypeClass()) && AttributeUtils.isNumberType(startColumn.getTypeClass())) : false;
final boolean isEndColumnNumeric = endColumn != null ? (!AttributeUtils.isDynamicType(endColumn.getTypeClass()) && AttributeUtils.isNumberType(endColumn.getTypeClass())) : false;
AttributeColumnsController ac = Lookup.getDefault().lookup(AttributeColumnsController.class);
Object value;
double start, end;
for (Element row : ac.getTableAttributeRows(table)) {
if (startColumnIndex != -1) {
value = row.getAttribute(startColumn);
if (value != null) {
if (isStartColumnNumeric) {
start = ((Number) value).doubleValue();
} else {
start = parseDouble(value.toString(), defaultStart);
}
} else {
start = defaultStart;
}
} else {
start = defaultStart;
}
if (endColumnIndex != -1) {
value = row.getAttribute(endColumn);
if (value != null) {
if (isEndColumnNumeric) {
end = ((Number) value).doubleValue();
} else {
end = parseDouble(value.toString(), defaultEnd);
}
} else {
end = defaultEnd;
}
} else {
end = defaultEnd;
}
if (!Double.isInfinite(start) && !Double.isInfinite(end) && start > end) {
// When start>end, check what column was provided and keep its value. If both columns were provided, set an infinite interval:
if (startColumnIndex == -1) {
start = Double.NEGATIVE_INFINITY;
} else if (endColumnIndex == -1) {
end = Double.POSITIVE_INFINITY;
} else {
start = Double.NEGATIVE_INFINITY;
end = Double.POSITIVE_INFINITY;
}
}
IntervalSet timeInterval = new IntervalSet(new double[] { start, end });
row.setAttribute(timeIntervalColumn, timeInterval);
}
return timeIntervalColumn;
}
use of org.gephi.datalab.api.AttributeColumnsController in project gephi by gephi.
the class AttributeColumnsMergeStrategiesControllerImpl method mergeDateColumnsToTimeInterval.
@Override
public Column mergeDateColumnsToTimeInterval(Table table, Column startColumn, Column endColumn, SimpleDateFormat dateFormat, String defaultStartDate, String defaultEndDate) {
checkTableAndOneColumn(table, startColumn, endColumn);
if (dateFormat == null) {
throw new IllegalArgumentException("Date format can't be null can't be null");
}
Column timeIntervalColumn = getTimeIntervalColumn(table);
final int startColumnIndex = startColumn != null ? startColumn.getIndex() : -1;
final int endColumnIndex = endColumn != null ? endColumn.getIndex() : -1;
double defaultStart = parseDateToDouble(dateFormat, defaultStartDate, Double.NEGATIVE_INFINITY);
double defaultEnd = parseDateToDouble(dateFormat, defaultEndDate, Double.POSITIVE_INFINITY);
AttributeColumnsController ac = Lookup.getDefault().lookup(AttributeColumnsController.class);
Object value;
double start, end;
for (Element row : ac.getTableAttributeRows(table)) {
if (startColumnIndex != -1) {
value = row.getAttribute(startColumn);
start = parseDateToDouble(dateFormat, value != null ? value.toString() : null, defaultStart);
} else {
start = defaultStart;
}
if (endColumnIndex != -1) {
value = row.getAttribute(endColumn);
end = parseDateToDouble(dateFormat, value != null ? value.toString() : null, defaultEnd);
} else {
end = defaultEnd;
}
if (!Double.isInfinite(start) && !Double.isInfinite(end) && start > end) {
// When start>end, check what column was provided and keep its value. If both columns were provided, set an infinite interval:
if (startColumnIndex == -1) {
start = Double.NEGATIVE_INFINITY;
} else if (endColumnIndex == -1) {
end = Double.POSITIVE_INFINITY;
} else {
start = Double.NEGATIVE_INFINITY;
end = Double.POSITIVE_INFINITY;
}
}
IntervalSet timeInterval = new IntervalSet(new double[] { start, end });
row.setAttribute(timeIntervalColumn, timeInterval);
}
return timeIntervalColumn;
}
use of org.gephi.datalab.api.AttributeColumnsController in project gephi by gephi.
the class AttributeColumnsMergeStrategiesControllerImpl method averageNumberMerge.
@Override
public Column averageNumberMerge(Table table, Column[] columnsToMerge, String newColumnTitle) {
checkTableAndColumnsAreNumberOrNumberList(table, columnsToMerge);
AttributeColumnsController ac = Lookup.getDefault().lookup(AttributeColumnsController.class);
Column newColumn;
// Create as BIGDECIMAL column by default. Then it can be duplicated to other type.
newColumn = ac.addAttributeColumn(table, newColumnTitle, BigDecimal.class);
if (newColumn == null) {
return null;
}
BigDecimal average;
for (Element row : ac.getTableAttributeRows(table)) {
average = StatisticsUtils.average(ac.getRowNumbers(row, columnsToMerge));
row.setAttribute(newColumn, average);
}
return newColumn;
}
use of org.gephi.datalab.api.AttributeColumnsController in project gephi by gephi.
the class AttributeColumnsMergeStrategiesControllerImpl method joinWithSeparatorMerge.
@Override
public Column joinWithSeparatorMerge(Table table, Column[] columnsToMerge, Class newColumnType, String newColumnTitle, String separator) {
if (table == null || columnsToMerge == null) {
throw new IllegalArgumentException("Table or columns can't be null");
}
AttributeColumnsController ac = Lookup.getDefault().lookup(AttributeColumnsController.class);
Column newColumn;
// Create as STRING column by default. Then it can be duplicated to other type.
newColumn = ac.addAttributeColumn(table, newColumnTitle, newColumnType != null ? newColumnType : String.class);
if (newColumn == null) {
return null;
}
if (separator == null) {
separator = "";
}
Object value;
StringBuilder sb;
final int columnsCount = columnsToMerge.length;
GraphModel graphModel = table.getGraph().getModel();
TimeFormat timeFormat = graphModel.getTimeFormat();
DateTimeZone timeZone = graphModel.getTimeZone();
for (Element row : ac.getTableAttributeRows(table)) {
sb = new StringBuilder();
for (int i = 0; i < columnsCount; i++) {
value = row.getAttribute(columnsToMerge[i]);
if (value != null) {
sb.append(AttributeUtils.print(value, timeFormat, timeZone));
if (i < columnsCount - 1) {
sb.append(separator);
}
}
}
ac.setAttributeValue(sb.toString(), row, newColumn);
}
return newColumn;
}
use of org.gephi.datalab.api.AttributeColumnsController in project gephi by gephi.
the class AttributeColumnsMergeStrategiesControllerImpl method maxValueNumbersMerge.
@Override
public Column maxValueNumbersMerge(Table table, Column[] columnsToMerge, String newColumnTitle) {
checkTableAndColumnsAreNumberOrNumberList(table, columnsToMerge);
AttributeColumnsController ac = Lookup.getDefault().lookup(AttributeColumnsController.class);
Column newColumn;
// Create as BIGDECIMAL column by default. Then it can be duplicated to other type.
newColumn = ac.addAttributeColumn(table, newColumnTitle, BigDecimal.class);
if (newColumn == null) {
return null;
}
BigDecimal max;
for (Element row : ac.getTableAttributeRows(table)) {
max = StatisticsUtils.maxValue(ac.getRowNumbers(row, columnsToMerge));
row.setAttribute(newColumn, max);
}
return newColumn;
}
Aggregations