From 4efabf523105725e1a9bbd593034d2e32eda448c Mon Sep 17 00:00:00 2001 From: Severiano Badajoz Date: Mon, 6 Apr 2020 10:20:11 -0700 Subject: [PATCH] refactor to remove non-standard terms in centroid files (#1330) * refactor to remove non-standard terms * switch key/value -> label/coords * fix missing parenth --- client/__tests__/util/centroid.test.js | 2 +- .../graph/overlays/centroidLabels.js | 22 +++---- client/src/util/centroid.js | 62 +++++++++---------- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/client/__tests__/util/centroid.test.js b/client/__tests__/util/centroid.test.js index 140b78cd..fd1588fb 100644 --- a/client/__tests__/util/centroid.test.js +++ b/client/__tests__/util/centroid.test.js @@ -69,7 +69,7 @@ describe("centroid", () => { }); }); - test("field3 (boolean obsAnnotation", () => { + test("field3 (boolean obsAnnotation)", () => { const centroidResult = calcCentroid( world.obsAnnotations, world.obsLayout, diff --git a/client/src/components/graph/overlays/centroidLabels.js b/client/src/components/graph/overlays/centroidLabels.js index db6aa706..22afcb68 100644 --- a/client/src/components/graph/overlays/centroidLabels.js +++ b/client/src/components/graph/overlays/centroidLabels.js @@ -39,36 +39,36 @@ class CentroidLabels extends PureComponent { const labelSVGS = []; let fontSize = "15px"; let fontWeight = null; - labels.forEach((value, key) => { + labels.forEach((coords, label) => { fontSize = "15px"; fontWeight = null; - if (key === dilatedValue) { + if (label === dilatedValue) { fontSize = "18px"; fontWeight = "800"; } // Mirror LSB middle truncation - let label = key; - if (label.length > categoryLabelDisplayStringLongLength) { - label = `${key.slice( + let displayLabel = label; + if (displayLabel.length > categoryLabelDisplayStringLongLength) { + displayLabel = `${label.slice( 0, categoryLabelDisplayStringLongLength / 2 - )}…${key.slice(-categoryLabelDisplayStringLongLength / 2)}`; + )}…${label.slice(-categoryLabelDisplayStringLongLength / 2)}`; } labelSVGS.push( - {label} + {displayLabel} ); diff --git a/client/src/util/centroid.js b/client/src/util/centroid.js index 4dc6ba60..318b7f80 100644 --- a/client/src/util/centroid.js +++ b/client/src/util/centroid.js @@ -10,15 +10,15 @@ import { unassignedCategoryLabel } from "../globals"; */ /* -Generates a mapping of categorical values to data needed to calculate centroids -categoricalValue -> { +Generates a mapping of labels to data needed to calculate centroids +label -> { length: int, holdsFinite: Boolean, xCoordinates: Float32Array, yCoordinates: Float32Array } */ -const getCoordinatesByCategoricalValues = ( +const getCoordinatesByLabel = ( obsAnnotations, obsLayout, categoryName, @@ -46,33 +46,33 @@ const getCoordinatesByCategoricalValues = ( // Iterate over all cells for (let i = 0, len = categoryArray.length; i < len; i += 1) { - // Fetch the categorical value of the current cell - const categoryValue = categoryArray[i]; + // Fetch the label of the current cell + const label = categoryArray[i]; - // Get the index of the categoryValue within the category - const categoryValueIndex = categoryValueIndices.get(categoryValue); + // Get the index of the label within the category + const labelIndex = categoryValueIndices.get(label); - // If the category is truncated and this value is removed, - // it will not be assigned a category value and will not be + // If the category's labels are truncated and this label is removed, + // it will not be assigned a label and will not be // labeled on the graph // If the user created this category, - // do not create a label for the `unassigned` value + // do not create a coord for the `unassigned` label if ( - categoryValueIndex !== undefined && - !(isUserAnno && categoryValue === unassignedCategoryLabel) + labelIndex !== undefined && + !(isUserAnno && label === unassignedCategoryLabel) ) { // Create/fetch the scratchpad value - let coords = coordsByCategoryLabel.get(categoryValue); + let coords = coordsByCategoryLabel.get(label); if (coords === undefined) { - // Get the number of cells which are in the categorical value - const numInCategoricalValue = categoryValueCounts[categoryValueIndex]; + // Get the number of cells which are in the label + const numInLabel = categoryValueCounts[labelIndex]; coords = { hasFinite: false, - xCoordinates: new Float32Array(numInCategoricalValue), - yCoordinates: new Float32Array(numInCategoricalValue), + xCoordinates: new Float32Array(numInLabel), + yCoordinates: new Float32Array(numInLabel), length: 0 }; - coordsByCategoryLabel.set(categoryValue, coords); + coordsByCategoryLabel.set(label, coords); } coords.hasFinite = @@ -91,9 +91,9 @@ const getCoordinatesByCategoricalValues = ( }; /* - calcMedianCentroid calculates the median coordinates for categorical values in a given metadata field + calcMedianCentroid calculates the median coordinates for labels in a given category - categoricalValue -> [x-Coordinate, y-Coordinate] + label -> [x-Coordinate, y-Coordinate] */ const calcMedianCentroid = ( @@ -104,8 +104,8 @@ const calcMedianCentroid = ( categoricalSelection, schemaObsByName ) => { - // generate a map describing the coordinates for each value within the given category - const dataMap = getCoordinatesByCategoricalValues( + // generate a map describing the coordinates for each label within the given category + const dataMap = getCoordinatesByLabel( obsAnnotations, obsLayout, categoryName, @@ -114,25 +114,25 @@ const calcMedianCentroid = ( schemaObsByName ); - // categoricalValue => [medianXCoordinate, medianYCoordinate] + // label => [medianXCoordinate, medianYCoordinate] const coordinates = new Map(); // Iterate over the recently created map - dataMap.forEach((value, key) => { - // If there are coordinates for this categorical value, - // and there is a finite coordinate for the category value - if (value.length > 0 && value.hasFinite) { + dataMap.forEach((coords, label) => { + // If there are coordinates for this label, + // and there is a finite coordinate for the label + if (coords.length > 0 && coords.hasFinite) { const calculatedCoordinates = []; // Find and store the median x and y coordinate - calculatedCoordinates[0] = quantile([0.5], value.xCoordinates)[0]; - calculatedCoordinates[1] = quantile([0.5], value.yCoordinates)[0]; + calculatedCoordinates[0] = quantile([0.5], coords.xCoordinates)[0]; + calculatedCoordinates[1] = quantile([0.5], coords.yCoordinates)[0]; - coordinates.set(key, calculatedCoordinates); + coordinates.set(label, calculatedCoordinates); } }); - // return the map: categoricalValue -> [medianXCoordinate, medianYCoordinate] + // return the map: label -> [medianXCoordinate, medianYCoordinate] return coordinates; };