-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmultiscale.py
More file actions
192 lines (166 loc) · 5.91 KB
/
multiscale.py
File metadata and controls
192 lines (166 loc) · 5.91 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
#bsub -Pcellmap -n8 -Is /bin/zsh
#./multiscale.py /nrs/cellmap/arthurb/aphid/final warped.patch128.stride8.scales12.k00.01.k0.1.reps2.zarr pyramid_anisotropic_v2.zarr 4
import argparse
import tensorstore as ts
import numpy as np
import json
import os
import shutil
from datetime import datetime
# Parse command line arguments
parser = argparse.ArgumentParser(
description="warps a slice according to a coordinate map"
)
parser.add_argument(
"basepath",
help="filepath to stitched planes"
)
parser.add_argument(
"inpath",
help="filepath to s0"
)
parser.add_argument(
"outpath",
help="filepath to pyramid"
)
parser.add_argument(
"nlevels",
type=int,
help="numbers of scales (beyond s0)"
)
parser.add_argument(
"chunkxy",
type=int,
help="of the zarr output",
)
parser.add_argument(
"chunkz",
type=int,
help="of the zarr output",
)
args = parser.parse_args()
basepath = args.basepath
inpath = args.inpath
outpath = args.outpath
nlevels = args.nlevels
chunkxy = args.chunkxy
chunkz = args.chunkz
print("basepath =", basepath)
print("inpath =", inpath)
print("outpath =", outpath)
print("nlevels =", nlevels)
print("chunkxy =", chunkxy)
print("chunkz =", chunkz)
# --- CONFIGURATION FOR MEMORY SAFETY ---
# Adjust these based on your machine's available RAM
MEMORY_LIMIT_BYTES = 16_000_000_000 # bytes
CONCURRENCY_LIMIT = 16 # processes
def write_ome_zarr_v2_metadata(output_root, num_levels, scale_factors, axes=('z', 'y', 'x')):
datasets = []
for level in range(num_levels + 1):
current_level_scales = [base ** level for base in scale_factors]
current_level_translations = [x / 2 - 0.5 for x in current_level_scales]
#1: 1-0, 1-0, 1-0, 1-0
#2: 1-0, 2-0.5, 4-1.5, 8-3.5, 16-7.5
#4: 1-0, 4-1.5, 16-7.5, 32-
#
#123456789abcdefg
#159d
#1
datasets.append({
"path": f"s{level}",
"coordinateTransformations": [
{"type": "scale", "scale": current_level_scales},
{"type": "translation", "translation": current_level_translations}]
})
metadata = {
"multiscales": [{
"version": "0.4",
"name": "pyramid",
"axes": [{"name": ax, "type": "space"} for ax in axes],
"coordinateTransformations": [{"type": "scale", "scale": [1.0] * len(axes)}],
"datasets": datasets,
}]
}
os.makedirs(output_root, exist_ok=True)
with open(os.path.join(output_root, '.zattrs'), 'w') as f:
json.dump(metadata, f, indent=2)
with open(os.path.join(output_root, '.zgroup'), 'w') as f:
json.dump({"zarr_format": 2}, f, indent=2)
def create_pyramid_v2_safe(
input_path: str,
output_root: str,
num_levels: int = 4,
scale_factors: tuple = (1, 2, 2),
chunk_size: tuple = (64, 64, 64)
):
# 1. Define a Context to limit resources
# This prevents the "bad_alloc" crash by forcing TensorStore to queue tasks
# rather than running them all at once.
context_spec = {
'cache_pool': {'total_bytes_limit': MEMORY_LIMIT_BYTES},
'data_copy_concurrency': {'limit': CONCURRENCY_LIMIT},
'file_io_concurrency': {'limit': CONCURRENCY_LIMIT},
}
print(f"Opening source: {input_path}")
source_ts = ts.open({
'driver': 'zarr',
'kvstore': {'driver': 'file', 'path': input_path},
'context': context_spec # <--- Apply limits here
}).result()
if len(scale_factors) != source_ts.ndim:
raise ValueError(f"Scale factors {scale_factors} do not match dimensions {source_ts.ndim}")
dtype = source_ts.dtype.numpy_dtype
current_source = source_ts
for level in range(1, num_levels + 1):
target_path = f"{output_root}/s{level}"
print(f"--- Processing Level s{level} (Safe Mode) ---")
print(f"Target: {target_path}")
if os.path.exists(target_path):
print("output directory already exists. exiting")
exit()
# Downsample View
downsampled_view = ts.downsample(current_source, scale_factors, method='mean')
if dtype != downsampled_view.dtype.numpy_dtype:
downsampled_view = downsampled_view.astype(dtype)
spec = {
'driver': 'zarr',
'kvstore': {'driver': 'file', 'path': target_path},
'context': context_spec, # <--- Apply limits here as well
'metadata': {
'shape': downsampled_view.shape,
'dtype': dtype.str,
'chunks': chunk_size,
'compressor': {
'id': 'zstd',
'level': 3,
},
'dimension_separator':'/',
'fill_value':0,
'order': 'C',
'zarr_format': 2
}
}
# Create and Write
print("Opening target directory...")
target_ts = ts.open(spec, create=True).result()
# This write will now obey the concurrency limits set in 'context_spec'
print("Writing target directory...")
for z in range(0, downsampled_view.shape[0], chunk_size[0]):
zs = range(z, min(downsampled_view.shape[0], z+chunk_size[0]))
if not downsampled_view[zs,...].storage_statistics(query_not_stored=True).result().not_stored:
print(datetime.now(), 'z =', zs[0], ':', zs[-1], ' saving')
target_ts[zs,...].write(downsampled_view[zs,...]).result()
else:
print(datetime.now(), 'z =', zs[0], ':', zs[-1], ' skipping')
current_source = target_ts
write_ome_zarr_v2_metadata(output_root, num_levels, scale_factors)
print(f"Done. Safe Pyramid located at: {output_root}")
create_pyramid_v2_safe(
input_path=os.path.join(basepath,inpath),
output_root=os.path.join(basepath,outpath),
num_levels=nlevels,
scale_factors=(2, 2, 2),
chunk_size=(chunkz, chunkxy, chunkxy) # Your requested chunk size
)