-
Notifications
You must be signed in to change notification settings - Fork 24
fix(ts): order generated axes time, then channel, then space #664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thewtex
merged 2 commits into
fideus-labs:main
from
vboussot:fix/ts-canonical-axis-order
Aug 21, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) Fideus Labs LLC | ||
| // SPDX-License-Identifier: MIT | ||
| /** | ||
| * Normalization of an image's axes to the OME-Zarr specification order. | ||
| * | ||
| * Mirrors `py/ngff_zarr/methods/_support.py`. | ||
| */ | ||
|
|
||
| import * as zarr from "zarrita"; | ||
|
|
||
| import { NgffImage } from "../types/ngff_image.ts"; | ||
| import type { ZarrCodec } from "./codecs.ts"; | ||
| import { defaultCodecs } from "./codecs.ts"; | ||
| import { | ||
| calculateStride, | ||
| componentTypeOf, | ||
| transposeArray, | ||
| } from "./transpose.ts"; | ||
| import { zarrGet, zarrSet } from "./worker_pool.ts"; | ||
|
|
||
| /** The OME-Zarr specification axis order: time, then channel, then space. */ | ||
| export const CANONICAL_AXIS_ORDER = ["t", "c", "z", "y", "x"]; | ||
|
|
||
| /** Every chunk origin of a `shape`/`chunks` grid, in row-major order. */ | ||
| function* chunkOrigins(shape: number[], chunks: number[]): Generator<number[]> { | ||
| const counts = shape.map((size, axis) => Math.ceil(size / chunks[axis])); | ||
| const total = counts.reduce((a, b) => a * b, 1); | ||
| for (let flat = 0; flat < total; flat++) { | ||
| const origin = new Array<number>(shape.length); | ||
| let rest = flat; | ||
| for (let axis = shape.length - 1; axis >= 0; axis--) { | ||
| origin[axis] = (rest % counts[axis]) * chunks[axis]; | ||
| rest = Math.floor(rest / counts[axis]); | ||
| } | ||
| yield origin; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return `image` with its dims in the spec axis order `(t, c, z, y, x)`. | ||
| * | ||
| * OME-Zarr requires axes ordered by type: time, then channel, then space. | ||
| * Conversion sources produce channel-last layouts, so the multiscale pipeline | ||
| * normalizes them here and the generated metadata is spec-ordered. | ||
| * | ||
| * An image whose dims fall outside the canonical set is returned unchanged: | ||
| * an axis model that was not expressible before RFC-3 carries no spec ordering | ||
| * to normalize to. | ||
| * | ||
| * Where the Python port transposes lazily through dask, this writes a reordered | ||
| * copy into a new in-memory zarr array. The copy proceeds one source chunk at | ||
| * a time, so peak working memory is a chunk rather than the whole image, but | ||
| * the whole image is read: a remote store is fetched in full even when no | ||
| * downsampling was requested. | ||
| * | ||
| * @param image - The image to normalize. | ||
| * @param codecs - Codec pipeline for the reordered array; defaults to | ||
| * {@link defaultCodecs}. | ||
| * @returns The image itself when already ordered, otherwise a reordered copy. | ||
| */ | ||
| export async function canonicalAxisOrder( | ||
| image: NgffImage, | ||
| codecs?: ZarrCodec[], | ||
| ): Promise<NgffImage> { | ||
| const dims = image.dims; | ||
| const newDims = CANONICAL_AXIS_ORDER.filter((dim) => dims.includes(dim)); | ||
| if ( | ||
| newDims.length !== dims.length || | ||
| newDims.every((dim, index) => dim === dims[index]) | ||
| ) { | ||
| return image; | ||
| } | ||
|
|
||
| const permutation = newDims.map((dim) => dims.indexOf(dim)); | ||
| const sourceShape = [...image.data.shape]; | ||
| const sourceChunks = [...image.data.chunks]; | ||
| const shape = permutation.map((index) => sourceShape[index]); | ||
| const chunkShape = permutation.map((index) => sourceChunks[index]); | ||
|
|
||
| const store: Map<string, Uint8Array> = new Map(); | ||
| const array = await zarr.create(zarr.root(store).resolve("/0"), { | ||
| shape, | ||
| chunk_shape: chunkShape, | ||
| data_type: image.data.dtype, | ||
| fill_value: 0, | ||
| codecs: codecs ?? defaultCodecs(image.data.dtype), | ||
| }); | ||
|
|
||
| // One source chunk at a time: the region read and the transposed buffer are | ||
| // both chunk-sized, so an image far larger than memory still converts. | ||
| for (const origin of chunkOrigins(sourceShape, sourceChunks)) { | ||
| const region = origin.map((start, axis) => ({ | ||
| start, | ||
| stop: Math.min(start + sourceChunks[axis], sourceShape[axis]), | ||
| })); | ||
| const block = await zarrGet( | ||
| image.data, | ||
| region.map(({ start, stop }) => zarr.slice(start, stop)), | ||
| ); | ||
| const blockShape = [...block.shape]; | ||
| const transposed = transposeArray( | ||
| block.data, | ||
| blockShape, | ||
| permutation, | ||
| componentTypeOf(block.data), | ||
| ); | ||
| const targetShape = permutation.map((index) => blockShape[index]); | ||
| await zarrSet( | ||
| array, | ||
| permutation.map((index) => | ||
| zarr.slice(region[index].start, region[index].stop) | ||
| ), | ||
| { | ||
| data: transposed, | ||
| shape: targetShape, | ||
| stride: calculateStride(targetShape), | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| return new NgffImage({ | ||
| data: array as zarr.Array<zarr.DataType, zarr.Readable>, | ||
| dims: newDims, | ||
| scale: image.scale, | ||
| translation: image.translation, | ||
| name: image.name, | ||
| axesUnits: image.axesUnits, | ||
| axesOrientations: image.axesOrientations, | ||
| axesTypes: image.axesTypes, | ||
| computedCallbacks: image.computedCallbacks, | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.