From 57702ac398e61f58a86f4cfd2965ed48a7f9ab95 Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Mon, 17 Jun 2024 20:48:38 -0400 Subject: [PATCH 1/2] ndarray.ptp -> np.ptp for numpy v2 --- fastplotlib/widgets/histogram_lut.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastplotlib/widgets/histogram_lut.py b/fastplotlib/widgets/histogram_lut.py index a3edffcbd..02c21aa38 100644 --- a/fastplotlib/widgets/histogram_lut.py +++ b/fastplotlib/widgets/histogram_lut.py @@ -163,7 +163,7 @@ def _calculate_histogram(self, data): # used if data ptp <= 10 because event things get weird # with tiny world objects due to floating point error # so if ptp <= 10, scale up by a factor - self._scale_factor: int = max(1, 100 * int(10 / data_ss.ptp())) + self._scale_factor: int = max(1, 100 * int(10 / np.ptp(data_ss))) edges = edges * self._scale_factor From 4c0022ad3817fc0f2b6993f393921d6e95a0c689 Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Tue, 17 Sep 2024 11:54:00 -0400 Subject: [PATCH 2/2] fix rectangle selector get_selected_data() for images --- fastplotlib/graphics/selectors/_rectangle.py | 37 ++++++++++---------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/fastplotlib/graphics/selectors/_rectangle.py b/fastplotlib/graphics/selectors/_rectangle.py index 38bb0d2f9..02e5ec6b3 100644 --- a/fastplotlib/graphics/selectors/_rectangle.py +++ b/fastplotlib/graphics/selectors/_rectangle.py @@ -18,7 +18,7 @@ def parent(self) -> Graphic | None: return self._parent @property - def selection(self) -> Sequence[float] | List[Sequence[float]]: + def selection(self) -> np.ndarray[float]: """ (xmin, xmax, ymin, ymax) of the rectangle selection """ @@ -319,10 +319,11 @@ def get_selected_data( # do not need to check for mode for images, because the selector is bounded by the image shape # will always be `full` if "Image" in source.__class__.__name__: - col_ixs = slice(ixs[0][0], ixs[0][-1] + 1) - row_ixs = slice(ixs[1][0], ixs[1][-1] + 1) + row_ixs, col_ixs = ixs + row_slice = slice(row_ixs[0], row_ixs[-1] + 1) + col_slice = slice(col_ixs[0], col_ixs[-1] + 1) - return source.data[row_ixs, col_ixs] + return source.data[row_slice, col_slice] if mode not in ["full", "partial", "ignore"]: raise ValueError( @@ -414,7 +415,7 @@ def get_selected_data( def get_selected_indices( self, graphic: Graphic = None - ) -> Union[np.ndarray, List[np.ndarray]]: + ) -> np.ndarray | tuple[np.ndarray]: """ Returns the indices of the ``Graphic`` data bounded by the current selection. @@ -429,7 +430,7 @@ def get_selected_indices( ------- Union[np.ndarray, List[np.ndarray]] data indicies of the selection - | list of [x_indices_array, y_indices_array] if the graphic is an image + | tuple of [row_indices, y_indices_array] if the graphic is an image | list of indices along the x-dimension for each line if graphic is a line collection | array of indices along the x-dimension if graphic is a line """ @@ -437,14 +438,14 @@ def get_selected_indices( source = self._get_source(graphic) # selector (xmin, xmax, ymin, ymax) values - bounds = self.selection + xmin, xmax, ymin, ymax = self.selection # image data does not need to check for mode because the selector is always bounded # to the image if "Image" in source.__class__.__name__: - xs = np.arange(bounds[0], bounds[1], dtype=int) - ys = np.arange(bounds[2], bounds[3], dtype=int) - return [xs, ys] + col_ixs = np.arange(xmin, xmax, dtype=int) + row_ixs = np.arange(ymin, ymax, dtype=int) + return row_ixs, col_ixs if "Line" in source.__class__.__name__: if isinstance(source, GraphicCollection): @@ -452,20 +453,20 @@ def get_selected_indices( for g in source.graphics: data = g.data.value g_ixs = np.where( - (data[:, 0] >= bounds[0] - g.offset[0]) - & (data[:, 0] <= bounds[1] - g.offset[0]) - & (data[:, 1] >= bounds[2] - g.offset[1]) - & (data[:, 1] <= bounds[3] - g.offset[1]) + (data[:, 0] >= xmin - g.offset[0]) + & (data[:, 0] <= xmax - g.offset[0]) + & (data[:, 1] >= ymin - g.offset[1]) + & (data[:, 1] <= ymax - g.offset[1]) )[0] ixs.append(g_ixs) else: # map only this graphic data = source.data.value ixs = np.where( - (data[:, 0] >= bounds[0]) - & (data[:, 0] <= bounds[1]) - & (data[:, 1] >= bounds[2]) - & (data[:, 1] <= bounds[3]) + (data[:, 0] >= xmin) + & (data[:, 0] <= xmax) + & (data[:, 1] >= ymin) + & (data[:, 1] <= ymax) )[0] return ixs