Skip to content

Commit

Permalink
Move nested handling to io.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
will-moore committed Mar 31, 2021
1 parent e10b2ae commit 3cdabea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
loadMultiscales,
MAGENTA_GREEN,
MAX_CHANNELS,
nested,
open,
parseMatrix,
range,
Expand Down Expand Up @@ -115,13 +116,29 @@ function loadMultiChannel(config: MultichannelConfig, data: ZarrPixelSource<stri
};
}

function isNested(attrs: Ome.Attrs) {
let version;
if ('plate' in attrs) {
version = attrs.plate.version;
} else if ('omero' in attrs) {
version = attrs.multiscales[0].version;
} else if ('well' in attrs) {
version = attrs.well.version;
}
return version && version !== '0.1';
}

export async function createSourceData(config: ImageLayerConfig): Promise<SourceData> {
const node = await open(config.source);
let data: ZarrArray[];

if (node instanceof ZarrGroup) {
const attrs = (await node.attrs.asObject()) as Ome.Attrs;

if (isNested(attrs)) {
node.store = nested(node.store);
}

if ('plate' in attrs) {
return loadPlate(config, node, attrs.plate);
}
Expand Down
19 changes: 5 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,15 @@ export async function loadMultiscales(grp: ZarrGroup, multiscales: Ome.Multiscal

export function nested(store: ZarrArray['store']) {
const get = (target: ZarrArray['store'], key: string | number | symbol) => {
if (key === 'getItem' || key === 'containsItem' || key === 'setItem') {
return (path: string, ...args: any[]) => {
if (key === 'getItem' || key === 'containsItem') {
return (path: string, ...args: unknown[]) => {
if (path.endsWith('.zarray') || path.endsWith('.zattrs') || path.endsWith('.zgroup')) {
if (key === 'setItem') {
// TypeScript: setItem() needs 'value'
return target[key](path, args[0]);
} else {
return target[key](path, ...args);
}
return target[key](path, ...args);
}
const prefix = path.split('/');
const chunkKey = prefix.pop() as string;
const chunkKey = prefix.pop()!;
const newPath = [...prefix, chunkKey.replaceAll('.', '/')].join('/');
if (key === 'setItem') {
return target[key](newPath, args[0]);
} else {
return target[key](newPath, ...args);
}
return target[key](newPath, ...args);
};
}
return Reflect.get(target, key);
Expand Down

0 comments on commit 3cdabea

Please sign in to comment.