Skip to content

Commit 10a5d8e

Browse files
committed
Export internal functions for @zarrita/ndarray
1 parent 14145a2 commit 10a5d8e

File tree

9 files changed

+263
-161
lines changed

9 files changed

+263
-161
lines changed

demo.html

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<script type="importmap">
77
{
88
"imports": {
9-
"zarrita": "./packages/core/dist/src/index.js",
9+
"zarrita": "./packages/zarrita/dist/index.js",
10+
"@zarrita/core": "./packages/core/dist/src/index.js",
1011
"@zarrita/storage/fetch": "./packages/storage/dist/src/fetch.js",
1112
"numcodecs/blosc": "./packages/core/node_modules/numcodecs/blosc.js",
1213
"numcodecs/lz4": "./packages/core/node_modules/numcodecs/lz4.js",
@@ -19,9 +20,7 @@
1920
</head>
2021
<script type="module">
2122
import * as zarr from "zarrita";
22-
import FetchStore from "@zarrita/storage/fetch";
23-
24-
let store = zarr.root(new FetchStore(new URL("fixtures", import.meta.url)));
23+
let store = zarr.root(new zarr.FetchStore(new URL("fixtures", import.meta.url)));
2524

2625
{
2726
let grp = await zarr.open.v2(store.resolve("v2/data.zarr"), {
+113-120
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { describe, expect, it } from "vitest";
22

33
import * as zarr from "../../src/index.js";
4+
import { get, set, slice } from "../../src/index.js";
45
import { IndexError } from "../../src/indexing/indexer.js";
5-
import * as ops from "../../src/indexing/ops.js";
6-
import { slice } from "../../src/indexing/util.js";
76

87
const DATA = {
98
// biome-ignore format: the array should not be formatted
@@ -12,126 +11,120 @@ const DATA = {
1211
stride: [12, 4, 1],
1312
};
1413

15-
export function run_suite(name: string, getter: unknown) {
16-
let get = getter as typeof ops.get;
17-
18-
describe(name, async () => {
19-
let arr = await zarr.create(new Map(), {
20-
shape: [2, 3, 4],
21-
data_type: "int32",
22-
chunk_shape: [1, 2, 2],
23-
});
24-
await ops.set(arr, null, DATA);
25-
26-
it.each([
27-
undefined,
28-
null,
29-
[null, null, null],
30-
[slice(null), slice(null), slice(null)],
31-
[null, slice(null), slice(null)],
32-
])("Reads entire array: selection = %j", async (sel) => {
33-
let { data, shape, stride } = await get(arr, sel);
34-
expect({ data, shape, stride }).toStrictEqual(DATA);
35-
});
14+
describe("slice", async () => {
15+
let arr = await zarr.create(new Map(), {
16+
shape: [2, 3, 4],
17+
data_type: "int32",
18+
chunk_shape: [1, 2, 2],
19+
});
20+
await set(arr, null, DATA);
3621

37-
it.each([
38-
[
39-
[1, slice(1, 3), null],
40-
{
41-
data: new Int32Array([16, 17, 18, 19, 20, 21, 22, 23]),
42-
shape: [2, 4],
43-
stride: [4, 1],
44-
},
45-
],
46-
[
47-
[null, slice(1, 3), slice(2)],
48-
{
49-
data: new Int32Array([4, 5, 8, 9, 16, 17, 20, 21]),
50-
shape: [2, 2, 2],
51-
stride: [4, 2, 1],
52-
},
53-
],
54-
[
55-
[null, null, 0],
56-
{
57-
data: new Int32Array([0, 4, 8, 12, 16, 20]),
58-
shape: [2, 3],
59-
stride: [3, 1],
60-
},
61-
],
62-
[
63-
[slice(3), slice(2), slice(2)],
64-
{
65-
data: new Int32Array([0, 1, 4, 5, 12, 13, 16, 17]),
66-
shape: [2, 2, 2],
67-
stride: [4, 2, 1],
68-
},
69-
],
70-
[
71-
[1, null, null],
72-
{
73-
// biome-ignore format: the array should not be formatted
74-
data: new Int32Array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]),
75-
shape: [3, 4],
76-
stride: [4, 1],
77-
},
78-
],
79-
[
80-
[0, slice(null, null, 2), slice(null, null, 3)],
81-
{
82-
data: new Int32Array([0, 3, 8, 11]),
83-
shape: [2, 2],
84-
stride: [2, 1],
85-
},
86-
],
87-
[
88-
[null, null, slice(null, null, 4)],
89-
{
90-
data: new Int32Array([0, 4, 8, 12, 16, 20]),
91-
shape: [2, 3, 1],
92-
stride: [3, 1, 1],
93-
},
94-
],
95-
[
96-
[1, null, slice(null, 3, 2)],
97-
{
98-
data: new Int32Array([12, 14, 16, 18, 20, 22]),
99-
shape: [3, 2],
100-
stride: [2, 1],
101-
},
102-
],
103-
[
104-
[null, 1, null],
105-
{
106-
data: new Int32Array([4, 5, 6, 7, 16, 17, 18, 19]),
107-
shape: [2, 4],
108-
stride: [4, 1],
109-
},
110-
],
111-
[
112-
[1, 2, null],
113-
{
114-
data: new Int32Array([20, 21, 22, 23]),
115-
shape: [4],
116-
stride: [1],
117-
},
118-
],
119-
])("Reads fancy slices: selection - %j", async (sel, expected) => {
120-
let { data, shape, stride } = await get(arr, sel);
121-
expect({ data, shape, stride }).toStrictEqual(expected);
122-
});
22+
it.each([
23+
undefined,
24+
null,
25+
[null, null, null],
26+
[slice(null), slice(null), slice(null)],
27+
[null, slice(null), slice(null)],
28+
])("Reads entire array: selection = %j", async (sel) => {
29+
let chunk = await get(arr, sel);
30+
expect(chunk).toStrictEqual(DATA);
31+
});
12332

124-
it("Reads a scalar", async () => {
125-
let sel = [1, 1, 1];
126-
let val = await get(arr, sel);
127-
expect(val).toBe(17);
128-
});
33+
it.each([
34+
[
35+
[1, slice(1, 3), null],
36+
{
37+
data: new Int32Array([16, 17, 18, 19, 20, 21, 22, 23]),
38+
shape: [2, 4],
39+
stride: [4, 1],
40+
},
41+
],
42+
[
43+
[null, slice(1, 3), slice(2)],
44+
{
45+
data: new Int32Array([4, 5, 8, 9, 16, 17, 20, 21]),
46+
shape: [2, 2, 2],
47+
stride: [4, 2, 1],
48+
},
49+
],
50+
[
51+
[null, null, 0],
52+
{
53+
data: new Int32Array([0, 4, 8, 12, 16, 20]),
54+
shape: [2, 3],
55+
stride: [3, 1],
56+
},
57+
],
58+
[
59+
[slice(3), slice(2), slice(2)],
60+
{
61+
data: new Int32Array([0, 1, 4, 5, 12, 13, 16, 17]),
62+
shape: [2, 2, 2],
63+
stride: [4, 2, 1],
64+
},
65+
],
66+
[
67+
[1, null, null],
68+
{
69+
// biome-ignore format: the array should not be formatted
70+
data: new Int32Array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]),
71+
shape: [3, 4],
72+
stride: [4, 1],
73+
},
74+
],
75+
[
76+
[0, slice(null, null, 2), slice(null, null, 3)],
77+
{
78+
data: new Int32Array([0, 3, 8, 11]),
79+
shape: [2, 2],
80+
stride: [2, 1],
81+
},
82+
],
83+
[
84+
[null, null, slice(null, null, 4)],
85+
{
86+
data: new Int32Array([0, 4, 8, 12, 16, 20]),
87+
shape: [2, 3, 1],
88+
stride: [3, 1, 1],
89+
},
90+
],
91+
[
92+
[1, null, slice(null, 3, 2)],
93+
{
94+
data: new Int32Array([12, 14, 16, 18, 20, 22]),
95+
shape: [3, 2],
96+
stride: [2, 1],
97+
},
98+
],
99+
[
100+
[null, 1, null],
101+
{
102+
data: new Int32Array([4, 5, 6, 7, 16, 17, 18, 19]),
103+
shape: [2, 4],
104+
stride: [4, 1],
105+
},
106+
],
107+
[
108+
[1, 2, null],
109+
{
110+
data: new Int32Array([20, 21, 22, 23]),
111+
shape: [4],
112+
stride: [1],
113+
},
114+
],
115+
])("Reads fancy slices: selection - %j", async (sel, expected) => {
116+
let { data, shape, stride } = await get(arr, sel);
117+
expect({ data, shape, stride }).toStrictEqual(expected);
118+
});
129119

130-
it("Does not support negative indices", async () => {
131-
let sel = [0, slice(null, null, -2), slice(null, null, 3)];
132-
await expect(get(arr, sel)).rejects.toThrowError(IndexError);
133-
});
120+
it("Reads a scalar", async () => {
121+
let sel = [1, 1, 1];
122+
let val = await get(arr, sel);
123+
expect(val).toBe(17);
134124
});
135-
}
136125

137-
run_suite("builtins", ops.get);
126+
it("Does not support negative indices", async () => {
127+
let sel = [0, slice(null, null, -2), slice(null, null, 3)];
128+
await expect(get(arr, sel)).rejects.toThrowError(IndexError);
129+
});
130+
});

packages/core/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export { create } from "./create.js";
1010
export { registry } from "./codecs.js";
1111
export { get, set } from "./indexing/ops.js";
1212
export { slice } from "./indexing/util.js";
13+
export { IndexError } from "./indexing/indexer.js";
1314
export { withConsolidated, tryWithConsolidated } from "./consolidated.js";
1415

1516
export type { Listable } from "./consolidated.js";
@@ -25,3 +26,5 @@ export type {
2526
// internal exports for @zarrita/ndarray
2627
export { get as _zarrita_internal_get } from "./indexing/get.js";
2728
export { set as _zarrita_internal_set } from "./indexing/set.js";
29+
export { get_strides as _zarrita_internal_get_strides } from "./util.js";
30+
export { slice_indices as _zarrita_internal_slice_indices } from "./indexing/util.js";

packages/ndarray/__tests__/index.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import * as path from "node:path";
22
import * as url from "node:url";
33
import { describe, expect, it } from "vitest";
44

5-
import { get } from "../src/index.js";
6-
75
import { FileSystemStore } from "@zarrita/storage";
86
import * as zarr from "zarrita";
97

8+
import { get } from "../src/index.js";
9+
1010
let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
1111

1212
let root = path.resolve(__dirname, "../../../fixtures/v2/data.zarr");

packages/ndarray/__tests__/setter.test.ts

+6-28
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,15 @@
11
import ndarray, { type TypedArray } from "ndarray";
22
import { assign } from "ndarray-ops";
33
import { describe, expect, it } from "vitest";
4+
import {
5+
type Projection,
6+
_zarrita_internal_get_strides as get_strides,
7+
slice,
8+
_zarrita_internal_slice_indices as slice_indices,
9+
} from "zarrita";
410

5-
import { type Projection, slice } from "zarrita";
6-
7-
import { slice_indices } from "../../core/src/indexing/util.js";
811
import { _internal_setter } from "../src/index.js";
912

10-
/** Compute strides for 'C' or 'F' ordered array from shape */
11-
function get_strides(shape: readonly number[], order: "C" | "F") {
12-
return (order === "C" ? row_major_stride : col_major_stride)(shape);
13-
}
14-
15-
function row_major_stride(shape: readonly number[]) {
16-
const ndim = shape.length;
17-
const stride: number[] = globalThis.Array(ndim);
18-
for (let i = ndim - 1, step = 1; i >= 0; i--) {
19-
stride[i] = step;
20-
step *= shape[i];
21-
}
22-
return stride;
23-
}
24-
25-
function col_major_stride(shape: readonly number[]) {
26-
const ndim = shape.length;
27-
const stride: number[] = globalThis.Array(ndim);
28-
for (let i = 0, step = 1; i < ndim; i++) {
29-
stride[i] = step;
30-
step *= shape[i];
31-
}
32-
return stride;
33-
}
34-
3513
function to_c<T extends TypedArray>({
3614
data,
3715
shape,

0 commit comments

Comments
 (0)