Skip to content

Commit bad0aac

Browse files
committed
Adds vectors as lines functionality
1 parent ee2acaa commit bad0aac

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

Diff for: glue_jupyter/bqplot/scatter/layer_artist.py

+65-3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ def __init__(self, view, viewer_state, layer_state=None, layer=None):
128128

129129
# Vectors
130130

131+
self.vector_lines = lines_cls(scales=self.view.scales, x=[0.], y=[0.])
132+
self.vector_lines.colors = [color2hex(self.state.color)]
133+
self.vector_lines.visible = False
134+
131135
self.scale_size_vector = bqplot.LinearScale(min=0, max=1)
132136
self.scale_color_vector = bqplot.ColorScale()
133137
self.scale_rotation_vector = bqplot.LinearScale(min=-np.pi, max=np.pi)
@@ -162,6 +166,7 @@ def __init__(self, view, viewer_state, layer_state=None, layer=None):
162166
self.line_mark_gl,
163167
self.line_mark,
164168
self.vector_mark,
169+
self.vector_lines,
165170
]
166171

167172
def compute_density_map(self, *args, **kwargs):
@@ -253,12 +258,25 @@ def _update_data(self):
253258
self.vector_mark.size = length
254259
self.vector_mark.rotation = angle
255260

261+
# maybe i need vector lines here too?
262+
vector_line_coords = self._build_line_vector_points()
263+
x_vector_coords = vector_line_coords[:,0]
264+
y_vector_coords = vector_line_coords[:,1]
265+
self.vector_lines.x = x_vector_coords
266+
self.vector_lines.y = y_vector_coords
267+
with open('/tmp/debug.log', 'a') as log:
268+
log.write('calculated vector lines coordinates:\n')
269+
log.write(f'x vector coords : {x_vector_coords[:10]}\n')
270+
log.write(f'y vector coords : {y_vector_coords[:10]}\n')
271+
256272
else:
257273

258274
self.vector_mark.x = []
259275
self.vector_mark.y = []
260276

261277
def _update_visual_attributes(self, changed, force=False):
278+
with open('/tmp/debug.log', 'a') as log:
279+
log.write("Updateing visual attributes\n")
262280

263281
if not self.enabled:
264282
return
@@ -345,9 +363,12 @@ def _update_visual_attributes(self, changed, force=False):
345363
and self.state.vx_att is not None
346364
and self.state.vy_att is not None
347365
):
366+
with open('/tmp/debug.log', 'a') as log:
367+
log.write("Vector visible \n")
368+
self.vector_mark.visible = False
369+
self.vector_lines.visible = True
348370

349371
if self.state.cmap_mode == "Fixed":
350-
breakpoint()
351372
if force or "color" in changed or "cmap_mode" in changed:
352373
self.vector_mark.color = None
353374
self.vector_mark.colors = [color2hex(self.state.color)]
@@ -360,7 +381,8 @@ def _update_visual_attributes(self, changed, force=False):
360381
self.scale_color_vector.max = float_or_none(self.state.cmap_vmax)
361382

362383

363-
for mark in [self.scatter_mark, self.line_mark_gl, self.line_mark, self.vector_mark, self.density_mark]:
384+
for mark in [self.scatter_mark, self.line_mark_gl, self.line_mark,
385+
self.vector_mark, self.vector_lines, self.density_mark]:
364386

365387
if mark is None:
366388
continue
@@ -378,7 +400,7 @@ def _update_visual_attributes(self, changed, force=False):
378400
self.line_mark_gl.visible = self.state.visible and self.state.line_visible
379401
self.density_mark.visible = (self.state.visible and self.state.density_map
380402
and self.state.markers_visible)
381-
self.vector_mark.visible = self.state.visible and self.state.vector_visible
403+
self.vector_lines.visible = self.state.visible and self.state.vector_visible
382404

383405
def _update_scatter(self, force=False, **kwargs):
384406

@@ -387,7 +409,9 @@ def _update_scatter(self, force=False, **kwargs):
387409
self.density_mark is None
388410
or self.scatter_mark is None
389411
or self.line_mark_gl is None
412+
# TODO: check which of these are needed
390413
or self.vector_mark is None
414+
or self.vector_lines is None
391415
or self._viewer_state.x_att is None
392416
or self._viewer_state.y_att is None
393417
or self.state.layer is None
@@ -447,3 +471,41 @@ def _update_zorder(self, *args):
447471
for item in (layer.density_mark, layer.scatter_mark, layer.line_mark_gl,
448472
layer.line_mark, layer.vector_mark)
449473
]
474+
475+
def _build_line_vector_points(self):
476+
"""
477+
Function builds an array of coordinate pairs separated by nans for plotting individual lines
478+
to replicate vector behaviour
479+
480+
Returns: np.array
481+
array of structure ([x1, y1], [x2, y2], [np.nan, np.nan])
482+
"""
483+
x = self._viewer_state.x_att
484+
y = self._viewer_state.y_att
485+
vx = self._viewer_state.vx_att
486+
vy = self._viewer_state.vy_att
487+
488+
point_pairs = []
489+
for row in range(0, len(x)):
490+
length = np.sqrt(vx[row]**2 + vy[row]**2)
491+
angle = np.arctan2(vy[row], vx[row])
492+
x_delta = length * np.cos(angle)
493+
y_delta = length * np.sin(angle)
494+
x2 = x[row] + x_delta
495+
y2 = y[row] + y_delta
496+
point_1 = [x[row], y[row]]
497+
point_pairs.append(point_1)
498+
point_2 = [x2[row], y2[row]]
499+
point_pairs.append(point_2)
500+
point_3 = [np.nan, np.nan]
501+
point_pairs.append(point_3)
502+
503+
with open('/tmp/debug.log', 'a') as f:
504+
f.write('Built the vectors \n')
505+
f.write('point pairs : {}\n'.format(point_pairs))
506+
507+
return np.array(point_pairs)
508+
509+
510+
511+

0 commit comments

Comments
 (0)