siafun
Version:
A collection of structure induction algorithms
116 lines (115 loc) • 4.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const _ = require("lodash");
const arrayutils_1 = require("arrayutils");
function createPointMatrix(selectedPoints, points, points2, symmetric) {
const matrix = getEmptyMatrix(points.length, points2.length);
selectedPoints.forEach(p => matrix[p[0]][p[1]] = 1);
if (symmetric)
selectedPoints.forEach(p => matrix[p[1]][p[0]] = 1);
return matrix;
}
exports.createPointMatrix = createPointMatrix;
function toPatterns(alignments, points, points2) {
return alignments.map(a => {
const currentSegments = toSegments(a);
const dist = currentSegments[1][0] - currentSegments[0][0];
const vector = points[0].map((_, i) => i == 0 ? dist : 0);
const segmentPoints = currentSegments.map((s, i) => s.map(j => [points, points2][i][j]));
return { points: segmentPoints[0], vectors: [vector], occurrences: segmentPoints };
});
}
exports.toPatterns = toPatterns;
function toSegments(alignmentPoints) {
let currentSegments = _.zip(...alignmentPoints);
//sort ascending
currentSegments.forEach(o => o.sort((a, b) => a - b));
//remove duplicates
return currentSegments.map(occ => _.uniq(occ));
}
function getEmptyMatrix(numRows, numCols) {
return _.range(0, numRows).map(_r => _.fill(new Array(numCols), 0));
}
exports.getEmptyMatrix = getEmptyMatrix;
function modForReal(n, mod) {
return ((n % mod) + mod) % mod;
}
exports.modForReal = modForReal;
function allIndexesOf(array, value) {
return allIndexesWith(array, a => a === value);
}
exports.allIndexesOf = allIndexesOf;
function allIndexesWith(array, condition) {
return array.map((a, i) => condition(a) ? i : null).filter(i => i != null);
}
exports.allIndexesWith = allIndexesWith;
function cartesianProduct(arr) {
return arr.reduce((a, b) => a.map(x => b.map(y => x.concat([y])))
.reduce((a, b) => a.concat(b), []), [[]]);
}
exports.cartesianProduct = cartesianProduct;
function getEntropy(data) {
return -1 * _.sum(data.map(d => d ? d * Math.log(d) : 0));
}
exports.getEntropy = getEntropy;
function getMedian(data) {
data = _.sortBy(data);
const middle = _.floor(data.length / 2);
return data.length % 2 ? data[middle] : (data[middle - 1] + data[middle]) / 2;
}
exports.getMedian = getMedian;
function toOrderedPointString(points) {
const clone = _.clone(points);
clone.sort(arrayutils_1.compareArrays);
return JSON.stringify(clone);
}
exports.toOrderedPointString = toOrderedPointString;
function pointsToIndices(occurrences, points) {
const pointStrings = points.map(p => JSON.stringify(p));
return occurrences.map(occ => occ.map(pat => pat.map(p => getPointIndex(p, pointStrings))));
}
exports.pointsToIndices = pointsToIndices;
function getPointIndex(point, pointStrings) {
//quantize to get rid of float errors!
return pointStrings.indexOf(JSON.stringify(roundPoint(point, 8)));
}
function roundPoint(point, precision) {
return point.map(x => _.round(x, precision));
}
function loadOrPerformAndCache(file, func, options, logString) {
if (logString && options.loggingLevel > 0)
console.log(logString);
if (file && options.cacheDir) {
return loadCached(file, options.cacheDir)
|| saveCached(file, func(), options.cacheDir);
}
return func();
}
exports.loadOrPerformAndCache = loadOrPerformAndCache;
function loadCached(file, cacheDir) {
if (cacheDir && fs.existsSync(cacheDir + file)) {
return loadJson(cacheDir + file);
}
}
exports.loadCached = loadCached;
function saveCached(file, contents, cacheDir) {
if (cacheDir) {
return saveJson(cacheDir + file, contents);
}
}
exports.saveCached = saveCached;
function loadJson(file) {
return JSON.parse(fs.readFileSync(file, 'utf8'));
}
exports.loadJson = loadJson;
function saveJson(path, json) {
try {
fs.writeFileSync(path, JSON.stringify(json));
}
catch (e) {
console.log('failed to cache ' + path);
}
return json;
}
exports.saveJson = saveJson;