mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-08 08:18:12 +08:00
refactor to remove non-standard terms in centroid files (#1330)
* refactor to remove non-standard terms * switch key/value -> label/coords * fix missing parenth
This commit is contained in:
@@ -69,7 +69,7 @@ describe("centroid", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("field3 (boolean obsAnnotation", () => {
|
test("field3 (boolean obsAnnotation)", () => {
|
||||||
const centroidResult = calcCentroid(
|
const centroidResult = calcCentroid(
|
||||||
world.obsAnnotations,
|
world.obsAnnotations,
|
||||||
world.obsLayout,
|
world.obsLayout,
|
||||||
|
|||||||
@@ -39,36 +39,36 @@ class CentroidLabels extends PureComponent {
|
|||||||
const labelSVGS = [];
|
const labelSVGS = [];
|
||||||
let fontSize = "15px";
|
let fontSize = "15px";
|
||||||
let fontWeight = null;
|
let fontWeight = null;
|
||||||
labels.forEach((value, key) => {
|
labels.forEach((coords, label) => {
|
||||||
fontSize = "15px";
|
fontSize = "15px";
|
||||||
fontWeight = null;
|
fontWeight = null;
|
||||||
if (key === dilatedValue) {
|
if (label === dilatedValue) {
|
||||||
fontSize = "18px";
|
fontSize = "18px";
|
||||||
fontWeight = "800";
|
fontWeight = "800";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mirror LSB middle truncation
|
// Mirror LSB middle truncation
|
||||||
let label = key;
|
let displayLabel = label;
|
||||||
if (label.length > categoryLabelDisplayStringLongLength) {
|
if (displayLabel.length > categoryLabelDisplayStringLongLength) {
|
||||||
label = `${key.slice(
|
displayLabel = `${label.slice(
|
||||||
0,
|
0,
|
||||||
categoryLabelDisplayStringLongLength / 2
|
categoryLabelDisplayStringLongLength / 2
|
||||||
)}…${key.slice(-categoryLabelDisplayStringLongLength / 2)}`;
|
)}…${label.slice(-categoryLabelDisplayStringLongLength / 2)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
labelSVGS.push(
|
labelSVGS.push(
|
||||||
<g
|
<g
|
||||||
// eslint-disable-next-line react/no-array-index-key
|
// eslint-disable-next-line react/no-array-index-key
|
||||||
key={key}
|
key={label}
|
||||||
className="centroid-label"
|
className="centroid-label"
|
||||||
transform={`translate(${value[0]}, ${value[1]})`}
|
transform={`translate(${coords[0]}, ${coords[1]})`}
|
||||||
data-testclass="centroid-label"
|
data-testclass="centroid-label"
|
||||||
data-testid={`${key}-centroid-label`}
|
data-testid={`${label}-centroid-label`}
|
||||||
>
|
>
|
||||||
<text
|
<text
|
||||||
transform={inverseTransform}
|
transform={inverseTransform}
|
||||||
textAnchor="middle"
|
textAnchor="middle"
|
||||||
data-label={key}
|
data-label={label}
|
||||||
style={{
|
style={{
|
||||||
fontFamily: "Roboto Condensed",
|
fontFamily: "Roboto Condensed",
|
||||||
fontSize,
|
fontSize,
|
||||||
@@ -92,7 +92,7 @@ class CentroidLabels extends PureComponent {
|
|||||||
}
|
}
|
||||||
pointerEvents="visiblePainted"
|
pointerEvents="visiblePainted"
|
||||||
>
|
>
|
||||||
{label}
|
{displayLabel}
|
||||||
</text>
|
</text>
|
||||||
</g>
|
</g>
|
||||||
);
|
);
|
||||||
|
|||||||
+31
-31
@@ -10,15 +10,15 @@ import { unassignedCategoryLabel } from "../globals";
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Generates a mapping of categorical values to data needed to calculate centroids
|
Generates a mapping of labels to data needed to calculate centroids
|
||||||
categoricalValue -> {
|
label -> {
|
||||||
length: int,
|
length: int,
|
||||||
holdsFinite: Boolean,
|
holdsFinite: Boolean,
|
||||||
xCoordinates: Float32Array,
|
xCoordinates: Float32Array,
|
||||||
yCoordinates: Float32Array
|
yCoordinates: Float32Array
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
const getCoordinatesByCategoricalValues = (
|
const getCoordinatesByLabel = (
|
||||||
obsAnnotations,
|
obsAnnotations,
|
||||||
obsLayout,
|
obsLayout,
|
||||||
categoryName,
|
categoryName,
|
||||||
@@ -46,33 +46,33 @@ const getCoordinatesByCategoricalValues = (
|
|||||||
|
|
||||||
// Iterate over all cells
|
// Iterate over all cells
|
||||||
for (let i = 0, len = categoryArray.length; i < len; i += 1) {
|
for (let i = 0, len = categoryArray.length; i < len; i += 1) {
|
||||||
// Fetch the categorical value of the current cell
|
// Fetch the label of the current cell
|
||||||
const categoryValue = categoryArray[i];
|
const label = categoryArray[i];
|
||||||
|
|
||||||
// Get the index of the categoryValue within the category
|
// Get the index of the label within the category
|
||||||
const categoryValueIndex = categoryValueIndices.get(categoryValue);
|
const labelIndex = categoryValueIndices.get(label);
|
||||||
|
|
||||||
// If the category is truncated and this value is removed,
|
// If the category's labels are truncated and this label is removed,
|
||||||
// it will not be assigned a category value and will not be
|
// it will not be assigned a label and will not be
|
||||||
// labeled on the graph
|
// labeled on the graph
|
||||||
// If the user created this category,
|
// 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 (
|
if (
|
||||||
categoryValueIndex !== undefined &&
|
labelIndex !== undefined &&
|
||||||
!(isUserAnno && categoryValue === unassignedCategoryLabel)
|
!(isUserAnno && label === unassignedCategoryLabel)
|
||||||
) {
|
) {
|
||||||
// Create/fetch the scratchpad value
|
// Create/fetch the scratchpad value
|
||||||
let coords = coordsByCategoryLabel.get(categoryValue);
|
let coords = coordsByCategoryLabel.get(label);
|
||||||
if (coords === undefined) {
|
if (coords === undefined) {
|
||||||
// Get the number of cells which are in the categorical value
|
// Get the number of cells which are in the label
|
||||||
const numInCategoricalValue = categoryValueCounts[categoryValueIndex];
|
const numInLabel = categoryValueCounts[labelIndex];
|
||||||
coords = {
|
coords = {
|
||||||
hasFinite: false,
|
hasFinite: false,
|
||||||
xCoordinates: new Float32Array(numInCategoricalValue),
|
xCoordinates: new Float32Array(numInLabel),
|
||||||
yCoordinates: new Float32Array(numInCategoricalValue),
|
yCoordinates: new Float32Array(numInLabel),
|
||||||
length: 0
|
length: 0
|
||||||
};
|
};
|
||||||
coordsByCategoryLabel.set(categoryValue, coords);
|
coordsByCategoryLabel.set(label, coords);
|
||||||
}
|
}
|
||||||
|
|
||||||
coords.hasFinite =
|
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 = (
|
const calcMedianCentroid = (
|
||||||
@@ -104,8 +104,8 @@ const calcMedianCentroid = (
|
|||||||
categoricalSelection,
|
categoricalSelection,
|
||||||
schemaObsByName
|
schemaObsByName
|
||||||
) => {
|
) => {
|
||||||
// generate a map describing the coordinates for each value within the given category
|
// generate a map describing the coordinates for each label within the given category
|
||||||
const dataMap = getCoordinatesByCategoricalValues(
|
const dataMap = getCoordinatesByLabel(
|
||||||
obsAnnotations,
|
obsAnnotations,
|
||||||
obsLayout,
|
obsLayout,
|
||||||
categoryName,
|
categoryName,
|
||||||
@@ -114,25 +114,25 @@ const calcMedianCentroid = (
|
|||||||
schemaObsByName
|
schemaObsByName
|
||||||
);
|
);
|
||||||
|
|
||||||
// categoricalValue => [medianXCoordinate, medianYCoordinate]
|
// label => [medianXCoordinate, medianYCoordinate]
|
||||||
const coordinates = new Map();
|
const coordinates = new Map();
|
||||||
|
|
||||||
// Iterate over the recently created map
|
// Iterate over the recently created map
|
||||||
dataMap.forEach((value, key) => {
|
dataMap.forEach((coords, label) => {
|
||||||
// If there are coordinates for this categorical value,
|
// If there are coordinates for this label,
|
||||||
// and there is a finite coordinate for the category value
|
// and there is a finite coordinate for the label
|
||||||
if (value.length > 0 && value.hasFinite) {
|
if (coords.length > 0 && coords.hasFinite) {
|
||||||
const calculatedCoordinates = [];
|
const calculatedCoordinates = [];
|
||||||
|
|
||||||
// Find and store the median x and y coordinate
|
// Find and store the median x and y coordinate
|
||||||
calculatedCoordinates[0] = quantile([0.5], value.xCoordinates)[0];
|
calculatedCoordinates[0] = quantile([0.5], coords.xCoordinates)[0];
|
||||||
calculatedCoordinates[1] = quantile([0.5], value.yCoordinates)[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;
|
return coordinates;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user