Skip to content

Instantly share code, notes, and snippets.

@mingrui
Created August 9, 2018 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingrui/119de48a79ca1e7ebce17233abbe71bb to your computer and use it in GitHub Desktop.
Save mingrui/119de48a79ca1e7ebce17233abbe71bb to your computer and use it in GitHub Desktop.
slide runner map_async load test
def load_async(arg):
print('load_async')
tick = time.time()
filename, reader_location, level, reader_size, location, size = arg
slide = openslide.open_slide(filename)
region = slide.read_region(location=reader_location, level=level, size=reader_size)
# Convert to numpy array
cachedImage = np.array(region, dtype=np.uint8)
print('3', time.time() - tick)
return (cachedImage, reader_location, level, location, size)
#############################################################################################
"""
read_region: cached version of openslide's read_region.
Reads from the original slide with 50% overlap to each side.
"""
def read_region(self, location, level, size, cache_factor=3, forceRead = False):
# print('read_region size:', size)
self.lastReadRequest = dict()
self.lastReadRequest['location'] = location
self.lastReadRequest['level'] = level
self.lastReadRequest['size'] = size
reader_location = (int(location[0]-1.*size[0]*self.slide.level_downsamples[level]),
int(location[1]-1.*size[1]*self.slide.level_downsamples[level]))
reader_size = (int(size[0]*cache_factor), int(size[1]*cache_factor))
readNew = False
if (not(level == self.cachedLevel) or (self.cachedLocation is None)):
readNew = True
if (self.cachedLocation is not None):
x0 = int((location[0]-self.cachedLocation[0])/self.slide.level_downsamples[level])
y0 = int((location[1]-self.cachedLocation[1])/self.slide.level_downsamples[level])
x1 = x0 + size[0]
y1 = y0 + size[1]
if (x0<0) or (y0<0): # out of cache
readNew = True
if ((y1>self.cachedImage.shape[0]) or
(x1>self.cachedImage.shape[1])):
readNew = True
if forceRead:
# region = self.slide.read_region(location=reader_location,level=level,size=reader_size)
# x0, x1, y0, y1 = self.calculate_cache(np.array(region, dtype=np.uint8), reader_location, level, location, size)
args = ((self.filename, reader_location, level, reader_size, location, size),)
pool.map_async(load_async, args, callback=self.load_async_cb)
else:
if readNew:
region = self.slide.read_region(location=reader_location, level=level, size=reader_size)
x0, x1, y0, y1 = self.calculate_cache(np.array(region, dtype=np.uint8), reader_location, level, location, size)
else:
pass
return self.cachedImage[y0:y1, x0:x1, :]
def calculate_cache(self, cached_image, reader_location, level, location, size):
self.cachedImage = cached_image
self.cachedLevel = level
self.cachedLocation = reader_location
x0 = int((location[0] - self.cachedLocation[0]) / self.slide.level_downsamples[level])
y0 = int((location[1] - self.cachedLocation[1]) / self.slide.level_downsamples[level])
x1 = x0 + size[0]
y1 = y0 + size[1]
return x0, x1, y0, y1
# how to use:
# args = ((self.filename, reader_location, level, reader_size, location, size),)
# pool.map_async(load_async, args, callback=self.load_async_cb)
def load_async_cb(self, arg):
cached_image, reader_location, level, location, size = arg[0]
self.cachedImage = cached_image
self.cachedLevel = level
self.cachedLocation = reader_location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment