Skip to content

Commit eb9dea5

Browse files
committed
refactor(ts): rename DaskArray to LazyArray
1 parent 3cfa3d0 commit eb9dea5

File tree

10 files changed

+130
-78
lines changed

10 files changed

+130
-78
lines changed

ts/src/io/zarr_reader.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as zarr from "zarrita";
22
import { Multiscales } from "../types/multiscales.ts";
33
import { NgffImage } from "../types/ngff_image.ts";
44
import type { Metadata } from "../types/zarr_metadata.ts";
5-
import { DaskArray } from "../types/dask_array.ts";
5+
import { LazyArray } from "../types/lazy_array.ts";
66
import { MetadataSchema } from "../schemas/zarr_metadata.ts";
77

88
export interface ZarrReaderOptions {
@@ -18,37 +18,38 @@ export class ZarrReader {
1818

1919
async fromNgffZarr(
2020
storePath: string,
21-
options: ZarrReaderOptions = {},
21+
options: ZarrReaderOptions = {}
2222
): Promise<Multiscales> {
2323
const validate = options.validate ?? this.validate;
2424

2525
try {
2626
const store = new zarr.FetchStore(storePath);
2727
const root = zarr.root(store);
28-
28+
2929
// Try to use consolidated metadata for better performance
3030
let consolidatedRoot;
3131
try {
3232
consolidatedRoot = await zarr.tryWithConsolidated(store);
3333
} catch {
3434
consolidatedRoot = store;
3535
}
36-
37-
const group = await zarr.open(zarr.root(consolidatedRoot), { kind: "group" });
36+
37+
const group = await zarr.open(zarr.root(consolidatedRoot), {
38+
kind: "group",
39+
});
3840
const attrs = group.attrs as unknown;
3941

4042
if (!attrs || !(attrs as Record<string, unknown>).multiscales) {
4143
throw new Error("No multiscales metadata found in Zarr store");
4244
}
4345

44-
const multiscalesMetadata = (attrs as Record<string, unknown>).multiscales?.[0] as unknown;
46+
const multiscalesMetadata = (attrs as Record<string, unknown>)
47+
.multiscales?.[0] as unknown;
4548

4649
if (validate) {
4750
const result = MetadataSchema.safeParse(multiscalesMetadata);
4851
if (!result.success) {
49-
throw new Error(
50-
`Invalid OME-Zarr metadata: ${result.error.message}`,
51-
);
52+
throw new Error(`Invalid OME-Zarr metadata: ${result.error.message}`);
5253
}
5354
}
5455

@@ -61,7 +62,7 @@ export class ZarrReader {
6162
kind: "array",
6263
});
6364

64-
const daskArray = new DaskArray({
65+
const lazyArray = new LazyArray({
6566
shape: zarrArray.shape,
6667
dtype: zarrArray.dtype,
6768
chunks: [zarrArray.chunks],
@@ -96,7 +97,7 @@ export class ZarrReader {
9697
}, {} as Record<string, string>);
9798

9899
const ngffImage = new NgffImage({
99-
data: daskArray,
100+
data: lazyArray,
100101
dims,
101102
scale,
102103
translation,
@@ -119,15 +120,15 @@ export class ZarrReader {
119120
throw new Error(
120121
`Failed to read OME-Zarr: ${
121122
error instanceof Error ? error.message : String(error)
122-
}`,
123+
}`
123124
);
124125
}
125126
}
126127

127128
async readArrayData(
128129
storePath: string,
129130
arrayPath: string,
130-
selection?: unknown[],
131+
selection?: unknown[]
131132
): Promise<unknown> {
132133
try {
133134
const store = new zarr.FetchStore(storePath);
@@ -145,7 +146,7 @@ export class ZarrReader {
145146
throw new Error(
146147
`Failed to read array data: ${
147148
error instanceof Error ? error.message : String(error)
148-
}`,
149+
}`
149150
);
150151
}
151152
}

ts/src/mod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
export * from "./types/units.ts";
22
export * from "./types/methods.ts";
3-
export * from "./types/dask_array.ts";
3+
export * from "./types/lazy_array.ts";
44
export * from "./types/zarr_metadata.ts";
55
export * from "./types/ngff_image.ts";
66
export * from "./types/multiscales.ts";
77

88
export * from "./schemas/units.ts";
99
export * from "./schemas/methods.ts";
10-
export * from "./schemas/dask_array.ts";
10+
export * from "./schemas/lazy_array.ts";
1111
export * from "./schemas/zarr_metadata.ts";
1212
export * from "./schemas/ngff_image.ts";
1313
export * from "./schemas/multiscales.ts";

ts/src/schemas/dask_array.ts renamed to ts/src/schemas/lazy_array.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { z } from "zod";
22

3-
export const DaskArrayMetadataSchema = z.object({
3+
export const LazyArrayMetadataSchema = z.object({
44
shape: z.array(z.number().int().nonnegative()),
55
dtype: z.string(),
66
chunks: z.array(z.array(z.number().int().nonnegative())),

ts/src/schemas/ngff_image.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { z } from "zod";
22
import { UnitsSchema } from "./units.ts";
3-
import { DaskArrayMetadataSchema } from "./dask_array.ts";
3+
import { LazyArrayMetadataSchema } from "./lazy_array.ts";
44

55
export const NgffImageOptionsSchema = z.object({
6-
data: DaskArrayMetadataSchema,
6+
data: LazyArrayMetadataSchema,
77
dims: z.array(z.string()),
88
scale: z.record(z.string(), z.number()),
99
translation: z.record(z.string(), z.number()),

ts/src/types/dask_array.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
export interface DaskArrayMetadata {
1+
export interface LazyArrayMetadata {
22
shape: number[];
33
dtype: string;
44
chunks: number[][];
55
name: string;
66
}
77

8-
export class DaskArray {
8+
export class LazyArray {
99
public readonly shape: number[];
1010
public readonly dtype: string;
1111
public readonly chunks: number[][];
1212
public readonly name: string;
1313

14-
constructor(metadata: DaskArrayMetadata) {
14+
constructor(metadata: LazyArrayMetadata) {
1515
this.shape = [...metadata.shape];
1616
this.dtype = metadata.dtype;
1717
this.chunks = metadata.chunks.map((chunk) => [...chunk]);
@@ -31,11 +31,12 @@ export class DaskArray {
3131
}
3232

3333
toString(): string {
34-
const chunkStr = this.chunksize.length > 0
35-
? `chunksize=(${this.chunksize.join(", ")})`
36-
: "chunksize=()";
37-
return `dask.array<${this.name}, shape=(${
38-
this.shape.join(", ")
39-
}), dtype=${this.dtype}, ${chunkStr}, chunktype=TypedArray>`;
34+
const chunkStr =
35+
this.chunksize.length > 0
36+
? `chunksize=(${this.chunksize.join(", ")})`
37+
: "chunksize=()";
38+
return `dask.array<${this.name}, shape=(${this.shape.join(", ")}), dtype=${
39+
this.dtype
40+
}, ${chunkStr}, chunktype=TypedArray>`;
4041
}
4142
}

ts/src/types/lazy_array.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export interface LazyArrayMetadata {
2+
shape: number[];
3+
dtype: string;
4+
chunks: number[][];
5+
name: string;
6+
}
7+
8+
export class LazyArray {
9+
public readonly shape: number[];
10+
public readonly dtype: string;
11+
public readonly chunks: number[][];
12+
public readonly name: string;
13+
14+
constructor(metadata: LazyArrayMetadata) {
15+
this.shape = [...metadata.shape];
16+
this.dtype = metadata.dtype;
17+
this.chunks = metadata.chunks.map((chunk) => [...chunk]);
18+
this.name = metadata.name;
19+
}
20+
21+
get ndim(): number {
22+
return this.shape.length;
23+
}
24+
25+
get size(): number {
26+
return this.shape.reduce((acc, dim) => acc * dim, 1);
27+
}
28+
29+
get chunksize(): number[] {
30+
return this.chunks[0] || [];
31+
}
32+
33+
toString(): string {
34+
const chunkStr = this.chunksize.length > 0
35+
? `chunksize=(${this.chunksize.join(", ")})`
36+
: "chunksize=()";
37+
return `dask.array<${this.name}, shape=(${
38+
this.shape.join(", ")
39+
}), dtype=${this.dtype}, ${chunkStr}, chunktype=TypedArray>`;
40+
}
41+
}

ts/src/types/ngff_image.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { DaskArray } from "./dask_array.ts";
1+
import type { LazyArray } from "./lazy_array.ts";
22
import type { Units } from "./units.ts";
33

44
export type ComputedCallback = () => void;
55

66
export interface NgffImageOptions {
7-
data: DaskArray;
7+
data: LazyArray;
88
dims: string[];
99
scale: Record<string, number>;
1010
translation: Record<string, number>;
@@ -14,7 +14,7 @@ export interface NgffImageOptions {
1414
}
1515

1616
export class NgffImage {
17-
public readonly data: DaskArray;
17+
public readonly data: LazyArray;
1818
public readonly dims: string[];
1919
public readonly scale: Record<string, number>;
2020
public readonly translation: Record<string, number>;

ts/src/utils/factory.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DaskArray } from "../types/dask_array.ts";
1+
import { LazyArray } from "../types/lazy_array.ts";
22
import { NgffImage } from "../types/ngff_image.ts";
33
import { Multiscales } from "../types/multiscales.ts";
44
import type {
@@ -18,17 +18,17 @@ export function createNgffImage(
1818
dims: string[],
1919
scale: Record<string, number>,
2020
translation: Record<string, number>,
21-
name = "image",
21+
name = "image"
2222
): NgffImage {
23-
const daskArray = new DaskArray({
23+
const lazyArray = new LazyArray({
2424
shape,
2525
dtype,
2626
chunks: [shape],
2727
name,
2828
});
2929

3030
return new NgffImage({
31-
data: daskArray,
31+
data: lazyArray,
3232
dims,
3333
scale,
3434
translation,
@@ -41,7 +41,7 @@ export function createNgffImage(
4141
export function createAxis(
4242
name: SupportedDims,
4343
type: AxesType,
44-
unit?: Units,
44+
unit?: Units
4545
): Axis {
4646
return {
4747
name,
@@ -67,7 +67,7 @@ export function createTranslation(translation: number[]): Translation {
6767
export function createDataset(
6868
path: string,
6969
scale: number[],
70-
translation: number[],
70+
translation: number[]
7171
): Dataset {
7272
return {
7373
path,
@@ -82,7 +82,7 @@ export function createMetadata(
8282
axes: Axis[],
8383
datasets: Dataset[],
8484
name = "image",
85-
version = "0.4",
85+
version = "0.4"
8686
): Metadata {
8787
return {
8888
axes: [...axes],
@@ -98,7 +98,7 @@ export function createMultiscales(
9898
images: NgffImage[],
9999
metadata: Metadata,
100100
scaleFactors?: (Record<string, number> | number)[],
101-
method?: Methods,
101+
method?: Methods
102102
): Multiscales {
103103
return new Multiscales({
104104
images: [...images],

ts/test/schemas_test.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
TranslationSchema,
88
} from "../src/schemas/zarr_metadata.ts";
99
import { MethodsSchema } from "../src/schemas/methods.ts";
10-
import { DaskArrayMetadataSchema } from "../src/schemas/dask_array.ts";
10+
import { LazyArrayMetadataSchema } from "../src/schemas/lazy_array.ts";
1111

1212
Deno.test("AxisSchema validation", () => {
1313
const validAxis = {
@@ -90,13 +90,15 @@ Deno.test("MetadataSchema validation", () => {
9090
{ name: "y", type: "space" },
9191
{ name: "x", type: "space" },
9292
],
93-
datasets: [{
94-
path: "0",
95-
coordinateTransformations: [
96-
{ type: "scale", scale: [1.0, 1.0] },
97-
{ type: "translation", translation: [0.0, 0.0] },
98-
],
99-
}],
93+
datasets: [
94+
{
95+
path: "0",
96+
coordinateTransformations: [
97+
{ type: "scale", scale: [1.0, 1.0] },
98+
{ type: "translation", translation: [0.0, 0.0] },
99+
],
100+
},
101+
],
100102
name: "test_image",
101103
version: "0.4",
102104
};
@@ -116,27 +118,32 @@ Deno.test("MethodsSchema validation", () => {
116118
assertThrows(() => MethodsSchema.parse("invalid_method"));
117119
});
118120

119-
Deno.test("DaskArrayMetadataSchema validation", () => {
121+
Deno.test("LazyArrayMetadataSchema validation", () => {
120122
const validMetadata = {
121123
shape: [256, 256],
122124
dtype: "uint8",
123-
chunks: [[64, 64], [64, 64], [64, 64], [64, 64]],
125+
chunks: [
126+
[64, 64],
127+
[64, 64],
128+
[64, 64],
129+
[64, 64],
130+
],
124131
name: "test_array",
125132
};
126133

127-
const result = DaskArrayMetadataSchema.parse(validMetadata);
134+
const result = LazyArrayMetadataSchema.parse(validMetadata);
128135
assertEquals(result.shape, [256, 256]);
129136
assertEquals(result.dtype, "uint8");
130137
assertEquals(result.name, "test_array");
131138
});
132139

133-
Deno.test("DaskArrayMetadataSchema validation - negative shape", () => {
140+
Deno.test("LazyArrayMetadataSchema validation - negative shape", () => {
134141
const invalidMetadata = {
135142
shape: [-256, 256],
136143
dtype: "uint8",
137144
chunks: [[64, 64]],
138145
name: "test_array",
139146
};
140147

141-
assertThrows(() => DaskArrayMetadataSchema.parse(invalidMetadata));
148+
assertThrows(() => LazyArrayMetadataSchema.parse(invalidMetadata));
142149
});

0 commit comments

Comments
 (0)