tweak: Interface for findPaths

so that it can work with an ordered list.
This commit is contained in:
NunoSempere 2022-06-19 17:30:02 -04:00
parent dc7cfbe0c5
commit 8b03f0a36b
4 changed files with 32 additions and 33 deletions

View File

@ -3,7 +3,7 @@ import { run } from "@quri/squiggle-lang";
export async function aggregatePathsThroughMixtureOfDistributions({ export async function aggregatePathsThroughMixtureOfDistributions({
pathsArray, pathsArray,
nodes, orderedList,
VERBOSE, VERBOSE,
}) { }) {
let print = (x) => { let print = (x) => {
@ -12,7 +12,7 @@ export async function aggregatePathsThroughMixtureOfDistributions({
} }
}; };
let result = pathsArray.map((paths, i) => { let result = pathsArray.map((paths, i) => {
print(nodes[i].name); print(orderedList[i].name);
let multipliedDistributions = paths.map( let multipliedDistributions = paths.map(
(path) => path.multipliedDistributionsInPath (path) => path.multipliedDistributionsInPath
); );
@ -60,7 +60,7 @@ export async function aggregatePathsThroughMixtureOfDistributions({
console.groupEnd(); console.groupEnd();
print(""); print("");
return { return {
name: nodes[i].name, name: orderedList[i].name,
meanOfAggregatedDistributions: mean, meanOfAggregatedDistributions: mean,
ninetyPercentileConfidenceIntervalOfAggregatedDistributions: ninetyPercentileConfidenceIntervalOfAggregatedDistributions:
ci90percentAnswer, ci90percentAnswer,
@ -84,7 +84,7 @@ export const geomMean = (arr) => {
export function aggregatePathsThroughMixtureOfMeans({ export function aggregatePathsThroughMixtureOfMeans({
pathsArray, pathsArray,
nodes, orderedList,
VERBOSE, VERBOSE,
}) { }) {
let print = (x) => { let print = (x) => {
@ -94,7 +94,7 @@ export function aggregatePathsThroughMixtureOfMeans({
}; };
let result = pathsArray.map((paths, i) => { let result = pathsArray.map((paths, i) => {
print(nodes[i].name); print(orderedList[i].name);
let expectedRelativeValues = paths let expectedRelativeValues = paths
.map((path) => path.expectedRelativeValue) .map((path) => path.expectedRelativeValue)
.filter((x) => x != undefined); .filter((x) => x != undefined);
@ -112,7 +112,7 @@ export function aggregatePathsThroughMixtureOfMeans({
} }
} }
return { return {
name: nodes[i].name, name: orderedList[i].name,
aggregatedMeans: answer, aggregatedMeans: answer,
arrayMeans: expectedRelativeValues, arrayMeans: expectedRelativeValues,
allPositive: hasNegative.length == 0, allPositive: hasNegative.length == 0,
@ -123,7 +123,7 @@ export function aggregatePathsThroughMixtureOfMeans({
export async function aggregatePaths({ export async function aggregatePaths({
pathsArray, pathsArray,
nodes, orderedList,
aggregationType, aggregationType,
VERBOSE, VERBOSE,
}) { }) {
@ -134,19 +134,19 @@ export async function aggregatePaths({
console.log("Warning: this may take a long time"); console.log("Warning: this may take a long time");
return await aggregatePathsThroughMixtureOfDistributions({ return await aggregatePathsThroughMixtureOfDistributions({
pathsArray, pathsArray,
nodes, orderedList,
VERBOSE, VERBOSE,
}); });
} else if (aggregationType == "mean") { } else if (aggregationType == "mean") {
return aggregatePathsThroughMixtureOfMeans({ return aggregatePathsThroughMixtureOfMeans({
pathsArray, pathsArray,
nodes, orderedList,
VERBOSE, VERBOSE,
}); });
} else { } else {
return aggregatePathsThroughMixtureOfMeans({ return aggregatePathsThroughMixtureOfMeans({
pathsArray, pathsArray,
nodes, orderedList,
VERBOSE, VERBOSE,
}); });
} }

View File

@ -1,7 +1,7 @@
// IMPORTS // IMPORTS
import * as fs from "fs"; import * as fs from "fs";
import { mergeSort } from "./mergeSort.js"; import { mergeSort } from "./mergeSort.js";
import { findDistancesFromAllElementsToAllReferencePoints } from "./findPaths.js"; import { findDistances } from "./findPaths.js";
import { aggregatePaths } from "./aggregatePaths.js"; import { aggregatePaths } from "./aggregatePaths.js";
// DEFS // DEFS
@ -11,11 +11,6 @@ const outputFilePath = "./output/output.json";
// HELPERS // HELPERS
const findElementPosition = (name, orderedList) => {
let node = orderedList.find((node) => node.name == name);
return node.position;
};
// MAIN // MAIN
async function main() { async function main() {
// read file // read file
@ -43,25 +38,13 @@ async function main() {
console.log(""); console.log("");
// find Paths // find Paths
let nodes = orderedList.map((element, i) => ({ let paths = await findDistances({ orderedList, links });
...element,
position: i,
}));
const linksWithPosition = links.map((link) => ({
...link,
sourceElementPosition: findElementPosition(link.source, nodes),
targetElementPosition: findElementPosition(link.target, nodes),
}));
let paths = await findDistancesFromAllElementsToAllReferencePoints({
nodes,
links: linksWithPosition,
});
// console.log(JSON.stringify(paths, null, 4)); // console.log(JSON.stringify(paths, null, 4));
// Aggregate paths. // Aggregate paths.
let aggregatedPaths = await aggregatePaths({ let aggregatedPaths = await aggregatePaths({
pathsArray: paths, pathsArray: paths,
nodes, orderedList,
aggregationType: "mean", // alternatively: aggregationType: "distribution" aggregationType: "mean", // alternatively: aggregationType: "distribution"
VERBOSE: false, VERBOSE: false,
}); });

View File

@ -312,3 +312,20 @@ export async function findDistancesFromAllElementsToAllReferencePoints({
} }
return distancesForAllElements; return distancesForAllElements;
} }
export async function findDistances({ orderedList, links }) {
let nodes = orderedList.map((element, i) => ({
...element,
position: i,
}));
const linksWithPosition = links.map((link) => ({
...link,
sourceElementPosition: findElementPosition(link.source, nodes),
targetElementPosition: findElementPosition(link.target, nodes),
}));
let result = await findDistancesFromAllElementsToAllReferencePoints({
nodes,
links: linksWithPosition,
});
return result;
}

View File

@ -1,6 +1,5 @@
import { mergeSort } from "./mergeSort.js"; import { mergeSort } from "./mergeSort.js";
import { findDistancesFromAllElementsToAllReferencePoints } from "./findPaths.js"; import { findDistances } from "./findPaths.js";
import { aggregatePaths } from "./aggregatePaths.js"; import { aggregatePaths } from "./aggregatePaths.js";
export const findPaths = findDistancesFromAllElementsToAllReferencePoints; export { mergeSort, aggregatePaths, findDistances };
export { mergeSort, aggregatePaths };