From 072bbc813dca59da4765b109c3d9fd2776576099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Sok=C3=B3=C5=82?= Date: Wed, 8 Nov 2023 23:56:48 +0100 Subject: [PATCH 1/2] REFACTOR: Adhere to NumPy 2.0 changes --- src/pytools/data/_linkage.py | 2 +- src/pytools/data/_matrix.py | 14 ++++++------ src/pytools/viz/_matplot.py | 6 ++--- src/pytools/viz/dendrogram/_style.py | 4 ++-- src/pytools/viz/matrix/_matrix.py | 22 +++++++++---------- src/pytools/viz/matrix/base/_base.py | 4 ++-- .../pytools/viz/dendrogram/test_dendrogram.py | 4 ++-- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/pytools/data/_linkage.py b/src/pytools/data/_linkage.py index 5038cf45f..1a8166b10 100644 --- a/src/pytools/data/_linkage.py +++ b/src/pytools/data/_linkage.py @@ -30,7 +30,7 @@ # Type variables # -LinkageMatrix = npt.NDArray[np.float_] +LinkageMatrix = npt.NDArray[np.float64] # diff --git a/src/pytools/data/_matrix.py b/src/pytools/data/_matrix.py index f549adc00..1b7a47c48 100644 --- a/src/pytools/data/_matrix.py +++ b/src/pytools/data/_matrix.py @@ -57,7 +57,7 @@ class Matrix(HasExpressionRepr, Generic[T_Number]): names: Tuple[Optional[npt.NDArray[Any]], Optional[npt.NDArray[Any]]] #: the weights of the rows and columns - weights: Tuple[Optional[npt.NDArray[np.float_]], Optional[npt.NDArray[np.float_]]] + weights: Tuple[Optional[npt.NDArray[np.float64]], Optional[npt.NDArray[np.float64]]] #: the labels for the row and column axes name_labels: Tuple[Optional[str], Optional[str]] @@ -155,8 +155,8 @@ def _arg_to_array( else: def _ensure_positive( - w: Optional[npt.NDArray[np.float_]], axis: int - ) -> Optional[npt.NDArray[np.float_]]: + w: Optional[npt.NDArray[np.float64]], axis: int + ) -> Optional[npt.NDArray[np.float64]]: if w is not None and (w < 0).any(): raise ValueError( f"arg weights[{axis}] should be all positive, " @@ -356,7 +356,7 @@ def _message(error: str) -> str: def _top_items_mask( - weights: Optional[npt.NDArray[np.float_]], + weights: Optional[npt.NDArray[np.float64]], current_size: int, target_size: Tuple[Optional[int], Optional[float]], ) -> npt.NDArray[np.bool_]: @@ -389,7 +389,7 @@ def _top_items_mask( # THe target weight is expressed as a ratio of total weight # (0 < target_ratio <= 1). - weights_sorted_cumsum: npt.NDArray[np.float_] = weights[ + weights_sorted_cumsum: npt.NDArray[np.float64] = weights[ ix_weights_descending_stable ].cumsum() mask[ @@ -405,12 +405,12 @@ def _top_items_mask( def _resize_rows( values: npt.NDArray[T_Number], - weights: Optional[npt.NDArray[np.float_]], + weights: Optional[npt.NDArray[np.float64]], names: Optional[npt.NDArray[Any]], current_size: int, target_size: Tuple[Optional[int], Optional[float]], ) -> Tuple[ - npt.NDArray[T_Number], Optional[npt.NDArray[np.float_]], Optional[npt.NDArray[Any]] + npt.NDArray[T_Number], Optional[npt.NDArray[np.float64]], Optional[npt.NDArray[Any]] ]: mask = _top_items_mask( weights=weights, current_size=current_size, target_size=target_size diff --git a/src/pytools/viz/_matplot.py b/src/pytools/viz/_matplot.py index 0e9385643..b4eb59596 100644 --- a/src/pytools/viz/_matplot.py +++ b/src/pytools/viz/_matplot.py @@ -286,13 +286,13 @@ def color_for_value(self, z: Union[int, float]) -> RgbaColor: pass @overload - def color_for_value(self, z: npt.NDArray[np.float_]) -> npt.NDArray[np.float_]: + def color_for_value(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: """[overload]""" pass def color_for_value( - self, z: Union[int, float, npt.NDArray[np.float_]] - ) -> Union[RgbaColor, npt.NDArray[np.float_]]: + self, z: Union[int, float, npt.NDArray[np.float64]] + ) -> Union[RgbaColor, npt.NDArray[np.float64]]: """ Get the color(s) associated with the given value(s), based on the color map and normalization defined for this style. diff --git a/src/pytools/viz/dendrogram/_style.py b/src/pytools/viz/dendrogram/_style.py index 1dbe28f4a..df46b99d1 100644 --- a/src/pytools/viz/dendrogram/_style.py +++ b/src/pytools/viz/dendrogram/_style.py @@ -222,7 +222,7 @@ def draw_leaf_labels( def _get_ytick_locations( self, *, weights: Sequence[float] - ) -> npt.NDArray[np.float_]: + ) -> npt.NDArray[np.float64]: """ Get the tick locations for the y axis. @@ -231,7 +231,7 @@ def _get_ytick_locations( """ weights_array = np.array(weights) # noinspection PyTypeChecker - ytick_locations: npt.NDArray[np.float_] = -( + ytick_locations: npt.NDArray[np.float64] = -( np.arange(len(weights)) * self.padding + weights_array.cumsum() - weights_array / 2 diff --git a/src/pytools/viz/matrix/_matrix.py b/src/pytools/viz/matrix/_matrix.py index 47d96a4f1..db660c76d 100644 --- a/src/pytools/viz/matrix/_matrix.py +++ b/src/pytools/viz/matrix/_matrix.py @@ -168,16 +168,16 @@ def draw_matrix( Optional[npt.NDArray[Any]], ], weights: Tuple[ - Optional[npt.NDArray[np.float_]], - Optional[npt.NDArray[np.float_]], + Optional[npt.NDArray[np.float64]], + Optional[npt.NDArray[np.float64]], ], ) -> None: """[see superclass]""" ax: Axes = self.ax colors = self.colors - weights_rows: npt.NDArray[np.float_] - weights_columns: npt.NDArray[np.float_] + weights_rows: npt.NDArray[np.float64] + weights_columns: npt.NDArray[np.float64] # replace undefined weights with all ones weights_rows, weights_columns = tuple( np.ones(n) if w is None else w for w, n in zip(weights, data.shape) @@ -185,8 +185,8 @@ def draw_matrix( # calculate the horizontal and vertical matrix cell bounds based on the # cumulative sums of the axis weights; default all weights to 1 if not defined - column_bounds: npt.NDArray[np.float_] - row_bounds: npt.NDArray[np.float_] + column_bounds: npt.NDArray[np.float64] + row_bounds: npt.NDArray[np.float64] row_bounds = -np.array([0, *weights_rows]).cumsum() column_bounds = np.array([0, *weights_columns]).cumsum() @@ -213,7 +213,7 @@ def draw_matrix( # draw the matrix cells for c, (x0, x1) in enumerate(zip(column_bounds, column_bounds[1:])): for r, (y1, y0) in enumerate(zip(row_bounds, row_bounds[1:])): - color: npt.NDArray[np.float_] = cell_colors[r, c] + color: npt.NDArray[np.float64] = cell_colors[r, c] ax.add_patch( Rectangle( ( @@ -237,7 +237,7 @@ def draw_matrix( y_tick_locations = (row_bounds[:-1] + row_bounds[1:]) / 2 def _set_ticks( - tick_locations: npt.NDArray[np.float_], + tick_locations: npt.NDArray[np.float64], tick_labels: npt.NDArray[Any], axis: Axis, tick_params: Dict[str, Any], @@ -474,15 +474,15 @@ def draw_matrix( Optional[npt.NDArray[Any]], ], weights: Tuple[ - Optional[npt.NDArray[np.float_]], - Optional[npt.NDArray[np.float_]], + Optional[npt.NDArray[np.float64]], + Optional[npt.NDArray[np.float64]], ], ) -> None: """[see superclass]""" def _axis_marks( axis_names: Optional[npt.NDArray[Any]], - axis_weights: Optional[npt.NDArray[np.float_]], + axis_weights: Optional[npt.NDArray[np.float64]], ) -> Optional[Iterable[str]]: axis_names_iter: Iterable[Any] diff --git a/src/pytools/viz/matrix/base/_base.py b/src/pytools/viz/matrix/base/_base.py index 49ef75613..328870464 100644 --- a/src/pytools/viz/matrix/base/_base.py +++ b/src/pytools/viz/matrix/base/_base.py @@ -67,8 +67,8 @@ def draw_matrix( Optional[npt.NDArray[Any]], ], weights: Tuple[ - Optional[npt.NDArray[np.float_]], - Optional[npt.NDArray[np.float_]], + Optional[npt.NDArray[np.float64]], + Optional[npt.NDArray[np.float64]], ], ) -> None: """ diff --git a/test/test/pytools/viz/dendrogram/test_dendrogram.py b/test/test/pytools/viz/dendrogram/test_dendrogram.py index b65209cd1..e4cb1e521 100644 --- a/test/test/pytools/viz/dendrogram/test_dendrogram.py +++ b/test/test/pytools/viz/dendrogram/test_dendrogram.py @@ -25,7 +25,7 @@ def linkage_matrix() -> npt.NDArray[np.int_]: @pytest.fixture # type: ignore -def linkage_tree(linkage_matrix: npt.NDArray[np.float_]) -> LinkageTree: +def linkage_tree(linkage_matrix: npt.NDArray[np.float64]) -> LinkageTree: """Create a linkage tree for drawing tests.""" return LinkageTree( scipy_linkage_matrix=linkage_matrix, @@ -34,7 +34,7 @@ def linkage_tree(linkage_matrix: npt.NDArray[np.float_]) -> LinkageTree: ) -def test_dendrogram_drawer_text(linkage_matrix: npt.NDArray[np.float_]) -> None: +def test_dendrogram_drawer_text(linkage_matrix: npt.NDArray[np.float64]) -> None: leaf_names = list("ABCDEFGH") leaf_weights = [(w + 1) / 36 for w in range(8)] From 457e4dbdc82ae5db69dd3e61ab5393e9d35bb270 Mon Sep 17 00:00:00 2001 From: j-ittner Date: Thu, 4 Jul 2024 18:28:07 +0200 Subject: [PATCH 2/2] BUILD: update dependencies to support NumPy 2 --- .pre-commit-config.yaml | 2 +- azure-pipelines.yml | 2 +- environment.yml | 2 +- pyproject.toml | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cfb4ef8bf..75e7c27c6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -50,6 +50,6 @@ repos: language_version: python310 pass_filenames: false additional_dependencies: - - numpy~=1.24 + - numpy~=2.0 - pytest - packaging diff --git a/azure-pipelines.yml b/azure-pipelines.yml index e1b6aaa8f..939c73cfd 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -89,7 +89,7 @@ stages: - script: | # package dependencies for mypy dependencies=( - numpy~=1.24 + numpy~=2.0 packaging pytest ) diff --git a/environment.yml b/environment.yml index ae2a1280d..317b54662 100644 --- a/environment.yml +++ b/environment.yml @@ -5,7 +5,7 @@ dependencies: # run - joblib ~= 1.2 - matplotlib ~= 3.6 - - numpy ~= 1.24 + - numpy ~= 2.0 - pandas ~= 2.0 - python ~= 3.10.14 - scipy ~= 1.10 diff --git a/pyproject.toml b/pyproject.toml index 8e7ad8b56..7dd5ad8d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,7 @@ license = "Apache Software License v2.0" requires = [ "joblib ~=1.0", "matplotlib ~=3.6", - "numpy >=1.23,<2a", # cannot use ~= due to conda bug + "numpy >=1.23,<3a", # cannot use ~= due to conda bug "pandas >=1.5", "scipy ~=1.9", "typing_inspect ~=0.7", @@ -80,7 +80,7 @@ typing_extensions = "~=4.0.0" # maximum requirements of gamma-pytools joblib = "~=1.3" matplotlib = "~=3.8" -numpy = ">=1.26,<2a" # cannot use ~= due to conda bug +numpy = ">=2,<3a" # cannot use ~= due to conda bug pandas = "~=2.0" python = ">=3.12,<4a" # cannot use ~= due to conda bug scipy = "~=1.12"