Search in sources :

Example 1 with SheetRow

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
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) TimeMap(org.gephi.graph.api.types.TimeMap) SheetParser(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser) TimeSet(org.gephi.graph.api.types.TimeSet) TimestampMap(org.gephi.graph.api.types.TimestampMap) TimestampSet(org.gephi.graph.api.types.TimestampSet) TimeRepresentation(org.gephi.graph.api.TimeRepresentation) IntervalDoubleMap(org.gephi.graph.api.types.IntervalDoubleMap) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) IOException(java.io.IOException) BigInteger(java.math.BigInteger) SheetRow(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow) BigInteger(java.math.BigInteger) TimestampLongMap(org.gephi.graph.api.types.TimestampLongMap) HashMap(java.util.HashMap) IntervalLongMap(org.gephi.graph.api.types.IntervalLongMap) TimestampMap(org.gephi.graph.api.types.TimestampMap) Map(java.util.Map) TimestampIntegerMap(org.gephi.graph.api.types.TimestampIntegerMap) IntervalIntegerMap(org.gephi.graph.api.types.IntervalIntegerMap) IntervalStringMap(org.gephi.graph.api.types.IntervalStringMap) TimestampDoubleMap(org.gephi.graph.api.types.TimestampDoubleMap) TimestampStringMap(org.gephi.graph.api.types.TimestampStringMap) IntervalDoubleMap(org.gephi.graph.api.types.IntervalDoubleMap) TimeMap(org.gephi.graph.api.types.TimeMap)

Example 2 with SheetRow

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;
}
Also used : EdgeDraft(org.gephi.io.importer.api.EdgeDraft) SheetRow(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow) EdgeDirection(org.gephi.io.importer.api.EdgeDirection) Map(java.util.Map)

Example 3 with SheetRow

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;
}
Also used : SheetRow(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow) NodeDraft(org.gephi.io.importer.api.NodeDraft) Map(java.util.Map)

Example 4 with SheetRow

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);
    }
}
Also used : ArrayList(java.util.ArrayList) SheetParser(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser) IOException(java.io.IOException) SheetRow(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow) SpreadsheetGeneralConfiguration(org.gephi.io.importer.plugin.file.spreadsheet.process.SpreadsheetGeneralConfiguration) JTable(javax.swing.JTable) TableModelListener(javax.swing.event.TableModelListener) TableModel(javax.swing.table.TableModel)

Example 5 with SheetRow

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;
}
Also used : SheetRow(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow)

Aggregations

SheetRow (org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow)7 IOException (java.io.IOException)3 Map (java.util.Map)3 SheetParser (org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser)3 ArrayList (java.util.ArrayList)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 JTable (javax.swing.JTable)1 TableModelListener (javax.swing.event.TableModelListener)1 TableModel (javax.swing.table.TableModel)1 TimeRepresentation (org.gephi.graph.api.TimeRepresentation)1 IntervalDoubleMap (org.gephi.graph.api.types.IntervalDoubleMap)1 IntervalIntegerMap (org.gephi.graph.api.types.IntervalIntegerMap)1 IntervalLongMap (org.gephi.graph.api.types.IntervalLongMap)1 IntervalStringMap (org.gephi.graph.api.types.IntervalStringMap)1 TimeMap (org.gephi.graph.api.types.TimeMap)1 TimeSet (org.gephi.graph.api.types.TimeSet)1 TimestampDoubleMap (org.gephi.graph.api.types.TimestampDoubleMap)1