@vitessce/sets-utils 4.0.4 → 4.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/dist/index.js +986 -5396
  2. package/dist-tsc/CellSetsZarrLoader.d.ts +23 -0
  3. package/dist-tsc/CellSetsZarrLoader.d.ts.map +1 -1
  4. package/dist-tsc/CellSetsZarrLoader.js +90 -6
  5. package/dist-tsc/CellSetsZarrLoader.js.map +1 -1
  6. package/dist-tsc/CellSetsZarrLoader.test.js +76 -1
  7. package/dist-tsc/CellSetsZarrLoader.test.js.map +1 -1
  8. package/dist-tsc/cell-set-utils.d.ts +78 -1
  9. package/dist-tsc/cell-set-utils.d.ts.map +1 -1
  10. package/dist-tsc/cell-set-utils.js +308 -66
  11. package/dist-tsc/cell-set-utils.js.map +1 -1
  12. package/dist-tsc/cell-set-utils.test.fixtures.d.ts +14 -0
  13. package/dist-tsc/cell-set-utils.test.fixtures.js +18 -0
  14. package/dist-tsc/cell-set-utils.test.fixtures.js.map +1 -1
  15. package/dist-tsc/cell-set-utils.test.js +319 -2
  16. package/dist-tsc/cell-set-utils.test.js.map +1 -1
  17. package/dist-tsc/expr-utils.d.ts +51 -46
  18. package/dist-tsc/expr-utils.d.ts.map +1 -1
  19. package/dist-tsc/expr-utils.js +297 -222
  20. package/dist-tsc/expr-utils.js.map +1 -1
  21. package/dist-tsc/expr-utils.test.js +77 -5
  22. package/dist-tsc/expr-utils.test.js.map +1 -1
  23. package/dist-tsc/index.d.ts +3 -2
  24. package/dist-tsc/index.js +3 -2
  25. package/dist-tsc/index.js.map +1 -1
  26. package/dist-tsc/io.d.ts.map +1 -1
  27. package/dist-tsc/io.js +1 -3
  28. package/dist-tsc/io.js.map +1 -1
  29. package/dist-tsc/membership.d.ts +43 -0
  30. package/dist-tsc/membership.d.ts.map +1 -0
  31. package/dist-tsc/membership.js +212 -0
  32. package/dist-tsc/membership.js.map +1 -0
  33. package/dist-tsc/membership.test.d.ts +2 -0
  34. package/dist-tsc/membership.test.d.ts.map +1 -0
  35. package/dist-tsc/membership.test.js +128 -0
  36. package/dist-tsc/membership.test.js.map +1 -0
  37. package/package.json +6 -5
  38. package/src/CellSetsZarrLoader.js +99 -6
  39. package/src/CellSetsZarrLoader.test.js +88 -1
  40. package/src/cell-set-utils.js +322 -57
  41. package/src/cell-set-utils.test.fixtures.js +19 -0
  42. package/src/cell-set-utils.test.js +392 -0
  43. package/src/expr-utils.js +315 -248
  44. package/src/expr-utils.test.js +121 -5
  45. package/src/index.js +11 -0
  46. package/src/io.js +1 -3
  47. package/src/membership.js +217 -0
  48. package/src/membership.test.js +136 -0
@@ -4,7 +4,7 @@ import { isNil, isEqual, range } from 'lodash-es';
4
4
  import { featureCollection as turfFeatureCollection, point as turfPoint } from '@turf/helpers';
5
5
  import { centroid } from '@turf/centroid';
6
6
  import concaveman from 'concaveman';
7
- import { getDefaultColor, PALETTE } from '@vitessce/utils';
7
+ import { getDefaultColor, PALETTE, MISSING_VALUE_PLACEHOLDER } from '@vitessce/utils';
8
8
  import {
9
9
  HIERARCHICAL_SCHEMAS,
10
10
  } from './constants.js';
@@ -360,7 +360,7 @@ export function treeInitialize(datatype) {
360
360
  export function nodeToRenderProps(node, path, cellSetColor) {
361
361
  const level = path.length - 1;
362
362
  return {
363
- title: node.name,
363
+ title: node.name ?? MISSING_VALUE_PLACEHOLDER,
364
364
  nodeKey: pathToKey(path),
365
365
  path,
366
366
  size: getNodeLength(node),
@@ -403,7 +403,10 @@ function colorMixWithUncertainty(originalColor, p, mixingColor = [128, 128, 128]
403
403
  * and cellColors is an object mapping cellIds to color [r,g,b] arrays.
404
404
  */
405
405
  export function treeToCellColorsBySetNames(currTree, selectedNamePaths, cellSetColor, theme) {
406
- let cellColorsArray = [];
406
+ // Insert into the Map directly rather than accumulating an intermediate array of
407
+ // [cellId, color] tuples. Spreading the accumulator once per selected set is
408
+ // O(numObservations * numSelectedSets); this is O(numObservations).
409
+ const cellColors = new Map();
407
410
  selectedNamePaths.forEach((setNamePath) => {
408
411
  const node = treeFindNodeByNamePath(currTree, setNamePath);
409
412
  if (node) {
@@ -412,16 +415,15 @@ export function treeToCellColorsBySetNames(currTree, selectedNamePaths, cellSetC
412
415
  cellSetColor?.find(d => isEqual(d.path, setNamePath))?.color
413
416
  || getDefaultColor(theme)
414
417
  );
415
- cellColorsArray = [
416
- ...cellColorsArray,
417
- ...nodeSet.map(([cellId, prob]) => [
418
+ nodeSet.forEach(([cellId, prob]) => {
419
+ cellColors.set(
418
420
  cellId,
419
421
  (isNil(prob) ? nodeColor : colorMixWithUncertainty(nodeColor, prob)),
420
- ]),
421
- ];
422
+ );
423
+ });
422
424
  }
423
425
  });
424
- return new Map(cellColorsArray);
426
+ return cellColors;
425
427
  }
426
428
 
427
429
  /**
@@ -438,25 +440,24 @@ export function treeToCellColorsBySetNames(currTree, selectedNamePaths, cellSetC
438
440
  * and cellColors is an object mapping cellIds to color [r,g,b] arrays.
439
441
  */
440
442
  export function treeToSelectedSetMap(currTree, selectedNamePaths) {
441
- let result = [];
443
+ const result = new Map();
442
444
  selectedNamePaths.forEach((setNamePath) => {
443
445
  const node = treeFindNodeByNamePath(currTree, setNamePath);
444
446
  if (node) {
445
447
  const nodeSet = nodeToSet(node);
446
- result = [
447
- ...result,
448
- ...nodeSet.map(([cellId]) => [
448
+ nodeSet.forEach(([cellId]) => {
449
+ result.set(
449
450
  cellId,
450
451
  // TODO: should this be the full path
451
452
  // (rather than only the node name)?
452
453
  // Or the index of the selected set
453
454
  // with respect to the selectedNamePaths array?
454
455
  setNamePath,
455
- ]),
456
- ];
456
+ );
457
+ });
457
458
  }
458
459
  });
459
- return new Map(result);
460
+ return result;
460
461
  }
461
462
 
462
463
  /**
@@ -472,22 +473,267 @@ export function treeToSelectedSetMap(currTree, selectedNamePaths) {
472
473
  * and cellColors is an object mapping cellIds to color [r,g,b] arrays.
473
474
  */
474
475
  export function treeToCellSetColorIndicesBySetNames(currTree, selectedNamePaths, cellSetColor) {
475
- let cellColorsArray = [];
476
+ const cellColorIndices = new Map();
476
477
  selectedNamePaths?.forEach((setNamePath) => {
477
478
  const node = treeFindNodeByNamePath(currTree, setNamePath);
478
479
  if (node) {
479
480
  const nodeSet = nodeToSet(node);
480
481
  const nodeColorIndex = cellSetColor?.findIndex(d => isEqual(d.path, setNamePath));
481
- cellColorsArray = [
482
- ...cellColorsArray,
483
- ...nodeSet.map(([cellId]) => [
484
- cellId,
485
- nodeColorIndex,
486
- ]),
487
- ];
482
+ nodeSet.forEach(([cellId]) => {
483
+ cellColorIndices.set(cellId, nodeColorIndex);
484
+ });
485
+ }
486
+ });
487
+ return cellColorIndices;
488
+ }
489
+
490
+ // Cache of observation-ID-to-position lookups, keyed weakly by the obsIndex array
491
+ // itself. Loaders memoize obsIndex per store path, so views that share an
492
+ // observation axis share the one Map rather than each building their own.
493
+ const obsIndexMapCache = new WeakMap();
494
+
495
+ /**
496
+ * Get a mapping from observation ID to its position in obsIndex, memoized on the
497
+ * obsIndex array reference.
498
+ * @param {string[]} obsIndex The observation index.
499
+ * @returns {Map<string, number>} Mapping from observation ID to integer position.
500
+ */
501
+ export function getObsIndexMap(obsIndex) {
502
+ let obsIndexMap = obsIndexMapCache.get(obsIndex);
503
+ if (!obsIndexMap) {
504
+ obsIndexMap = new Map();
505
+ for (let i = 0; i < obsIndex.length; i += 1) {
506
+ obsIndexMap.set(obsIndex[i], i);
507
+ }
508
+ obsIndexMapCache.set(obsIndex, obsIndexMap);
509
+ }
510
+ return obsIndexMap;
511
+ }
512
+
513
+ /**
514
+ * Build a positionally-indexed color encoding for a set selection.
515
+ *
516
+ * Unlike treeToCellColorsBySetNames, the result is aligned to obsIndex by position
517
+ * rather than keyed by observation ID, so consumers index into it with the same
518
+ * integer they already use to read coordinates. That avoids both a per-observation
519
+ * string hash lookup on every render and the per-observation color array that
520
+ * uncertainty mixing would otherwise allocate.
521
+ *
522
+ * @param {object} currTree A tree object.
523
+ * @param {array} selectedNamePaths Array of arrays of strings, representing set "paths".
524
+ * @param {object[]} cellSetColor Array of objects with the properties `path` and `color`.
525
+ * @param {string[]} obsIndex The observation index to align the result to.
526
+ * @param {string} theme "light" or "dark" for the vitessce theme.
527
+ * @returns {object} An object `{ colorIndices, colorProbs, colors }` where
528
+ * `colorIndices` is a typed array parallel to obsIndex, holding 0 for observations
529
+ * in no selected set and `i + 1` for those in `selectedNamePaths[i]`;
530
+ * `colors` holds the [r, g, b] color per selected set;
531
+ * and `colorProbs` is a Float32Array of per-observation confidence scores, or null
532
+ * when no selected set carries them.
533
+ */
534
+ /**
535
+ * Build the positional color encoding directly from raw categorical codes,
536
+ * skipping the tree walk that treeToColorIndicesArray performs. Applicable when
537
+ * every selected path resolves to a (hierarchy, category) pair in the provided
538
+ * columns, where the category MISSING_VALUE_PLACEHOLDER resolves to the
539
+ * observations with a negative (missing) code. Selections that do not resolve —
540
+ * user-defined selections from additionalObsSets, or paths deeper than two
541
+ * levels — return null so the caller can fall back to the tree route.
542
+ *
543
+ * The output is identical to treeToColorIndicesArray for the tree built from the
544
+ * same columns: index 0 means "in no selected set", and where an observation is
545
+ * in multiple selected sets (across hierarchies), the later path wins.
546
+ *
547
+ * @param {object} params
548
+ * @param {{ path: string[], codes: ArrayLike<number>,
549
+ * categories: string[] }[]} params.columns Raw codes per hierarchy; path is the
550
+ * hierarchy's path in the tree, e.g. ['Cell Type Annotations'].
551
+ * @param {string[]} params.obsIndex The observation index the columns align to.
552
+ * @param {array} params.selectedNamePaths Array of selected set "paths".
553
+ * @param {object[]} params.cellSetColor Array of objects with `path` and `color`.
554
+ * @param {string} params.theme "light" or "dark" for the vitessce theme.
555
+ * @returns {object|null} `{ colorIndices, colorProbs, colors }` as in
556
+ * treeToColorIndicesArray (colorProbs always null: the codes route carries no
557
+ * per-observation scores), or null when a selected path does not resolve.
558
+ */
559
+ /**
560
+ * The smallest unsigned typed array able to index `count` selected sets, with 0
561
+ * reserved for "in no selected set".
562
+ * @param {number} count Number of selected sets.
563
+ * @returns {Uint8ArrayConstructor|Uint16ArrayConstructor|Uint32ArrayConstructor}
564
+ */
565
+ function getIndicesArrayType(count) {
566
+ if (count + 1 <= 256) {
567
+ return Uint8Array;
568
+ }
569
+ return count + 1 <= 65536 ? Uint16Array : Uint32Array;
570
+ }
571
+
572
+ /**
573
+ * Positional membership for a set selection: element i is 1 + the position in
574
+ * selectedNamePaths of the selected set containing observation i, or 0 when it
575
+ * is in none. Where an observation is in several selected sets, the later path
576
+ * wins, matching treeToSelectedSetMap and treeToColorIndicesArray. The
577
+ * observation index map is only built once a selected path resolves to a node.
578
+ * @param {object} currTree A tree object.
579
+ * @param {array} selectedNamePaths Array of selected set "paths".
580
+ * @param {string[]} obsIndex The observation index to align to.
581
+ * @returns {Uint8Array|Uint16Array|Uint32Array} One entry per observation.
582
+ */
583
+ export function treeToSetIndicesArray(currTree, selectedNamePaths, obsIndex) {
584
+ const numObs = obsIndex?.length || 0;
585
+ const paths = selectedNamePaths || [];
586
+ const IndicesArrayType = getIndicesArrayType(paths.length);
587
+ const indices = new IndicesArrayType(numObs);
588
+ let obsIndexMap = null;
589
+ paths.forEach((setNamePath, i) => {
590
+ const node = numObs > 0 && currTree
591
+ ? treeFindNodeByNamePath(currTree, setNamePath)
592
+ : null;
593
+ if (node) {
594
+ if (obsIndexMap === null) {
595
+ obsIndexMap = getObsIndexMap(obsIndex);
596
+ }
597
+ nodeToSet(node).forEach(([cellId]) => {
598
+ const obsI = obsIndexMap.get(cellId);
599
+ if (obsI !== undefined) {
600
+ indices[obsI] = i + 1;
601
+ }
602
+ });
603
+ }
604
+ });
605
+ return indices;
606
+ }
607
+
608
+ /**
609
+ * The same positional membership as treeToSetIndicesArray, computed from raw
610
+ * categorical codes without touching the tree: one typed-array read per
611
+ * observation per hierarchy. The category MISSING_VALUE_PLACEHOLDER resolves to
612
+ * observations with a negative (missing) code.
613
+ * @param {object} params
614
+ * @param {{ path: string[], codes: ArrayLike<number>,
615
+ * categories: string[] }[]} params.columns Raw codes per hierarchy.
616
+ * @param {string[]} params.obsIndex The observation index the columns align to.
617
+ * @param {array} params.selectedNamePaths Array of selected set "paths".
618
+ * @returns {Uint8Array|Uint16Array|Uint32Array|null} One entry per observation,
619
+ * or null when a selected path does not resolve (a user-defined selection, a path
620
+ * deeper than two levels, or an unknown category) so the caller can use the tree.
621
+ */
622
+ export function setIndicesFromCodes({ columns, obsIndex, selectedNamePaths }) {
623
+ const numObs = obsIndex?.length || 0;
624
+ const paths = selectedNamePaths || [];
625
+ // Per column, a map from category code to (selected path index + 1). Later
626
+ // selected paths overwrite earlier ones within a column; across columns, the
627
+ // max index per observation reproduces the same later-path-wins rule.
628
+ const selIdxByCode = columns.map(
629
+ ({ categories }) => new Int32Array(categories.length),
630
+ );
631
+ // Per column, the (selected path index + 1) for observations with a negative
632
+ // (missing) code, when the placeholder-named set is selected.
633
+ const selIdxMissing = new Int32Array(columns.length);
634
+ for (let i = 0; i < paths.length; i += 1) {
635
+ const setNamePath = paths[i];
636
+ const colIndex = columns.findIndex(({ path }) => (
637
+ path.length === setNamePath.length - 1
638
+ && path.every((part, k) => part === setNamePath[k])
639
+ ));
640
+ if (colIndex === -1) {
641
+ return null;
642
+ }
643
+ const category = setNamePath[setNamePath.length - 1];
644
+ const catIndex = columns[colIndex].categories.indexOf(category);
645
+ if (category === MISSING_VALUE_PLACEHOLDER) {
646
+ // The set of observations whose code is negative. Should a real category
647
+ // share the placeholder name, the tree merges both into one set, so both
648
+ // are included here as well.
649
+ selIdxMissing[colIndex] = i + 1;
650
+ } else if (catIndex === -1) {
651
+ return null;
652
+ }
653
+ if (catIndex !== -1) {
654
+ selIdxByCode[colIndex][catIndex] = i + 1;
655
+ }
656
+ }
657
+ const IndicesArrayType = getIndicesArrayType(paths.length);
658
+ const indices = new IndicesArrayType(numObs);
659
+ for (let j = 0; j < columns.length; j += 1) {
660
+ const { codes } = columns[j];
661
+ const table = selIdxByCode[j];
662
+ const missingSel = selIdxMissing[j];
663
+ for (let i = 0; i < numObs; i += 1) {
664
+ const code = codes[i];
665
+ const v = code >= 0 ? table[code] : missingSel;
666
+ if (v > indices[i]) {
667
+ indices[i] = v;
668
+ }
669
+ }
670
+ }
671
+ return indices;
672
+ }
673
+
674
+ export function colorIndicesFromCodes({
675
+ columns, obsIndex, selectedNamePaths, cellSetColor, theme,
676
+ }) {
677
+ const colorIndices = setIndicesFromCodes({ columns, obsIndex, selectedNamePaths });
678
+ if (colorIndices === null) {
679
+ return null;
680
+ }
681
+ const colors = (selectedNamePaths || []).map(setNamePath => (
682
+ cellSetColor?.find(d => isEqual(d.path, setNamePath))?.color
683
+ || getDefaultColor(theme)
684
+ ));
685
+ return { colorIndices, colorProbs: null, colors };
686
+ }
687
+
688
+ export function treeToColorIndicesArray(
689
+ currTree, selectedNamePaths, cellSetColor, obsIndex, theme,
690
+ ) {
691
+ const numObs = obsIndex?.length || 0;
692
+ const paths = selectedNamePaths || [];
693
+ // Reserve 0 for "in no selected set", so the palette needs paths.length + 1 values.
694
+ const IndicesArrayType = getIndicesArrayType(paths.length);
695
+ const colorIndices = new IndicesArrayType(numObs);
696
+ const colors = [];
697
+ // Allocated lazily: most set hierarchies carry no confidence scores, and skipping
698
+ // the array entirely lets consumers take a cheaper code path.
699
+ let colorProbs = null;
700
+
701
+ // Built for every selected path, including paths absent from the tree, so that
702
+ // colors[colorIndices[i] - 1] stays aligned with selectedNamePaths. The
703
+ // observation index map is only built once a selected path resolves to a node,
704
+ // so an empty selection costs nothing beyond the (zeroed) indices array.
705
+ let obsIndexMap = null;
706
+ paths.forEach((setNamePath, i) => {
707
+ colors.push(
708
+ cellSetColor?.find(d => isEqual(d.path, setNamePath))?.color
709
+ || getDefaultColor(theme),
710
+ );
711
+ const node = numObs > 0 && currTree
712
+ ? treeFindNodeByNamePath(currTree, setNamePath)
713
+ : null;
714
+ if (node) {
715
+ if (obsIndexMap === null) {
716
+ obsIndexMap = getObsIndexMap(obsIndex);
717
+ }
718
+ const nodeSet = nodeToSet(node);
719
+ nodeSet.forEach(([cellId, prob]) => {
720
+ const obsI = obsIndexMap.get(cellId);
721
+ if (obsI !== undefined) {
722
+ // Later paths overwrite earlier ones, matching treeToCellColorsBySetNames.
723
+ colorIndices[obsI] = i + 1;
724
+ if (!isNil(prob)) {
725
+ if (colorProbs === null) {
726
+ colorProbs = new Float32Array(numObs).fill(1);
727
+ }
728
+ colorProbs[obsI] = prob;
729
+ } else if (colorProbs !== null) {
730
+ colorProbs[obsI] = 1;
731
+ }
732
+ }
733
+ });
488
734
  }
489
735
  });
490
- return new Map(cellColorsArray);
736
+ return { colorIndices, colorProbs, colors };
491
737
  }
492
738
 
493
739
  /**
@@ -504,7 +750,7 @@ export function treeToCellSetColorIndicesBySetNames(currTree, selectedNamePaths,
504
750
  * `obsId`, `name`, and `color`.
505
751
  */
506
752
  export function treeToObjectsBySetNames(currTree, selectedNamePaths, setColor, theme) {
507
- let cellsArray = [];
753
+ const cellsArray = [];
508
754
  for (let i = 0; i < selectedNamePaths.length; i += 1) {
509
755
  const setNamePath = selectedNamePaths[i];
510
756
  const node = treeFindNodeByNamePath(currTree, setNamePath);
@@ -514,11 +760,13 @@ export function treeToObjectsBySetNames(currTree, selectedNamePaths, setColor, t
514
760
  setColor?.find(d => isEqual(d.path, setNamePath))?.color
515
761
  || getDefaultColor(theme)
516
762
  );
517
- cellsArray = cellsArray.concat(nodeSet.map(([cellId]) => ({
518
- obsId: cellId,
519
- name: node.name,
520
- color: nodeColor,
521
- })));
763
+ nodeSet.forEach(([cellId]) => {
764
+ cellsArray.push({
765
+ obsId: cellId,
766
+ name: node.name ?? MISSING_VALUE_PLACEHOLDER,
767
+ color: nodeColor,
768
+ });
769
+ });
522
770
  }
523
771
  }
524
772
  return cellsArray;
@@ -527,7 +775,7 @@ export function treeToObjectsBySetNames(currTree, selectedNamePaths, setColor, t
527
775
  export function treeToCellPolygonsBySetNames(
528
776
  currTree, obsIndex, obsEmbedding, selectedNamePaths, cellSetColor, theme,
529
777
  ) {
530
- const obsIndexMap = new Map(obsIndex.map((key, i) => ([key, i])));
778
+ const obsIndexMap = getObsIndexMap(obsIndex);
531
779
  const cellSetPolygons = [];
532
780
  selectedNamePaths.forEach((setNamePath) => {
533
781
  const node = treeFindNodeByNamePath(currTree, setNamePath);
@@ -603,7 +851,7 @@ export function treeToSetSizesBySetNames(
603
851
  || getDefaultColor(theme);
604
852
  const nodeProps = {
605
853
  key: generateKey(),
606
- name: node.name,
854
+ name: node.name ?? MISSING_VALUE_PLACEHOLDER,
607
855
  size: nodeSet.length,
608
856
  color: nodeColor,
609
857
  setNamePath: clusterPath,
@@ -638,7 +886,7 @@ export function treeToObsIdsBySetNames(currTree, selectedNamePaths) {
638
886
  const nodeSet = nodeToSet(node);
639
887
  indices.push({
640
888
  key: generateKey(),
641
- name: node.name,
889
+ name: node.name ?? MISSING_VALUE_PLACEHOLDER,
642
890
  path: setNamePath,
643
891
  size: nodeSet.length,
644
892
  // TODO: handle the case where the ID is in the set but missing
@@ -669,7 +917,7 @@ export function treeToObsIndicesBySetNames(currTree, selectedNamePaths, obsIndex
669
917
  const nodeSet = nodeToSet(node);
670
918
  indices.push({
671
919
  key: generateKey(),
672
- name: node.name,
920
+ name: node.name ?? MISSING_VALUE_PLACEHOLDER,
673
921
  path: setNamePath,
674
922
  size: nodeSet.length,
675
923
  // TODO: handle the case where the ID is in the set but missing
@@ -798,30 +1046,47 @@ export function getCellSetPolygons(params) {
798
1046
  return [];
799
1047
  }
800
1048
 
1049
+ /**
1050
+ * Get every leaf set in a tree, with the path that identifies it.
1051
+ *
1052
+ * A single depth-first walk visits each leaf exactly once. Iterating one level at a
1053
+ * time instead re-emitted any leaf shallower than the tree height once per remaining
1054
+ * level, and re-resolved each path against the tree to find its node.
1055
+ *
1056
+ * @param {object} currTree A tree object.
1057
+ * @returns {{ path: string[], set: array[] }[]} The leaf sets, in tree order.
1058
+ */
1059
+ export function treeToLeafSets(currTree) {
1060
+ const leafSets = [];
1061
+ function visitNode(node, prevPath) {
1062
+ const nodePath = [...prevPath, node.name];
1063
+ if (node.children) {
1064
+ node.children.forEach(child => visitNode(child, nodePath));
1065
+ } else {
1066
+ leafSets.push({ path: nodePath, set: nodeToSet(node) });
1067
+ }
1068
+ }
1069
+ if (currTree) {
1070
+ currTree.tree.forEach(lzn => visitNode(lzn, []));
1071
+ }
1072
+ return leafSets;
1073
+ }
1074
+
1075
+ /**
1076
+ * Get a mapping from observation ID to the paths of the leaf sets containing it.
1077
+ * @param {object} currTree A tree object.
1078
+ * @returns {Map<string, string[][]>} Mapping from observation ID to set paths.
1079
+ */
801
1080
  export function treeToMembershipMap(currTree) {
802
1081
  const result = new Map();
803
- if (currTree) {
804
- currTree.tree.forEach((lzn) => {
805
- const height = nodeToHeight(lzn);
806
- range(height).forEach((i) => {
807
- const levelIndex = i + 1;
808
- const levelNodePaths = nodeToLevelDescendantNamePaths(lzn, levelIndex, [], true);
809
- levelNodePaths.forEach((setNamePath) => {
810
- const node = treeFindNodeByNamePath(currTree, setNamePath);
811
- // If this is a child node, then we are interested.
812
- if (node && !node.children) {
813
- const nodeSet = nodeToSet(node);
814
- nodeSet.forEach(([obsId]) => {
815
- if (result.has(obsId)) {
816
- result.get(obsId).push(setNamePath);
817
- } else {
818
- result.set(obsId, [setNamePath]);
819
- }
820
- });
821
- }
822
- });
823
- });
1082
+ treeToLeafSets(currTree).forEach(({ path, set }) => {
1083
+ set.forEach(([obsId]) => {
1084
+ if (result.has(obsId)) {
1085
+ result.get(obsId).push(path);
1086
+ } else {
1087
+ result.set(obsId, [path]);
1088
+ }
824
1089
  });
825
- }
1090
+ });
826
1091
  return result;
827
1092
  }
@@ -69,3 +69,22 @@ export const tree = {
69
69
  },
70
70
  ],
71
71
  };
72
+
73
+ // A tree whose sets carry per-observation confidence scores, as produced by the
74
+ // `scorePath` obsSets option. Used to exercise the uncertainty-mixing branch of
75
+ // treeToCellColorsBySetNames.
76
+ export const treeWithScores = {
77
+ version: '0.1.3',
78
+ datatype: 'cell',
79
+ tree: [
80
+ {
81
+ name: 'Cell Type Annotations',
82
+ children: [
83
+ {
84
+ name: 'Pericytes',
85
+ set: [['cell_1', 1.0], ['cell_2', 0.5], ['cell_3', 0.0]],
86
+ },
87
+ ],
88
+ },
89
+ ],
90
+ };