Skip to content

Commit 3558e73

Browse files
authoredSep 26, 2021
Load each Well to get path to first image for each (#119)
* Load each Well to get path to first image for each * Use new utils.getAttrsOnly() for loading plate Wells
1 parent 95b37b3 commit 3558e73

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed
 

‎src/ome.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { ZarrPixelSource } from '@hms-dbmi/viv';
22
import pMap from 'p-map';
33
import { Group as ZarrGroup, openGroup, ZarrArray } from 'zarr';
44
import type { ImageLayerConfig, SourceData } from './state';
5-
import { join, loadMultiscales, guessTileSize, range, parseMatrix } from './utils';
5+
import {
6+
getAttrsOnly,
7+
guessTileSize,
8+
join,
9+
loadMultiscales,
10+
parseMatrix,
11+
range
12+
} from './utils';
613

714
export async function loadWell(config: ImageLayerConfig, grp: ZarrGroup, wellAttrs: Ome.Well): Promise<SourceData> {
815
// Can filter Well fields by URL query ?acquisition=ID
@@ -119,7 +126,7 @@ export async function loadPlate(config: ImageLayerConfig, grp: ZarrGroup, plateA
119126
const wellPaths = plateAttrs.wells.map((well) => well.path);
120127

121128
// Use first image as proxy for others.
122-
const wellAttrs = (await grp.getItem(wellPaths[0]).then((g) => g.attrs.asObject())) as Ome.Attrs;
129+
const wellAttrs = await getAttrsOnly<{ well: Ome.Well }>(grp, wellPaths[0]);
123130
if (!('well' in wellAttrs)) {
124131
throw Error('Path for image is not valid, not a well.');
125132
}
@@ -133,10 +140,16 @@ export async function loadPlate(config: ImageLayerConfig, grp: ZarrGroup, plateA
133140
const { datasets } = imgAttrs.multiscales[0];
134141
const resolution = datasets[datasets.length - 1].path;
135142

143+
async function getImgPath(wellPath:string) {
144+
const wellAttrs = await getAttrsOnly<{ well: Ome.Well }>(grp, wellPath);
145+
return join(wellPath, wellAttrs.well.images[0].path);
146+
}
147+
const wellImagePaths = await Promise.all(wellPaths.map(getImgPath));
148+
136149
// Create loader for every Well. Some loaders may be undefined if Wells are missing.
137150
const mapper = ([key, path]: string[]) => grp.getItem(path).then((arr) => [key, arr]) as Promise<[string, ZarrArray]>;
138151
const promises = await pMap(
139-
wellPaths.map((p) => [p, join(p, imgPath, resolution)]),
152+
wellImagePaths.map((p) => [p, join(p, resolution)]),
140153
mapper,
141154
{ concurrency: 10 }
142155
);

‎src/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ export async function open(source: string | Store) {
5555
});
5656
}
5757

58+
const decoder = new TextDecoder();
59+
export function getAttrsOnly<T = unknown>(grp: ZarrGroup, path: string) {
60+
return (grp.store as AsyncStore<ArrayBuffer>)
61+
.getItem(join(grp.path, path, ".zattrs"))
62+
.then((b) => decoder.decode(b))
63+
.then((text) => JSON.parse(text) as T);
64+
}
65+
5866
export async function loadMultiscales(grp: ZarrGroup, multiscales: Ome.Multiscale[]) {
5967
const { datasets } = multiscales[0] || [{ path: '0' }];
6068
const nodes = await Promise.all(datasets.map(({ path }) => grp.getItem(path)));

0 commit comments

Comments
 (0)
Please sign in to comment.