Skip to content

Commit

Permalink
Merge pull request #6 from sumanthratna/master
Browse files Browse the repository at this point in the history
Fix svs2dask_array
  • Loading branch information
jlevy44 authored Mar 13, 2020
2 parents 00588aa + c2b0e54 commit ac36bd0
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions pathflowai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,40 +84,49 @@ def df2sql(df, sql_file, patch_size, mode='replace'):

# https://github.com/qupath/qupath/wiki/Supported-image-formats
def svs2dask_array(svs_file, tile_size=1000, overlap=0, remove_last=True, allow_unknown_chunksizes=False):
"""Convert SVS, TIF or TIFF to dask array.
Parameters
----------
svs_file:str
Image file.
tile_size:int
Size of chunk to be read in.
overlap:int
Do not modify, overlap between neighboring tiles.
remove_last:bool
Remove last tile because it has a custom size.
allow_unknown_chunksizes: bool
Allow different chunk sizes, more flexible, but slowdown.
Returns
-------
dask.array
Dask Array.
>>> arr=svs2dask_array(svs_file, tile_size=1000, overlap=0, remove_last=True, allow_unknown_chunksizes=False)
>>> arr2=arr.compute()
>>> arr3=to_pil(cv2.resize(arr2, dsize=(1440,700), interpolation=cv2.INTER_CUBIC))
>>> arr3.save(test_image_name)"""
img=openslide.open_slide(svs_file)
gen=deepzoom.DeepZoomGenerator(img, tile_size=tile_size, overlap=overlap, limit_bounds=True)
max_level = len(gen.level_dimensions)-1
n_tiles_x, n_tiles_y = gen.level_tiles[max_level]
get_tile = lambda i,j: np.array(gen.get_tile(max_level,(i,j))).transpose((1,0,2))
sample_tile = get_tile(0,0)
sample_tile_shape = sample_tile.shape
dask_get_tile = dask.delayed(get_tile, pure=True)
arr=da.concatenate([da.concatenate([da.from_delayed(dask_get_tile(i,j),sample_tile_shape,np.uint8) for j in range(n_tiles_y - (0 if not remove_last else 1))],allow_unknown_chunksizes=allow_unknown_chunksizes,axis=1) for i in range(n_tiles_x - (0 if not remove_last else 1))],allow_unknown_chunksizes=allow_unknown_chunksizes)#.transpose([1,0,2])
return arr
"""Convert SVS, TIF or TIFF to dask array.
Parameters
----------
svs_file : str
Image file.
tile_size : int
Size of chunk to be read in.
overlap : int
Do not modify, overlap between neighboring tiles.
remove_last : bool
Remove last tile because it has a custom size.
allow_unknown_chunksizes : bool
Allow different chunk sizes, more flexible, but slowdown.
Returns
-------
arr : dask.array.Array
A Dask Array representing the contents of the image file.
>>> arr = svs2dask_array(svs_file, tile_size=1000, overlap=0, remove_last=True, allow_unknown_chunksizes=False)
>>> arr2 = arr.compute()
>>> arr3 = to_pil(cv2.resize(arr2, dsize=(1440, 700), interpolation=cv2.INTER_CUBIC))
>>> arr3.save(test_image_name)
"""
# https://github.com/jlevy44/PathFlowAI/blob/master/pathflowai/utils.py
img = openslide.open_slide(svs_file)
if type(img) is openslide.OpenSlide:
gen = deepzoom.DeepZoomGenerator(
img, tile_size=tile_size, overlap=overlap, limit_bounds=True)
max_level = len(gen.level_dimensions) - 1
n_tiles_x, n_tiles_y = gen.level_tiles[max_level]

@dask.delayed(pure=True)
def get_tile(level, column, row):
tile = gen.get_tile(level, (column, row))
return np.array(tile).transpose((1, 0, 2))

sample_tile_shape = get_tile(max_level, 0, 0).shape.compute()
rows = range(n_tiles_y - (0 if not remove_last else 1))
cols = range(n_tiles_x - (0 if not remove_last else 1))
arr = da.concatenate([da.concatenate([da.from_delayed(get_tile(max_level, col, row), sample_tile_shape, np.uint8) for row in rows],
allow_unknown_chunksizes=allow_unknown_chunksizes, axis=1) for col in cols], allow_unknown_chunksizes=allow_unknown_chunksizes).transpose([1, 0, 2])
return arr
else: # img is instance of openslide.ImageSlide
return dask_image.imread.imread(svs_file)

def img2npy_(input_dir,basename, svs_file):
"""Convert SVS, TIF, TIFF to NPY.
Expand Down

0 comments on commit ac36bd0

Please sign in to comment.