Search in sources :

Example 11 with TimeMap

use of org.gephi.graph.api.types.TimeMap in project gephi by gephi.

the class AbstractProcessor method flushEdgeWeight.

protected void flushEdgeWeight(ContainerUnloader container, EdgeDraft edgeDraft, Edge edge, boolean newEdge) {
    Column weightColumn = graphModel.getEdgeTable().getColumn("weight");
    ColumnDraft weightColumnDraft = container.getEdgeColumn("weight");
    boolean weightColumnDraftIsDynamic = weightColumnDraft != null && weightColumnDraft.isDynamic();
    if (weightColumn.isDynamic() != weightColumnDraftIsDynamic) {
        Class weightColumnDraftTypeClass = weightColumnDraft != null ? weightColumnDraft.getResolvedTypeClass(container) : Double.class;
        if (!columnsTypeMismatchAlreadyWarned.contains(weightColumn)) {
            columnsTypeMismatchAlreadyWarned.add(weightColumn);
            String error = NbBundle.getMessage(AbstractProcessor.class, "AbstractProcessor.error.columnTypeMismatch", weightColumn.getId(), weightColumn.getTypeClass().getSimpleName(), weightColumnDraftTypeClass.getSimpleName());
            report.logIssue(new Issue(error, Issue.Level.SEVERE));
        }
        return;
    }
    if (weightColumn.isDynamic()) {
        Object val = edgeDraft.getValue("weight");
        if (val != null && val instanceof TimeMap) {
            TimeMap valMap = (TimeMap) val;
            if (Number.class.isAssignableFrom(valMap.getTypeClass())) {
                final TimeMap newMap;
                if (val instanceof IntervalMap) {
                    newMap = new IntervalDoubleMap();
                } else {
                    newMap = new TimestampDoubleMap();
                }
                TimeMap existingMap = (TimeMap) edge.getAttribute("weight");
                if (existingMap != null) {
                    Object[] keys2 = existingMap.toKeysArray();
                    Object[] vals2 = existingMap.toValuesArray();
                    for (int i = 0; i < keys2.length; i++) {
                        newMap.put(keys2[i], ((Number) vals2[i]).doubleValue());
                    }
                }
                Object[] keys1 = valMap.toKeysArray();
                Object[] vals1 = valMap.toValuesArray();
                for (int i = 0; i < keys1.length; i++) {
                    try {
                        newMap.put(keys1[i], ((Number) vals1[i]).doubleValue());
                    } catch (IllegalArgumentException e) {
                    // Overlapping intervals, ignore
                    }
                }
                edge.setAttribute("weight", newMap);
            }
        }
    } else if (!newEdge) {
        if (edgeDraft.getTimeSet() != null || edgeDraft.getValue("timeset") != null || edge.getAttribute("timeset") != null) {
            // Don't merge double (non dynamic) weights when the edges have dynamic time intervals/timestamps, they are the same edge in different periods of time
            return;
        }
        // Merge the existing edge and the draft edge weights:
        double result = edge.getWeight();
        edgeCountForAverage.addTo(edge, 1);
        int edgeCount = edgeCountForAverage.getInt(edge);
        switch(containers[0].getEdgesMergeStrategy()) {
            case AVG:
                result = (edge.getWeight() * edgeCount + edgeDraft.getWeight()) / (edgeCount + 1);
                break;
            case MAX:
                result = Math.max(edgeDraft.getWeight(), edge.getWeight());
                break;
            case MIN:
                result = Math.min(edgeDraft.getWeight(), edge.getWeight());
                break;
            case SUM:
                result = edgeDraft.getWeight() + edge.getWeight();
                break;
            case FIRST:
                result = edge.getWeight();
                break;
            case LAST:
                result = edgeDraft.getWeight();
                break;
            default:
                break;
        }
        edge.setWeight(result);
    }
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) Issue(org.gephi.io.importer.api.Issue) TimeMap(org.gephi.graph.api.types.TimeMap) IntervalDoubleMap(org.gephi.graph.api.types.IntervalDoubleMap) Column(org.gephi.graph.api.Column) TimestampDoubleMap(org.gephi.graph.api.types.TimestampDoubleMap) IntervalMap(org.gephi.graph.api.types.IntervalMap)

Aggregations

TimeMap (org.gephi.graph.api.types.TimeMap)11 ColumnDraft (org.gephi.io.importer.api.ColumnDraft)4 Element (org.gephi.graph.api.Element)3 TimeSet (org.gephi.graph.api.types.TimeSet)3 Column (org.gephi.graph.api.Column)2 IntervalDoubleMap (org.gephi.graph.api.types.IntervalDoubleMap)2 TimestampDoubleMap (org.gephi.graph.api.types.TimestampDoubleMap)2 EdgeDraft (org.gephi.io.importer.api.EdgeDraft)2 Issue (org.gephi.io.importer.api.Issue)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Edge (org.gephi.graph.api.Edge)1 Index (org.gephi.graph.api.Index)1 TimeRepresentation (org.gephi.graph.api.TimeRepresentation)1 IntervalIntegerMap (org.gephi.graph.api.types.IntervalIntegerMap)1