Hi,
I am new to npz format and I don’t know how to plot data or visualize it
can someone please help me out. Any help will be appreciated
thanks
npz is a compressed format which has numpy arrays stored as ‘keys’. The data can be loaded and slices of the cubic volume can be (quickly) plotted with:
import numpy as np
import matplotlib.pyplot as plt
training_data = np.load('data_train.npy', allow_pickle=True, mmap_mode='r')
training_data = training_data['data']
plt.imshow(training_data[:,:,0])
plt.show()
1 Like
Hi, just simple volume viewer, enjoy
#@title viewer
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from ipywidgets.widgets import *
from ipywidgets import widgets
from IPython.display import display
import matplotlib as mpl
plt.style.use('seaborn-white')
class SliceViewer():
'''
Arguments:
traces_volume - np.array with shape (nz, nx, ny)
labels_volume - np.array with shape (nz, nx, ny)
figsize - tuple, plot size
'''
def __init__(self, traces_volume, labels_volume,
figsize=(7, 7), tr_cmap='gray', lb_cmap='Greens'):
assert traces_volume.shape == labels_volume.shape, 'Shapes must be equal'
self.tr_volume = traces_volume
self.lb_volume = labels_volume
self.figsize = figsize
self.tr_cmap = tr_cmap
self.lb_cmap = lb_cmap
self.nz, self.nx, self.ny = self.tr_volume.shape
self.widget = self.__create_widget()
def __create_widget(self):
x_slider = IntSlider(min=0, max=self.nx - 1, step=1, value=int(self.nx/2))
y_slider = IntSlider(min=0, max=self.ny - 1, step=1, value=int(self.ny/2))
z_slider = IntSlider(min=0, max=self.nz - 1, step=1, value=int(self.nz/2))
alpha = IntSlider(min=0, max=100, step=5, value=70)
selector = ['z','x','y']
w = interact(self.__show, x=x_slider, y=y_slider,
z=z_slider, alpha=alpha, dim=selector)
return w
def __get_slice(self, x, y, z, dim):
if dim == 'x':
tr = self.tr_volume[:, x, :]
lb = self.lb_volume[:, x, :]
elif dim == 'y':
tr = self.tr_volume[:, :, y]
lb = self.lb_volume[:, :, y]
else:
tr = self.tr_volume[z, :, :]
lb = self.lb_volume[z, :, :]
return tr, lb
def __set_labels(self, ax, dim):
if dim == 'x':
ax.set_xlabel('y')
ax.set_ylabel('z')
elif dim == 'y':
ax.set_xlabel('x')
ax.set_ylabel('z')
else:
ax.set_xlabel('y')
ax.set_ylabel('x')
def __show(self, x, y, z, alpha, dim):
tr, lb = self.__get_slice(x, y, z, dim)
fig, ax = plt.subplots(figsize=self.figsize)
plt.imshow(tr, cmap=self.tr_cmap, aspect='equal')
cmap = mpl.colors.ListedColormap(['red', 'green', 'blue', 'cyan', 'yellow', 'orange'])
plt.imshow(lb, alpha=(100-alpha)/100, aspect='equal', cmap=cmap)
self.__set_labels(ax, dim)
plt.grid()
plt.show()
display()
SliceViewer(train_dataset, train_labels, figsize=(12, 12))