Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make frequencies tend to 0 in absence of data #1325

Merged
merged 6 commits into from
Apr 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions src/util/processFrequencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,37 @@ export const checkIfNormalizableFromRawData = (data, pivots, nodes, visibility)
}
}
});
const minFrequency = Math.min(...pivotTotals);
const allowNormalization = minFrequency > 0.001;
// const minFrequency = Math.min(...pivotTotals);
const allowNormalization = true;
return allowNormalization;
};

export const computeMatrixFromRawData = (data, pivots, nodes, visibility, colorScale, colorBy, normalizeFrequencies) => {
export const computeMatrixFromRawData = (
data,
pivots,
nodes,
visibility,
colorScale,
colorBy,
normalizeFrequencies
) => {
/* color scale domain forms the categories in the stream graph */
const categories = colorScale.legendValues.filter((d) => d !== undefined);
categories.push(unassigned_label); /* for tips without a colorBy */
const isGenotype = isColorByGenotype(colorBy);
const matrix = {}; /* SHAPE: rows: categories (colorBys), columns: pivots */
const pivotsLen = pivots.length;
categories.forEach((x) => {matrix[x] = new Array(pivotsLen).fill(0);});
categories.forEach((x) => {
matrix[x] = new Array(pivotsLen).fill(0);
});
// let debugTipsSeen = 0;
const debugPivotTotals = new Array(pivotsLen).fill(0);
data.forEach((d) => {
if (visibility[d.idx] === NODE_VISIBLE) {
// debugTipsSeen++;
const category = assignCategory(colorScale, categories, nodes[d.idx], colorBy, isGenotype) || unassigned_label;
const category =
assignCategory(colorScale, categories, nodes[d.idx], colorBy, isGenotype) ||
unassigned_label;
// if (category === unassigned_label) return;
for (let i = 0; i < pivotsLen; i++) {
matrix[category][i] += d.values[i];
Expand All @@ -77,11 +89,14 @@ export const computeMatrixFromRawData = (data, pivots, nodes, visibility, colorS
});

if (normalizeFrequencies) {
const nCategories = Object.keys(matrix).length;
const minVal = 1e-10;
const minVal = 1e-7;
Object.keys(matrix).forEach((cat) => {
debugPivotTotals.forEach((norm, i) => {
matrix[cat][i] = (matrix[cat][i] + minVal) / (nCategories * minVal + norm);
if (norm > minVal) {
matrix[cat][i] /= norm;
} else {
matrix[cat][i] = 0.0;
}
});
});
}
Expand All @@ -104,19 +119,22 @@ export const processFrequenciesJSON = (rawJSON, tree, controls) => {
throw new Error("tree not loaded");
}
const data = [];
tree.nodes.filter((d) => !d.hasChildren).forEach((n) => {
if (!rawJSON[n.name]) {
console.warn(`No tip frequency information for ${n.name}`);
return;
}
data.push({
idx: n.arrayIdx,
values: rawJSON[n.name].frequencies,
weight: rawJSON[n.name].weight
tree.nodes
.filter((d) => !d.hasChildren)
.forEach((n) => {
if (!rawJSON[n.name]) {
console.warn(`No tip frequency information for ${n.name}`);
return;
}
data.push({
idx: n.arrayIdx,
values: rawJSON[n.name].frequencies,
weight: rawJSON[n.name].weight
});
});
});

const normalizeFrequencies = controls.normalizeFrequencies &&
const normalizeFrequencies =
controls.normalizeFrequencies &&
checkIfNormalizableFromRawData(data, pivots, tree.nodes, tree.visibility);

const matrix = computeMatrixFromRawData(
Expand Down