@vitessce/utils 2.0.3-beta.0 → 2.0.3
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.mjs +9995 -0
- package/{dist → dist-tsc}/index.js +0 -0
- package/package.json +6 -6
- package/src/Pool.js +64 -0
- package/src/components.js +142 -0
- package/{dist → src}/gating.js +16 -16
- package/src/index.js +10 -0
- package/{dist → src}/interpolate-colors.js +78 -60
- package/src/interpolate-colors.test.js +29 -0
- package/src/root.js +84 -0
- package/dist/Pool.js +0 -58
- package/dist/components.js +0 -114
- package/dist/interpolate-colors.test.js +0 -24
- package/dist/root.js +0 -74
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitessce/utils",
|
|
3
|
-
"version": "2.0.3
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"author": "Gehlenborg Lab",
|
|
5
5
|
"homepage": "http://vitessce.io",
|
|
6
6
|
"repository": {
|
|
@@ -8,19 +8,19 @@
|
|
|
8
8
|
"url": "git+https://github.com/vitessce/vitessce.git"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"main": "dist/index.
|
|
11
|
+
"main": "dist/index.mjs",
|
|
12
12
|
"files": [
|
|
13
|
-
"dist"
|
|
13
|
+
"dist",
|
|
14
|
+
"src"
|
|
14
15
|
],
|
|
15
16
|
"dependencies": {
|
|
16
17
|
"bowser": "^2.11.0",
|
|
17
18
|
"lodash": "^4.17.21",
|
|
18
19
|
"lz-string": "^1.4.4",
|
|
19
|
-
"@vitessce/sets-utils": "2.0.3
|
|
20
|
+
"@vitessce/sets-utils": "2.0.3"
|
|
20
21
|
},
|
|
21
22
|
"scripts": {
|
|
22
|
-
"
|
|
23
|
-
"build": "tsc",
|
|
23
|
+
"bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",
|
|
24
24
|
"test": "pnpm exec vitest --run -r ../../../ --dir ."
|
|
25
25
|
}
|
|
26
26
|
}
|
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/{dist → src}/gating.js
RENAMED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Originally in src/components/gating/utils.js
|
|
2
2
|
export const VALUE_TRANSFORM_OPTIONS = [
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
{ name: 'None', value: null },
|
|
4
|
+
{ name: 'Log', value: 'log1p' },
|
|
5
|
+
{ name: 'ArcSinh', value: 'arcsinh' },
|
|
6
6
|
];
|
|
7
7
|
/**
|
|
8
8
|
* Get a feature value transform function such as
|
|
@@ -14,17 +14,17 @@ export const VALUE_TRANSFORM_OPTIONS = [
|
|
|
14
14
|
* (or the original number in the identity case).
|
|
15
15
|
*/
|
|
16
16
|
export function getValueTransformFunction(featureValueTransform, coefficient) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
30
|
}
|
package/src/index.js
ADDED
|
@@ -2,66 +2,77 @@
|
|
|
2
2
|
/* eslint-disable no-param-reassign */
|
|
3
3
|
import { treeToCellColorsBySetNames } from '@vitessce/sets-utils';
|
|
4
4
|
import { getDefaultColor } from './components';
|
|
5
|
+
|
|
5
6
|
// The functions defined here have been adapted from d3-interpolate,
|
|
6
7
|
// d3-color, and d3-scale-chromatic.
|
|
7
8
|
// Color string "rgb(r,g,b)" representations are replaced by color array [r, g, b]
|
|
8
9
|
// representations, to allow them to work nicely with deck.gl,
|
|
9
10
|
// without the need to converting back and forth between string and array formats.
|
|
11
|
+
|
|
10
12
|
// Reference: https://github.com/d3/d3-scale-chromatic/blob/431d21da776f97c632f53a855bd822edfbbcd56e/src/diverging/RdBu.js
|
|
11
13
|
// eslint-disable-next-line max-len
|
|
12
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]];
|
|
13
15
|
// eslint-disable-next-line max-len
|
|
14
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
|
+
|
|
15
18
|
// Reference: https://github.com/d3/d3-interpolate/blob/96d54051d1c2fec55f240edd0ec5401715b10390/src/rgb.js
|
|
16
19
|
function rgbSpline(spline) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
+
};
|
|
36
39
|
}
|
|
40
|
+
|
|
37
41
|
// Reference: https://github.com/d3/d3-interpolate/blob/594a32af1fe1118812b439012c2cb742e907c0c0/src/basis.js
|
|
38
42
|
function basis(values) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
+
};
|
|
56
61
|
}
|
|
62
|
+
|
|
63
|
+
|
|
57
64
|
// Reference: https://github.com/d3/d3-scale-chromatic/blob/ade54c13e8dfdb9807801a794eaec1a37f926b8a/src/ramp.js
|
|
58
65
|
const interpolateRgbBasis = rgbSpline(basis);
|
|
66
|
+
|
|
59
67
|
function interpolateSequentialMulti(range) {
|
|
60
|
-
|
|
61
|
-
|
|
68
|
+
const n = range.length;
|
|
69
|
+
return t => range[Math.max(0, Math.min(n - 1, Math.floor(t * n)))];
|
|
62
70
|
}
|
|
71
|
+
|
|
63
72
|
export const interpolateRdBu = interpolateRgbBasis(schemeRdBu);
|
|
64
73
|
export const interpolatePlasma = interpolateSequentialMulti(schemePlasma);
|
|
74
|
+
|
|
75
|
+
|
|
65
76
|
/**
|
|
66
77
|
* Get a mapping of cell IDs to cell colors based on
|
|
67
78
|
* gene / cell set selection coordination state.
|
|
@@ -75,27 +86,34 @@ export const interpolatePlasma = interpolateSequentialMulti(schemePlasma);
|
|
|
75
86
|
* @returns {Map} Mapping from cell IDs to [r, g, b] color arrays.
|
|
76
87
|
*/
|
|
77
88
|
export function getCellColors(params) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
// with defaults and returns the processed tree object.
|
|
95
|
-
return treeToCellColorsBySetNames(cellSets, cellSetSelection, cellSetColor, theme);
|
|
96
|
-
}
|
|
97
|
-
if (obsIndex && theme) {
|
|
98
|
-
return new Map(obsIndex.map(o => ([o, getDefaultColor(theme)])));
|
|
89
|
+
const {
|
|
90
|
+
cellColorEncoding,
|
|
91
|
+
expressionData,
|
|
92
|
+
cellSets, cellSetSelection,
|
|
93
|
+
cellSetColor,
|
|
94
|
+
obsIndex,
|
|
95
|
+
theme,
|
|
96
|
+
} = params;
|
|
97
|
+
if (cellColorEncoding === 'geneSelection' && expressionData && obsIndex) {
|
|
98
|
+
// TODO: allow other color maps.
|
|
99
|
+
const geneExpColormap = interpolatePlasma;
|
|
100
|
+
const colors = new Map();
|
|
101
|
+
for (let i = 0; i < expressionData.length; i += 1) {
|
|
102
|
+
const value = expressionData[i];
|
|
103
|
+
const cellColor = geneExpColormap(value / 255);
|
|
104
|
+
colors.set(obsIndex[i], cellColor);
|
|
99
105
|
}
|
|
100
|
-
return
|
|
106
|
+
return colors;
|
|
107
|
+
}
|
|
108
|
+
if (cellColorEncoding === 'cellSetSelection' && cellSetSelection && cellSets) {
|
|
109
|
+
// Cell sets can potentially lack set colors since the color property
|
|
110
|
+
// is not a required part of the schema.
|
|
111
|
+
// The `initializeSets` function fills in any empty colors
|
|
112
|
+
// with defaults and returns the processed tree object.
|
|
113
|
+
return treeToCellColorsBySetNames(cellSets, cellSetSelection, cellSetColor, theme);
|
|
114
|
+
}
|
|
115
|
+
if (obsIndex && theme) {
|
|
116
|
+
return new Map(obsIndex.map(o => ([o, getDefaultColor(theme)])));
|
|
117
|
+
}
|
|
118
|
+
return new Map();
|
|
101
119
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { interpolateRdBu, interpolatePlasma } from './interpolate-colors';
|
|
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
|
+
});
|
package/src/root.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/* eslint-disable no-plusplus */
|
|
2
|
+
// Originally in src/utils.js
|
|
3
|
+
|
|
4
|
+
// Adapted from https://github.com/feross/fromentries/blob/29b52a850bb3a47c390937631c2638edf3443942/index.js
|
|
5
|
+
export function fromEntries(iterable) {
|
|
6
|
+
return [...iterable]
|
|
7
|
+
.reduce((obj, { 0: key, 1: val }) => Object.assign(obj, { [key]: val }), {});
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function commaNumber(n) {
|
|
11
|
+
const nf = new Intl.NumberFormat('en-US');
|
|
12
|
+
return nf.format(n);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Capitalize a the first letter of a string.
|
|
17
|
+
* @param {string} word A string to capitalize.
|
|
18
|
+
* @returns {string} The word parameter with the first letter capitalized.
|
|
19
|
+
*/
|
|
20
|
+
export function capitalize(word) {
|
|
21
|
+
return word ? word.charAt(0).toUpperCase() + word.slice(1) : '';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get the longest string in the array of strings.
|
|
26
|
+
* @param {string[]} strings The array of strings.
|
|
27
|
+
* @returns The longest string.
|
|
28
|
+
*/
|
|
29
|
+
export function getLongestString(strings) {
|
|
30
|
+
return strings.reduce(
|
|
31
|
+
(prevLongest, currentValue) => (
|
|
32
|
+
prevLongest.length > currentValue.length ? prevLongest : currentValue
|
|
33
|
+
),
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Generate a new scope name which does not
|
|
39
|
+
* conflict / overlap with a previous scope name.
|
|
40
|
+
* Really these just need to be unique within the coordination object.
|
|
41
|
+
* So in theory they could be String(Math.random()) or uuidv4() or something.
|
|
42
|
+
* However it may be good to make them more human-readable and memorable
|
|
43
|
+
* since eventually we will want to expose a UI to update the coordination.
|
|
44
|
+
* @param {string[]} prevScopes Previous scope names.
|
|
45
|
+
* @returns {string} The new scope name.
|
|
46
|
+
*/
|
|
47
|
+
export function getNextScope(prevScopes) {
|
|
48
|
+
// Keep an ordered list of valid characters.
|
|
49
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
50
|
+
// Store the value of the next character for each position
|
|
51
|
+
// in the new string.
|
|
52
|
+
// For example, [0] -> "A", [1] -> "B", [0, 1] -> "AB"
|
|
53
|
+
const nextCharIndices = [0];
|
|
54
|
+
|
|
55
|
+
// Generate a new scope name,
|
|
56
|
+
// potentially conflicting with an existing name.
|
|
57
|
+
// Reference: https://stackoverflow.com/a/12504061
|
|
58
|
+
function next() {
|
|
59
|
+
const r = [];
|
|
60
|
+
nextCharIndices.forEach((charIndex) => {
|
|
61
|
+
r.unshift(chars[charIndex]);
|
|
62
|
+
});
|
|
63
|
+
let increment = true;
|
|
64
|
+
for (let i = 0; i < nextCharIndices.length; i++) {
|
|
65
|
+
const val = ++nextCharIndices[i];
|
|
66
|
+
if (val >= chars.length) {
|
|
67
|
+
nextCharIndices[i] = 0;
|
|
68
|
+
} else {
|
|
69
|
+
increment = false;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (increment) {
|
|
74
|
+
nextCharIndices.push(0);
|
|
75
|
+
}
|
|
76
|
+
return r.join('');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let nextScope;
|
|
80
|
+
do {
|
|
81
|
+
nextScope = next();
|
|
82
|
+
} while (prevScopes.includes(nextScope));
|
|
83
|
+
return nextScope;
|
|
84
|
+
}
|
package/dist/Pool.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
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
|
-
}
|