Skip to content
Open
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
6 changes: 3 additions & 3 deletions public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { colormaps as colorizercolormaps, features as colorizerfeatures } from "
import {
CreateLoaderOptions,
ImageInfo,
IVolumeLoader,
VolumeLoader,
LoadSpec,
Lut,
JsonImageInfoLoader,
Expand Down Expand Up @@ -1105,7 +1105,7 @@ function createTestVolume(dtype: NumberType): RawArrayLoaderOptions {
};
}

async function createLoader(data: TestDataSpec): Promise<IVolumeLoader[]> {
async function createLoader(data: TestDataSpec): Promise<VolumeLoader[]> {
if (data.type === "opencell") {
return [new OpenCellLoader()];
}
Expand Down Expand Up @@ -1145,7 +1145,7 @@ async function createLoader(data: TestDataSpec): Promise<IVolumeLoader[]> {
}
}

async function loadVolume(name: string, loadSpec: LoadSpec, loader: IVolumeLoader): Promise<void> {
async function loadVolume(name: string, loadSpec: LoadSpec, loader: VolumeLoader): Promise<void> {
const fullDims = await loader.loadDims(loadSpec);
console.log(fullDims);

Expand Down
4 changes: 2 additions & 2 deletions public/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Volume, Light } from "../src/index.js";
import { VolumeFileFormat } from "../src/loaders/index.js";
import { IVolumeLoader } from "../src/loaders/IVolumeLoader.js";
import { VolumeLoader } from "../src/loaders/VolumeLoader.js";
import type { NumberType } from "../src/types.js";

export interface TestDataSpec {
Expand All @@ -26,7 +26,7 @@ export interface State {
timerId: number;
scene: number;

loader: IVolumeLoader[];
loader: VolumeLoader[];

density: number;
maskAlpha: number;
Expand Down
2 changes: 1 addition & 1 deletion src/View3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from "./types.js";
import { IDrawableObject } from "./drawables/IDrawableObject.js";
import { Axis } from "./types.js";
import { PerChannelCallback } from "./loaders/IVolumeLoader.js";
import { PerChannelCallback } from "./loaders/VolumeLoader.js";
import { WorkerLoader } from "./workers/VolumeLoaderContext.js";
import Line3d from "./drawables/lines/Line3d.js";
import EventDispatcher from "./EventDispatcher.js";
Expand Down
18 changes: 12 additions & 6 deletions src/Volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import Channel from "./Channel.js";
import Histogram from "./Histogram.js";
import { Lut } from "./Lut.js";
import { getColorByChannelIndex } from "./constants/colors.js";
import type { IVolumeLoader, PerChannelCallback } from "./loaders/IVolumeLoader.js";
import { cloneLoadSpec, defaultLoadSpec, LoadSpec, regionToBox3 } from "./loaders/IVolumeLoader.js";
import {
cloneLoadSpec,
defaultLoadSpec,
LoadSpec,
regionToBox3,
type PerChannelCallback,
type VolumeLoader,
} from "./loaders/VolumeLoader.js";
import { pickLevelToLoadUnscaled } from "./loaders/VolumeLoaderUtils.js";
import type { NumberType, TypedArray } from "./types.js";
import { type ImageInfo, CImageInfo, defaultImageInfo } from "./ImageInfo.js";
Expand All @@ -19,7 +25,7 @@ interface VolumeDataObserver {
onVolumeLoadError: (vol: Volume, error: unknown) => void;
}

export type VolumeEvent = {
export type VolumeEvents = {
loadStart: void;
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this exposed as an external API at all?


Expand All @@ -28,10 +34,10 @@ export type VolumeEvent = {
* @class
* @param {ImageInfo} imageInfo
*/
export default class Volume extends EventDispatcher<VolumeEvent> {
export default class Volume extends EventDispatcher<VolumeEvents> {
public imageInfo: CImageInfo;
public loadSpec: Required<LoadSpec>;
public loader?: IVolumeLoader;
public loader?: VolumeLoader;
/** `LoadSpec` representing the minimum data required to display what's in the viewer (subregion, channels, etc.).
* Used to intelligently issue load requests whenever required by a state change. Modify with `updateRequiredData`.
*/
Expand Down Expand Up @@ -78,7 +84,7 @@ export default class Volume extends EventDispatcher<VolumeEvent> {
private volumeDataObservers: VolumeDataObserver[];
private loaded: boolean;

constructor(imageInfo: ImageInfo = defaultImageInfo(), loadSpec: LoadSpec = new LoadSpec(), loader?: IVolumeLoader) {
constructor(imageInfo: ImageInfo = defaultImageInfo(), loadSpec: LoadSpec = new LoadSpec(), loader?: VolumeLoader) {
super();
this.loaded = false;
this.imageInfo = new CImageInfo(imageInfo);
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Histogram from "./Histogram.js";
import { Lut, remapControlPoints } from "./Lut.js";
import { type AxisName, type ColorizeFeature, type NumberType, type TripleViewPanes, ViewportCorner } from "./types.js";
import { VolumeFileFormat, createVolumeLoader, PrefetchDirection } from "./loaders/index.js";
import { LoadSpec } from "./loaders/IVolumeLoader.js";
import { LoadSpec } from "./loaders/VolumeLoader.js";
import { OMEZarrLoader } from "./loaders/OmeZarrLoader.js";
import { JsonImageInfoLoader } from "./loaders/JsonImageInfoLoader.js";
import {
Expand All @@ -33,7 +33,8 @@ import type { VolumeDims } from "./VolumeDims.js";
export type { ImageInfo } from "./ImageInfo.js";
export type { ControlPoint } from "./Lut.js";
export type { CreateLoaderOptions } from "./loaders/index.js";
export type { IVolumeLoader, PerChannelCallback, ThreadableVolumeLoader } from "./loaders/IVolumeLoader.js";
// TODO export `IVolumeLoader`, `ThreadableVolumeLoader` as `VolumeLoader` aliases?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider exporting with a deprecated flag to explain the necessary migration?

export type { PerChannelCallback, VolumeLoader } from "./loaders/VolumeLoader.js";
export type { ZarrLoaderFetchOptions } from "./loaders/OmeZarrLoader.js";
export type { WorkerLoader } from "./workers/VolumeLoaderContext.js";
export {
Expand Down
9 changes: 2 additions & 7 deletions src/loaders/JsonImageInfoLoader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
ThreadableVolumeLoader,
type LoadSpec,
type RawChannelDataCallback,
type LoadedVolumeInfo,
} from "./IVolumeLoader.js";
import { VolumeLoader, type LoadSpec, type RawChannelDataCallback, type LoadedVolumeInfo } from "./VolumeLoader.js";
import { computeAtlasSize, type ImageInfo } from "../ImageInfo.js";
import type { VolumeDims } from "../VolumeDims.js";
import VolumeCache, { isChunk } from "../VolumeCache.js";
Expand Down Expand Up @@ -117,7 +112,7 @@ const convertImageInfo = (json: JsonImageInfo): ImageInfo => {
};
};

class JsonImageInfoLoader extends ThreadableVolumeLoader {
class JsonImageInfoLoader extends VolumeLoader {
urls: string[];
jsonInfo: (JsonImageInfo | undefined)[];
syncChannels = false;
Expand Down
22 changes: 17 additions & 5 deletions src/loaders/OmeZarrLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import type { VolumeDims } from "../VolumeDims.js";
import VolumeCache from "../VolumeCache.js";
import { getDataRange } from "../utils/num_utils.js";
import SubscribableRequestQueue from "../utils/SubscribableRequestQueue.js";
import type { RawChannelDataCallback, LoadedVolumeInfo, Region } from "./IVolumeLoader.js";
import { ThreadableVolumeLoader, LoadSpec, box3ToRegion } from "./IVolumeLoader.js";
import {
VolumeLoader,
LoadSpec,
type RawChannelDataCallback,
type LoadedVolumeInfo,
box3ToRegion,
Region,
} from "./VolumeLoader.js";
import {
composeSubregion,
computePackedAtlasDims,
Expand Down Expand Up @@ -90,7 +96,7 @@ const DEFAULT_FETCH_OPTIONS = {
maxPrefetchChunks: 30,
};

class OMEZarrLoader extends ThreadableVolumeLoader {
class OMEZarrLoader extends VolumeLoader {
/** The ID of the subscriber responsible for "actual loads" (non-prefetch requests) */
private loadSubscriber: SubscriberId | undefined;
/** The ID of the subscriber responsible for prefetches, so that requests can be cancelled and reissued */
Expand Down Expand Up @@ -567,13 +573,19 @@ class OMEZarrLoader extends ThreadableVolumeLoader {
}

this.lowResWarmSubscriber = this.requestQueue.addSubscriber();
const { plan, availableBytes } = planLowResPrefetch(this.sources, this.lowResCache.maxSize, MINIMUM_LOW_RES_CACHED_EXTENT);
const { plan, availableBytes } = planLowResPrefetch(
this.sources,
this.lowResCache.maxSize,
MINIMUM_LOW_RES_CACHED_EXTENT
);
for (const { sourceIndex, level, coords } of plan) {
const coordsTCZYX = this.orderByTCZYX(coords, 0, sourceIndex);
this.prefetchChunk(sourceIndex, level, coordsTCZYX, this.lowResWarmSubscriber, true);
}
if (plan.length > 0) {
console.info(`Prefetching ${plan.length} low-res chunks. ${(this.lowResCache.maxSize - availableBytes)/1_000_000}MB will be used.`);
console.info(
`Prefetching ${plan.length} low-res chunks. ${(this.lowResCache.maxSize - availableBytes) / 1_000_000}MB will be used.`
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/loaders/OpenCellLoader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ThreadableVolumeLoader, LoadSpec, RawChannelDataCallback, LoadedVolumeInfo } from "./IVolumeLoader.js";
import { VolumeLoader, LoadSpec, RawChannelDataCallback, LoadedVolumeInfo } from "./VolumeLoader.js";
import { computeAtlasSize, type ImageInfo } from "../ImageInfo.js";
import type { VolumeDims } from "../VolumeDims.js";
import { JsonImageInfoLoader } from "./JsonImageInfoLoader.js";
import { getDataRange } from "../utils/num_utils.js";

class OpenCellLoader extends ThreadableVolumeLoader {
class OpenCellLoader extends VolumeLoader {
async loadDims(_: LoadSpec): Promise<VolumeDims[]> {
const d: VolumeDims = {
shape: [1, 2, 27, 600, 600],
Expand Down
9 changes: 2 additions & 7 deletions src/loaders/RawArrayLoader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
ThreadableVolumeLoader,
type LoadSpec,
type RawChannelDataCallback,
type LoadedVolumeInfo,
} from "./IVolumeLoader.js";
import { VolumeLoader, type LoadSpec, type RawChannelDataCallback, type LoadedVolumeInfo } from "./VolumeLoader.js";
import { computePackedAtlasDims } from "./VolumeLoaderUtils.js";
import type { ImageInfo } from "../ImageInfo.js";
import type { VolumeDims } from "../VolumeDims.js";
Expand Down Expand Up @@ -94,7 +89,7 @@ const convertImageInfo = (json: RawArrayInfo, dtype: NumberType): ImageInfo => {
};
};

class RawArrayLoader extends ThreadableVolumeLoader {
class RawArrayLoader extends VolumeLoader {
data: RawArrayData;
jsonInfo: RawArrayInfo;

Expand Down
9 changes: 2 additions & 7 deletions src/loaders/TiffLoader.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { fromUrl, type GeoTIFF, type GeoTIFFImage } from "geotiff";
import { ErrorObject, deserializeError } from "serialize-error";

import {
ThreadableVolumeLoader,
LoadSpec,
type RawChannelDataCallback,
type LoadedVolumeInfo,
} from "./IVolumeLoader.js";
import { VolumeLoader, LoadSpec, type RawChannelDataCallback, type LoadedVolumeInfo } from "./VolumeLoader.js";
import { computePackedAtlasDims, MAX_ATLAS_EDGE } from "./VolumeLoaderUtils.js";
import { VolumeLoadError, VolumeLoadErrorType, wrapVolumeLoadError } from "./VolumeLoadError.js";
import { type ImageInfo, CImageInfo } from "../ImageInfo.js";
Expand Down Expand Up @@ -128,7 +123,7 @@ const getPixelType = (pxSize: number): string => (pxSize === 1 ? "uint8" : pxSiz

// Despite the class `TiffLoader` extends, this loader is not threadable, since geotiff internally uses features that
// aren't available on workers. It uses its own specialized workers anyways.
class TiffLoader extends ThreadableVolumeLoader {
class TiffLoader extends VolumeLoader {
private url: string[];
dims?: OMEDims;

Expand Down
62 changes: 22 additions & 40 deletions src/loaders/IVolumeLoader.ts → src/loaders/VolumeLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,46 +103,8 @@ export type RawChannelDataCallback = (
* Loaders may keep state for reuse between volume creation and volume loading, and should be kept alive until volume
* loading is complete. (See `createVolume`)
*/
export interface IVolumeLoader {
/** Use VolumeDims to further refine a `LoadSpec` for use in `createVolume` */
loadDims(loadSpec: LoadSpec): Promise<VolumeDims[]>;

/**
* Create an empty `Volume` from a `LoadSpec`, which must be passed to `loadVolumeData` to begin loading.
* Optionally pass a callback to respond whenever new channel data is loaded into the volume.
*/
createVolume(loadSpec: LoadSpec, onChannelLoaded?: PerChannelCallback): Promise<Volume>;

/**
* Begin loading a volume's data, as specified in its `LoadSpec`.
*
* Pass a callback to respond when this request loads a new channel. This callback will execute after the one
* assigned in `createVolume`, if any.
*
* The returned `Promise` resolves once all channels load, or rejects with any error that occurs during loading.
*/
// TODO make this return a promise that resolves when loading is done?
// TODO this is not cancellable in the sense that any async requests initiated here are not stored
// in a way that they can be interrupted.
// TODO explicitly passing a `LoadSpec` is now rarely useful. Remove?
loadVolumeData(volume: Volume, loadSpec?: LoadSpec, onChannelLoaded?: PerChannelCallback): Promise<void>;

/** Change which directions to prioritize when prefetching. Currently only implemented on `OMEZarrLoader`. */
setPrefetchPriority(directions: PrefetchDirection[]): void;

/**
* By default channel data can arrive out of order and at different times.
* This can cause the rendering to update in a way that is not visually appealing.
* In particular, during time series playback or Z slice playback, we would like
* to see all channels update at the same time.
* @param sync Set true to force all requested channels to load at the same time
*/
syncMultichannelLoading(sync: boolean): void;
}

/** Abstract class which allows loaders to accept and return types that are easier to transfer to/from a worker. */
export abstract class ThreadableVolumeLoader implements IVolumeLoader {
/** Unchanged from `IVolumeLoader`. See that interface for details. */
export abstract class VolumeLoader {
/** Use `VolumeDims` to further refine a `LoadSpec` for use in `createVolume` */
abstract loadDims(loadSpec: LoadSpec): Promise<VolumeDims[]>;

/**
Expand Down Expand Up @@ -173,10 +135,18 @@ export abstract class ThreadableVolumeLoader implements IVolumeLoader {
onData: RawChannelDataCallback
): Promise<void>;

/** Change which directions to prioritize when prefetching. Currently only implemented on `OMEZarrLoader`. */
setPrefetchPriority(_directions: PrefetchDirection[]): void {
// no-op by default
}

/**
* By default channel data can arrive out of order and at different times.
* This can cause the rendering to update in a way that is not visually appealing.
* In particular, during time series playback or Z slice playback, we would like
* to see all channels update at the same time.
* @param sync Set true to force all requested channels to load at the same time
*/
Comment on lines +143 to +149

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* By default channel data can arrive out of order and at different times.
* This can cause the rendering to update in a way that is not visually appealing.
* In particular, during time series playback or Z slice playback, we would like
* to see all channels update at the same time.
* @param sync Set true to force all requested channels to load at the same time
*/
/**
* Set whether to force all requested channels to load at the same time.
*
* By default channel data can arrive out of order and at different times.
* This can cause the rendering to update in a way that is not visually appealing.
* For example, during time series playback or Z slice playback, we would like
* to see all channels update at the same time.
*/

Really minor nit, but I feel like the wording of the current method string explains the problem first and not what the method actually does? But maybe not important because the method name is descriptive.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the suggested change here removes the @param line

syncMultichannelLoading(_sync: boolean): void {
// default behavior is async, to update channels as they arrive, depending on each
// loader's implementation details.
Expand All @@ -194,6 +164,18 @@ export abstract class ThreadableVolumeLoader implements IVolumeLoader {
return vol;
}

/**
* Begin loading a volume's data, as specified in its `LoadSpec`.
*
* Pass a callback to respond when this request loads a new channel. This callback will execute after the one
* assigned in `createVolume`, if any.
*
* The returned `Promise` resolves once all channels load, or rejects with any error that occurs during loading.
*/
// TODO make this return a promise that resolves when loading is done?
// TODO this is not cancellable in the sense that any async requests initiated here are not stored
// in a way that they can be interrupted.
// TODO explicitly passing a `LoadSpec` is now rarely useful. Remove?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these TODOs being addressed? shoudl they be elevated to issue tickets?

async loadVolumeData(
volume: Volume,
loadSpecOverride?: LoadSpec,
Expand Down
2 changes: 1 addition & 1 deletion src/loaders/VolumeLoaderUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box3, Vector2, Vector3 } from "three";

import { CImageInfo, type ImageInfo } from "../ImageInfo.js";
import { LoadSpec, type Region, regionToBox3 } from "./IVolumeLoader.js";
import { LoadSpec, type Region, regionToBox3 } from "./VolumeLoader.js";

export const MAX_ATLAS_EDGE = 4096;

Expand Down
4 changes: 2 additions & 2 deletions src/loaders/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ThreadableVolumeLoader } from "./IVolumeLoader.js";
import { VolumeLoader } from "./VolumeLoader.js";
import { OMEZarrLoader, type ZarrLoaderFetchOptions } from "./OmeZarrLoader.js";
import { JsonImageInfoLoader } from "./JsonImageInfoLoader.js";
import { RawArrayLoader, RawArrayLoaderOptions } from "./RawArrayLoader.js";
Expand Down Expand Up @@ -38,7 +38,7 @@ export function pathToFileType(path: string): VolumeFileFormat {
export async function createVolumeLoader(
path: string | string[],
options?: CreateLoaderOptions
): Promise<ThreadableVolumeLoader> {
): Promise<VolumeLoader> {
const pathString = Array.isArray(path) ? path[0] : path;
const fileType = options?.fileType || pathToFileType(pathString);
const pathArrayForTiffLoader = Array.isArray(path) ? path : [path];
Expand Down
2 changes: 1 addition & 1 deletion src/test/RequestQueue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Vector3 } from "three";
import type { TypedArray } from "zarrita";

import RequestQueue, { type Request } from "../utils/RequestQueue.js";
import { LoadSpec, loadSpecToString, regionToBox3 } from "../loaders/IVolumeLoader.js";
import { LoadSpec, loadSpecToString, regionToBox3 } from "../loaders/VolumeLoader.js";

// Fixes a type error where process is undefined
declare const process: {
Expand Down
7 changes: 5 additions & 2 deletions src/test/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import VolumeMaker from "../VolumeMaker.js";
import { LUT_ARRAY_LENGTH } from "../Lut.js";
import Channel from "../Channel.js";
import { CImageInfo, ImageInfo } from "../ImageInfo.js";
import { IVolumeLoader, LoadSpec } from "../loaders/IVolumeLoader.js";
import { VolumeLoader, LoadSpec } from "../loaders/VolumeLoader.js";
import { getDataRange } from "../utils/num_utils.js";

// PREPARE SOME TEST DATA TO TRY TO DISPLAY A VOLUME.
Expand Down Expand Up @@ -60,13 +60,16 @@ const multiscaleTestimgdata: ImageInfo = {
],
};

function createTestLoader(multiscaleLevelDims: ImageInfo["multiscaleLevelDims"]): IVolumeLoader {
function createTestLoader(multiscaleLevelDims: ImageInfo["multiscaleLevelDims"]): VolumeLoader {
return {
loadDims: vi.fn().mockResolvedValue(multiscaleLevelDims),
createVolume: vi.fn(),
loadVolumeData: vi.fn(),
setPrefetchPriority: vi.fn(),
syncMultichannelLoading: vi.fn(),
createImageInfo: vi.fn(),
loadRawChannelData: vi.fn(),
updateFetchOptions: vi.fn(),
};
}

Expand Down
Loading
Loading