def test_vectorData_datatype1(): """ test data of coord-like shape :return: """ with app_context(): # create the viewer and window viewer = ViewerApp() N = 10 pos = np.zeros(shape=(N, 4), dtype=np.float32) try: viewer.add_vectors(pos) except Exception as ex: print("exception thrown when creating coord-like vector layer: "+str(ex))
def test_vectorData_datatype_notimpl2(self): """ test data of vispy-coordinate shape (not allowed) :return: """ with app_context(): # create the viewer and window viewer = ViewerApp() N = 10 pos = np.zeros(shape=(N, 2), dtype=np.float32) try: viewer.add_vectors(pos) except InvalidDataFormatError: print('invalid data format') except Exception as ex: print("exception thrown when creating not implemented vector layer: "+str(ex))
def test_vectorData_datatype_notimpl(): """ test data of improper shape :return: """ with app_context(): # create the viewer and window viewer = ViewerApp() N = 10 M = 5 pos = np.zeros(shape=(N, M, 4), dtype=np.float32) try: viewer.add_vectors(pos) except InvalidDataFormatError: print('invalid data format') except Exception as ex: print("exception thrown when creating not implemented vector layer: "+str(ex))
def test_vectorData_image_assignment(self): """ test replacing vector data after layer construction :return: """ with app_context(): # create the viewer and window viewer = ViewerApp() N = 10 pos = np.zeros(shape=(N, 4), dtype=np.float32) pos2 = np.zeros(shape=(N, N, 2), dtype=np.float32) try: layer = viewer.add_vectors(pos) layer.vectors = pos2 except Exception as ex: print("exception thrown when : "+str(ex))
from napari import ViewerApp from napari.util import app_context from skimage import data import numpy as np with app_context(): # create the viewer and window viewer = ViewerApp() layer = viewer.add_image(data.camera(), name='photographer') layer.colormap = 'gray' # sample vector coord-like data n = 1000 pos = np.zeros((n, 4), dtype=np.float32) phi_space = np.linspace(0, 4 * np.pi, n) radius_space = np.linspace(0, 100, n) # assign x-y position pos[:, 0] = radius_space * np.cos(phi_space) + 350 pos[:, 1] = radius_space * np.sin(phi_space) + 256 # assign x-y projection pos[:, 2] = 2 * radius_space * np.cos(phi_space) pos[:, 3] = 2 * radius_space * np.sin(phi_space) # add the vectors layer = viewer.add_vectors(pos, width=0.4)
from napari.util import app_context import numpy as np with app_context(): # create the viewer and window viewer = ViewerApp() n = 100 m = 200 image = 0.2*np.random.random((n, m)) + 0.5 layer = viewer.add_image(image, clim_range=[0, 1], name='background') layer.colormap = 'gray' # sample vector image-like data # n x m grid of slanted lines # random data on the open interval (-1, 1) pos = np.zeros(shape=(n, m, 2), dtype=np.float32) rand1 = 2*(np.random.random_sample(n * m)-0.5) rand2 = 2*(np.random.random_sample(n * m)-0.5) # assign projections for each vector pos[:, :, 0] = rand1.reshape((n, m)) pos[:, :, 1] = rand2.reshape((n, m)) # add the vectors vect = viewer.add_vectors(pos, width=0.2, length=2.5) print(image.shape, pos.shape)