Clean up max-category front-end limit (#2347)

* remove topN category truncation from component rendering layer

* clean up category item limit implementation

* name change for clarity

* fix snapshot

* comments
This commit is contained in:
Bruce Martin
2021-07-29 16:05:10 -07:00
committed by GitHub
parent 27575b8d86
commit 8136387127
11 changed files with 215 additions and 167 deletions
+4 -9
View File
@@ -18,15 +18,11 @@ function caseInsensitiveCompare(a: any, b: any) {
return textA < textB ? -1 : textA > textB ? 1 : 0;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
const catLabelSort = (isUserAnno: any, values: any) => {
const catLabelSort = (isUserAnno: boolean, values: any[]): any[] => {
/* this sort could be memoized for perf */
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
const strings: any = [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
const ints: any = [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
const strings: string[] = [];
const ints: number[] = [];
const unassignedOrNaN: any = [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
@@ -43,11 +39,10 @@ const catLabelSort = (isUserAnno: any, values: any) => {
});
strings.sort(caseInsensitiveCompare);
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'a' implicitly has an 'any' type.
ints.sort((a, b) => +a - +b);
unassignedOrNaN.sort(caseInsensitiveCompare);
return ints.concat(strings, unassignedOrNaN);
return (<any>ints).concat(strings, unassignedOrNaN);
};
export default catLabelSort;
+6 -3
View File
@@ -76,10 +76,13 @@ export function summarizeCategorical(col: any) {
categoryCounts.set(val, curCount + 1);
}
}
const sortedCategoryByCounts = new Map(
[...categoryCounts.entries()].sort((a, b) => b[1] - a[1])
);
return {
categorical: true,
categories: [...categoryCounts.keys()],
categoryCounts,
numCategories: categoryCounts.size,
categories: [...sortedCategoryByCounts.keys()],
categoryCounts: sortedCategoryByCounts,
numCategories: sortedCategoryByCounts.size,
};
}
@@ -4,8 +4,6 @@ Helper functions for the controls reducer
import difference from "lodash.difference";
import * as globals from "../../globals";
import { rangeFill as fillRange } from "../range";
import fromEntries from "../fromEntries";
import { isCategoricalAnnotation } from "./annotationsHelpers";
@@ -29,41 +27,9 @@ Remember that option values can be ANY js type, except undefined/null.
// number of options
numCategoryValues: number,
// isTruncated - true if the options for selection has
// been truncated (ie, was too large to implement)
}
}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
function topNCategories(colSchema: any, summary: any, N: any) {
/* return top N categories by occurrences in the data */
const { categories: allCategories } = colSchema;
const counts = allCategories.map(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
(cat: any) => summary.categoryCounts.get(cat) ?? 0
);
if (allCategories.length <= N) {
return [allCategories, allCategories, counts];
}
const sortIndex = fillRange(new Array(allCategories.length)).sort(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
(a: any, b: any) => counts[b] - counts[a]
);
const topNindices = new Set(sortIndex.slice(0, N));
const _topNCategories = [];
const topNCounts = [];
for (let i = 0; i < allCategories.length; i += 1) {
if (topNindices.has(i)) {
_topNCategories.push(allCategories[i]);
topNCounts.push(counts[i]);
}
}
return [allCategories, _topNCategories, topNCounts];
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function isSelectableCategoryName(schema: any, name: any) {
@@ -93,7 +59,6 @@ export function selectableCategoryNames(schema: any, names: any) {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function createCategorySummaryFromDfCol(dfCol: any, colSchema: any) {
const N = globals.maxCategoricalOptionsToDisplay;
const { writable: isUserAnno } = colSchema;
/*
@@ -102,24 +67,22 @@ export function createCategorySummaryFromDfCol(dfCol: any, colSchema: any) {
if they are not actively used in the current annoMatrix view.
*/
const summary = dfCol.summarizeCategorical();
const [
allCategoryValues,
categoryValues,
categoryValueCounts,
] = topNCategories(colSchema, summary, N);
const { categories: allCategoryValues } = colSchema;
const categoryValues = allCategoryValues;
const categoryValueCounts = allCategoryValues.map(
(cat: any) => summary.categoryCounts.get(cat) ?? 0
);
const categoryValueIndices = new Map(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
categoryValues.map((v: any, i: any) => [v, i])
);
const numCategoryValues = categoryValueIndices.size;
const isTruncated = categoryValues.length < summary.numCategories;
return {
allCategoryValues, // array: of natively typed category values (all of them)
categoryValues, // array: of natively typed category values (top N only)
categoryValueIndices, // map: category value (native type) -> category index (top N only)
numCategoryValues, // number: of values in the category (top N)
isTruncated, // bool: true if list was truncated (ie, if topN != all)
categoryValueCounts, // array: cardinality of each category, (top N)
isUserAnno, // bool
};