Skip to content

Commit c604a8e

Browse files
committed
function: write using zarr
1 parent 4d8a1eb commit c604a8e

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

histomicstk/cli/SuperpixelSegmentation/SuperpixelSegmentation.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
tile_grid_w_mask,
1515
get_ancestor_tileids, get_trim_dict,
1616
Mask, tilejob,
17-
write_to_tiff_vips)
17+
write_to_tiff_vips, write_to_tiff_zarr)
1818

1919
import histomicstk
2020
from histomicstk.cli import utils
@@ -126,13 +126,8 @@ def createSuperPixelsParallel(opts):
126126
if hasattr(opts, 'callback'):
127127
opts.callback('file', 0, 2 if opts.outputAnnotationFile else 1)
128128

129-
img = pyvips.Image.black(
130-
tiparams.get('region', {}).get('width', meta['sizeX']) / scale,
131-
tiparams.get('region', {}).get('height', meta['sizeY']) / scale,
132-
bands=4)
133-
img = img.copy(interpretation=pyvips.Interpretation.RGB)
134-
135-
found = write_to_tiff_vips(opts, grid, strips, strips_found, meta, scale, tiparams, coordx)
129+
found = write_to_tiff_zarr(opts, grid, strips, strips_found,
130+
w, h, alltile_metadata, coordx, coordy)
136131

137132
bboxes = []
138133
bboxesUser = []

histomicstk/cli/SuperpixelSegmentation/parallelization_utilities.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ def first_order(tasks):
397397
]
398398
return ancestors, dependents
399399

400+
def get_wsi_size(alltile_metadata):
401+
xs = set()
402+
ys = set()
403+
for _x,_y in alltile_metadata.keys():
404+
xs.add(_x)
405+
ys.add(_y)
406+
last_x = max(xs)
407+
last_y = max(ys)
408+
409+
wsi_w = last_x + alltile_metadata[(last_x,0)][1]
410+
wsi_h = last_y + alltile_metadata[(0,last_y)][0]
411+
return wsi_h, wsi_w
400412

401413
def tile_grid(ts, opts, averageSize, overlap, tileSize, tiparams, verbose=False):
402414
""" returns a dense 2D grid of tasks over (h,w) grid """
@@ -980,3 +992,55 @@ def write_to_tiff_vips(opts, grid, strips, strips_found, meta, scale, tiparams,
980992
bigtiff=True, compression='lzw', predictor='horizontal')
981993

982994
return found
995+
996+
def write_to_tiff_zarr(opts, grid, strips, strips_found, w, h, alltile_metadata, coordx, coordy, write_all=True):
997+
998+
source = large_image.new()
999+
1000+
1001+
if write_all:
1002+
#write zeros for dropped tiles
1003+
for cx, cy in [(x, y) for x, y in itertools.product(range(w), range(h)) if (x,y) not in grid]:
1004+
height, width = alltile_metadata[(coordx[cx],coordy[cy])]
1005+
data = numpy.zeros([height, width, 3]).astype('B')
1006+
1007+
source.addTile(data[:,:,:3],
1008+
coordx[cx],
1009+
coordy[cy],
1010+
mask = numpy.ones(data.shape[:2]).astype('B'))
1011+
else:
1012+
wsi_h, wsi_w = get_wsi_size(alltile_metadata)
1013+
#write 0 at the last pixel to fix wsi height and width
1014+
## Non-zero values are written sometimes to remaining array (cause is unknown). So, this is unreliable.
1015+
data = numpy.zeros([1, 1, 3]).astype('B')
1016+
source.addTile(data[:,:,:3],
1017+
wsi_w-1,
1018+
wsi_h-1,
1019+
mask = numpy.ones(data.shape[:2]).astype('B')
1020+
)
1021+
1022+
1023+
found = 0
1024+
for cx, cy in grid:
1025+
stripidx = (coordx[cx], cy)
1026+
data = strips[stripidx][1]
1027+
1028+
d1 = numpy.where(data[:,:,0]!=0, data[:,:,0]+found, data[:,:,0])
1029+
1030+
data = numpy.dstack((
1031+
((d1) % 256).astype(int),
1032+
((d1)/ 256).astype(int) % 256,
1033+
((d1) / 65536).astype(int) % 256,
1034+
data[:,:,1])).astype('B')
1035+
1036+
source.addTile(data[:,:,:3],
1037+
coordx[cx],
1038+
coordy[cy],
1039+
mask = data[:,:,3]
1040+
)
1041+
1042+
found += strips_found[stripidx]
1043+
1044+
source.write(opts.outputImageFile, lossy=False)
1045+
1046+
return found

0 commit comments

Comments
 (0)