diff --git a/src/hybridSimulation.js b/src/hybridSimulation.js index f5a5c18..a11cffc 100644 --- a/src/hybridSimulation.js +++ b/src/hybridSimulation.js @@ -1,12 +1,11 @@ -import { dispatch } from "d3-dispatch"; +import {dispatch} from "d3-dispatch"; import constant from "./constant"; import interpBruteForce from "./interpolation/interpBruteForce"; import interpolationPivots from "./interpolation/interpolationPivots"; import neighbourSamplingDistance from "./neighbourSamplingDistance"; -import { takeSampleFrom } from "./interpolation/helpers"; +import {takeSampleFrom} from "./interpolation/helpers"; export default function (nodes, config) { - var hybrid, fullSimulation, SAMPLE_ITERATIONS = readConf(config, "iteration", 300), @@ -152,6 +151,6 @@ export default function (nodes, config) { }; } -function readConf(config, prop, default){ - return config.hasOwnProperty(prop) ? config.prop : default, +function readConf(config, prop, def){ + return config.hasOwnProperty(prop) ? config.prop : def; } diff --git a/src/interpolation/interpBruteForce.js b/src/interpolation/interpBruteForce.js index 27748e7..8f6a668 100644 --- a/src/interpolation/interpBruteForce.js +++ b/src/interpolation/interpBruteForce.js @@ -1,5 +1,5 @@ -import { pointOnCircle, takeSampleFrom } from "./helpers"; -import { placeNearToNearestNeighbour } from "./interpCommon"; +import {pointOnCircle, takeSampleFrom} from "./helpers"; +import {placeNearToNearestNeighbour} from "./interpCommon"; export default function(sampleSet, remainderSet, distanceFn) { // var distance = calculateEuclideanDistance; diff --git a/src/interpolation/interpCommon.js b/src/interpolation/interpCommon.js index 4966c4c..82d15a7 100644 --- a/src/interpolation/interpCommon.js +++ b/src/interpolation/interpCommon.js @@ -1,4 +1,4 @@ -import { pointOnCircle, sumDistError } from "./helpers"; +import {pointOnCircle, sumDistError} from "./helpers"; export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleSubset, realDistances) { let diff --git a/src/link.js b/src/link.js index 9cb3acd..40d4e6b 100644 --- a/src/link.js +++ b/src/link.js @@ -1,6 +1,7 @@ import constant from "./constant"; import jiggle from "./jiggle"; import {map} from "d3-collection"; +import {getStress} from "./stress"; /** * Extended link force algorithm to include the stress metric for @@ -101,27 +102,6 @@ export default function(links) { } } - /** - * Calculates the stress. Basically, it computes the difference between - * high dimensional distance and real distance. The lower the stress is, - * the better layout. - * @return {number} - stress of the layout. - */ - function getStress() { - var m = links.length, - totalDiffSq = 0, - totalHighDistSq = 0, - link; - for (var i = 0, source, target, realDist, highDist; i < m; i++) { - link = links[i], source = link.source, target = link.target; - realDist = Math.hypot(target.x-source.x, target.y-source.y); - highDist = distances[i]; - totalDiffSq += Math.pow(realDist-highDist, 2); - totalHighDistSq += highDist * highDist; - } - return Math.sqrt(totalDiffSq/totalHighDistSq); - } - force.initialize = function(_) { nodes = _; initialize(); @@ -148,7 +128,7 @@ export default function(links) { }; force.stress = function() { - return getStress(); + return getStress(nodes, function(s,t){return distance({source: s, target: t});}); } return force; diff --git a/src/neighbourSamplingDistance.js b/src/neighbourSamplingDistance.js index 9a61001..7f02062 100644 --- a/src/neighbourSamplingDistance.js +++ b/src/neighbourSamplingDistance.js @@ -1,5 +1,6 @@ import constant from "./constant"; import jiggle from "./jiggle"; +import {getStress} from "./stress"; /** * Set the node id accessor to the specified i. @@ -216,28 +217,6 @@ export default function () { } } - /** - * Calculates the stress. Basically, it computes the difference between - * high dimensional distance and real distance. The lower the stress is, - * the better layout. - * @return {number} - stress of the layout. - */ - function getStress() { - let totalDiffSq = 0, totalHighDistSq = 0; - for (let i = 0, source, target, realDist, highDist; i < nodes.length; i++) { - for (let j = 0; j < nodes.length; j++) { - if (i !== j) { - source = nodes[i], target = nodes[j]; - realDist = Math.hypot(target.x - source.x, target.y - source.y); - highDist = +distance(source, target); - totalDiffSq += Math.pow(realDist - highDist, 2); - totalHighDistSq += highDist * highDist; - } - } - } - return Math.sqrt(totalDiffSq / totalHighDistSq); - } - /** * Calculates the average velocity of the force calculation at the * current iteration. @@ -280,7 +259,7 @@ export default function () { }; force.stress = function () { - return getStress(); + return getStress(nodes, distance); }; force.velocity = function () { diff --git a/src/stress.js b/src/stress.js new file mode 100644 index 0000000..de20514 --- /dev/null +++ b/src/stress.js @@ -0,0 +1,19 @@ +/** + * Calculates the stress. Basically, it computes the difference between + * high dimensional distance and real distance. The lower the stress is, + * the better layout. + * @return {number} - stress of the layout. + */ +export function getStress(nodes, distance) { + let totalDiffSq = 0, totalHighDistSq = 0; + for (let j = 0; j < nodes.length; j++) { + for (let i = 0; i < j; i++) { + let source = nodes[i], target = nodes[j]; + let lowDDist = Math.hypot(target.x - source.x, target.y - source.y); + let highDDist = distance(source, target); + totalDiffSq += Math.pow(highDDist - lowDDist, 2); + totalHighDistSq += highDDist * highDDist; + } + } + return Math.sqrt(totalDiffSq / totalHighDistSq); +}