Skip to content

Commit

Permalink
Type cast continuous values in tree
Browse files Browse the repository at this point in the history
Conceptually these values only make sense if they are numeric.
Previously some implicit casting was happening, but this resulted in
bugs such as negative values not being cast and thus not displayed in
the scatterplot.

Note one slight side-effect which may be considered a regression:
previously string values which were not numbers (e.g. "abc" but not
"1.0") would show up in the hover/click info-boxes. Now such values will
be changed to `undefined` and therefore not shown in those info-boxes.

Timing of the changes introduced here (AppleM1, Firefox 111):
* nextclade/sars-cov-2/21L   7-9ms
* ncov/gisaid/global/6m      4-10ms
* zika                       2ms

Closes #1626
  • Loading branch information
jameshadfield committed Mar 27, 2023
1 parent 471fab1 commit c7ac36f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/actions/recomputeReduxState.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getDefaultFrequenciesState } from "../reducers/frequencies";
import { countTraitsAcrossTree, calcTotalTipsInTree } from "../util/treeCountingHelpers";
import { calcEntropyInView } from "../util/entropy";
import { treeJsonToState } from "../util/treeJsonProcessing";
import { castIncorrectTypes } from "../util/castJsonTypes";
import { entropyCreateState } from "../util/entropyCreateStateFromJsons";
import { determineColorByGenotypeMutType, calcNodeColor } from "../util/colorHelpers";
import { calcColorScale, createVisibleLegendValues } from "../util/colorScale";
Expand Down Expand Up @@ -768,11 +769,13 @@ export const createStateFromQueryOrJSONs = ({
frequencies = getDefaultFrequenciesState();
/* new tree state(s) */
tree = treeJsonToState(json.tree);
castIncorrectTypes(metadata, tree);
tree.debug = "LEFT";
tree.name = mainTreeName;
metadata.mainTreeNumTips = calcTotalTipsInTree(tree.nodes);
if (secondTreeDataset) {
treeToo = treeJsonToState(secondTreeDataset.tree);
castIncorrectTypes(metadata, treeToo);
treeToo.debug = "RIGHT";
treeToo.name = secondTreeName;
/* TODO: calc & display num tips in 2nd tree */
Expand Down
50 changes: 50 additions & 0 deletions src/util/castJsonTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Values in the JSON should be appropriately typed however this may not always be the case.
* For instance, certain values of a continuous trait may be strings, and we should cast
* these to numbers. See https://github.com/nextstrain/auspice/issues/1626 for more discussion
* of this particular case. Feel free to add more checks / casts to this function!
* @param {Object} metadata
* @param {Object} tree
* @returns {undefined} Any type casting is in-place
*/
export const castIncorrectTypes = (metadata, tree) => {
try {
const continuousKeys = new Set();
Object.entries(metadata.colorings || {}).forEach(([key, details]) => {
if (details.type==="continuous") {
continuousKeys.add(key);
}
});
tree.nodes.forEach((node) => {
for (const key of continuousKeys) {
const attr = node?.node_attrs?.[key];
if (!attr) continue;
toNumber(attr);
}
});
} catch (err) {
// type casting shouldn't be required (the JSON should be correctly typed)
// so any errors shouldn't prevent Auspice loading
console.error(err);
}
};

export function toNumber(attr) {
switch (typeof attr.value) {
case "number":
break;
case "string":
const value = attr.value.trim();
if (value === "" || isNaN(value) || value ==="Infinity" || value ==="-Infinity") {
// Note: Number("")=0
// undefined values are handled appropriately (e.g. scatterplots, tooltips etc)
attr.value = undefined;
} else {
attr.value = Number(value);
}
break;
default:
// any other types (Boolean, Null ('object')) are not valid for a continuous scale
attr.value = undefined;
}
}
20 changes: 20 additions & 0 deletions test/jsonParsing.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { toNumber } from "../src/util/castJsonTypes";

/**
* function used to cast node values of continuous traits to numbers
*/
test("Continuous scale values are cast to Numbers or are undefined", () => {
const numbers = ["123", "1e5", "1", "0", "-1", 0, 1, -1];
const notNumbers = ["", " ", " Infinity", "infinity", null, "abc", "", true, false, "true", undefined];
numbers.forEach((n) => {
const attr = {value: n};
toNumber(attr); // modifies in place
expect(typeof attr.value).toStrictEqual('number');
expect(Number.isFinite(attr.value)).toBe(true);
});
notNumbers.forEach((n) => {
const attr = {value: n};
toNumber(attr); // modifies in place
expect(attr.value).toBeUndefined();
});
});

0 comments on commit c7ac36f

Please sign in to comment.