Search in sources :

Example 1 with IntervalDoubleMap

use of org.gephi.graph.api.types.IntervalDoubleMap 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

Column (org.gephi.graph.api.Column)1 IntervalDoubleMap (org.gephi.graph.api.types.IntervalDoubleMap)1 IntervalMap (org.gephi.graph.api.types.IntervalMap)1 TimeMap (org.gephi.graph.api.types.TimeMap)1 TimestampDoubleMap (org.gephi.graph.api.types.TimestampDoubleMap)1 ColumnDraft (org.gephi.io.importer.api.ColumnDraft)1 Issue (org.gephi.io.importer.api.Issue)1