Update 'src/interpolation/interpolationPivots.js'

This commit is contained in:
brian
2018-03-16 10:20:00 +07:00
parent 6335389c7c
commit 047d89f004

View File

@@ -1,135 +1,135 @@
import {pointOnCircle, takeSampleFrom} from "./helpers"; import {takeSampleFrom} from "./helpers";
import {placeNearToNearestNeighbour} from "./interpCommon"; import {placeNearToNearestNeighbour} from "./interpCommon";
/** /**
* Perform interpolation where the "parent" node is is estimated by pivot-based searching. * Perform interpolation where the "parent" node is is estimated by pivot-based searching.
* - Pre-processing: assign random samples as pivots, * - Pre-processing: assign random samples as pivots,
* put the others in each pivot's bucket. * put the others in each pivot's bucket.
* ie. a non-pivot sample X may be in * ie. a non-pivot sample X may be in
* - bucket 3 of pivot A, * - bucket 3 of pivot A,
* - bucket 1 of pivot B, * - bucket 1 of pivot B,
* - bucket 5 of pivot C, * - bucket 5 of pivot C,
* all at the same time * all at the same time
* For each point to be interpolated: * For each point to be interpolated:
* - Phase 1: for each pivot: compare distance against the pivot * - Phase 1: for each pivot: compare distance against the pivot
* compare against other points in the same bucket of that pivot * compare against other points in the same bucket of that pivot
* note down the parent found * note down the parent found
* this is essentially a near neighbour estimation problem. * this is essentially a near neighbour estimation problem.
* - Phase 2 and 3 are passed onto placeNearToNearestNeighbour * - Phase 2 and 3 are passed onto placeNearToNearestNeighbour
* @param {list} sampleSet - nodes already plotted on the 2D graph * @param {list} sampleSet - nodes already plotted on the 2D graph
* @param {list} remainderSet - nodes to be interpolated onto the graph * @param {list} remainderSet - nodes to be interpolated onto the graph
* @param {function} distanceFn - f(nodex, nodey) that calculate high-dimensional * @param {function} distanceFn - f(nodex, nodey) that calculate high-dimensional
* distance between 2 nodes * distance between 2 nodes
* @param {number} endingIts - for phase 3, how many iterations to refine the * @param {number} endingIts - for phase 3, how many iterations to refine the
* placement of each interpolated point * placement of each interpolated point
*/ */
export default function(sampleSet, remainderSet, numPivots, distanceFn, endingIts) { export default function(sampleSet, remainderSet, numPivots, distanceFn, endingIts) {
// Pivot based parent finding // Pivot based parent finding
let numBuckets = Math.floor(Math.sqrt(sampleSet.length)); let numBuckets = Math.floor(Math.sqrt(sampleSet.length));
let numNonPivots = sampleSet.length - numPivots; let numNonPivots = sampleSet.length - numPivots;
let sets = takeSampleFrom(sampleSet, numPivots); let sets = takeSampleFrom(sampleSet, numPivots);
let pivots = sets.sample; let pivots = sets.sample;
let nonPivotSamples = sets.remainder; let nonPivotSamples = sets.remainder;
let pivotsBuckets = []; // [ For each Pivot:[For each bucket:[each point in bucket]] ] let pivotsBuckets = []; // [ For each Pivot:[For each bucket:[each point in bucket]] ]
for (let i = 0; i < numPivots; i++) { for (let i = 0; i < numPivots; i++) {
pivotsBuckets[i] = []; pivotsBuckets[i] = [];
for (let j = 0; j < numBuckets; j++) { for (let j = 0; j < numBuckets; j++) {
pivotsBuckets[i][j] = []; pivotsBuckets[i][j] = [];
} }
} }
// Pre-calculate distance between each non-pivot sample to each pivot // Pre-calculate distance between each non-pivot sample to each pivot
// At the same time, determine the bucket width for each pivot based on furthest non-pivot sample // At the same time, determine the bucket width for each pivot based on furthest non-pivot sample
let distCache = []; // [ For each non-pivot sample:[For each Pivot: distance] ] let distCache = []; // [ For each non-pivot sample:[For each Pivot: distance] ]
let bucketWidths = []; // [ For each Pivot: width of each bucket ] let bucketWidths = []; // [ For each Pivot: width of each bucket ]
for (let i = 0; i < nonPivotSamples.length; i++) for (let i = 0; i < nonPivotSamples.length; i++)
distCache[i] = []; distCache[i] = [];
for (let j = 0; j < numPivots; j++) { for (let j = 0; j < numPivots; j++) {
let pivot = pivots[j]; let pivot = pivots[j];
let maxDist = -1; let maxDist = -1;
for (let i = 0; i < numNonPivots; i++) { for (let i = 0; i < numNonPivots; i++) {
let sample = nonPivotSamples[i]; let sample = nonPivotSamples[i];
distCache[i][j] = distanceFn(pivot, sample); distCache[i][j] = distanceFn(pivot, sample);
if (distCache[i][j] > maxDist) if (distCache[i][j] > maxDist)
maxDist = distCache[i][j]; maxDist = distCache[i][j];
} }
bucketWidths.push(maxDist / numBuckets); bucketWidths.push(maxDist / numBuckets);
} }
// Put samples (pivot not included) into buckets // Put samples (pivot not included) into buckets
for (let j = 0; j < numPivots; j++) { for (let j = 0; j < numPivots; j++) {
let bucketWidth = bucketWidths[j]; let bucketWidth = bucketWidths[j];
for (let i = 0; i < numNonPivots; i++) { for (let i = 0; i < numNonPivots; i++) {
let sample = nonPivotSamples[i]; let sample = nonPivotSamples[i];
let bucketNumber = Math.floor(distCache[i][j] / bucketWidth); let bucketNumber = Math.floor(distCache[i][j] / bucketWidth);
if (bucketNumber >= numBuckets) { if (bucketNumber >= numBuckets) {
bucketNumber = numBuckets - 1; bucketNumber = numBuckets - 1;
} else if (bucketNumber < 0) { // Should never be negative anyway } else if (bucketNumber < 0) { // Should never be negative anyway
bucketNumber = 0; bucketNumber = 0;
} }
pivotsBuckets[j][bucketNumber].push(sample); pivotsBuckets[j][bucketNumber].push(sample);
} }
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
let sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample; let sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample;
//Plot each of the remainder nodes //Plot each of the remainder nodes
for (let i = remainderSet.length-1; i>=0; i--) { for (let i = remainderSet.length-1; i>=0; i--) {
let node = remainderSet[i]; let node = remainderSet[i];
let sampleSubsetDistanceCache = [], let sampleSubsetDistanceCache = [],
minDist, nearSample; minDist, nearSample;
// Pivot based parent search // Pivot based parent search
for (let p = 0; p < numPivots; p++) { for (let p = 0; p < numPivots; p++) {
let pivot = pivots[p]; let pivot = pivots[p];
let bucketWidth = bucketWidths[p]; let bucketWidth = bucketWidths[p];
let dist = distanceFn(node, pivot); let dist = distanceFn(node, pivot);
let index = sampleSubset.indexOf(pivot); let index = sampleSubset.indexOf(pivot);
if (index !== -1) { if (index !== -1) {
sampleSubsetDistanceCache[index] = dist; sampleSubsetDistanceCache[index] = dist;
} }
if (minDist === undefined || dist < minDist){ if (minDist === undefined || dist < minDist){
minDist = dist; minDist = dist;
nearSample = pivot; nearSample = pivot;
} }
let bucketNumber = Math.floor(dist / bucketWidth); let bucketNumber = Math.floor(dist / bucketWidth);
if (bucketNumber >= numBuckets) { if (bucketNumber >= numBuckets) {
bucketNumber = numBuckets - 1; bucketNumber = numBuckets - 1;
} else if (bucketNumber < 0) { // Should never be negative anyway } else if (bucketNumber < 0) { // Should never be negative anyway
bucketNumber = 0; bucketNumber = 0;
} }
for (let j = pivotsBuckets[p][bucketNumber].length-1; j>=0; j--) { for (let j = pivotsBuckets[p][bucketNumber].length-1; j>=0; j--) {
let candidateNode = pivotsBuckets[p][bucketNumber][j]; let candidateNode = pivotsBuckets[p][bucketNumber][j];
let index = sampleSubset.indexOf(candidateNode); let index = sampleSubset.indexOf(candidateNode);
if (index !== -1 && sampleSubsetDistanceCache[index] !== undefined) if (index !== -1 && sampleSubsetDistanceCache[index] !== undefined)
dist = sampleSubsetDistanceCache[index] dist = sampleSubsetDistanceCache[index]
else { else {
dist = distanceFn(candidateNode, node); dist = distanceFn(candidateNode, node);
if (index !== -1) if (index !== -1)
sampleSubsetDistanceCache[index] = dist; sampleSubsetDistanceCache[index] = dist;
} }
if (dist < minDist){ if (dist < minDist){
minDist = dist; minDist = dist;
nearSample = candidateNode; nearSample = candidateNode;
} }
} }
} }
// Fill in holes in cache // Fill in holes in cache
for (let k = 0; k < sampleSubset.length; k++) { for (let k = 0; k < sampleSubset.length; k++) {
if (sampleSubsetDistanceCache[k] === undefined) if (sampleSubsetDistanceCache[k] === undefined)
sampleSubsetDistanceCache[k] = distanceFn(node, sampleSubset[k]); sampleSubsetDistanceCache[k] = distanceFn(node, sampleSubset[k]);
} }
placeNearToNearestNeighbour(node, nearSample, minDist, sampleSubset, sampleSubsetDistanceCache, endingIts); placeNearToNearestNeighbour(node, nearSample, minDist, sampleSubset, sampleSubsetDistanceCache, endingIts);
} }
} }