@@ -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
401413def 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