Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
parseUrlSuffix,
splitPipelineUrl,
} from "#src/kvstore/url.js";
import type { LayerTypeName } from "#src/layer/index.js";
import type { MeshSource, MultiscaleMeshSource } from "#src/mesh/frontend.js";
import type { SegmentPropertyMap } from "#src/segmentation_display_state/property_map.js";
import type { SegmentationGraphSource } from "#src/segmentation_graph/source.js";
Expand Down Expand Up @@ -189,13 +190,21 @@ export interface SingleChannelMetadata {
coefficient?: number;
}

// Another data source that belongs with this one, unlike a subsource, which is part of it.
export interface RelatedDataSource {
readonly name: string;
readonly url: string;
readonly suggestedLayerType: LayerTypeName;
}

export interface DataSource {
subsources: DataSubsourceEntry[];
modelTransform: CoordinateSpaceTransform;
canChangeModelSpaceRank?: boolean;
state?: Trackable;
canonicalUrl?: string;
channelMetadata?: ChannelMetadata;
relatedSources?: RelatedDataSource[];
}

export interface DataSourceRedirect {
Expand Down
49 changes: 42 additions & 7 deletions src/datasource/zarr/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
DataSource,
GetKvStoreBasedDataSourceOptions,
KvStoreBasedDataSourceProvider,
RelatedDataSource,
} from "#src/datasource/index.js";
import { getKvStorePathCompletions } from "#src/datasource/kvstore_completions.js";
import { VolumeChunkSourceParameters } from "#src/datasource/zarr/base.js";
Expand All @@ -50,7 +51,10 @@ import {
parseV3Metadata,
} from "#src/datasource/zarr/metadata/parse.js";
import type { OmeMultiscaleMetadata } from "#src/datasource/zarr/ome.js";
import { parseOmeMetadata } from "#src/datasource/zarr/ome.js";
import {
parseOmeLabelsGroup,
parseOmeMetadata,
} from "#src/datasource/zarr/ome.js";
import type { AutoDetectRegistry } from "#src/kvstore/auto_detect.js";
import { simpleFilePresenceAutoDetectDirectorySpec } from "#src/kvstore/auto_detect.js";
import { WithSharedKvStoreContext } from "#src/kvstore/chunk_source_frontend.js";
Expand Down Expand Up @@ -474,6 +478,33 @@ function resolveUrl(options: GetKvStoreBasedDataSourceOptions) {
};
}

async function getOmeLabelSources(
sharedKvStoreContext: SharedKvStoreContext,
url: string,
options: { zarrVersion: 2 | 3 } & Partial<ProgressOptions>,
): Promise<RelatedDataSource[] | undefined> {
const labelsGroupUrl = kvstoreEnsureDirectoryPipelineUrl(
joinBaseUrlAndPath(url, "labels"),
);
try {
const metadata = await getMetadata(sharedKvStoreContext, labelsGroupUrl, {
...options,
expectedNodeType: "group",
});
if (metadata === undefined) return undefined;
return parseOmeLabelsGroup(
labelsGroupUrl,
metadata.userAttributes,
options.zarrVersion,
);
} catch (error) {
if (error instanceof Error && error.name === "AbortError") throw error;
// A bad labels group must not stop the image from opening.
console.warn(`Ignoring OME labels group ${labelsGroupUrl}:`, error);
return undefined;
}
}

export class ZarrDataSource implements KvStoreBasedDataSourceProvider {
constructor(public zarrVersion: 2 | 3 | undefined = undefined) {}
get scheme() {
Expand Down Expand Up @@ -515,6 +546,7 @@ export class ZarrDataSource implements KvStoreBasedDataSourceProvider {
explicitDimensionSeparator: dimensionSeparator,
});
let channelMetadata: ChannelMetadata | undefined;
let relatedSources: RelatedDataSource[] | undefined;
if (metadata === undefined) {
throw new Error("No zarr metadata found");
}
Expand All @@ -530,15 +562,17 @@ export class ZarrDataSource implements KvStoreBasedDataSourceProvider {
throw new Error("Neither array nor OME multiscale metadata found");
}
channelMetadata = omeMetadata.channels;
multiscaleInfo = await resolveOmeMultiscale(
sharedKvStoreContext,
omeMetadata.multiscale,
{
[multiscaleInfo, relatedSources] = await Promise.all([
resolveOmeMultiscale(sharedKvStoreContext, omeMetadata.multiscale, {
...progressOptions,
zarrVersion: metadata.zarrVersion,
explicitDimensionSeparator: dimensionSeparator,
},
);
}),
getOmeLabelSources(sharedKvStoreContext, kvStoreUrl, {
...progressOptions,
zarrVersion: metadata.zarrVersion,
}),
]);
} else {
multiscaleInfo = getMultiscaleInfoForSingleArray(
kvStoreUrl,
Expand All @@ -560,6 +594,7 @@ export class ZarrDataSource implements KvStoreBasedDataSourceProvider {
canonicalUrl: `${kvStoreUrl}|zarr${metadata.zarrVersion}:`,
modelTransform,
channelMetadata,
relatedSources,
subsources: [
{
id: "default",
Expand Down
40 changes: 39 additions & 1 deletion src/datasource/zarr/ome.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/

import { describe, it, expect } from "vitest";
import { parseOmeMetadata } from "#src/datasource/zarr/ome.js";
import {
parseOmeLabelsGroup,
parseOmeMetadata,
} from "#src/datasource/zarr/ome.js";
import { createIdentity } from "#src/util/matrix.js";

const intrinsicCoordinateSystem = {
Expand Down Expand Up @@ -1417,3 +1420,38 @@ it("should handle anisotropic scales with rotations for 0.6 (issue #952)", () =>
// Column 4 - [0, 0, 0, 1]
expect(baseTransform.slice(12)).toEqual(new Float64Array([0, 0, 0, 1]));
});

describe("OME-Zarr labels group", () => {
it("lists the label images of a 0.5 labels group as segmentation sources", () => {
const attrs = {
ome: { version: "0.5", labels: ["cells", "nuclei/v2"] },
};
expect(
parseOmeLabelsGroup("http://host/image.zarr/labels/", attrs, 3),
).toEqual([
{
name: "cells",
url: "http://host/image.zarr/labels/cells/|zarr3:",
suggestedLayerType: "segmentation",
},
{
name: "nuclei/v2",
url: "http://host/image.zarr/labels/nuclei/v2/|zarr3:",
suggestedLayerType: "segmentation",
},
]);
});

it("lists the label images of a 0.4 labels group as segmentation sources", () => {
const attrs = { labels: ["cells"] };
expect(
parseOmeLabelsGroup("http://host/image.zarr/labels/", attrs, 2),
).toEqual([
{
name: "cells",
url: "http://host/image.zarr/labels/cells/|zarr2:",
suggestedLayerType: "segmentation",
},
]);
});
});
22 changes: 22 additions & 0 deletions src/datasource/zarr/ome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { makeCoordinateSpace } from "#src/coordinate_transform.js";
import type {
SingleChannelMetadata,
ChannelMetadata,
RelatedDataSource,
} from "#src/datasource/index.js";
import {
joinBaseUrlAndPath,
Expand All @@ -38,6 +39,7 @@ import {
verifyObject,
verifyObjectProperty,
verifyOptionalObjectProperty,
verifyStringArray,
verifyString,
} from "#src/util/json.js";
import { clampToInterval } from "#src/util/lerp.js";
Expand Down Expand Up @@ -830,6 +832,26 @@ function parseOmeMultiscale(
}
}

export function parseOmeLabelsGroup(
labelsGroupUrl: string,
attrs: unknown,
zarrVersion: 2 | 3,
): RelatedDataSource[] | undefined {
verifyObject(attrs);
const metadata =
verifyOptionalObjectProperty(attrs, "ome", verifyObject) ?? attrs;
const paths = verifyOptionalObjectProperty(
metadata,
"labels",
verifyStringArray,
);
return paths?.map((path) => ({
name: path,
url: `${kvstoreEnsureDirectoryPipelineUrl(joinBaseUrlAndPath(labelsGroupUrl, path))}|zarr${zarrVersion}:`,
suggestedLayerType: "segmentation",
}));
}

export function parseOmeMetadata(
url: string,
attrs: any,
Expand Down
10 changes: 8 additions & 2 deletions src/display_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { RefCounted } from "#src/util/disposable.js";
import { FramerateMonitor } from "#src/util/framerate.js";
import type { mat4 } from "#src/util/geom.js";
import { parseFixedLengthArray, verifyFloat01 } from "#src/util/json.js";
import { NullarySignal } from "#src/util/signal.js";
import { NullarySignal, Signal } from "#src/util/signal.js";
import {
TrackableScreenshotMode,
ScreenshotMode,
Expand Down Expand Up @@ -428,14 +428,20 @@ interface PanelMonitorState {
addedToResizeObserver?: boolean;
}

export interface MultiChannelSetup {
readonly name: string;
// Palette JSON of the tools for the layers that the setup created.
readonly toolsJson: readonly object[];
}

export class DisplayContext extends RefCounted implements FrameNumberCounter {
canvas = document.createElement("canvas");
gl: GL;
updateStarted = new NullarySignal();
updateFinished = new NullarySignal();
continuousCameraMotionStarted = new NullarySignal();
continuousCameraMotionFinished = new NullarySignal();
multiChannelSetupFinished = new NullarySignal();
multiChannelSetupFinished = new Signal<(setup: MultiChannelSetup) => void>();
changed = this.updateFinished;
panels = new Set<RenderedPanel>();
canvasRect: DOMRect | undefined;
Expand Down
7 changes: 7 additions & 0 deletions src/layer/annotation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,13 @@ for (const control of Object.values(LAYER_CONTROLS)) {
registerLayerControl(AnnotationUserLayer, control);
}

declare module "#src/layer/index.js" {
interface RegisteredLayerTypes {
annotation: AnnotationUserLayer;
pointAnnotation: AnnotationUserLayer;
}
}

registerLayerType(AnnotationUserLayer);
registerLayerType(AnnotationUserLayer, "pointAnnotation");

Expand Down
6 changes: 6 additions & 0 deletions src/layer/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,12 @@ class ShaderCodeOverlay extends Overlay {
}
}

declare module "#src/layer/index.js" {
interface RegisteredLayerTypes {
image: ImageUserLayer;
}
}

registerLayerType(ImageUserLayer);
registerVolumeLayerType(VolumeType.IMAGE, ImageUserLayer);
// Use ImageUserLayer as a fallback layer type if there is a `volume` subsource.
Expand Down
7 changes: 7 additions & 0 deletions src/layer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,13 @@ export class LayerSubsetSpecification extends LayerListSpecification {
export type UserLayerConstructor<LayerType extends UserLayer = UserLayer> =
typeof UserLayer & AnyConstructor<LayerType>;

export interface RegisteredLayerTypes {
new: NewUserLayer;
auto: AutoUserLayer;
}

export type LayerTypeName = keyof RegisteredLayerTypes;

export const layerTypes = new Map<string, UserLayerConstructor>();
const volumeLayerTypes = new Map<VolumeType, UserLayerConstructor>();
export interface LayerTypeGuess {
Expand Down
Loading
Loading