@vitessce/utils 2.0.3 → 3.0.1

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.
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Pool for workers to decode chunks of the images.
3
+ * This is a line-for-line copy of GeoTIFFs old implementation: https://github.com/geotiffjs/geotiff.js/blob/v1.0.0-beta.6/src/pool.js
4
+ */
5
+ export default class Pool {
6
+ /**
7
+ * @constructor
8
+ * @param {object} Worker The worker class to be used for processing.
9
+ */
10
+ constructor(createWorker: any);
11
+ workers: any[];
12
+ idleWorkers: any[];
13
+ waitQueue: any[];
14
+ decoder: any;
15
+ process(): Promise<void>;
16
+ waitForWorker(): Promise<any>;
17
+ finishTask(currentWorker: any): Promise<void>;
18
+ destroy(): void;
19
+ }
20
+ //# sourceMappingURL=Pool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Pool.d.ts","sourceRoot":"","sources":["../src/Pool.js"],"names":[],"mappings":"AAMA;;;GAGG;AACH;IACE;;;OAGG;IACH,+BAYC;IAXC,eAAiB;IACjB,mBAAqB;IACrB,iBAAmB;IACnB,aAAmB;IAWrB,yBAEC;IAED,8BAYC;IAED,8CAOC;IAED,gBAKC;CACF"}
@@ -0,0 +1,58 @@
1
+ // https://developer.mozilla.org/en-US/docs/Web/API/NavigatorConcurrentHardware/hardwareConcurrency
2
+ // We need to give a different way of getting this for safari, so 4 is probably a safe bet
3
+ // for parallel processing in the meantime. More can't really hurt since they'll just block
4
+ // each other and not the UI thread, which is the real benefit.
5
+ const defaultPoolSize = typeof navigator !== 'undefined' ? navigator.hardwareConcurrency || 4 : null;
6
+ /**
7
+ * Pool for workers to decode chunks of the images.
8
+ * This is a line-for-line copy of GeoTIFFs old implementation: https://github.com/geotiffjs/geotiff.js/blob/v1.0.0-beta.6/src/pool.js
9
+ */
10
+ export default class Pool {
11
+ /**
12
+ * @constructor
13
+ * @param {object} Worker The worker class to be used for processing.
14
+ */
15
+ constructor(createWorker) {
16
+ this.workers = [];
17
+ this.idleWorkers = [];
18
+ this.waitQueue = [];
19
+ this.decoder = null;
20
+ // eslint-disable-next-line no-plusplus
21
+ for (let i = 0; i < defaultPoolSize; ++i) {
22
+ const w = createWorker();
23
+ this.workers.push(w);
24
+ this.idleWorkers.push(w);
25
+ }
26
+ }
27
+ // eslint-disable-next-line class-methods-use-this
28
+ async process() {
29
+ throw new Error('Pool needs to implement "process" method');
30
+ }
31
+ async waitForWorker() {
32
+ const idleWorker = this.idleWorkers.pop();
33
+ if (idleWorker) {
34
+ return idleWorker;
35
+ }
36
+ const waiter = {};
37
+ const promise = new Promise((resolve) => {
38
+ waiter.resolve = resolve;
39
+ });
40
+ this.waitQueue.push(waiter);
41
+ return promise;
42
+ }
43
+ async finishTask(currentWorker) {
44
+ const waiter = this.waitQueue.pop();
45
+ if (waiter) {
46
+ waiter.resolve(currentWorker);
47
+ }
48
+ else {
49
+ this.idleWorkers.push(currentWorker);
50
+ }
51
+ }
52
+ destroy() {
53
+ // eslint-disable-next-line no-plusplus
54
+ for (let i = 0; i < this.workers.length; ++i) {
55
+ this.workers[i].terminate();
56
+ }
57
+ }
58
+ }
@@ -0,0 +1,35 @@
1
+ export function getDefaultColor(theme: any): number[];
2
+ export function createDefaultUpdateStatus(componentName: any): (message: any) => void;
3
+ export function createDefaultUpdateCellsSelection(componentName: any): (cellsSelection: any) => void;
4
+ export function createDefaultUpdateCellsHover(componentName: any): (hoverInfo: any) => void;
5
+ export function createDefaultUpdateGenesHover(componentName: any): (hoverInfo: any) => void;
6
+ export function createDefaultUpdateTracksHover(componentName: any): (hoverInfo: any) => void;
7
+ export function createDefaultUpdateViewInfo(componentName: any): (viewInfo: any) => void;
8
+ export function createDefaultClearPleaseWait(): () => void;
9
+ /**
10
+ * Copy a typed array into a new array buffer.
11
+ * @param {Uint8Array} arr The typed array to be copied.
12
+ * @returns {Uint8Array} The copied array.
13
+ */
14
+ export function copyUint8Array(arr: Uint8Array): Uint8Array;
15
+ export function asEsModule(component: any): {
16
+ __esModule: boolean;
17
+ default: any;
18
+ };
19
+ export function formatBytes(bytes: any, decimals?: number): string;
20
+ export const DEFAULT_DARK_COLOR: number[];
21
+ export const DEFAULT_LIGHT_COLOR: number[];
22
+ export const PALETTE: number[][];
23
+ export const VIEWER_PALETTE: number[][];
24
+ export const COLORMAP_OPTIONS: string[];
25
+ export namespace DEFAULT_GL_OPTIONS {
26
+ const webgl2: boolean;
27
+ }
28
+ export function getStatsForResolution(loader: any, resolution: any): {
29
+ height: any;
30
+ width: any;
31
+ depthDownsampled: number;
32
+ totalBytes: number;
33
+ };
34
+ export function canLoadResolution(loader: any, resolution: any): boolean;
35
+ //# sourceMappingURL=components.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../src/components.js"],"names":[],"mappings":"AAIA,sDAEC;AAyCD,sFAEC;AAED,qGAEC;AAED,4FAEC;AAED,4FAEC;AAED,6FAEC;AAED,yFAEC;AAED,2DAEC;AAGD;;;;GAIG;AACH,oCAHW,UAAU,GACR,UAAU,CAOtB;AAED;;;EAKC;AAGD,mEAWC;AA1GD,0CAA+C;AAC/C,2CAAmD;AAOnD,iCAUE;AAEF,wCASE;AAEF,wCAWE;;;;AAkEK;;;;;EAYN;AAEM,yEAkBN"}
@@ -0,0 +1,114 @@
1
+ // Originally in src/components/utils.js
2
+ export const DEFAULT_DARK_COLOR = [50, 50, 50];
3
+ export const DEFAULT_LIGHT_COLOR = [200, 200, 200];
4
+ export function getDefaultColor(theme) {
5
+ return theme === 'dark' ? DEFAULT_DARK_COLOR : DEFAULT_LIGHT_COLOR;
6
+ }
7
+ // From https://personal.sron.nl/~pault/#sec:qualitative
8
+ export const PALETTE = [
9
+ [68, 119, 170],
10
+ [136, 204, 238],
11
+ [68, 170, 153],
12
+ [17, 119, 51],
13
+ [153, 153, 51],
14
+ [221, 204, 119],
15
+ [204, 102, 119],
16
+ [136, 34, 85],
17
+ [170, 68, 153],
18
+ ];
19
+ export const VIEWER_PALETTE = [
20
+ [0, 0, 255],
21
+ [0, 255, 0],
22
+ [255, 0, 255],
23
+ [255, 255, 0],
24
+ [0, 255, 255],
25
+ [255, 255, 255],
26
+ [255, 128, 0],
27
+ [255, 0, 0],
28
+ ];
29
+ export const COLORMAP_OPTIONS = [
30
+ 'viridis',
31
+ 'greys',
32
+ 'magma',
33
+ 'jet',
34
+ 'hot',
35
+ 'bone',
36
+ 'copper',
37
+ 'summer',
38
+ 'density',
39
+ 'inferno',
40
+ ];
41
+ export const DEFAULT_GL_OPTIONS = { webgl2: true };
42
+ export function createDefaultUpdateStatus(componentName) {
43
+ return message => console.warn(`${componentName} updateStatus: ${message}`);
44
+ }
45
+ export function createDefaultUpdateCellsSelection(componentName) {
46
+ return cellsSelection => console.warn(`${componentName} updateCellsSelection: ${cellsSelection}`);
47
+ }
48
+ export function createDefaultUpdateCellsHover(componentName) {
49
+ return hoverInfo => console.warn(`${componentName} updateCellsHover: ${hoverInfo.cellId}`);
50
+ }
51
+ export function createDefaultUpdateGenesHover(componentName) {
52
+ return hoverInfo => console.warn(`${componentName} updateGenesHover: ${hoverInfo.geneId}`);
53
+ }
54
+ export function createDefaultUpdateTracksHover(componentName) {
55
+ return hoverInfo => console.warn(`${componentName} updateTracksHover: ${hoverInfo}`);
56
+ }
57
+ export function createDefaultUpdateViewInfo(componentName) {
58
+ return viewInfo => console.warn(`${componentName} updateViewInfo: ${viewInfo}`);
59
+ }
60
+ export function createDefaultClearPleaseWait() {
61
+ return () => { };
62
+ }
63
+ /**
64
+ * Copy a typed array into a new array buffer.
65
+ * @param {Uint8Array} arr The typed array to be copied.
66
+ * @returns {Uint8Array} The copied array.
67
+ */
68
+ export function copyUint8Array(arr) {
69
+ const newBuffer = new ArrayBuffer(arr.buffer.byteLength);
70
+ const newArr = new Uint8Array(newBuffer);
71
+ newArr.set(arr);
72
+ return newArr;
73
+ }
74
+ export function asEsModule(component) {
75
+ return {
76
+ __esModule: true,
77
+ default: component,
78
+ };
79
+ }
80
+ export function formatBytes(bytes, decimals = 2) {
81
+ if (bytes === 0)
82
+ return '0 Bytes';
83
+ const k = 1024;
84
+ const dm = decimals < 0 ? 0 : decimals;
85
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
86
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
87
+ // eslint-disable-next-line no-restricted-properties
88
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
89
+ }
90
+ export const getStatsForResolution = (loader, resolution) => {
91
+ const { shape, labels } = loader[resolution];
92
+ const height = shape[labels.indexOf('y')];
93
+ const width = shape[labels.indexOf('x')];
94
+ const depth = shape[labels.indexOf('z')];
95
+ // eslint-disable-next-line no-bitwise
96
+ const depthDownsampled = Math.max(1, depth >> resolution);
97
+ // Check memory allocation limits for Float32Array (used in XR3DLayer for rendering)
98
+ const totalBytes = 4 * height * width * depthDownsampled;
99
+ return {
100
+ height, width, depthDownsampled, totalBytes,
101
+ };
102
+ };
103
+ export const canLoadResolution = (loader, resolution) => {
104
+ const { totalBytes, height, width, depthDownsampled, } = getStatsForResolution(loader, resolution);
105
+ const maxHeapSize = window.performance?.memory
106
+ && window.performance?.memory?.jsHeapSizeLimit / 2;
107
+ const maxSize = maxHeapSize || (2 ** 31) - 1;
108
+ // 2048 is a normal texture size limit although some browsers go larger.
109
+ return (totalBytes < maxSize
110
+ && height <= 2048
111
+ && depthDownsampled <= 2048
112
+ && width <= 2048
113
+ && depthDownsampled > 1);
114
+ };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Get a feature value transform function such as
3
+ * log1p or arcsinh.
4
+ * @param {string} featureValueTransform The function name.
5
+ * @param {number} coefficient The transform coefficient.
6
+ * @returns {function} The function which takes one number
7
+ * as a parameter and returns the transformed number
8
+ * (or the original number in the identity case).
9
+ */
10
+ export function getValueTransformFunction(featureValueTransform: string, coefficient: number): Function;
11
+ export const VALUE_TRANSFORM_OPTIONS: ({
12
+ name: string;
13
+ value: null;
14
+ } | {
15
+ name: string;
16
+ value: string;
17
+ })[];
18
+ //# sourceMappingURL=gating.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gating.d.ts","sourceRoot":"","sources":["../src/gating.js"],"names":[],"mappings":"AAMA;;;;;;;;GAQG;AACH,iEANW,MAAM,eACN,MAAM,YAmBhB;AA5BD;;;;;;KAIE"}
@@ -0,0 +1,30 @@
1
+ // Originally in src/components/gating/utils.js
2
+ export const VALUE_TRANSFORM_OPTIONS = [
3
+ { name: 'None', value: null },
4
+ { name: 'Log', value: 'log1p' },
5
+ { name: 'ArcSinh', value: 'arcsinh' },
6
+ ];
7
+ /**
8
+ * Get a feature value transform function such as
9
+ * log1p or arcsinh.
10
+ * @param {string} featureValueTransform The function name.
11
+ * @param {number} coefficient The transform coefficient.
12
+ * @returns {function} The function which takes one number
13
+ * as a parameter and returns the transformed number
14
+ * (or the original number in the identity case).
15
+ */
16
+ export function getValueTransformFunction(featureValueTransform, coefficient) {
17
+ // Set transform function
18
+ let transformFunction;
19
+ switch (featureValueTransform) {
20
+ case 'log1p':
21
+ transformFunction = v => Math.log(1 + v * coefficient);
22
+ break;
23
+ case 'arcsinh':
24
+ transformFunction = v => Math.asinh(v * coefficient);
25
+ break;
26
+ default:
27
+ transformFunction = v => v;
28
+ }
29
+ return transformFunction;
30
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./root.js";
2
+ export * from "./components.js";
3
+ export { getCellColors } from "./interpolate-colors.js";
4
+ export { default as Pool } from "./Pool.js";
5
+ export { getValueTransformFunction, VALUE_TRANSFORM_OPTIONS } from "./gating.js";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":""}
package/dist-tsc/index.js CHANGED
@@ -1,5 +1,7 @@
1
- export * from './root';
2
- export * from './components';
3
- export { getValueTransformFunction, VALUE_TRANSFORM_OPTIONS, } from './gating';
4
- export { getCellColors, } from './interpolate-colors';
5
- export { default as Pool } from './Pool';
1
+ // eslint-disable-next-line react-refresh/only-export-components
2
+ export * from './root.js';
3
+ // eslint-disable-next-line react-refresh/only-export-components
4
+ export * from './components.js';
5
+ export { getValueTransformFunction, VALUE_TRANSFORM_OPTIONS, } from './gating.js';
6
+ export { getCellColors, } from './interpolate-colors.js';
7
+ export { default as Pool } from './Pool.js';
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Get a mapping of cell IDs to cell colors based on
3
+ * gene / cell set selection coordination state.
4
+ * @param {object} params
5
+ * @param {array[]} params.cellSetSelection Selected cell sets.
6
+ * @param {object[]} params.cellSetColor Array of cell set color
7
+ * objects, each containing a path and color [r, g, b].
8
+ * @param {string[]} params.obsIndex Array of cell IDs,
9
+ * in order to initialize all cells to the default color.
10
+ * @param {string} params.theme The current theme,
11
+ * in order to get the theme-based default color.
12
+ * @returns {Map} Mapping from cell IDs to [r, g, b] color arrays.
13
+ */
14
+ export function getCellColors(params: {
15
+ cellSetSelection: array[];
16
+ cellSetColor: object[];
17
+ obsIndex: string[];
18
+ theme: string;
19
+ }): Map<any, any>;
20
+ export function interpolateRdBu(t: any): any[];
21
+ export function interpolatePlasma(t: any): any;
22
+ //# sourceMappingURL=interpolate-colors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolate-colors.d.ts","sourceRoot":"","sources":["../src/interpolate-colors.js"],"names":[],"mappings":"AA2EA;;;;;;;;;;;;GAYG;AACH;IAT2B,gBAAgB,EAAhC,OAAO;IACU,YAAY,EAA7B,MAAM,EAAE;IAES,QAAQ,EAAzB,MAAM,EAAE;IAEO,KAAK,EAApB,MAAM;kBAmBhB;AAnEU,+CAAuB;AAgCzB,+CAA2D"}
@@ -0,0 +1,87 @@
1
+ /* eslint-disable no-nested-ternary */
2
+ /* eslint-disable no-param-reassign */
3
+ import { treeToCellColorsBySetNames } from '@vitessce/sets-utils';
4
+ import { getDefaultColor } from './components.js';
5
+ // The functions defined here have been adapted from d3-interpolate,
6
+ // d3-color, and d3-scale-chromatic.
7
+ // Color string "rgb(r,g,b)" representations are replaced by color array [r, g, b]
8
+ // representations, to allow them to work nicely with deck.gl,
9
+ // without the need to converting back and forth between string and array formats.
10
+ // Reference: https://github.com/d3/d3-scale-chromatic/blob/431d21da776f97c632f53a855bd822edfbbcd56e/src/diverging/RdBu.js
11
+ // eslint-disable-next-line max-len
12
+ const schemeRdBu = [[103, 0, 31], [178, 24, 43], [214, 96, 77], [244, 165, 130], [253, 219, 199], [247, 247, 247], [209, 229, 240], [146, 197, 222], [67, 147, 195], [33, 102, 172], [5, 48, 97]];
13
+ // eslint-disable-next-line max-len
14
+ const schemePlasma = [[13, 8, 135], [16, 7, 136], [19, 7, 137], [22, 7, 138], [25, 6, 140], [27, 6, 141], [29, 6, 142], [32, 6, 143], [34, 6, 144], [36, 6, 145], [38, 5, 145], [40, 5, 146], [42, 5, 147], [44, 5, 148], [46, 5, 149], [47, 5, 150], [49, 5, 151], [51, 5, 151], [53, 4, 152], [55, 4, 153], [56, 4, 154], [58, 4, 154], [60, 4, 155], [62, 4, 156], [63, 4, 156], [65, 4, 157], [67, 3, 158], [68, 3, 158], [70, 3, 159], [72, 3, 159], [73, 3, 160], [75, 3, 161], [76, 2, 161], [78, 2, 162], [80, 2, 162], [81, 2, 163], [83, 2, 163], [85, 2, 164], [86, 1, 164], [88, 1, 164], [89, 1, 165], [91, 1, 165], [92, 1, 166], [94, 1, 166], [96, 1, 166], [97, 0, 167], [99, 0, 167], [100, 0, 167], [102, 0, 167], [103, 0, 168], [105, 0, 168], [106, 0, 168], [108, 0, 168], [110, 0, 168], [111, 0, 168], [113, 0, 168], [114, 1, 168], [116, 1, 168], [117, 1, 168], [119, 1, 168], [120, 1, 168], [122, 2, 168], [123, 2, 168], [125, 3, 168], [126, 3, 168], [128, 4, 168], [129, 4, 167], [131, 5, 167], [132, 5, 167], [134, 6, 166], [135, 7, 166], [136, 8, 166], [138, 9, 165], [139, 10, 165], [141, 11, 165], [142, 12, 164], [143, 13, 164], [145, 14, 163], [146, 15, 163], [148, 16, 162], [149, 17, 161], [150, 19, 161], [152, 20, 160], [153, 21, 159], [154, 22, 159], [156, 23, 158], [157, 24, 157], [158, 25, 157], [160, 26, 156], [161, 27, 155], [162, 29, 154], [163, 30, 154], [165, 31, 153], [166, 32, 152], [167, 33, 151], [168, 34, 150], [170, 35, 149], [171, 36, 148], [172, 38, 148], [173, 39, 147], [174, 40, 146], [176, 41, 145], [177, 42, 144], [178, 43, 143], [179, 44, 142], [180, 46, 141], [181, 47, 140], [182, 48, 139], [183, 49, 138], [184, 50, 137], [186, 51, 136], [187, 52, 136], [188, 53, 135], [189, 55, 134], [190, 56, 133], [191, 57, 132], [192, 58, 131], [193, 59, 130], [194, 60, 129], [195, 61, 128], [196, 62, 127], [197, 64, 126], [198, 65, 125], [199, 66, 124], [200, 67, 123], [201, 68, 122], [202, 69, 122], [203, 70, 121], [204, 71, 120], [204, 73, 119], [205, 74, 118], [206, 75, 117], [207, 76, 116], [208, 77, 115], [209, 78, 114], [210, 79, 113], [211, 81, 113], [212, 82, 112], [213, 83, 111], [213, 84, 110], [214, 85, 109], [215, 86, 108], [216, 87, 107], [217, 88, 106], [218, 90, 106], [218, 91, 105], [219, 92, 104], [220, 93, 103], [221, 94, 102], [222, 95, 101], [222, 97, 100], [223, 98, 99], [224, 99, 99], [225, 100, 98], [226, 101, 97], [226, 102, 96], [227, 104, 95], [228, 105, 94], [229, 106, 93], [229, 107, 93], [230, 108, 92], [231, 110, 91], [231, 111, 90], [232, 112, 89], [233, 113, 88], [233, 114, 87], [234, 116, 87], [235, 117, 86], [235, 118, 85], [236, 119, 84], [237, 121, 83], [237, 122, 82], [238, 123, 81], [239, 124, 81], [239, 126, 80], [240, 127, 79], [240, 128, 78], [241, 129, 77], [241, 131, 76], [242, 132, 75], [243, 133, 75], [243, 135, 74], [244, 136, 73], [244, 137, 72], [245, 139, 71], [245, 140, 70], [246, 141, 69], [246, 143, 68], [247, 144, 68], [247, 145, 67], [247, 147, 66], [248, 148, 65], [248, 149, 64], [249, 151, 63], [249, 152, 62], [249, 154, 62], [250, 155, 61], [250, 156, 60], [250, 158, 59], [251, 159, 58], [251, 161, 57], [251, 162, 56], [252, 163, 56], [252, 165, 55], [252, 166, 54], [252, 168, 53], [252, 169, 52], [253, 171, 51], [253, 172, 51], [253, 174, 50], [253, 175, 49], [253, 177, 48], [253, 178, 47], [253, 180, 47], [253, 181, 46], [254, 183, 45], [254, 184, 44], [254, 186, 44], [254, 187, 43], [254, 189, 42], [254, 190, 42], [254, 192, 41], [253, 194, 41], [253, 195, 40], [253, 197, 39], [253, 198, 39], [253, 200, 39], [253, 202, 38], [253, 203, 38], [252, 205, 37], [252, 206, 37], [252, 208, 37], [252, 210, 37], [251, 211, 36], [251, 213, 36], [251, 215, 36], [250, 216, 36], [250, 218, 36], [249, 220, 36], [249, 221, 37], [248, 223, 37], [248, 225, 37], [247, 226, 37], [247, 228, 37], [246, 230, 38], [246, 232, 38], [245, 233, 38], [245, 235, 39], [244, 237, 39], [243, 238, 39], [243, 240, 39], [242, 242, 39], [241, 244, 38], [241, 245, 37], [240, 247, 36], [240, 249, 33]];
15
+ // Reference: https://github.com/d3/d3-interpolate/blob/96d54051d1c2fec55f240edd0ec5401715b10390/src/rgb.js
16
+ function rgbSpline(spline) {
17
+ return (colors) => {
18
+ const n = colors.length;
19
+ let r = new Array(n);
20
+ let g = new Array(n);
21
+ let b = new Array(n);
22
+ let i;
23
+ let color;
24
+ // eslint-disable-next-line no-plusplus
25
+ for (i = 0; i < n; ++i) {
26
+ color = [colors[i][0], colors[i][1], colors[i][2]];
27
+ r[i] = color[0] || 0;
28
+ g[i] = color[1] || 0;
29
+ b[i] = color[2] || 0;
30
+ }
31
+ r = spline(r);
32
+ g = spline(g);
33
+ b = spline(b);
34
+ return t => [r(t), g(t), b(t)];
35
+ };
36
+ }
37
+ // Reference: https://github.com/d3/d3-interpolate/blob/594a32af1fe1118812b439012c2cb742e907c0c0/src/basis.js
38
+ function basis(values) {
39
+ function innerBasis(t1, v0, v1, v2, v3) {
40
+ const t2 = t1 * t1;
41
+ const t3 = t2 * t1;
42
+ return ((1 - 3 * t1 + 3 * t2 - t3) * v0
43
+ + (4 - 6 * t2 + 3 * t3) * v1
44
+ + (1 + 3 * t1 + 3 * t2 - 3 * t3) * v2
45
+ + t3 * v3) / 6;
46
+ }
47
+ const n = values.length - 1;
48
+ return (t) => {
49
+ const i = t <= 0 ? (t = 0) : t >= 1 ? (t = 1, n - 1) : Math.floor(t * n);
50
+ const v1 = values[i];
51
+ const v2 = values[i + 1];
52
+ const v0 = i > 0 ? values[i - 1] : 2 * v1 - v2;
53
+ const v3 = i < n - 1 ? values[i + 2] : 2 * v2 - v1;
54
+ return innerBasis((t - i / n) * n, v0, v1, v2, v3);
55
+ };
56
+ }
57
+ // Reference: https://github.com/d3/d3-scale-chromatic/blob/ade54c13e8dfdb9807801a794eaec1a37f926b8a/src/ramp.js
58
+ const interpolateRgbBasis = rgbSpline(basis);
59
+ function interpolateSequentialMulti(range) {
60
+ const n = range.length;
61
+ return t => range[Math.max(0, Math.min(n - 1, Math.floor(t * n)))];
62
+ }
63
+ export const interpolateRdBu = interpolateRgbBasis(schemeRdBu);
64
+ export const interpolatePlasma = interpolateSequentialMulti(schemePlasma);
65
+ /**
66
+ * Get a mapping of cell IDs to cell colors based on
67
+ * gene / cell set selection coordination state.
68
+ * @param {object} params
69
+ * @param {array[]} params.cellSetSelection Selected cell sets.
70
+ * @param {object[]} params.cellSetColor Array of cell set color
71
+ * objects, each containing a path and color [r, g, b].
72
+ * @param {string[]} params.obsIndex Array of cell IDs,
73
+ * in order to initialize all cells to the default color.
74
+ * @param {string} params.theme The current theme,
75
+ * in order to get the theme-based default color.
76
+ * @returns {Map} Mapping from cell IDs to [r, g, b] color arrays.
77
+ */
78
+ export function getCellColors(params) {
79
+ const { cellSets, cellSetSelection, cellSetColor, obsIndex, theme, } = params;
80
+ if (cellSetSelection && cellSets) {
81
+ return treeToCellColorsBySetNames(cellSets, cellSetSelection, cellSetColor, theme);
82
+ }
83
+ if (obsIndex && theme) {
84
+ return new Map(obsIndex.map(o => ([o, getDefaultColor(theme)])));
85
+ }
86
+ return new Map();
87
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=interpolate-colors.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolate-colors.test.d.ts","sourceRoot":"","sources":["../src/interpolate-colors.test.js"],"names":[],"mappings":""}
@@ -0,0 +1,25 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { interpolateRdBu, interpolatePlasma } from './interpolate-colors.js';
3
+ const expectRgb = ([testR, testG, testB], [r, g, b]) => ([
4
+ expect(testR).toBeCloseTo(r),
5
+ expect(testG).toBeCloseTo(g),
6
+ expect(testB).toBeCloseTo(b),
7
+ ]);
8
+ describe('components/interpolate-colors', () => {
9
+ describe('interpolateRdBu()', () => {
10
+ it('maps 0 to a red color', () => {
11
+ expectRgb(interpolateRdBu(0), [103, 0, 31]);
12
+ });
13
+ it('maps 1 to a blue color', () => {
14
+ expectRgb(interpolateRdBu(1), [5, 48, 97]);
15
+ });
16
+ });
17
+ describe('interpolatePlasma()', () => {
18
+ it('maps 0 to dark cool color', () => {
19
+ expectRgb(interpolatePlasma(0), [13, 8, 135]);
20
+ });
21
+ it('maps 1 to bright warm color', () => {
22
+ expectRgb(interpolatePlasma(1), [240, 249, 33]);
23
+ });
24
+ });
25
+ });
@@ -0,0 +1,26 @@
1
+ export function fromEntries(iterable: any): any;
2
+ export function commaNumber(n: any): string;
3
+ /**
4
+ * Capitalize a the first letter of a string.
5
+ * @param {string} word A string to capitalize.
6
+ * @returns {string} The word parameter with the first letter capitalized.
7
+ */
8
+ export function capitalize(word: string): string;
9
+ /**
10
+ * Get the longest string in the array of strings.
11
+ * @param {string[]} strings The array of strings.
12
+ * @returns The longest string.
13
+ */
14
+ export function getLongestString(strings: string[]): string;
15
+ /**
16
+ * Generate a new scope name which does not
17
+ * conflict / overlap with a previous scope name.
18
+ * Really these just need to be unique within the coordination object.
19
+ * So in theory they could be String(Math.random()) or uuidv4() or something.
20
+ * However it may be good to make them more human-readable and memorable
21
+ * since eventually we will want to expose a UI to update the coordination.
22
+ * @param {string[]} prevScopes Previous scope names.
23
+ * @returns {string} The new scope name.
24
+ */
25
+ export function getNextScope(prevScopes: string[]): string;
26
+ //# sourceMappingURL=root.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"root.d.ts","sourceRoot":"","sources":["../src/root.js"],"names":[],"mappings":"AAIA,gDAGC;AAED,4CAGC;AAED;;;;GAIG;AACH,iCAHW,MAAM,GACJ,MAAM,CAIlB;AAED;;;;GAIG;AACH,0CAHW,MAAM,EAAE,UASlB;AAED;;;;;;;;;GASG;AACH,yCAHW,MAAM,EAAE,GACN,MAAM,CAuClB"}
@@ -0,0 +1,74 @@
1
+ /* eslint-disable no-plusplus */
2
+ // Originally in src/utils.js
3
+ // Adapted from https://github.com/feross/fromentries/blob/29b52a850bb3a47c390937631c2638edf3443942/index.js
4
+ export function fromEntries(iterable) {
5
+ return [...iterable]
6
+ .reduce((obj, { 0: key, 1: val }) => Object.assign(obj, { [key]: val }), {});
7
+ }
8
+ export function commaNumber(n) {
9
+ const nf = new Intl.NumberFormat('en-US');
10
+ return nf.format(n);
11
+ }
12
+ /**
13
+ * Capitalize a the first letter of a string.
14
+ * @param {string} word A string to capitalize.
15
+ * @returns {string} The word parameter with the first letter capitalized.
16
+ */
17
+ export function capitalize(word) {
18
+ return word ? word.charAt(0).toUpperCase() + word.slice(1) : '';
19
+ }
20
+ /**
21
+ * Get the longest string in the array of strings.
22
+ * @param {string[]} strings The array of strings.
23
+ * @returns The longest string.
24
+ */
25
+ export function getLongestString(strings) {
26
+ return strings.reduce((prevLongest, currentValue) => (prevLongest.length > currentValue.length ? prevLongest : currentValue));
27
+ }
28
+ /**
29
+ * Generate a new scope name which does not
30
+ * conflict / overlap with a previous scope name.
31
+ * Really these just need to be unique within the coordination object.
32
+ * So in theory they could be String(Math.random()) or uuidv4() or something.
33
+ * However it may be good to make them more human-readable and memorable
34
+ * since eventually we will want to expose a UI to update the coordination.
35
+ * @param {string[]} prevScopes Previous scope names.
36
+ * @returns {string} The new scope name.
37
+ */
38
+ export function getNextScope(prevScopes) {
39
+ // Keep an ordered list of valid characters.
40
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
41
+ // Store the value of the next character for each position
42
+ // in the new string.
43
+ // For example, [0] -> "A", [1] -> "B", [0, 1] -> "AB"
44
+ const nextCharIndices = [0];
45
+ // Generate a new scope name,
46
+ // potentially conflicting with an existing name.
47
+ // Reference: https://stackoverflow.com/a/12504061
48
+ function next() {
49
+ const r = [];
50
+ nextCharIndices.forEach((charIndex) => {
51
+ r.unshift(chars[charIndex]);
52
+ });
53
+ let increment = true;
54
+ for (let i = 0; i < nextCharIndices.length; i++) {
55
+ const val = ++nextCharIndices[i];
56
+ if (val >= chars.length) {
57
+ nextCharIndices[i] = 0;
58
+ }
59
+ else {
60
+ increment = false;
61
+ break;
62
+ }
63
+ }
64
+ if (increment) {
65
+ nextCharIndices.push(0);
66
+ }
67
+ return r.join('');
68
+ }
69
+ let nextScope;
70
+ do {
71
+ nextScope = next();
72
+ } while (prevScopes.includes(nextScope));
73
+ return nextScope;
74
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitessce/utils",
3
- "version": "2.0.3",
3
+ "version": "3.0.1",
4
4
  "author": "Gehlenborg Lab",
5
5
  "homepage": "http://vitessce.io",
6
6
  "repository": {
@@ -8,19 +8,28 @@
8
8
  "url": "git+https://github.com/vitessce/vitessce.git"
9
9
  },
10
10
  "license": "MIT",
11
- "main": "dist/index.mjs",
11
+ "type": "module",
12
+ "main": "dist/index.js",
12
13
  "files": [
14
+ "src",
13
15
  "dist",
14
- "src"
16
+ "dist-tsc"
15
17
  ],
16
18
  "dependencies": {
17
19
  "bowser": "^2.11.0",
18
- "lodash": "^4.17.21",
20
+ "lodash-es": "^4.17.21",
19
21
  "lz-string": "^1.4.4",
20
- "@vitessce/sets-utils": "2.0.3"
22
+ "@vitessce/sets-utils": "3.0.1"
21
23
  },
22
24
  "scripts": {
23
25
  "bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",
24
- "test": "pnpm exec vitest --run -r ../../../ --dir ."
26
+ "test": "pnpm exec vitest --run"
27
+ },
28
+ "module": "dist/index.js",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist-tsc/index.d.ts",
32
+ "import": "./dist/index.js"
33
+ }
25
34
  }
26
35
  }
package/src/index.js CHANGED
@@ -1,10 +1,12 @@
1
- export * from './root';
2
- export * from './components';
1
+ // eslint-disable-next-line react-refresh/only-export-components
2
+ export * from './root.js';
3
+ // eslint-disable-next-line react-refresh/only-export-components
4
+ export * from './components.js';
3
5
  export {
4
6
  getValueTransformFunction,
5
7
  VALUE_TRANSFORM_OPTIONS,
6
- } from './gating';
8
+ } from './gating.js';
7
9
  export {
8
10
  getCellColors,
9
- } from './interpolate-colors';
10
- export { default as Pool } from './Pool';
11
+ } from './interpolate-colors.js';
12
+ export { default as Pool } from './Pool.js';