-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata-aphid-1-plane.py
More file actions
72 lines (64 loc) · 2.47 KB
/
data-aphid-1-plane.py
File metadata and controls
72 lines (64 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import requests
import re
import h5py
import numpy as np
import skimage.io as skio
import tensorstore as ts
url='http://em-services-1.int.janelia.org:8080/render-ws/v1/owner/cellmap/project/jrc_aphid_salivary_1/stack/v3_acquire'
tile_space = (2, 3)
crop = ((0,None), (0,None))
def get_tilepath(Z):
r = requests.get(f"{url}/zRange/{Z},{Z}/layoutFile?format=SCHEFFER")
s = set([re.sub(r"\?.*","", s)[11:] for s in r.text.split('\n')[1:-1]])
assert len(s)==1
return s.pop()
def load_data(planepath, scale):
# Define the tile space. This specifies how the different tiles are distributed
# in space, and should normally be derived from the metadata provided by the
# microscope.
tile_id_map = [["0-0-0", "0-0-1", "0-0-2"],
["0-1-0", "0-1-1", "0-1-2"]]
tile_id_map = np.array(tile_id_map)
# Load tile images.
tile_map = {}
for y in range(tile_id_map.shape[0]):
for x in range(tile_id_map.shape[1]):
tile_id = tile_id_map[y, x]
with h5py.File(f'{planepath}', 'r') as fid:
d = fid[f'{tile_id}/mipmap.{scale}']
idx1 = slice(*[c//2**scale if c else c for c in crop[0]])
idx2 = slice(*[c//2**scale if c else c for c in crop[1]])
tile_map[(x, y)] = np.array(d[0,idx1,idx2])
return tile_map
def save_plane(outpath, z, stitched, write_metadata, chunk_size):
r = requests.get(f"{url}/zValues")
nz = int(float(r.text[1:-1].split(',')[-1]))
za = ts.open({
'driver': 'zarr',
'kvstore': {"driver":"file", "path":os.path.join(outpath, 'stitched.s0.zarr')},
'metadata': {
"compressor": {"id":"zstd", "level":3},
"shape": [nz,*stitched.shape],
"chunks": [1,chunk_size,chunk_size],
"fill_value": 0,
'dtype': '|u1',
'dimension_separator': '/',
},
'create': True,
'open': True,
'delete_existing': False,
'assume_metadata': write_metadata==0,
}).result()
za[z,:,:] = stitched
def save_tiles(outpath, planepath, stitched, maxdim):
planename = os.path.basename(planepath)
padded_array = np.zeros(maxdim, dtype=np.uint8)
for k in stitched.keys():
padded_array[:,:] = 0
sh = stitched[k].shape
padded_array[:sh[0],:sh[1]] = stitched[k]
x = list(k)
x.reverse()
x = [0]+x
skio.imsave(f'{outpath}/{planename}-{x}.tif'.replace(" ",""), padded_array)