Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Commit

Permalink
Network Clustering fixes on usage joinCondition for clusterOutliers() (
Browse files Browse the repository at this point in the history
…#3388)

Fix for #3367

Changes:

- Clustered nodes with shared connections are added to the same cluster
- joinCondition value `null` handled as `undefined`
- Fixed bug on adding clusters if joinCondition present and returns true

Unit tests have been added to `Network.test.js` for these changes.
  • Loading branch information
wimrijnders authored and yotamberk committed Aug 26, 2017
1 parent dc33994 commit 6adbefd
Show file tree
Hide file tree
Showing 2 changed files with 295 additions and 124 deletions.
76 changes: 57 additions & 19 deletions lib/network/modules/Clustering.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,17 @@ class ClusterEngine {
options = this._checkOptions(options);
let clusters = [];
let usedNodes = {};
let edge, edges, node, nodeId, relevantEdgeCount;
let edge, edges, relevantEdgeCount;
// collect the nodes that will be in the cluster
for (let i = 0; i < this.body.nodeIndices.length; i++) {
let childNodesObj = {};
let childEdgesObj = {};
nodeId = this.body.nodeIndices[i];
let nodeId = this.body.nodeIndices[i];
let node = this.body.nodes[nodeId];

// if this node is already used in another cluster this session, we do not have to re-evaluate it.
if (usedNodes[nodeId] === undefined) {
relevantEdgeCount = 0;
node = this.body.nodes[nodeId];
edges = [];
for (let j = 0; j < node.edges.length; j++) {
edge = node.edges[j];
Expand All @@ -219,35 +219,73 @@ class ClusterEngine {

// this node qualifies, we collect its neighbours to start the clustering process.
if (relevantEdgeCount === edgeCount) {
var checkJoinCondition = function(node) {
if (options.joinCondition === undefined || options.joinCondition === null) {
return true;
}

let clonedOptions = NetworkUtil.cloneOptions(node);
return options.joinCondition(clonedOptions);
}

let gatheringSuccessful = true;
for (let j = 0; j < edges.length; j++) {
edge = edges[j];
let childNodeId = this._getConnectedId(edge, nodeId);
// add the nodes to the list by the join condition.
if (options.joinCondition === undefined) {
if (checkJoinCondition(node)) {
childEdgesObj[edge.id] = edge;
childNodesObj[nodeId] = this.body.nodes[nodeId];
childNodesObj[nodeId] = node;
childNodesObj[childNodeId] = this.body.nodes[childNodeId];
usedNodes[nodeId] = true;
}
else {
let clonedOptions = NetworkUtil.cloneOptions(this.body.nodes[nodeId]);
if (options.joinCondition(clonedOptions) === true) {
childEdgesObj[edge.id] = edge;
childNodesObj[nodeId] = this.body.nodes[nodeId];
usedNodes[nodeId] = true;
}
else {
// this node does not qualify after all.
gatheringSuccessful = false;
break;
}
} else {
// this node does not qualify after all.
gatheringSuccessful = false;
break;
}
}

// add to the cluster queue
if (Object.keys(childNodesObj).length > 0 && Object.keys(childEdgesObj).length > 0 && gatheringSuccessful === true) {
clusters.push({nodes: childNodesObj, edges: childEdgesObj})
/**
* Search for cluster data that contains any of the node id's
* @returns {Boolean} true if no joinCondition, otherwise return value of joinCondition
*/
var findClusterData = function() {
for (var n in clusters) {
// Search for a cluster containing any of the node id's
for (var m in childNodesObj) {
if (clusters[n].nodes[m] !== undefined) {
return clusters[n];
}
}
}

return undefined;
};


// If any of the found nodes is part of a cluster found in this method,
// add the current values to that cluster
var foundCluster = findClusterData();
if (foundCluster !== undefined) {
// Add nodes to found cluster if not present
for (let m in childNodesObj) {
if (foundCluster.nodes[m] === undefined) {
foundCluster.nodes[m] = childNodesObj[m];
}
}

// Add edges to found cluster, if not present
for (let m in childEdgesObj) {
if (foundCluster.edges[m] === undefined) {
foundCluster.edges[m] = childEdgesObj[m];
}
}
} else {
// Create a new cluster group
clusters.push({nodes: childNodesObj, edges: childEdgesObj})
}
}
}
}
Expand Down

0 comments on commit 6adbefd

Please sign in to comment.