Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

83 lines (67 sloc) 2.35 KB
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.4'
# jupytext_version: 1.1.7
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
import umap
import glob
import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
# %matplotlib notebook
files=glob.glob("./projects/nuclei/patches/*.png") #load 256 x 256 patches
imgs=[ cv2.imread(file)[None,::] for file in files]
imgs=np.vstack(imgs)
imgs.shape
imgs_vector=imgs.reshape(imgs.shape[0],-1)
embedding = umap.UMAP(n_neighbors=100,min_dist=0.0).fit_transform(imgs_vector)
# +
#https://stackoverflow.com/questions/42867400/python-show-image-upon-hovering-over-a-point
x=embedding[:, 0]
y=embedding[:, 1]
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot(x,y, ls="", marker="o")
im = OffsetImage(imgs[0,:,:,:], zoom=.5)
xybox=(50., 50.)
ab = AnnotationBbox(im, (0,0), xybox=xybox, xycoords='data',
boxcoords="offset points", pad=0.3, arrowprops=dict(arrowstyle="->"))
# add it to the axes and make it invisible
ax.add_artist(ab)
ab.set_visible(False)
def hover(event):
# if the mouse is over the scatter points
if line.contains(event)[0]:
# find out the index within the array from the event
ind, = line.contains(event)[1]["ind"]
# get the figure size
w,h = fig.get_size_inches()*fig.dpi
ws = (event.x > w/2.)*-1 + (event.x <= w/2.)
hs = (event.y > h/2.)*-1 + (event.y <= h/2.)
# if event occurs in the top or right quadrant of the figure,
# change the annotation box position relative to mouse.
ab.xybox = (xybox[0]*ws, xybox[1]*hs)
# make annotation box visible
ab.set_visible(True)
# place it at the position of the hovered scatter point
ab.xy =(x[ind], y[ind])
# set the image corresponding to that point
im.set_data(imgs[ind,:,:,:]) #if the dataset is too large to load into memory, can instead replace this command with a realtime load
else:
#if the mouse is not over a scatter point
ab.set_visible(False)
fig.canvas.draw_idle()
# add callback for mouse moves
fig.canvas.mpl_connect('motion_notify_event', hover)
plt.show()
# -
You can’t perform that action at this time.