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