-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
102 lines (96 loc) · 2.38 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import ndarray from "ndarray";
import ops from "ndarray-ops";
import { get_with_setter, set_with_setter } from "@zarrita/indexing";
import type {
GetOptions,
Indices,
Projection,
SetOptions,
Slice,
} from "@zarrita/indexing";
import type * as core from "@zarrita/core";
import type { Mutable, Readable } from "@zarrita/storage";
export const setter = {
prepare: ndarray,
set_scalar<D extends core.DataType>(
dest: ndarray.NdArray<core.TypedArray<D>>,
selection: (number | Indices)[],
value: core.Scalar<D>,
) {
(ops.assigns as any)(view(dest, selection), value);
},
set_from_chunk<D extends core.DataType>(
dest: ndarray.NdArray<core.TypedArray<D>>,
src: ndarray.NdArray<core.TypedArray<D>>,
mapping: Projection[],
) {
const s = unzip_selections(mapping);
ops.assign(view(dest, s.to), view(src, s.from));
},
};
/** @category Utility */
export async function get<
D extends core.DataType,
Store extends Readable,
Sel extends (null | Slice | number)[],
>(
arr: core.Array<D, Store>,
selection: Sel | null = null,
opts: GetOptions<Parameters<Store["get"]>[1]> = {},
) {
return get_with_setter<D, Store, ndarray.NdArray<core.TypedArray<D>>, Sel>(
arr,
selection,
opts,
setter,
);
}
/** @category Utility */
export async function set<D extends core.DataType>(
arr: core.Array<D, Mutable>,
selection: (null | Slice | number)[] | null,
value: core.Scalar<D> | ndarray.NdArray<core.TypedArray<D>>,
opts: SetOptions = {},
) {
return set_with_setter<D, ndarray.NdArray<core.TypedArray<D>>>(
arr,
selection,
value,
opts,
setter,
);
}
function unzip_selections(
mapping: Projection[],
): { to: (number | Indices)[]; from: (number | Indices)[] } {
const to = [], from = [];
for (const m of mapping) {
if (m.to !== null) to.push(m.to);
if (m.from !== null) from.push(m.from);
}
return { to, from };
}
/** Convert zarrita selection to ndarray view. */
function view<D extends core.DataType>(
arr: ndarray.NdArray<core.TypedArray<D>>,
sel: (number | Indices)[],
) {
const lo: number[] = [];
const hi: number[] = [];
const step: number[] = [];
const pick: (number | null)[] = [];
sel.forEach((s, i) => {
if (typeof s === "number") {
lo.push(0);
hi.push(arr.shape[i]);
step.push(1);
pick.push(s);
return;
}
lo.push(s[0]);
hi.push(s[1]);
step.push(s[2]);
pick.push(null);
});
return arr.hi(...hi).lo(...lo).step(...step).pick(...pick);
}