From cac32cc68efe30e25e717621c0c740e0ee5dbd25 Mon Sep 17 00:00:00 2001 From: Almar Klein Date: Tue, 12 Mar 2024 13:38:56 +0100 Subject: [PATCH 1/2] Remove duplicate code --- .../graphics/_features/_selection_features.py | 131 +----------------- fastplotlib/graphics/_features/_sizes.py | 12 +- .../graphics/selectors/_mesh_positions.py | 1 - .../graphics/selectors/_rectangle_region.py | 10 +- 4 files changed, 18 insertions(+), 136 deletions(-) delete mode 100644 fastplotlib/graphics/selectors/_mesh_positions.py diff --git a/fastplotlib/graphics/_features/_selection_features.py b/fastplotlib/graphics/_features/_selection_features.py index 9a2696f7c..7098739bf 100644 --- a/fastplotlib/graphics/_features/_selection_features.py +++ b/fastplotlib/graphics/_features/_selection_features.py @@ -2,131 +2,10 @@ import numpy as np +from ...utils import mesh_masks from ._base import GraphicFeature, FeatureEvent -""" -positions for indexing the BoxGeometry to set the "width" and "size" of the box -hacky, but I don't think we can morph meshes in pygfx yet: https://github.com/pygfx/pygfx/issues/346 -""" - -x_right = np.array( - [ - True, - True, - True, - True, - False, - False, - False, - False, - False, - True, - False, - True, - True, - False, - True, - False, - False, - True, - False, - True, - True, - False, - True, - False, - ] -) - -x_left = np.array( - [ - False, - False, - False, - False, - True, - True, - True, - True, - True, - False, - True, - False, - False, - True, - False, - True, - True, - False, - True, - False, - False, - True, - False, - True, - ] -) - -y_top = np.array( - [ - False, - True, - False, - True, - False, - True, - False, - True, - True, - True, - True, - True, - False, - False, - False, - False, - False, - False, - True, - True, - False, - False, - True, - True, - ] -) - -y_bottom = np.array( - [ - True, - False, - True, - False, - True, - False, - True, - False, - False, - False, - False, - False, - True, - True, - True, - True, - True, - True, - False, - False, - True, - True, - False, - False, - ] -) - - class LinearSelectionFeature(GraphicFeature): # A bit much to have a class for this but this allows it to integrate with the fastplotlib callback system """ @@ -253,10 +132,10 @@ def _set(self, value: Tuple[float, float]): if self.axis == "x": # change left x position of the fill mesh - self._parent.fill.geometry.positions.data[x_left, 0] = value[0] + self._parent.fill.geometry.positions.data[mesh_masks.x_left, 0] = value[0] # change right x position of the fill mesh - self._parent.fill.geometry.positions.data[x_right, 0] = value[1] + self._parent.fill.geometry.positions.data[mesh_masks.x_right, 0] = value[1] # change x position of the left edge line self._parent.edges[0].geometry.positions.data[:, 0] = value[0] @@ -266,10 +145,10 @@ def _set(self, value: Tuple[float, float]): elif self.axis == "y": # change bottom y position of the fill mesh - self._parent.fill.geometry.positions.data[y_bottom, 1] = value[0] + self._parent.fill.geometry.positions.data[mesh_masks.y_bottom, 1] = value[0] # change top position of the fill mesh - self._parent.fill.geometry.positions.data[y_top, 1] = value[1] + self._parent.fill.geometry.positions.data[mesh_masks.y_top, 1] = value[1] # change y position of the bottom edge line self._parent.edges[0].geometry.positions.data[:, 1] = value[0] diff --git a/fastplotlib/graphics/_features/_sizes.py b/fastplotlib/graphics/_features/_sizes.py index 8fceb8df3..403760508 100644 --- a/fastplotlib/graphics/_features/_sizes.py +++ b/fastplotlib/graphics/_features/_sizes.py @@ -61,8 +61,10 @@ def _fix_sizes(self, sizes, parent): if graphic_type == "ScatterGraphic": sizes = np.array(sizes) else: - raise ValueError(f"Sizes must be an array of shape (n,) where n == the number of data points provided.\ - Received shape={sizes.shape}.") + raise ValueError( + f"Sizes must be an array of shape (n,) where n == the number of data points provided.\ + Received shape={sizes.shape}." + ) return np.array(sizes) @@ -78,8 +80,10 @@ def __setitem__(self, key, value): # numpy will throw errors if it can't broadcast if value.size != self.buffer.data[key].size: - raise ValueError(f"{value.size} is not equal to buffer size {self.buffer.data[key].size}.\ - If you want to set size to a non-scalar value, make sure it's the right length!") + raise ValueError( + f"{value.size} is not equal to buffer size {self.buffer.data[key].size}.\ + If you want to set size to a non-scalar value, make sure it's the right length!" + ) self.buffer.data[key] = value self._update_range(key) diff --git a/fastplotlib/graphics/selectors/_mesh_positions.py b/fastplotlib/graphics/selectors/_mesh_positions.py deleted file mode 100644 index a22b22b17..000000000 --- a/fastplotlib/graphics/selectors/_mesh_positions.py +++ /dev/null @@ -1 +0,0 @@ -import numpy as np diff --git a/fastplotlib/graphics/selectors/_rectangle_region.py b/fastplotlib/graphics/selectors/_rectangle_region.py index a5a9a31cb..7f89b128e 100644 --- a/fastplotlib/graphics/selectors/_rectangle_region.py +++ b/fastplotlib/graphics/selectors/_rectangle_region.py @@ -3,10 +3,10 @@ import pygfx +from ...utils import mesh_masks from .._base import Graphic from .._features import GraphicFeature from ._base_selector import BaseSelector -from ._mesh_positions import x_right, x_left, y_top, y_bottom class RectangleBoundsFeature(GraphicFeature): @@ -58,16 +58,16 @@ def _set(self, value: Tuple[float, float, float, float]): # change fill mesh # change left x position of the fill mesh - self._parent.fill.geometry.positions.data[x_left, 0] = xmin + self._parent.fill.geometry.positions.data[mesh_masks.x_left, 0] = xmin # change right x position of the fill mesh - self._parent.fill.geometry.positions.data[x_right, 0] = xmax + self._parent.fill.geometry.positions.data[mesh_masks.x_right, 0] = xmax # change bottom y position of the fill mesh - self._parent.fill.geometry.positions.data[y_bottom, 1] = ymin + self._parent.fill.geometry.positions.data[mesh_masks.y_bottom, 1] = ymin # change top position of the fill mesh - self._parent.fill.geometry.positions.data[y_top, 1] = ymax + self._parent.fill.geometry.positions.data[mesh_masks.y_top, 1] = ymax # change the edge lines From 1a76d5621bc4d73f1223885fdb494e1fbd380db2 Mon Sep 17 00:00:00 2001 From: Almar Klein Date: Tue, 12 Mar 2024 14:41:04 +0100 Subject: [PATCH 2/2] Ah, even better --- fastplotlib/graphics/_features/_selection_features.py | 8 ++++---- fastplotlib/graphics/selectors/_rectangle_region.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/fastplotlib/graphics/_features/_selection_features.py b/fastplotlib/graphics/_features/_selection_features.py index 7098739bf..294bb15d6 100644 --- a/fastplotlib/graphics/_features/_selection_features.py +++ b/fastplotlib/graphics/_features/_selection_features.py @@ -132,10 +132,10 @@ def _set(self, value: Tuple[float, float]): if self.axis == "x": # change left x position of the fill mesh - self._parent.fill.geometry.positions.data[mesh_masks.x_left, 0] = value[0] + self._parent.fill.geometry.positions.data[mesh_masks.x_left] = value[0] # change right x position of the fill mesh - self._parent.fill.geometry.positions.data[mesh_masks.x_right, 0] = value[1] + self._parent.fill.geometry.positions.data[mesh_masks.x_right] = value[1] # change x position of the left edge line self._parent.edges[0].geometry.positions.data[:, 0] = value[0] @@ -145,10 +145,10 @@ def _set(self, value: Tuple[float, float]): elif self.axis == "y": # change bottom y position of the fill mesh - self._parent.fill.geometry.positions.data[mesh_masks.y_bottom, 1] = value[0] + self._parent.fill.geometry.positions.data[mesh_masks.y_bottom] = value[0] # change top position of the fill mesh - self._parent.fill.geometry.positions.data[mesh_masks.y_top, 1] = value[1] + self._parent.fill.geometry.positions.data[mesh_masks.y_top] = value[1] # change y position of the bottom edge line self._parent.edges[0].geometry.positions.data[:, 1] = value[0] diff --git a/fastplotlib/graphics/selectors/_rectangle_region.py b/fastplotlib/graphics/selectors/_rectangle_region.py index 7f89b128e..0d7dd3661 100644 --- a/fastplotlib/graphics/selectors/_rectangle_region.py +++ b/fastplotlib/graphics/selectors/_rectangle_region.py @@ -58,16 +58,16 @@ def _set(self, value: Tuple[float, float, float, float]): # change fill mesh # change left x position of the fill mesh - self._parent.fill.geometry.positions.data[mesh_masks.x_left, 0] = xmin + self._parent.fill.geometry.positions.data[mesh_masks.x_left] = xmin # change right x position of the fill mesh - self._parent.fill.geometry.positions.data[mesh_masks.x_right, 0] = xmax + self._parent.fill.geometry.positions.data[mesh_masks.x_right] = xmax # change bottom y position of the fill mesh - self._parent.fill.geometry.positions.data[mesh_masks.y_bottom, 1] = ymin + self._parent.fill.geometry.positions.data[mesh_masks.y_bottom] = ymin # change top position of the fill mesh - self._parent.fill.geometry.positions.data[mesh_masks.y_top, 1] = ymax + self._parent.fill.geometry.positions.data[mesh_masks.y_top] = ymax # change the edge lines