use of org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow in project gephi by gephi.
the class AbstractImporterSpreadsheet method autoDetectColumnTypes.
protected void autoDetectColumnTypes() {
try (SheetParser parser = createParser()) {
List<SheetRow> rows = getFirstRows(parser, MAX_ROWS_TO_ANALYZE_COLUMN_TYPES);
int rowCount = rows.size();
if (rowCount == 0) {
return;
}
Map<String, Integer> headerMap = parser.getHeaderMap();
if (headerMap.isEmpty()) {
return;
}
Map<String, LinkedHashSet<Class>> classMatchByHeader = new HashMap<>();
List<Class> classesToTry = Arrays.asList(new Class[] { // Classes to check, in order of preference
Boolean.class, Integer.class, Long.class, BigInteger.class, Double.class, BigDecimal.class, IntervalIntegerMap.class, IntervalLongMap.class, IntervalDoubleMap.class, IntervalStringMap.class, IntervalSet.class, TimestampIntegerMap.class, TimestampLongMap.class, TimestampDoubleMap.class, TimestampStringMap.class, TimestampSet.class });
// Initialize:
for (String column : headerMap.keySet()) {
classMatchByHeader.put(column, new LinkedHashSet<Class>());
// First assume all values match
classMatchByHeader.get(column).addAll(classesToTry);
}
// Try to parse all types:
for (SheetRow row : rows) {
for (Map.Entry<String, Integer> entry : headerMap.entrySet()) {
String column = entry.getKey();
int index = entry.getValue();
String value = row.get(index);
if (value != null) {
value = value.trim();
}
LinkedHashSet<Class> columnMatches = classMatchByHeader.get(column);
for (Class clazz : classesToTry) {
if (columnMatches.contains(clazz)) {
if (value != null && !value.isEmpty()) {
if (clazz.equals(Boolean.class)) {
// Special case for booleans to not accept 0/1, only true or false
if (!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")) {
columnMatches.remove(clazz);
}
} else {
try {
Object parsed;
if (clazz.equals(Integer.class)) {
parsed = Integer.parseInt(value);
} else if (clazz.equals(Long.class)) {
parsed = Long.parseLong(value);
} else if (clazz.equals(BigInteger.class)) {
parsed = new BigInteger(value);
} else if (clazz.equals(Double.class)) {
parsed = Double.parseDouble(value);
} else if (clazz.equals(BigDecimal.class)) {
parsed = new BigDecimal(value);
} else {
parsed = AttributeUtils.parse(value, clazz);
}
if (parsed instanceof TimeMap && ((TimeMap) parsed).isEmpty()) {
// Actually invalid
parsed = null;
}
if (parsed instanceof TimeSet && ((TimeSet) parsed).isEmpty()) {
// Actually invalid
parsed = null;
}
if (parsed == null) {
// Non empty value produced null, invalid parsing
columnMatches.remove(clazz);
}
} catch (Exception parseError) {
// Invalid value
columnMatches.remove(clazz);
}
}
}
}
}
}
}
// Obtain best match for each column:
TimeRepresentation foundTimeRepresentation = TimeRepresentation.INTERVAL;
for (String column : headerMap.keySet()) {
LinkedHashSet<Class> columnMatches = classMatchByHeader.get(column);
// Default
Class detectedClass = String.class;
// Use the detected type matching if any:
if (!columnMatches.isEmpty() && columnMatches.size() != classesToTry.size()) {
// First match
detectedClass = columnMatches.iterator().next();
}
// Change some typical column types to expected types when possible:
if (column.equalsIgnoreCase("id") || column.equalsIgnoreCase("label")) {
detectedClass = String.class;
}
if (detectedClass.equals(String.class)) {
// No other thing than String found, try to guess very probable dynamic types:
if (column.toLowerCase().contains("interval")) {
detectedClass = IntervalSet.class;
}
if (column.toLowerCase().contains("timestamp")) {
detectedClass = TimestampSet.class;
}
if (column.equalsIgnoreCase("timeset")) {
if (foundTimeRepresentation == TimeRepresentation.INTERVAL) {
detectedClass = IntervalSet.class;
} else {
detectedClass = TimestampSet.class;
}
}
}
if (getMode() == Mode.EDGES_TABLE) {
if (column.equalsIgnoreCase("source") || column.equalsIgnoreCase("target") || column.equalsIgnoreCase("type") || column.equalsIgnoreCase("kind")) {
detectedClass = String.class;
}
// Favor double types for weight column:
if (column.equalsIgnoreCase("weight")) {
if (columnMatches.contains(Double.class)) {
detectedClass = Double.class;
} else if (columnMatches.contains(IntervalDoubleMap.class)) {
detectedClass = IntervalDoubleMap.class;
} else if (columnMatches.contains(TimestampDoubleMap.class)) {
detectedClass = TimestampDoubleMap.class;
}
}
}
setColumnClass(column, detectedClass);
if (TimestampSet.class.isAssignableFrom(detectedClass) || TimestampMap.class.isAssignableFrom(detectedClass)) {
foundTimeRepresentation = TimeRepresentation.TIMESTAMP;
}
}
setTimeRepresentation(foundTimeRepresentation);
} catch (IOException ex) {
// NOOP
}
}
use of org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow in project gephi by gephi.
the class ImportEdgesProcess method execute.
@Override
public boolean execute() {
setupColumnsIndexesAndFindSpecialColumns(Arrays.asList(EDGE_SOURCE, EDGE_TARGET, EDGE_TYPE, EDGE_KIND, EDGE_ID, EDGE_LABEL), generalConfig.getColumnsClasses());
Integer sourceColumnIndex = specialColumnsIndexMap.get(EDGE_SOURCE);
Integer targetColumnIndex = specialColumnsIndexMap.get(EDGE_TARGET);
// Direction
Integer typeColumnIndex = specialColumnsIndexMap.get(EDGE_TYPE);
// Kind for parallel edges
Integer kindColumnIndex = specialColumnsIndexMap.get(EDGE_KIND);
Integer idColumnIndex = specialColumnsIndexMap.get(EDGE_ID);
Integer labelColumnIndex = specialColumnsIndexMap.get(EDGE_LABEL);
Progress.start(progressTicket);
for (SheetRow row : parser) {
if (cancel) {
break;
}
if (!checkRow(row)) {
continue;
}
String source = null;
String target = null;
String id = null;
String label = null;
EdgeDirection direction = EdgeDirection.DIRECTED;
String kind = null;
if (sourceColumnIndex != null) {
source = row.get(sourceColumnIndex);
}
if (targetColumnIndex != null) {
target = row.get(targetColumnIndex);
}
if (typeColumnIndex != null) {
String type = row.get(typeColumnIndex);
if ("undirected".equalsIgnoreCase(type)) {
direction = EdgeDirection.UNDIRECTED;
}
}
if (kindColumnIndex != null) {
kind = row.get(kindColumnIndex);
}
if (idColumnIndex != null) {
id = row.get(idColumnIndex);
}
if (labelColumnIndex != null) {
label = row.get(labelColumnIndex);
}
EdgeDraft edge = id != null ? container.factory().newEdgeDraft(id) : container.factory().newEdgeDraft();
if (label != null) {
edge.setLabel(label);
}
if (source == null || target == null) {
logError(getMessage("ImportEdgesProcess.error.noSourceOrTargetData"));
continue;
}
if (!container.nodeExists(source)) {
container.addNode(container.factory().newNodeDraft(source));
}
if (!container.nodeExists(target)) {
container.addNode(container.factory().newNodeDraft(target));
}
edge.setSource(container.getNode(source));
edge.setTarget(container.getNode(target));
edge.setDirection(direction);
if (kind != null) {
edge.setType(kind);
}
for (Map.Entry<String, Integer> columnEntry : headersIndexMap.entrySet()) {
String column = columnEntry.getKey();
Integer index = columnEntry.getValue();
Class type = headersClassMap.get(column);
if (type == null) {
continue;
}
Object value = row.get(index);
if (value != null) {
value = parseValue((String) value, type, column);
if (value != null) {
// Note: we allow any type on weight column, to support dynamic weights
if (column.equalsIgnoreCase("weight") && value instanceof Number) {
edge.setWeight(((Number) value).doubleValue());
} else {
edge.setValue(column, value);
}
}
}
}
container.addEdge(edge);
}
Progress.finish(progressTicket);
return !cancel;
}
use of org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow in project gephi by gephi.
the class ImportNodesProcess method execute.
@Override
public boolean execute() {
setupColumnsIndexesAndFindSpecialColumns(Arrays.asList(NODE_ID, NODE_LABEL), generalConfig.getColumnsClasses());
Integer idColumnIndex = specialColumnsIndexMap.get(NODE_ID);
Integer labelColumnIndex = specialColumnsIndexMap.get(NODE_LABEL);
Progress.start(progressTicket);
for (SheetRow row : parser) {
if (cancel) {
break;
}
if (!checkRow(row)) {
continue;
}
String id = null;
String label = null;
if (idColumnIndex != null) {
id = row.get(idColumnIndex);
}
if (labelColumnIndex != null) {
label = row.get(labelColumnIndex);
}
NodeDraft node = id != null ? container.factory().newNodeDraft(id) : container.factory().newNodeDraft();
if (label != null) {
node.setLabel(label);
}
for (Map.Entry<String, Integer> columnEntry : headersIndexMap.entrySet()) {
String column = columnEntry.getKey();
Integer index = columnEntry.getValue();
Class type = headersClassMap.get(column);
if (type == null) {
continue;
}
Object value = row.get(index);
if (value != null) {
value = parseValue((String) value, type, column);
if (value != null) {
node.setValue(column, value);
}
}
}
container.addNode(node);
}
Progress.finish(progressTicket);
return !cancel;
}
use of org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow in project gephi by gephi.
the class AbstractWizardVisualPanel1 method refreshPreviewTable.
public void refreshPreviewTable() {
try (SheetParser parser = importer.createParser()) {
Map<String, Integer> headerMap = parser.getHeaderMap();
final String[] headers = headerMap.keySet().toArray(new String[0]);
columnCount = headers.length;
hasSourceNodeColumn = false;
hasTargetNodeColumn = false;
int sourceColumnIndex = 0;
int targetColumnIndex = 0;
for (String header : headers) {
if (header.equalsIgnoreCase("source")) {
hasSourceNodeColumn = true;
sourceColumnIndex = headerMap.get(header);
}
if (header.equalsIgnoreCase("target")) {
hasTargetNodeColumn = true;
targetColumnIndex = headerMap.get(header);
}
}
ArrayList<String[]> records = new ArrayList<>();
hasRowsMissingSourcesOrTargets = false;
final SpreadsheetGeneralConfiguration.Mode mode = getSelectedMode();
int maxRowSize = 0;
String[] currentRecord;
Iterator<SheetRow> iterator = parser.iterator();
int count = 0;
while (iterator.hasNext() && count < MAX_ROWS_PREVIEW) {
count++;
final SheetRow row = iterator.next();
final int rowSize = row.size();
maxRowSize = Math.max(maxRowSize, row.size());
currentRecord = new String[rowSize];
for (int i = 0; i < rowSize; i++) {
currentRecord[i] = row.get(i);
}
// Search for missing source or target columns for edges table
if (mode == SpreadsheetGeneralConfiguration.Mode.EDGES_TABLE) {
if (rowSize <= sourceColumnIndex || rowSize <= targetColumnIndex || currentRecord[sourceColumnIndex] == null || currentRecord[targetColumnIndex] == null) {
hasRowsMissingSourcesOrTargets = true;
}
}
records.add(currentRecord);
}
final String[] columnNames = headers;
final String[][] values = records.toArray(new String[0][]);
final int rowSize = maxRowSize;
final JTable table = getPreviewTable();
table.setModel(new TableModel() {
@Override
public int getRowCount() {
return values.length;
}
@Override
public int getColumnCount() {
return rowSize;
}
@Override
public String getColumnName(int columnIndex) {
if (columnIndex > columnNames.length - 1) {
return null;
}
return columnNames[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (values[rowIndex].length > columnIndex) {
return values[rowIndex][columnIndex];
} else {
return null;
}
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
});
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
boolean needsHeader = headers.length > 0;
getPreviewTableScrollPane().setColumnHeaderView(needsHeader ? table.getTableHeader() : null);
}
});
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
use of org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow in project gephi by gephi.
the class ImportAdjacencyListProcess method execute.
@Override
public boolean execute() {
container.setFillLabelWithId(true);
Progress.start(progressTicket);
for (SheetRow row : parser) {
if (cancel) {
break;
}
int size = row.size();
if (size > 0) {
String source = row.get(0);
if (source != null) {
for (int i = 1; i < size; i++) {
String target = row.get(i);
if (target != null) {
addEdge(source.trim(), target.trim());
} else {
logError(getMessage("ImportAdjacencyListProcess.error.missingTarget", i));
}
}
} else {
logError(getMessage("ImportAdjacencyListProcess.error.missingSource"));
}
}
}
Progress.finish(progressTicket);
return !cancel;
}
Aggregations