Search in sources :

Example 26 with NodeBase

use of au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase in project constellation by constellation-app.

the class InfomapGreedy method sortTree.

@Override
protected void sortTree(final NodeBase parent) {
    if (parent.getSubInfomap() != null) {
        parent.getSubInfomap().sortTree();
    }
    final MultiMap<Double, NodeBase> sortedModules = new MultiMap<>((d1, d2) -> (int) Math.signum(d2 - d1));
    if (DEBUG && parent.getChildDegree() > 0) {
        for (final NodeBase child : parent.getChildren()) {
            LOGGER.log(Level.INFO, "{0}", child.getId());
        }
    }
    for (final NodeBase child : parent.getChildren()) {
        sortTree(child);
        final double rank = getNode(child).getData().getFlow();
        sortedModules.put(rank, child);
    }
    parent.releaseChildren();
    int sortedIndex = 0;
    for (final Map.Entry<Double, NodeBase> entry : sortedModules.entrySet()) {
        parent.addChild(entry.getValue());
        entry.getValue().setIndex(sortedIndex);
        sortedIndex++;
    }
}
Also used : NodeBase(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase) MultiMap(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.util.MultiMap) MultiMap(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.util.MultiMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 27 with NodeBase

use of au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase in project constellation by constellation-app.

the class InfomapGreedy method initConstantInfomapTerms.

@Override
protected void initConstantInfomapTerms() {
    nodeFlowLogNodeFlow = 0;
    // For each module...
    for (final NodeBase nodeBase : activeNetwork) {
        final Node node = getNode(nodeBase);
        nodeFlowLogNodeFlow += plogp(node.getData().getFlow());
    }
}
Also used : NodeBase(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase) Node(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.Node)

Example 28 with NodeBase

use of au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase in project constellation by constellation-app.

the class InfomapGreedy method transformNodeFlowToEnterFlow.

@Override
protected void transformNodeFlowToEnterFlow(final NodeBase parent) {
    for (final NodeBase moduleBase : parent.getChildren()) {
        final Node module = getNode(moduleBase);
        module.getData().setFlow(module.getData().getEnterFlow());
    }
}
Also used : NodeBase(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase) Node(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.Node)

Example 29 with NodeBase

use of au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase in project constellation by constellation-app.

the class InfomapGreedy method consolidateModules.

@Override
protected int consolidateModules(final boolean replaceExistingStructure, final boolean asSubModules) {
    if (DEBUG) {
        final String log = String.format("%s.consolidateModules(%s,%s)%n", getClass().getSimpleName(), replaceExistingStructure, asSubModules);
        LOGGER.log(Level.INFO, log);
    }
    final int numNodes = activeNetwork.size();
    final NodeBase[] modules = new NodeBase[numNodes];
    final boolean activeNetworkAlreadyHaveModuleLevel = activeNetwork.get(0).getParent() != getRoot();
    final boolean activeNetworkIsLeafNetwork = activeNetwork.get(0).isLeaf();
    if (asSubModules) {
        assert activeNetworkAlreadyHaveModuleLevel;
        // Release the pointers from modules to leaf nodes so that the new submodules will be inserted as its only children.
        for (final NodeBase module : getRoot().getChildren()) {
            module.releaseChildren();
        }
    } else {
        // Happens after optimizing fine-tune and when moving leaf nodes to super clusters.
        if (activeNetworkAlreadyHaveModuleLevel) {
            if (DEBUG) {
                final String log = String.format("Replace existing %d modules with its children before consolidating the %d dynamic modules...\n", getNumTopModules(), getNumActiveModules());
                LOGGER.log(Level.INFO, log);
            }
            getRoot().replaceChildrenWithGrandChildren();
            assert activeNetwork.get(0).getParent() == getRoot();
        }
        getRoot().releaseChildren();
    }
    // Create the new module nodes and re-parent the active network from its common parent to the new module level.
    for (int i = 0; i < numNodes; ++i) {
        final NodeBase node = activeNetwork.get(i);
        final int moduleIndex = node.getIndex();
        if (modules[moduleIndex] == null) {
            modules[moduleIndex] = treeData.getNodeFactory().createNode(moduleFlowData[moduleIndex]);
            node.getParent().addChild(modules[moduleIndex]);
            modules[moduleIndex].setIndex(moduleIndex);
        }
        modules[moduleIndex].addChild(node);
    }
    if (asSubModules) {
        if (DEBUG) {
            final String log = String.format("Consolidated %d submodules under %d modules, store module structure before releasing it...\n", getNumActiveModules(), getNumTopModules());
            LOGGER.log(Level.INFO, log);
        }
        // Store the module structure on the submodules.
        int moduleIndex = 0;
        for (final NodeBase module : getRoot().getChildren()) {
            for (final NodeBase subModule : module.getChildren()) {
                subModule.setIndex(moduleIndex);
            }
            moduleIndex++;
        }
        if (replaceExistingStructure) {
            // Remove the module level.
            getRoot().replaceChildrenWithGrandChildren();
        }
    }
    // Aggregate links from lower level to the new modular level
    /*
         typedef std::pair<NodeBase*, NodeBase*> NodePair
         typedef std::map<NodePair, double> EdgeMap
         EdgeMap moduleLinks
         */
    final Map<Tuple<NodeBase, NodeBase>, Double> moduleLinks = new TreeMap<>((lhs, rhs) -> {
        if (lhs.getFirst().getId() < rhs.getFirst().getId()) {
            return -1;
        }
        if (lhs.getFirst().getId() > rhs.getFirst().getId()) {
            return 1;
        }
        if (lhs.getSecond().getId() < rhs.getSecond().getId()) {
            return -1;
        }
        if (lhs.getSecond().getId() > rhs.getSecond().getId()) {
            return 1;
        }
        return 0;
    });
    for (final NodeBase node : activeNetwork) {
        final NodeBase parent = node.getParent();
        for (final Edge<NodeBase> edge : node.getOutEdges()) {
            final NodeBase otherParent = edge.getTarget().getParent();
            if (otherParent != parent) {
                NodeBase m1 = parent;
                NodeBase m2 = otherParent;
                // If undirected, the order may be swapped to aggregate the edge on an opposite one.
                if (config.isUndirected() && m1.getIndex() > m2.getIndex()) {
                    NodeBase t = m1;
                    m1 = m2;
                    m2 = t;
                }
                // Insert the node pair in the edge map.
                // If not inserted, add the flow value to existing node pair.
                final Tuple<NodeBase, NodeBase> key = Tuple.create(m1, m2);
                if (moduleLinks.containsKey(key)) {
                    final double d = moduleLinks.get(key);
                    moduleLinks.put(key, d + edge.getData().flow);
                } else {
                    moduleLinks.put(key, edge.getData().flow);
                }
            }
        }
    }
    // Add the aggregated edge flow structure to the new modules.
    for (final Map.Entry<Tuple<NodeBase, NodeBase>, Double> entry : moduleLinks.entrySet()) {
        final Tuple<NodeBase, NodeBase> nodePair = entry.getKey();
        final double value = entry.getValue();
        nodePair.getFirst().addOutEdge(nodePair.getSecond(), 0, value);
    }
    // Replace active network with its children if not at leaf level.
    if (!activeNetworkIsLeafNetwork && replaceExistingStructure) {
        for (final NodeBase node : activeNetwork) {
            node.replaceWithChildren();
        }
    }
    // Calculate the number of non-trivial modules.
    numNonTrivialTopModules = 0;
    for (final NodeBase module : getRoot().getChildren()) {
        if (module.getChildDegree() != 1) {
            numNonTrivialTopModules++;
        }
    }
    return getNumActiveModules();
}
Also used : TreeMap(java.util.TreeMap) NodeBase(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase) MultiMap(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.util.MultiMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Tuple(au.gov.asd.tac.constellation.utilities.datastructure.Tuple)

Example 30 with NodeBase

use of au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase in project constellation by constellation-app.

the class TreeData method addEdge.

public void addEdge(final int sourceIndex, final int targetIndex, final double weight, final double flow) {
    final NodeBase source = leafNodes.get(sourceIndex);
    final NodeBase target = leafNodes.get(targetIndex);
    source.addOutEdge(target, weight, flow);
    nLeafEdges++;
}
Also used : NodeBase(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase)

Aggregations

NodeBase (au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase)30 Node (au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.Node)6 PartitionQueue (au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.PartitionQueue)2 MultiMap (au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.util.MultiMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 FlowBase (au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.traits.FlowBase)1 TreeData (au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.tree.TreeData)1 Tuple (au.gov.asd.tac.constellation.utilities.datastructure.Tuple)1 File (java.io.File)1