@vitessce/utils 3.8.8 → 3.8.9

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.
package/dist/index.js CHANGED
@@ -893,6 +893,58 @@ class Pool {
893
893
  }
894
894
  }
895
895
  }
896
+ function aggregateFeatureArrays(arrays, strategy) {
897
+ if (!arrays || arrays.length === 0) return null;
898
+ let targetArray;
899
+ if (strategy === "first" || typeof strategy === "number" && strategy === 0) {
900
+ targetArray = arrays[0];
901
+ } else if (strategy === "last") {
902
+ targetArray = arrays[arrays.length - 1];
903
+ } else if (typeof strategy === "number") {
904
+ if (strategy >= 0 && strategy < arrays.length) {
905
+ targetArray = arrays[strategy];
906
+ } else {
907
+ throw new Error(`Array index out of bounds: ${strategy}`);
908
+ }
909
+ }
910
+ if (targetArray) {
911
+ return targetArray;
912
+ }
913
+ const numArrays = arrays.length;
914
+ const firstArrayLength = arrays[0].length;
915
+ if (arrays.some((arr) => arr.length !== firstArrayLength)) {
916
+ throw new Error("All arrays must have the same length for aggregation.");
917
+ }
918
+ if (strategy === "sum" || strategy === "mean") {
919
+ const resultArray = new Float64Array(firstArrayLength);
920
+ for (let i = 0; i < numArrays; i++) {
921
+ const arr = arrays[i];
922
+ for (let j = 0; j < firstArrayLength; j++) {
923
+ resultArray[j] += arr[j];
924
+ }
925
+ }
926
+ if (strategy === "mean") {
927
+ for (let i = 0; i < firstArrayLength; i++) {
928
+ resultArray[i] /= numArrays;
929
+ }
930
+ }
931
+ return resultArray;
932
+ }
933
+ if (strategy === "difference") {
934
+ if (numArrays !== 2) {
935
+ console.warn("Difference strategy requires exactly 2 arrays.");
936
+ return arrays[0];
937
+ }
938
+ const arr0 = arrays[0];
939
+ const arr1 = arrays[1];
940
+ const resultArray = new Float64Array(firstArrayLength);
941
+ for (let i = 0; i < firstArrayLength; i++) {
942
+ resultArray[i] = arr0[i] - arr1[i];
943
+ }
944
+ return resultArray;
945
+ }
946
+ throw new Error(`Unknown aggregation strategy: ${strategy}`);
947
+ }
896
948
  export {
897
949
  COLORMAP_OPTIONS,
898
950
  DEFAULT_DARK_COLOR,
@@ -904,6 +956,7 @@ export {
904
956
  Pool,
905
957
  VALUE_TRANSFORM_OPTIONS,
906
958
  VIEWER_PALETTE,
959
+ aggregateFeatureArrays,
907
960
  asEsModule,
908
961
  capitalize,
909
962
  cleanFeatureId,
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Aggregates multiple arrays of numbers into a single array using a specified strategy.
3
+ *
4
+ * This function aggregates element-wise across all input arrays.
5
+ *
6
+ * @param {Array<Array<number>>} arrays - List of arrays to aggregate
7
+ * (e.g., multiple gene expression arrays).
8
+ * @param {string|number} strategy - Aggregation method:
9
+ * - 'first', 'last', or number: Selects a specific array.
10
+ * - 'sum', 'mean': Mathematical aggregation.
11
+ * - 'difference': Subtracts the second array from the first (requires exactly 2 arrays).
12
+ * @returns {Array<number>|null} - The aggregated array or null on error/empty input.
13
+ */
14
+ export declare function aggregateFeatureArrays(arrays: Array<Array<number>>, strategy: string | number): number[] | Float64Array | null;
15
+ //# sourceMappingURL=expr.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expr.d.ts","sourceRoot":"","sources":["../src/expr.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,EAC5B,QAAQ,EAAE,MAAM,GAAG,MAAM,kCAqE1B"}
@@ -0,0 +1,73 @@
1
+ /* eslint-disable prefer-destructuring */
2
+ /**
3
+ * Aggregates multiple arrays of numbers into a single array using a specified strategy.
4
+ *
5
+ * This function aggregates element-wise across all input arrays.
6
+ *
7
+ * @param {Array<Array<number>>} arrays - List of arrays to aggregate
8
+ * (e.g., multiple gene expression arrays).
9
+ * @param {string|number} strategy - Aggregation method:
10
+ * - 'first', 'last', or number: Selects a specific array.
11
+ * - 'sum', 'mean': Mathematical aggregation.
12
+ * - 'difference': Subtracts the second array from the first (requires exactly 2 arrays).
13
+ * @returns {Array<number>|null} - The aggregated array or null on error/empty input.
14
+ */
15
+ export function aggregateFeatureArrays(arrays, strategy) {
16
+ if (!arrays || arrays.length === 0)
17
+ return null;
18
+ // Check these first to avoid any setup overhead for simple lookups.
19
+ let targetArray;
20
+ if (strategy === 'first' || (typeof strategy === 'number' && strategy === 0)) {
21
+ targetArray = arrays[0];
22
+ }
23
+ else if (strategy === 'last') {
24
+ targetArray = arrays[arrays.length - 1];
25
+ }
26
+ else if (typeof strategy === 'number') {
27
+ if (strategy >= 0 && strategy < arrays.length) {
28
+ targetArray = arrays[strategy];
29
+ }
30
+ else {
31
+ throw new Error(`Array index out of bounds: ${strategy}`);
32
+ }
33
+ }
34
+ // If a selection strategy was matched, return the array immediately
35
+ if (targetArray) {
36
+ return targetArray;
37
+ }
38
+ const numArrays = arrays.length;
39
+ const firstArrayLength = arrays[0].length;
40
+ // Validate lengths match for element-wise operations
41
+ if (arrays.some(arr => arr.length !== firstArrayLength)) {
42
+ throw new Error('All arrays must have the same length for aggregation.');
43
+ }
44
+ if (strategy === 'sum' || strategy === 'mean') {
45
+ const resultArray = new Float64Array(firstArrayLength);
46
+ for (let i = 0; i < numArrays; i++) {
47
+ const arr = arrays[i];
48
+ for (let j = 0; j < firstArrayLength; j++) {
49
+ resultArray[j] += arr[j];
50
+ }
51
+ }
52
+ if (strategy === 'mean') {
53
+ for (let i = 0; i < firstArrayLength; i++) {
54
+ resultArray[i] /= numArrays;
55
+ }
56
+ }
57
+ return resultArray;
58
+ }
59
+ if (strategy === 'difference') {
60
+ if (numArrays !== 2) {
61
+ console.warn('Difference strategy requires exactly 2 arrays.');
62
+ return arrays[0];
63
+ }
64
+ const arr0 = arrays[0];
65
+ const arr1 = arrays[1];
66
+ const resultArray = new Float64Array(firstArrayLength);
67
+ for (let i = 0; i < firstArrayLength; i++) {
68
+ resultArray[i] = arr0[i] - arr1[i];
69
+ }
70
+ return resultArray;
71
+ }
72
+ throw new Error(`Unknown aggregation strategy: ${strategy}`);
73
+ }
@@ -2,4 +2,5 @@ export { commaNumber, capitalize, pluralize, cleanFeatureId, getLongestString, g
2
2
  export { DEFAULT_DARK_COLOR, DEFAULT_LIGHT_COLOR, getDefaultColor, getDefaultForegroundColor, PALETTE, VIEWER_PALETTE, PATHOLOGY_PALETTE, LARGE_PATHOLOGY_PALETTE, COLORMAP_OPTIONS, DEFAULT_GL_OPTIONS, createDefaultUpdateStatus, createDefaultUpdateCellsSelection, createDefaultUpdateCellsHover, createDefaultUpdateGenesHover, createDefaultUpdateTracksHover, createDefaultUpdateViewInfo, createDefaultClearPleaseWait, copyUint8Array, asEsModule, formatBytes, } from './components.js';
3
3
  export { getValueTransformFunction, VALUE_TRANSFORM_OPTIONS, } from './gating.js';
4
4
  export { default as Pool } from './Pool.js';
5
+ export { aggregateFeatureArrays } from './expr.js';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,UAAU,EACV,SAAS,EACT,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,mBAAmB,EACnB,iCAAiC,EACjC,iCAAiC,EACjC,+BAA+B,EAC/B,SAAS,GACV,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,yBAAyB,EACzB,OAAO,EACP,cAAc,EACd,iBAAiB,EACjB,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,EAClB,yBAAyB,EACzB,iCAAiC,EACjC,6BAA6B,EAC7B,6BAA6B,EAC7B,8BAA8B,EAC9B,2BAA2B,EAC3B,4BAA4B,EAC5B,cAAc,EACd,UAAU,EACV,WAAW,GACZ,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,WAAW,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,UAAU,EACV,SAAS,EACT,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,mBAAmB,EACnB,iCAAiC,EACjC,iCAAiC,EACjC,+BAA+B,EAC/B,SAAS,GACV,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,yBAAyB,EACzB,OAAO,EACP,cAAc,EACd,iBAAiB,EACjB,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,EAClB,yBAAyB,EACzB,iCAAiC,EACjC,6BAA6B,EAC7B,6BAA6B,EAC7B,8BAA8B,EAC9B,2BAA2B,EAC3B,4BAA4B,EAC5B,cAAc,EACd,UAAU,EACV,WAAW,GACZ,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,WAAW,CAAC;AAC5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC"}
package/dist-tsc/index.js CHANGED
@@ -4,3 +4,4 @@ export { commaNumber, capitalize, pluralize, cleanFeatureId, getLongestString, g
4
4
  export { DEFAULT_DARK_COLOR, DEFAULT_LIGHT_COLOR, getDefaultColor, getDefaultForegroundColor, PALETTE, VIEWER_PALETTE, PATHOLOGY_PALETTE, LARGE_PATHOLOGY_PALETTE, COLORMAP_OPTIONS, DEFAULT_GL_OPTIONS, createDefaultUpdateStatus, createDefaultUpdateCellsSelection, createDefaultUpdateCellsHover, createDefaultUpdateGenesHover, createDefaultUpdateTracksHover, createDefaultUpdateViewInfo, createDefaultClearPleaseWait, copyUint8Array, asEsModule, formatBytes, } from './components.js';
5
5
  export { getValueTransformFunction, VALUE_TRANSFORM_OPTIONS, } from './gating.js';
6
6
  export { default as Pool } from './Pool.js';
7
+ export { aggregateFeatureArrays } from './expr.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitessce/utils",
3
- "version": "3.8.8",
3
+ "version": "3.8.9",
4
4
  "author": "HIDIVE Lab at HMS",
5
5
  "homepage": "http://vitessce.io",
6
6
  "repository": {
@@ -21,7 +21,7 @@
21
21
  "lz-string": "^1.4.4",
22
22
  "pluralize": "^8.0.0",
23
23
  "d3-array": "^2.4.0",
24
- "@vitessce/globals": "3.8.8"
24
+ "@vitessce/globals": "3.8.9"
25
25
  },
26
26
  "scripts": {
27
27
  "bundle": "pnpm exec vite build -c ../../../scripts/vite.config.mjs",
package/src/expr.ts ADDED
@@ -0,0 +1,87 @@
1
+ /* eslint-disable prefer-destructuring */
2
+
3
+ /**
4
+ * Aggregates multiple arrays of numbers into a single array using a specified strategy.
5
+ *
6
+ * This function aggregates element-wise across all input arrays.
7
+ *
8
+ * @param {Array<Array<number>>} arrays - List of arrays to aggregate
9
+ * (e.g., multiple gene expression arrays).
10
+ * @param {string|number} strategy - Aggregation method:
11
+ * - 'first', 'last', or number: Selects a specific array.
12
+ * - 'sum', 'mean': Mathematical aggregation.
13
+ * - 'difference': Subtracts the second array from the first (requires exactly 2 arrays).
14
+ * @returns {Array<number>|null} - The aggregated array or null on error/empty input.
15
+ */
16
+ export function aggregateFeatureArrays(
17
+ arrays: Array<Array<number>>,
18
+ strategy: string | number,
19
+ ) {
20
+ if (!arrays || arrays.length === 0) return null;
21
+
22
+ // Check these first to avoid any setup overhead for simple lookups.
23
+ let targetArray: number[] | undefined;
24
+
25
+ if (strategy === 'first' || (typeof strategy === 'number' && strategy === 0)) {
26
+ targetArray = arrays[0];
27
+ } else if (strategy === 'last') {
28
+ targetArray = arrays[arrays.length - 1];
29
+ } else if (typeof strategy === 'number') {
30
+ if (strategy >= 0 && strategy < arrays.length) {
31
+ targetArray = arrays[strategy];
32
+ } else {
33
+ throw new Error(`Array index out of bounds: ${strategy}`);
34
+ }
35
+ }
36
+
37
+ // If a selection strategy was matched, return the array immediately
38
+ if (targetArray) {
39
+ return targetArray;
40
+ }
41
+
42
+ const numArrays = arrays.length;
43
+ const firstArrayLength = arrays[0].length;
44
+
45
+ // Validate lengths match for element-wise operations
46
+ if (arrays.some(arr => arr.length !== firstArrayLength)) {
47
+ throw new Error('All arrays must have the same length for aggregation.');
48
+ }
49
+
50
+ if (strategy === 'sum' || strategy === 'mean') {
51
+ const resultArray = new Float64Array(firstArrayLength);
52
+
53
+ for (let i = 0; i < numArrays; i++) {
54
+ const arr = arrays[i];
55
+ for (let j = 0; j < firstArrayLength; j++) {
56
+ resultArray[j] += arr[j];
57
+ }
58
+ }
59
+
60
+ if (strategy === 'mean') {
61
+ for (let i = 0; i < firstArrayLength; i++) {
62
+ resultArray[i] /= numArrays;
63
+ }
64
+ }
65
+
66
+ return resultArray;
67
+ }
68
+
69
+ if (strategy === 'difference') {
70
+ if (numArrays !== 2) {
71
+ console.warn('Difference strategy requires exactly 2 arrays.');
72
+ return arrays[0];
73
+ }
74
+
75
+ const arr0 = arrays[0];
76
+ const arr1 = arrays[1];
77
+ const resultArray = new Float64Array(firstArrayLength);
78
+
79
+ for (let i = 0; i < firstArrayLength; i++) {
80
+ resultArray[i] = arr0[i] - arr1[i];
81
+ }
82
+
83
+ return resultArray;
84
+ }
85
+
86
+ throw new Error(`Unknown aggregation strategy: ${strategy}`);
87
+ }
package/src/index.ts CHANGED
@@ -40,3 +40,4 @@ export {
40
40
  VALUE_TRANSFORM_OPTIONS,
41
41
  } from './gating.js';
42
42
  export { default as Pool } from './Pool.js';
43
+ export { aggregateFeatureArrays } from './expr.js';