Skip to content

Commit

Permalink
function: write using zarr
Browse files Browse the repository at this point in the history
  • Loading branch information
abs711 committed Feb 11, 2025
1 parent 4d8a1eb commit c604a8e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
11 changes: 3 additions & 8 deletions histomicstk/cli/SuperpixelSegmentation/SuperpixelSegmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
tile_grid_w_mask,
get_ancestor_tileids, get_trim_dict,
Mask, tilejob,
write_to_tiff_vips)
write_to_tiff_vips, write_to_tiff_zarr)

import histomicstk
from histomicstk.cli import utils
Expand Down Expand Up @@ -126,13 +126,8 @@ def createSuperPixelsParallel(opts):
if hasattr(opts, 'callback'):
opts.callback('file', 0, 2 if opts.outputAnnotationFile else 1)

img = pyvips.Image.black(
tiparams.get('region', {}).get('width', meta['sizeX']) / scale,
tiparams.get('region', {}).get('height', meta['sizeY']) / scale,
bands=4)
img = img.copy(interpretation=pyvips.Interpretation.RGB)

found = write_to_tiff_vips(opts, grid, strips, strips_found, meta, scale, tiparams, coordx)
found = write_to_tiff_zarr(opts, grid, strips, strips_found,
w, h, alltile_metadata, coordx, coordy)

bboxes = []
bboxesUser = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,18 @@ def first_order(tasks):
]
return ancestors, dependents

def get_wsi_size(alltile_metadata):
xs = set()
ys = set()
for _x,_y in alltile_metadata.keys():
xs.add(_x)
ys.add(_y)
last_x = max(xs)
last_y = max(ys)

wsi_w = last_x + alltile_metadata[(last_x,0)][1]
wsi_h = last_y + alltile_metadata[(0,last_y)][0]
return wsi_h, wsi_w

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

return found

def write_to_tiff_zarr(opts, grid, strips, strips_found, w, h, alltile_metadata, coordx, coordy, write_all=True):

source = large_image.new()


if write_all:
#write zeros for dropped tiles
for cx, cy in [(x, y) for x, y in itertools.product(range(w), range(h)) if (x,y) not in grid]:
height, width = alltile_metadata[(coordx[cx],coordy[cy])]
data = numpy.zeros([height, width, 3]).astype('B')

source.addTile(data[:,:,:3],
coordx[cx],
coordy[cy],
mask = numpy.ones(data.shape[:2]).astype('B'))
else:
wsi_h, wsi_w = get_wsi_size(alltile_metadata)
#write 0 at the last pixel to fix wsi height and width
## Non-zero values are written sometimes to remaining array (cause is unknown). So, this is unreliable.
data = numpy.zeros([1, 1, 3]).astype('B')
source.addTile(data[:,:,:3],
wsi_w-1,
wsi_h-1,
mask = numpy.ones(data.shape[:2]).astype('B')
)


found = 0
for cx, cy in grid:
stripidx = (coordx[cx], cy)
data = strips[stripidx][1]

d1 = numpy.where(data[:,:,0]!=0, data[:,:,0]+found, data[:,:,0])

data = numpy.dstack((
((d1) % 256).astype(int),
((d1)/ 256).astype(int) % 256,
((d1) / 65536).astype(int) % 256,
data[:,:,1])).astype('B')

source.addTile(data[:,:,:3],
coordx[cx],
coordy[cy],
mask = data[:,:,3]
)

found += strips_found[stripidx]

source.write(opts.outputImageFile, lossy=False)

return found

0 comments on commit c604a8e

Please sign in to comment.