@vitessce/utils 2.0.3-beta.0 → 3.0.0

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,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,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,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":""}
@@ -0,0 +1,7 @@
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"}
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable no-nested-ternary */
2
2
  /* eslint-disable no-param-reassign */
3
3
  import { treeToCellColorsBySetNames } from '@vitessce/sets-utils';
4
- import { getDefaultColor } from './components';
4
+ import { getDefaultColor } from './components.js';
5
5
  // The functions defined here have been adapted from d3-interpolate,
6
6
  // d3-color, and d3-scale-chromatic.
7
7
  // Color string "rgb(r,g,b)" representations are replaced by color array [r, g, b]
@@ -66,32 +66,18 @@ export const interpolatePlasma = interpolateSequentialMulti(schemePlasma);
66
66
  * Get a mapping of cell IDs to cell colors based on
67
67
  * gene / cell set selection coordination state.
68
68
  * @param {object} params
69
- * @param {object} params.expressionMatrix { rows, cols, matrix }
70
- * @param {array} params.geneSelection Array of selected gene IDs.
71
- * @param {object} params.cellSets The cell sets tree.
72
- * @param {object} params.cellSetSelection Selected cell sets.
73
- * @param {string} params.cellColorEncoding Which to use for
74
- * coloring: gene expression or cell sets?
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.
75
76
  * @returns {Map} Mapping from cell IDs to [r, g, b] color arrays.
76
77
  */
77
78
  export function getCellColors(params) {
78
- const { cellColorEncoding, expressionData, cellSets, cellSetSelection, cellSetColor, obsIndex, theme, } = params;
79
- if (cellColorEncoding === 'geneSelection' && expressionData && obsIndex) {
80
- // TODO: allow other color maps.
81
- const geneExpColormap = interpolatePlasma;
82
- const colors = new Map();
83
- for (let i = 0; i < expressionData.length; i += 1) {
84
- const value = expressionData[i];
85
- const cellColor = geneExpColormap(value / 255);
86
- colors.set(obsIndex[i], cellColor);
87
- }
88
- return colors;
89
- }
90
- if (cellColorEncoding === 'cellSetSelection' && cellSetSelection && cellSets) {
91
- // Cell sets can potentially lack set colors since the color property
92
- // is not a required part of the schema.
93
- // The `initializeSets` function fills in any empty colors
94
- // with defaults and returns the processed tree object.
79
+ const { cellSets, cellSetSelection, cellSetColor, obsIndex, theme, } = params;
80
+ if (cellSetSelection && cellSets) {
95
81
  return treeToCellColorsBySetNames(cellSets, cellSetSelection, cellSetColor, theme);
96
82
  }
97
83
  if (obsIndex && theme) {
@@ -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":""}
@@ -1,4 +1,4 @@
1
- import { interpolateRdBu, interpolatePlasma } from './interpolate-colors';
1
+ import { interpolateRdBu, interpolatePlasma } from './interpolate-colors.js';
2
2
  const expectRgb = ([testR, testG, testB], [r, g, b]) => ([
3
3
  expect(testR).toBeCloseTo(r),
4
4
  expect(testG).toBeCloseTo(g),
@@ -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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitessce/utils",
3
- "version": "2.0.3-beta.0",
3
+ "version": "3.0.0",
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
+ "type": "module",
11
12
  "main": "dist/index.js",
12
13
  "files": [
13
- "dist"
14
+ "src",
15
+ "dist",
16
+ "dist-tsc"
14
17
  ],
15
18
  "dependencies": {
16
19
  "bowser": "^2.11.0",
17
- "lodash": "^4.17.21",
20
+ "lodash-es": "^4.17.21",
18
21
  "lz-string": "^1.4.4",
19
- "@vitessce/sets-utils": "2.0.3-beta.0"
22
+ "@vitessce/sets-utils": "3.0.0"
20
23
  },
21
24
  "scripts": {
22
- "start": "tsc --watch",
23
- "build": "tsc",
25
+ "bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",
24
26
  "test": "pnpm exec vitest --run -r ../../../ --dir ."
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/Pool.js ADDED
@@ -0,0 +1,64 @@
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
+ /**
8
+ * Pool for workers to decode chunks of the images.
9
+ * 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
10
+ */
11
+ export default class Pool {
12
+ /**
13
+ * @constructor
14
+ * @param {object} Worker The worker class to be used for processing.
15
+ */
16
+ constructor(createWorker) {
17
+ this.workers = [];
18
+ this.idleWorkers = [];
19
+ this.waitQueue = [];
20
+ this.decoder = null;
21
+
22
+ // eslint-disable-next-line no-plusplus
23
+ for (let i = 0; i < defaultPoolSize; ++i) {
24
+ const w = createWorker();
25
+ this.workers.push(w);
26
+ this.idleWorkers.push(w);
27
+ }
28
+ }
29
+
30
+ // eslint-disable-next-line class-methods-use-this
31
+ async process() {
32
+ throw new Error('Pool needs to implement "process" method');
33
+ }
34
+
35
+ async waitForWorker() {
36
+ const idleWorker = this.idleWorkers.pop();
37
+ if (idleWorker) {
38
+ return idleWorker;
39
+ }
40
+ const waiter = {};
41
+ const promise = new Promise((resolve) => {
42
+ waiter.resolve = resolve;
43
+ });
44
+
45
+ this.waitQueue.push(waiter);
46
+ return promise;
47
+ }
48
+
49
+ async finishTask(currentWorker) {
50
+ const waiter = this.waitQueue.pop();
51
+ if (waiter) {
52
+ waiter.resolve(currentWorker);
53
+ } else {
54
+ this.idleWorkers.push(currentWorker);
55
+ }
56
+ }
57
+
58
+ destroy() {
59
+ // eslint-disable-next-line no-plusplus
60
+ for (let i = 0; i < this.workers.length; ++i) {
61
+ this.workers[i].terminate();
62
+ }
63
+ }
64
+ }
@@ -0,0 +1,142 @@
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
+
5
+ export function getDefaultColor(theme) {
6
+ return theme === 'dark' ? DEFAULT_DARK_COLOR : DEFAULT_LIGHT_COLOR;
7
+ }
8
+
9
+ // From https://personal.sron.nl/~pault/#sec:qualitative
10
+ export const PALETTE = [
11
+ [68, 119, 170],
12
+ [136, 204, 238],
13
+ [68, 170, 153],
14
+ [17, 119, 51],
15
+ [153, 153, 51],
16
+ [221, 204, 119],
17
+ [204, 102, 119],
18
+ [136, 34, 85],
19
+ [170, 68, 153],
20
+ ];
21
+
22
+ export const VIEWER_PALETTE = [
23
+ [0, 0, 255],
24
+ [0, 255, 0],
25
+ [255, 0, 255],
26
+ [255, 255, 0],
27
+ [0, 255, 255],
28
+ [255, 255, 255],
29
+ [255, 128, 0],
30
+ [255, 0, 0],
31
+ ];
32
+
33
+ export const COLORMAP_OPTIONS = [
34
+ 'viridis',
35
+ 'greys',
36
+ 'magma',
37
+ 'jet',
38
+ 'hot',
39
+ 'bone',
40
+ 'copper',
41
+ 'summer',
42
+ 'density',
43
+ 'inferno',
44
+ ];
45
+
46
+ export const DEFAULT_GL_OPTIONS = { webgl2: true };
47
+
48
+ export function createDefaultUpdateStatus(componentName) {
49
+ return message => console.warn(`${componentName} updateStatus: ${message}`);
50
+ }
51
+
52
+ export function createDefaultUpdateCellsSelection(componentName) {
53
+ return cellsSelection => console.warn(`${componentName} updateCellsSelection: ${cellsSelection}`);
54
+ }
55
+
56
+ export function createDefaultUpdateCellsHover(componentName) {
57
+ return hoverInfo => console.warn(`${componentName} updateCellsHover: ${hoverInfo.cellId}`);
58
+ }
59
+
60
+ export function createDefaultUpdateGenesHover(componentName) {
61
+ return hoverInfo => console.warn(`${componentName} updateGenesHover: ${hoverInfo.geneId}`);
62
+ }
63
+
64
+ export function createDefaultUpdateTracksHover(componentName) {
65
+ return hoverInfo => console.warn(`${componentName} updateTracksHover: ${hoverInfo}`);
66
+ }
67
+
68
+ export function createDefaultUpdateViewInfo(componentName) {
69
+ return viewInfo => console.warn(`${componentName} updateViewInfo: ${viewInfo}`);
70
+ }
71
+
72
+ export function createDefaultClearPleaseWait() {
73
+ return () => {};
74
+ }
75
+
76
+
77
+ /**
78
+ * Copy a typed array into a new array buffer.
79
+ * @param {Uint8Array} arr The typed array to be copied.
80
+ * @returns {Uint8Array} The copied array.
81
+ */
82
+ export function copyUint8Array(arr) {
83
+ const newBuffer = new ArrayBuffer(arr.buffer.byteLength);
84
+ const newArr = new Uint8Array(newBuffer);
85
+ newArr.set(arr);
86
+ return newArr;
87
+ }
88
+
89
+ export function asEsModule(component) {
90
+ return {
91
+ __esModule: true,
92
+ default: component,
93
+ };
94
+ }
95
+
96
+
97
+ export function formatBytes(bytes, decimals = 2) {
98
+ if (bytes === 0) return '0 Bytes';
99
+
100
+ const k = 1024;
101
+ const dm = decimals < 0 ? 0 : decimals;
102
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
103
+
104
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
105
+
106
+ // eslint-disable-next-line no-restricted-properties
107
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
108
+ }
109
+
110
+ export const getStatsForResolution = (loader, resolution) => {
111
+ const { shape, labels } = loader[resolution];
112
+ const height = shape[labels.indexOf('y')];
113
+ const width = shape[labels.indexOf('x')];
114
+ const depth = shape[labels.indexOf('z')];
115
+ // eslint-disable-next-line no-bitwise
116
+ const depthDownsampled = Math.max(1, depth >> resolution);
117
+ // Check memory allocation limits for Float32Array (used in XR3DLayer for rendering)
118
+ const totalBytes = 4 * height * width * depthDownsampled;
119
+ return {
120
+ height, width, depthDownsampled, totalBytes,
121
+ };
122
+ };
123
+
124
+ export const canLoadResolution = (loader, resolution) => {
125
+ const {
126
+ totalBytes, height, width, depthDownsampled,
127
+ } = getStatsForResolution(
128
+ loader,
129
+ resolution,
130
+ );
131
+ const maxHeapSize = window.performance?.memory
132
+ && window.performance?.memory?.jsHeapSizeLimit / 2;
133
+ const maxSize = maxHeapSize || (2 ** 31) - 1;
134
+ // 2048 is a normal texture size limit although some browsers go larger.
135
+ return (
136
+ totalBytes < maxSize
137
+ && height <= 2048
138
+ && depthDownsampled <= 2048
139
+ && width <= 2048
140
+ && depthDownsampled > 1
141
+ );
142
+ };
package/src/gating.js ADDED
@@ -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
+ }
package/src/index.js ADDED
@@ -0,0 +1,12 @@
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 {
6
+ getValueTransformFunction,
7
+ VALUE_TRANSFORM_OPTIONS,
8
+ } from './gating.js';
9
+ export {
10
+ getCellColors,
11
+ } from './interpolate-colors.js';
12
+ export { default as Pool } from './Pool.js';
@@ -0,0 +1,104 @@
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
+
6
+ // The functions defined here have been adapted from d3-interpolate,
7
+ // d3-color, and d3-scale-chromatic.
8
+ // Color string "rgb(r,g,b)" representations are replaced by color array [r, g, b]
9
+ // representations, to allow them to work nicely with deck.gl,
10
+ // without the need to converting back and forth between string and array formats.
11
+
12
+ // Reference: https://github.com/d3/d3-scale-chromatic/blob/431d21da776f97c632f53a855bd822edfbbcd56e/src/diverging/RdBu.js
13
+ // eslint-disable-next-line max-len
14
+ 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]];
15
+ // eslint-disable-next-line max-len
16
+ 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]];
17
+
18
+ // Reference: https://github.com/d3/d3-interpolate/blob/96d54051d1c2fec55f240edd0ec5401715b10390/src/rgb.js
19
+ function rgbSpline(spline) {
20
+ return (colors) => {
21
+ const n = colors.length;
22
+ let r = new Array(n);
23
+ let g = new Array(n);
24
+ let b = new Array(n);
25
+ let i; let
26
+ color;
27
+ // eslint-disable-next-line no-plusplus
28
+ for (i = 0; i < n; ++i) {
29
+ color = [colors[i][0], colors[i][1], colors[i][2]];
30
+ r[i] = color[0] || 0;
31
+ g[i] = color[1] || 0;
32
+ b[i] = color[2] || 0;
33
+ }
34
+ r = spline(r);
35
+ g = spline(g);
36
+ b = spline(b);
37
+ return t => [r(t), g(t), b(t)];
38
+ };
39
+ }
40
+
41
+ // Reference: https://github.com/d3/d3-interpolate/blob/594a32af1fe1118812b439012c2cb742e907c0c0/src/basis.js
42
+ function basis(values) {
43
+ function innerBasis(t1, v0, v1, v2, v3) {
44
+ const t2 = t1 * t1; const
45
+ t3 = t2 * t1;
46
+ return ((1 - 3 * t1 + 3 * t2 - t3) * v0
47
+ + (4 - 6 * t2 + 3 * t3) * v1
48
+ + (1 + 3 * t1 + 3 * t2 - 3 * t3) * v2
49
+ + t3 * v3) / 6;
50
+ }
51
+
52
+ const n = values.length - 1;
53
+ return (t) => {
54
+ const i = t <= 0 ? (t = 0) : t >= 1 ? (t = 1, n - 1) : Math.floor(t * n);
55
+ const v1 = values[i];
56
+ const v2 = values[i + 1];
57
+ const v0 = i > 0 ? values[i - 1] : 2 * v1 - v2;
58
+ const v3 = i < n - 1 ? values[i + 2] : 2 * v2 - v1;
59
+ return innerBasis((t - i / n) * n, v0, v1, v2, v3);
60
+ };
61
+ }
62
+
63
+
64
+ // Reference: https://github.com/d3/d3-scale-chromatic/blob/ade54c13e8dfdb9807801a794eaec1a37f926b8a/src/ramp.js
65
+ const interpolateRgbBasis = rgbSpline(basis);
66
+
67
+ function interpolateSequentialMulti(range) {
68
+ const n = range.length;
69
+ return t => range[Math.max(0, Math.min(n - 1, Math.floor(t * n)))];
70
+ }
71
+
72
+ export const interpolateRdBu = interpolateRgbBasis(schemeRdBu);
73
+ export const interpolatePlasma = interpolateSequentialMulti(schemePlasma);
74
+
75
+
76
+ /**
77
+ * Get a mapping of cell IDs to cell colors based on
78
+ * gene / cell set selection coordination state.
79
+ * @param {object} params
80
+ * @param {array[]} params.cellSetSelection Selected cell sets.
81
+ * @param {object[]} params.cellSetColor Array of cell set color
82
+ * objects, each containing a path and color [r, g, b].
83
+ * @param {string[]} params.obsIndex Array of cell IDs,
84
+ * in order to initialize all cells to the default color.
85
+ * @param {string} params.theme The current theme,
86
+ * in order to get the theme-based default color.
87
+ * @returns {Map} Mapping from cell IDs to [r, g, b] color arrays.
88
+ */
89
+ export function getCellColors(params) {
90
+ const {
91
+ cellSets,
92
+ cellSetSelection,
93
+ cellSetColor,
94
+ obsIndex,
95
+ theme,
96
+ } = params;
97
+ if (cellSetSelection && cellSets) {
98
+ return treeToCellColorsBySetNames(cellSets, cellSetSelection, cellSetColor, theme);
99
+ }
100
+ if (obsIndex && theme) {
101
+ return new Map(obsIndex.map(o => ([o, getDefaultColor(theme)])));
102
+ }
103
+ return new Map();
104
+ }
@@ -0,0 +1,29 @@
1
+ import { interpolateRdBu, interpolatePlasma } from './interpolate-colors.js';
2
+
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
+
9
+ describe('components/interpolate-colors', () => {
10
+ describe('interpolateRdBu()', () => {
11
+ it('maps 0 to a red color', () => {
12
+ expectRgb(interpolateRdBu(0), [103, 0, 31]);
13
+ });
14
+
15
+ it('maps 1 to a blue color', () => {
16
+ expectRgb(interpolateRdBu(1), [5, 48, 97]);
17
+ });
18
+ });
19
+
20
+ describe('interpolatePlasma()', () => {
21
+ it('maps 0 to dark cool color', () => {
22
+ expectRgb(interpolatePlasma(0), [13, 8, 135]);
23
+ });
24
+
25
+ it('maps 1 to bright warm color', () => {
26
+ expectRgb(interpolatePlasma(1), [240, 249, 33]);
27
+ });
28
+ });
29
+ });