Refactor: Extract getStress
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { pointOnCircle, sumDistError } from "./helpers";
|
||||
import {pointOnCircle, sumDistError} from "./helpers";
|
||||
|
||||
export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleSubset, realDistances) {
|
||||
let
|
||||
|
||||
24
src/link.js
24
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;
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
19
src/stress.js
Normal file
19
src/stress.js
Normal file
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user