def show_quiver(pred, sparse=10): arrows = pred[1:].transpose([1, 2, 3, 0])[pred[0] > 0] coord = np.array(np.where(pred[0] > 0)).astype('float').transpose() ipv.figure() ipv.quiver(coord[::sparse, 0], coord[::sparse, 1], coord[::sparse, 2], arrows[::sparse, 0], arrows[::sparse, 1], arrows[::sparse, 2], size=1) ipv.show()
def plot_3D(self, type, fname): x = self.data['XWIN'] y = self.data['YWIN'] oceta = self.data['O-Ceta'] oxi = self.data['O-Cxi'] resids = [(((oceta[i]**2) + (oxi[i]**2))**.5) for i in range(len(oceta))] if type == 'trisurf': z = resids fig = plt.figure() ax = fig.gca(projection='3d') ax.set_xlabel('XWIN') ax.set_ylabel('YWIN') ax.set_zlabel('magnitude of residual') ax.plot_trisurf(x, y, z, cmap='magma') plt.savefig(os.path.join(self.directory, fname + '.png')) plt.show() elif type == 'quiver': x = np.array(x) y = np.array(y) over = self.searchhighestresids(90) #x = [self.data['XWIN'][i] for i in over] #y = [self.data['YWIN'][i] for i in over] z = np.array(resids) u = np.array(self.data['O-Ceta']) v = np.array(self.data['O-Cxi']) w = np.array([0 for i in range(len(v))]) p3.clear() quiver = p3.quiver(x, y, z, u, v, w, size=3) #p3.savefig(os.path.join(self.directory, fname + '.png')) p3.show()
def aniplot(folder, frame): if frame is None: frame = len( fnmatch.filter(os.listdir('/u/yali/' + folder + '/test/output'), '*.hdf5')) x = [] y = [] z = [] vx = [] vy = [] vz = [] v = [] for i in np.arange(frame): fname = str(format(i, '03d')) f = h5py.File("/u/yali/" + folder + "/test/output/snapshot_" + fname + ".hdf5", 'r') # Read-only access to the file #intE = [] #density = [] x.append(f['PartType0']['Coordinates'][:, 0]) y.append(f['PartType0']['Coordinates'][:, 1]) z.append(f['PartType0']['Coordinates'][:, 2]) #intE.append(f['Partype0']['InternalEnergy'][:]) vx.append(f['PartType0']['Velocities'][:, 0]) vy.append(f['PartType0']['Velocities'][:, 1]) vz.append(f['PartType0']['Velocities'][:, 2]) #density.append(f['PartType0']['Density'][:]) # and normalize x = np.array(x) y = np.array(y) z = np.array(z) vx = np.array(vx) vy = np.array(vy) vz = np.array(vz) v = np.array(np.sqrt(vx**2 + vy**2 + vz**2)) v -= v.min() v /= v.max() # map the normalized values to rgba colors cmap = cm.Reds colors = np.array([cmap(k) for k in v]) colors.shape # plot animation fig = ipv.figure() ipv.style.use('dark') # use just rgb colors, not rgba quiver = ipv.quiver(x, y, z, vx, vy, vz, size=5, color=colors[:, :, :3]) # create the animation widgets/slider ipv.animation_control(quiver, interval=500) ipv.show()
def animate_rollout_quiver(positions, vector): """Takes positions & vector(acceleration or velocity) and renders 3d quiver animation""" num_steps = len(positions) num_particles = positions[0].size(0) data = np.zeros((6, num_steps, num_particles)) for s in range(num_steps): pos = positions[s].numpy() v = vector[s].numpy() for i in range(3): data[i][s] = pos[:, i] data[i + 3][s] = v[:, i] fig = ipv.figure() color = [(1, 0, 0) if i < 64 else (0, 0, 1) for i in range(len(pos))] s = ipv.quiver(*data, color=color, size=5) ipv.animation_control(s) ipv.show()
def visualize_objects(self, train_idxs=None, test_idxs=None, max_time_steps=None, train_sim=False, test_sim=True, train_marker_size=4, test_marker_size=6): """ train_idxs - numpy array из индексов объектов (sat_id) тренировочной выборки, которые надо визуализировать. Если None - берем все объекты test_idxs - numpy array из индексов объектов (sat_id) тренировочной выборки, которые надо визуализировать. Если None - берем train_idxs max_time_steps - максимальное количество измерений для одного объекта (sat_id) train_sim - если False - используем реальные данные (колонки без приставки sim) test_sim - если False - используем реальные (предсказанные) данные (для этого в датафрейм нужно добавить приставки колонки с предсказаниями без приставки sim, как в трейне) """ ipv.clear() if train_idxs is None: train_idxs = np.array(self.train_data['sat_id'].unique()) if test_idxs is None: test_idxs = train_idxs if max_time_steps is None: max_time_steps_train = self.train_data.groupby( 'sat_id').count()['epoch'].max() max_time_steps_test = self.test_data.groupby( 'sat_id').count()['epoch'].max() max_time_steps = max(max_time_steps_train, max_time_steps_test) ## подготовка трейна и теста stream_train = self._prepare_stream('train', train_idxs, max_time_steps, train_sim) stream_test = self._prepare_stream('test', test_idxs, max_time_steps, test_sim) ## визуализация stream = np.dstack([stream_train[:, :, :], stream_test[:, :, :]]) selected = stream_train.shape[2] + test_idxs self.q = ipv.quiver(*stream[:, :, :], color="green", color_selected='red', size=train_marker_size, size_selected=test_marker_size, selected=selected) ## Чтобы можно было менять размеры и цвета size = FloatSlider(min=1, max=15, step=0.2) size_selected = FloatSlider(min=1, max=15, step=0.2) color = ColorPicker() color_selected = ColorPicker() jslink((self.q, 'size'), (size, 'value')) jslink((self.q, 'size_selected'), (size_selected, 'value')) jslink((self.q, 'color'), (color, 'value')) jslink((self.q, 'color_selected'), (color_selected, 'value')) # ipv.style.use('seaborn-darkgrid') ipv.animation_control(self.q, interval=75) ipv.show( [VBox([ipv.gcc(), size, size_selected, color, color_selected])])
def quickquiver(x, y, z, u, v, w, **kwargs): ipv.figure() ipv.quiver(x, y, z, u, v, w, **kwargs) return ipv.gcc()
def quickquiver(x, y, z, u, v, w, **kwargs): import ipyvolume as ipv ipv.figure() ipv.quiver(x, y, z, u, v, w, **kwargs) return ipv.gcc()
def scatter_normals( points, normals, labels=None, labels_gt=None, with_normals_errors_only=False, width=800, height=600, cmap=cm.RdBu, cmap_normals=cm.cool, aspect_ratio_preserve=True, axeslim='auto', reorder_colors=True, point_size_value=0.2, vector_size_value=1.0, title=None, ): assert len(points) == len( normals), "Length incorrect. These are not normals of points, may be." points = points.astype(np.float32) normals = normals.astype(np.float32) x = points[:, 0] y = points[:, 1] z = points[:, 2] u = normals[:, 0] v = normals[:, 1] w = normals[:, 2] if labels is None: labels = np.ones(shape=len(points), dtype=np.int) # draw ipv.figure(width=width, height=height) if axeslim == 'auto': if aspect_ratio_preserve: bounds = np.array([(v.min(), v.max()) for v in [x, y, z]]).T widths = bounds[1] - bounds[0] max_width = widths.max() pads = (max_width - widths) / 2 pads = np.vstack([-pads, pads]) axeslim = (bounds + pads).T ipv.xlim(axeslim[0][0], axeslim[0][1]) ipv.ylim(axeslim[1][0], axeslim[1][1]) ipv.zlim(axeslim[2][0], axeslim[2][1]) else: ipv.xlim(x.min(), x.max()) ipv.ylim(y.min(), y.max()) ipv.zlim(z.min(), z.max()) else: ipv.xlim(-axeslim, axeslim) ipv.ylim(-axeslim, axeslim) ipv.zlim(-axeslim, axeslim) # calc point colors colors_seg = cmap(np.linspace(0, 1, labels.max() + 1)) if reorder_colors: colors_seg = reorder_contrast(colors_seg) colors = np.array([colors_seg[label - 1][:3] for label in labels]) sc = ipv.scatter(x, y, z, size=point_size_value, marker="sphere", color=colors) colors_seg = cmap_normals(np.linspace(0, 1, labels.max() + 1)) if reorder_colors: colors_seg = reorder_contrast(colors_seg) colors_normals = np.array([colors_seg[label - 1][:3] for label in labels]) if labels_gt is None: quiver = ipv.quiver(x, y, z, u, v, w, size=vector_size_value, marker="arrow", color=colors_normals) else: if not with_normals_errors_only: quiver = ipv.quiver(x, y, z, u, v, w, size=vector_size_value, marker="arrow", color=colors_normals) # accuracy assert len(labels_gt) == len(labels) accuracy = (labels_gt == labels).sum() / len(labels) print("Accuracy: {}".format(accuracy)) # draw errors points_error = points[(labels_gt != labels)] x = points_error[:, 0] y = points_error[:, 1] z = points_error[:, 2] # normals with errors normals_error = normals[(labels_gt != labels)] u = normals_error[:, 0] v = normals_error[:, 1] w = normals_error[:, 2] sc2 = ipv.scatter(x, y, z, size=point_size_value, marker="sphere", color='red') point_size2 = FloatSlider(min=0, max=2, step=0.02, description='Error point size') jslink((sc2, 'size'), (point_size2, 'value')) if with_normals_errors_only: quiver = ipv.quiver(x, y, z, u, v, w, size=vector_size_value, marker="arrow", color=colors_normals) point_size = FloatSlider(min=0, max=2, step=0.1, description='Point size') vector_size = FloatSlider(min=0, max=5, step=0.1, description='Vector size') jslink((sc, 'size'), (point_size, 'value')) jslink((quiver, 'size'), (vector_size, 'value')) if labels_gt is not None: widget_list = [ipv.gcc(), point_size, point_size2, vector_size] else: widget_list = [ipv.gcc(), point_size, vector_size] if title is not None: widget_list = [Label(title)] + widget_list return display_widgets(widget_list)
def show_mesh(verts, triangles, face_colors=None, face_labels=None, face_cmap=cm.RdBu, face_reorder_colors=True, vertex_colors=None, vertex_labels=None, vertex_cmap=cm.RdBu, vertex_reorder_colors=True, vertex_normals=None, point_size_value=0.5, vector_size_value=0.5, width=800, height=600, axeslim='auto', aspect_ratio_preserve=True, verbose=0): """ vertex_normals - normals of vertices. """ if vertex_normals is not None: assert len(verts) == len( vertex_normals ), "Length incorrect. These are not normals of points, may be." x = verts[:, 0] y = verts[:, 1] z = verts[:, 2] ipv.figure(width=width, height=height) set_axes_lims(verts, axeslim=axeslim, aspect_ratio_preserve=aspect_ratio_preserve) # faces if face_labels is not None: face_color_dict = calc_colors_dict_by_labels( face_labels, cmap=face_cmap, reorder_colors=face_reorder_colors) for label in face_color_dict.keys(): triangles_set = triangles[face_labels == label] color = face_color_dict[label] _ = ipv.plot_trisurf(x, y, z, triangles=triangles_set, color=color) else: if face_colors is None: face_colors = '#f0f0f0' _ = ipv.plot_trisurf(x, y, z, triangles=triangles, color=face_colors) # vertices is_multilabels = isinstance(vertex_labels, (list, tuple)) if is_multilabels: vertex_colors_by_labels = [] for vl in vertex_labels: vertex_colors_by_labels.append( calc_colors_by_labels(vl, cmap=vertex_cmap, reorder_colors=vertex_reorder_colors)) else: vertex_colors_by_labels = calc_colors_by_labels( vertex_labels, cmap=vertex_cmap, reorder_colors=vertex_reorder_colors) vertex_colors_rgb = vertex_colors if vertex_colors_by_labels is None and vertex_colors_rgb is None: vertex_current_colors = 'red' elif vertex_colors_rgb is not None: vertex_current_colors = vertex_colors_rgb elif is_multilabels: vertex_current_colors = vertex_colors_by_labels[0] else: vertex_current_colors = vertex_colors_by_labels sc = ipv.scatter(x, y, z, size=point_size_value, marker='sphere', color=vertex_current_colors) point_size = FloatSlider(min=0, max=2, step=0.1, description='Vertex size') jslink((sc, 'size'), (point_size, 'value')) w_switch_vertex_colors = get_color_switch_widget(vertex_colors_by_labels, vertex_colors_rgb, sc) widget_list = [ipv.gcc(), point_size] # vertex normals if vertex_normals is not None: u = vertex_normals[:, 0] v = vertex_normals[:, 1] w = vertex_normals[:, 2] quiver = ipv.quiver(x, y, z, u, v, w, size=vector_size_value, marker="arrow", color='green') vector_size = FloatSlider(min=0, max=5, step=0.1, description='Nomals size') jslink((quiver, 'size'), (vector_size, 'value')) widget_list.append(vector_size) if w_switch_vertex_colors is not None: widget_list.append(w_switch_vertex_colors) return display_widgets(widget_list)
def scatter(points, labels=None, labels_gt=None, colors=None, cmap=cm.RdBu, reorder_colors=True, normals=None, width=800, height=600, axeslim='auto', aspect_ratio_preserve=True, point_size_value=0.5, vector_size_value=0.5, title=None): if normals is not None: assert len(points) == len( normals ), "Length incorrect. These are not normals of points, may be." points = points.astype(np.float32) x = points[:, 0] y = points[:, 1] z = points[:, 2] # draw ipv.figure(width=width, height=height) set_axes_lims(points, axeslim=axeslim, aspect_ratio_preserve=aspect_ratio_preserve) is_multilabels = isinstance(labels, (list, tuple)) if is_multilabels: colors_by_labels = [] for vl in labels: colors_by_labels.append( calc_colors_by_labels(vl, cmap=cmap, reorder_colors=reorder_colors)) else: colors_by_labels = calc_colors_by_labels(labels, cmap=cmap, reorder_colors=reorder_colors) colors_rgb = colors if colors_by_labels is None and colors_rgb is None: current_colors = 'red' elif colors_rgb is not None: current_colors = colors_rgb elif is_multilabels: current_colors = colors_by_labels[0] else: current_colors = colors_by_labels sc = ipv.scatter(x, y, z, size=point_size_value, marker="sphere", color=current_colors) point_size = FloatSlider(min=0, max=2, step=0.02, description='Point size') jslink((sc, 'size'), (point_size, 'value')) w_switch_colors = get_color_switch_widget(colors_by_labels, colors_rgb, sc) sc_errors, point_size_errors = draw_error_points(points, labels, labels_gt) container = ipv.gcc() fig = container.children[0] widget_list = [fig, point_size] if point_size_errors is not None: widget_list.append(point_size_errors) if w_switch_colors is not None: widget_list.append(w_switch_colors) # vertex normals if normals is not None: u = normals[:, 0] v = normals[:, 1] w = normals[:, 2] quiver = ipv.quiver(x, y, z, u, v, w, size=vector_size_value, marker="arrow", color='green') vector_size = FloatSlider(min=0, max=5, step=0.1, description='Nomals size') jslink((quiver, 'size'), (vector_size, 'value')) widget_list.append(vector_size) if title is not None: widget_list = [Label(title)] + widget_list return display_widgets(widget_list)
# Can also activate in jupyterlab --> Settings -> Enable Extension Manager (I think) import ipywidgets as widgets import numpy as np import ipyvolume as ipv from bokeh.models import ColumnDataSource, HoverTool from bokeh.plotting import figure x, y, z, u, v, w = np.random.random((6, 1000)) * 2 - 1 selected = np.random.randint(0, 1000, 100) ipv.figure() quiver = ipv.quiver(x, y, z, u, v, w, size=5, size_selected=8, selected=selected) from ipywidgets import FloatSlider, ColorPicker, VBox, jslink size = FloatSlider(min=0, max=30, step=0.1) size_selected = FloatSlider(min=0, max=30, step=0.1) color = ColorPicker() color_selected = ColorPicker() jslink((quiver, 'size'), (size, 'value')) jslink((quiver, 'size_selected'), (size_selected, 'value')) jslink((quiver, 'color'), (color, 'value')) jslink((quiver, 'color_selected'), (color_selected, 'value')) VBox([ipv.gcc(), size, size_selected, color, color_selected])