แก้ coding style
This commit is contained in:
@@ -14,12 +14,12 @@ import {quadtree} from "d3-quadtree";
|
|||||||
*/
|
*/
|
||||||
export default function() {
|
export default function() {
|
||||||
var nodes,
|
var nodes,
|
||||||
node,
|
node,
|
||||||
alpha,
|
alpha,
|
||||||
distance = constant(300),
|
distance = constant(300),
|
||||||
theta = 0.5;
|
theta = 0.5;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a quadtree at every iteration and apply the forces by visiting
|
* Constructs a quadtree at every iteration and apply the forces by visiting
|
||||||
* each node in a tree.
|
* each node in a tree.
|
||||||
* @param {number} _ - controls the stopping of the
|
* @param {number} _ - controls the stopping of the
|
||||||
@@ -52,10 +52,7 @@ export default function() {
|
|||||||
quad.data = children[Math.floor(Math.random() * children.length)];
|
quad.data = children[Math.floor(Math.random() * children.length)];
|
||||||
quad.x = quad.data.x;
|
quad.x = quad.data.x;
|
||||||
quad.y = quad.data.y;
|
quad.y = quad.data.y;
|
||||||
}
|
} else { // For leaf nodes, accumulate forces from coincident quadrants.
|
||||||
|
|
||||||
// For leaf nodes, accumulate forces from coincident quadrants.
|
|
||||||
else {
|
|
||||||
q = quad;
|
q = quad;
|
||||||
q.x = q.data.x;
|
q.x = q.data.x;
|
||||||
q.y = q.data.y;
|
q.y = q.data.y;
|
||||||
@@ -75,9 +72,9 @@ export default function() {
|
|||||||
function apply(quad, x1, _, x2) {
|
function apply(quad, x1, _, x2) {
|
||||||
|
|
||||||
var x = quad.data.x + quad.data.vx - node.x - node.vx,
|
var x = quad.data.x + quad.data.vx - node.x - node.vx,
|
||||||
y = quad.data.y + quad.data.vy - node.y - node.vy,
|
y = quad.data.y + quad.data.vy - node.y - node.vy,
|
||||||
w = x2 - x1,
|
w = x2 - x1,
|
||||||
l = Math.sqrt(x * x + y * y);
|
l = Math.sqrt(x * x + y * y);
|
||||||
|
|
||||||
// Apply the Barnes-Hut approximation if possible.
|
// Apply the Barnes-Hut approximation if possible.
|
||||||
// Limit forces for very close nodes; randomize direction if coincident.
|
// Limit forces for very close nodes; randomize direction if coincident.
|
||||||
@@ -93,10 +90,7 @@ export default function() {
|
|||||||
node.vy += y;
|
node.vy += y;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if (quad.length) return; // Otherwise, process points directly.
|
||||||
|
|
||||||
// Otherwise, process points directly.
|
|
||||||
else if (quad.length) return;
|
|
||||||
|
|
||||||
// Limit forces for very close nodes; randomize direction if coincident.
|
// Limit forces for very close nodes; randomize direction if coincident.
|
||||||
if (quad.data !== node || quad.next) {
|
if (quad.data !== node || quad.next) {
|
||||||
@@ -114,7 +108,7 @@ export default function() {
|
|||||||
} while (quad = quad.next);
|
} while (quad = quad.next);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the stress. Basically, it computes the difference between
|
* Calculates the stress. Basically, it computes the difference between
|
||||||
* high dimensional distance and real distance. The lower the stress is,
|
* high dimensional distance and real distance. The lower the stress is,
|
||||||
* the better layout.
|
* the better layout.
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ export default function (sim, forceS, forceF) {
|
|||||||
alreadyRanIterations,
|
alreadyRanIterations,
|
||||||
hybrid;
|
hybrid;
|
||||||
|
|
||||||
if(simulation != undefined) initSimulation();
|
if(simulation != undefined) initSimulation();
|
||||||
if(forceS != undefined || forceF != undefined) initForces();
|
if(forceS != undefined || forceF != undefined) initForces();
|
||||||
|
|
||||||
// Performed on first run
|
// Performed on first run
|
||||||
function initialize() {
|
function initialize() {
|
||||||
|
|||||||
@@ -10,8 +10,8 @@
|
|||||||
*/
|
*/
|
||||||
export function takeSampleFrom(sourceList, amount) {
|
export function takeSampleFrom(sourceList, amount) {
|
||||||
let randElements = [],
|
let randElements = [],
|
||||||
max = sourceList.length,
|
max = sourceList.length,
|
||||||
swap = false;
|
swap = false;
|
||||||
|
|
||||||
if (amount >= max) {
|
if (amount >= max) {
|
||||||
return {sample: sourceList, remainder: {}};
|
return {sample: sourceList, remainder: {}};
|
||||||
@@ -40,8 +40,7 @@ export function takeSampleFrom(sourceList, amount) {
|
|||||||
sample: remainder,
|
sample: remainder,
|
||||||
remainder: randElements
|
remainder: randElements
|
||||||
};
|
};
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return {
|
return {
|
||||||
sample: randElements,
|
sample: randElements,
|
||||||
remainder: remainder
|
remainder: remainder
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export default function(sampleSet, remainderSet, distanceFn, endingIts) {
|
|||||||
sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample,
|
sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample,
|
||||||
sampleSubsetDistanceCache = [];
|
sampleSubsetDistanceCache = [];
|
||||||
|
|
||||||
// For each datapoint "node" to be interpolated
|
// For each datapoint "node" to be interpolated
|
||||||
for (let i = remainderSet.length-1; i>=0; i--) {
|
for (let i = remainderSet.length-1; i>=0; i--) {
|
||||||
let
|
let
|
||||||
node = remainderSet[i],
|
node = remainderSet[i],
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleS
|
|||||||
lowBound = 0.0,
|
lowBound = 0.0,
|
||||||
highBound = 0.0;
|
highBound = 0.0;
|
||||||
|
|
||||||
// Determine the closest quadrant
|
// Determine the closest quadrant
|
||||||
if (dist0 == dist180) {
|
if (dist0 == dist180) {
|
||||||
if (dist90 > dist270)
|
if (dist90 > dist270)
|
||||||
lowBound = highBound = 270;
|
lowBound = highBound = 270;
|
||||||
@@ -55,14 +55,12 @@ export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleS
|
|||||||
lowBound = 90;
|
lowBound = 90;
|
||||||
highBound = 180;
|
highBound = 180;
|
||||||
}
|
}
|
||||||
|
} else if (dist90 > dist270) {
|
||||||
|
lowBound = 270;
|
||||||
|
highBound = 360;
|
||||||
} else {
|
} else {
|
||||||
if (dist90 > dist270) {
|
lowBound = 0;
|
||||||
lowBound = 270;
|
highBound = 90;
|
||||||
highBound = 360;
|
|
||||||
} else {
|
|
||||||
lowBound = 0;
|
|
||||||
highBound = 90;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the angle
|
// Determine the angle
|
||||||
@@ -84,8 +82,8 @@ export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleS
|
|||||||
|
|
||||||
function sumForcesToSample(node, samples, sampleCache) {
|
function sumForcesToSample(node, samples, sampleCache) {
|
||||||
let nodeVx = 0,
|
let nodeVx = 0,
|
||||||
nodeVy = 0,
|
nodeVy = 0,
|
||||||
x, y, l, i, sample;
|
x, y, l, i, sample;
|
||||||
|
|
||||||
for (i = samples.length-1; i >=0 ; i--) {
|
for (i = samples.length-1; i >=0 ; i--) {
|
||||||
sample = samples[i];
|
sample = samples[i];
|
||||||
|
|||||||
14
src/link.js
14
src/link.js
@@ -10,13 +10,13 @@ import jiggle from "./jiggle";
|
|||||||
*/
|
*/
|
||||||
export default function() {
|
export default function() {
|
||||||
var dataSizeFactor,
|
var dataSizeFactor,
|
||||||
distance = constant(30),
|
distance = constant(30),
|
||||||
distances = [],
|
distances = [],
|
||||||
nodes,
|
nodes,
|
||||||
stableVelocity = 0,
|
stableVelocity = 0,
|
||||||
stableVeloHandler = null,
|
stableVeloHandler = null,
|
||||||
latestVelocityDiff = 0,
|
latestVelocityDiff = 0,
|
||||||
iterations = 1;
|
iterations = 1;
|
||||||
|
|
||||||
function force(alpha) {
|
function force(alpha) {
|
||||||
let n = nodes.length;
|
let n = nodes.length;
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ export default function () {
|
|||||||
dataSizeFactor,
|
dataSizeFactor,
|
||||||
latestVelocityDiff = 0;
|
latestVelocityDiff = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply spring forces at each simulation iteration.
|
* Apply spring forces at each simulation iteration.
|
||||||
* @param {number} alpha - multiplier for amount of force applied
|
* @param {number} alpha - multiplier for amount of force applied
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export default function() {
|
|||||||
gains,
|
gains,
|
||||||
ystep;
|
ystep;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make a step in t-SNE algorithm and set the velocities for the nodes
|
* Make a step in t-SNE algorithm and set the velocities for the nodes
|
||||||
* to accumulate the values from solution.
|
* to accumulate the values from solution.
|
||||||
*/
|
*/
|
||||||
@@ -53,7 +53,9 @@ export default function() {
|
|||||||
let u = 2 * Math.random() - 1;
|
let u = 2 * Math.random() - 1;
|
||||||
let v = 2 * Math.random() - 1;
|
let v = 2 * Math.random() - 1;
|
||||||
let r = u * u + v * v;
|
let r = u * u + v * v;
|
||||||
if (r == 0 || r > 1) {return gaussRandom();}
|
if (r == 0 || r > 1) {
|
||||||
|
return gaussRandom();
|
||||||
|
}
|
||||||
return u * Math.sqrt(-2 * Math.log(r) / r);
|
return u * Math.sqrt(-2 * Math.log(r) / r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user