Skip to content

Commit

Permalink
Merge pull request #6850 from ales-erjavec/numpy2.0
Browse files Browse the repository at this point in the history
[ENH] Numpy 2.0 compatibility
  • Loading branch information
markotoplak authored Feb 7, 2025
2 parents 357b3af + 88f99c7 commit 416fd82
Show file tree
Hide file tree
Showing 25 changed files with 37 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Orange/classification/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Pull members from modules to Orange.classification namespace
# pylint: disable=wildcard-import
# pylint: disable=wildcard-import,broad-except

from .base_classification import (ModelClassification as Model,
LearnerClassification as Learner,
Expand All @@ -23,7 +23,7 @@
from .scoringsheet import *
try:
from .catgb import *
except ModuleNotFoundError:
except Exception:
pass
from .gb import *
try:
Expand Down
4 changes: 2 additions & 2 deletions Orange/data/io_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def get_arrays(self) -> Tuple[np.ndarray, np.ndarray,
(self.cols_W, float))
X, Y, M, W = [self._list_into_ndarray(lst, dt) for lst, dt in lists]
if X is None:
X = np.empty((self.data.shape[0], 0), dtype=np.float_)
X = np.empty((self.data.shape[0], 0), dtype=np.float64)
return X, Y, M, W

@staticmethod
Expand All @@ -393,7 +393,7 @@ def _list_into_ndarray(lst: List, dtype=None) -> Optional[np.ndarray]:
if dtype is not None:
array.astype(dtype)
else:
assert array.dtype == np.float_
assert array.dtype == np.float64
return array


Expand Down
2 changes: 1 addition & 1 deletion Orange/data/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def scale(values, min=0, max=1):
"""Return values scaled to [min, max]"""
if len(values) == 0:
return np.array([])
minval = np.float_(bn.nanmin(values))
minval = np.float64(bn.nanmin(values))
ptp = bn.nanmax(values) - minval
if ptp == 0:
return np.clip(values, min, max)
Expand Down
2 changes: 1 addition & 1 deletion Orange/preprocess/_relieff.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ cdef tuple prepare(X, y, is_discrete, contingencies):
is_defined = np.logical_not(np.isnan(y))
X = X[is_defined]
y = y[is_defined]
attr_stats = np.row_stack((np.nanmean(X, 0), np.nanstd(X, 0)))
attr_stats = np.vstack((np.nanmean(X, 0), np.nanstd(X, 0)))
is_discrete = np.asarray(is_discrete, dtype=np.int8)
contingency_tables(X, y, is_discrete, contingencies)
return X, y, attr_stats, is_discrete
Expand Down
2 changes: 1 addition & 1 deletion Orange/preprocess/discretize.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def transform(self, c):
if sp.issparse(c):
return self.digitize(c, self.points)
elif c.size:
return np.where(np.isnan(c), np.NaN, self.digitize(c, self.points))
return np.where(np.isnan(c), np.nan, self.digitize(c, self.points))
else:
return np.array([], dtype=int)

Expand Down
2 changes: 1 addition & 1 deletion Orange/preprocess/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def remove_unused_values(var, data):
if len(unique) == len(var.values):
return var
used_values = [var.values[i] for i in unique]
translation_table = np.array([np.NaN] * len(var.values))
translation_table = np.array([np.nan] * len(var.values))
translation_table[unique] = range(len(used_values))
return DiscreteVariable(var.name, values=used_values, sparse=var.sparse,
compute_value=Lookup(var, translation_table))
Expand Down
2 changes: 1 addition & 1 deletion Orange/regression/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ..classification.simple_tree import *
try:
from .catgb import *
except ModuleNotFoundError:
except Exception:
pass
from .gb import *
try:
Expand Down
2 changes: 1 addition & 1 deletion Orange/tests/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def test_multinomial(self):
def test_nan_columns(self):
data = Orange.data.Table("iris")
with data.unlocked():
data.X[:, (1, 3)] = np.NaN
data.X[:, (1, 3)] = np.nan
lr = LogisticRegressionLearner()
cv = CrossValidation(k=2, store_models=True)
res = cv(data, [lr])
Expand Down
2 changes: 1 addition & 1 deletion Orange/tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def test_preprocessor_chaining(self):
domain = Domain([DiscreteVariable("a", values="01"),
DiscreteVariable("b", values="01")],
DiscreteVariable("y", values="01"))
table = Table.from_list(domain, [[0, 1], [1, np.NaN]], [0, 1])
table = Table.from_list(domain, [[0, 1], [1, np.nan]], [0, 1])
pre1 = Continuize()(Impute()(table))
pre2 = table.transform(pre1.domain)
np.testing.assert_almost_equal(pre1.X, pre2.X)
Expand Down
4 changes: 2 additions & 2 deletions Orange/tests/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ class TestRemoveNaNColumns(unittest.TestCase):
def test_column_filtering(self):
data = Table("iris")
with data.unlocked():
data.X[:, (1, 3)] = np.NaN
data.X[:, (1, 3)] = np.nan

new_data = RemoveNaNColumns()(data)
self.assertEqual(len(new_data.domain.attributes),
len(data.domain.attributes) - 2)

data = Table("iris")
with data.unlocked():
data.X[0, 0] = np.NaN
data.X[0, 0] = np.nan
new_data = RemoveNaNColumns()(data)
self.assertEqual(len(new_data.domain.attributes),
len(data.domain.attributes))
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/data/owselectbydataindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def commit(self):
if self.data_subset and \
not np.intersect1d(subset_ids, self.data.ids).size:
self.Warning.instances_not_matching()
row_sel = np.in1d(self.data.ids, subset_ids)
row_sel = np.isin(self.data.ids, subset_ids)
matching_output = self.data[row_sel]
non_matching_output = self.data[~row_sel]
annotated_output = create_annotated_table(self.data, row_sel)
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/data/owselectrows.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ def commit(self):
filters.negate = True
non_matching_output = filters(self.data)

row_sel = np.in1d(self.data.ids, matching_output.ids)
row_sel = np.isin(self.data.ids, matching_output.ids)
annotated_output = create_annotated_table(self.data, row_sel)

# if hasattr(self.data, "name"):
Expand Down
8 changes: 4 additions & 4 deletions Orange/widgets/data/tests/test_owcontinuize.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,30 @@ def test_one_column_equal_values(self):
def test_one_column_nan_values_normalize_sd(self):
table = Table("iris")
with table.unlocked():
table[:, 2] = np.NaN
table[:, 2] = np.nan
self.send_signal(self.widget.Inputs.data, table)
# Normalize.NormalizeBySD
self.widget.continuous_treatment = 2
self.widget.commit.now()

table = Table("iris")
with table.unlocked():
table[1, 2] = np.NaN
table[1, 2] = np.nan
self.send_signal(self.widget.Inputs.data, table)
self.widget.commit.now()

def test_one_column_nan_values_normalize_span(self):
table = Table("iris")
with table.unlocked():
table[:, 2] = np.NaN
table[:, 2] = np.nan
self.send_signal(self.widget.Inputs.data, table)
# Normalize.NormalizeBySpan
self.widget.continuous_treatment = 1
self.widget.commit.now()

table = Table("iris")
with table.unlocked():
table[1, 2] = np.NaN
table[1, 2] = np.nan
self.send_signal(self.widget.Inputs.data, table)
self.widget.commit.now()

Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/model/tests/test_owlogisticregression.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_target_with_nan(self):
"""
table = Table("iris")
with table.unlocked():
table.Y[:5] = np.NaN
table.Y[:5] = np.nan
self.send_signal(self.widget.Inputs.data, table)
coef1 = self.get_output(self.widget.Outputs.coefficients)
table = table[5:]
Expand Down
1 change: 1 addition & 0 deletions Orange/widgets/tests/test_matplotlib_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

def add_intro(a):
r = "import matplotlib.pyplot as plt\n" + \
"import numpy as np\n" + \
"from numpy import array\n" + \
"plt.clf()"
return r + a
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/visualize/owboxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ def commit(self):
conditions = self._gather_conditions()
if conditions:
selected = Values(conditions, conjunction=False)(self.dataset)
selection = np.in1d(
selection = np.isin(
self.dataset.ids, selected.ids, assume_unique=True).nonzero()[0]
else:
selected, selection = None, []
Expand Down
6 changes: 3 additions & 3 deletions Orange/widgets/visualize/ownomogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ def offset(name, point):
names = list(chain.from_iterable(
[_get_labels(a, lr and lr[i] and lr[i][0] and lr[i][cls_index],
OWNomogram.get_ruler_values(p.min(), p.max(),
scale * p.ptp(), False))
scale * np.ptp(p), False))
for i, a, p in zip(attr_inds, attributes, points)]))
points = list(chain.from_iterable(points))

Expand Down Expand Up @@ -1107,7 +1107,7 @@ def create_main_nomogram(self, attributes, attr_inds, name_items, points,
name_item, attr, self.log_reg_cont_data_extremes[i][cls_index],
self.get_ruler_values(
point.min(), point.max(),
scale_x * point.ptp(), False),
scale_x * np.ptp(point), False),
scale_x, name_offset, - scale_x * min_p)
for i, attr, name_item, point in
zip(attr_inds, attributes, name_items, points)]
Expand Down Expand Up @@ -1151,7 +1151,7 @@ def key(x):
def key(x):
i, attr = x
if attr.is_discrete:
ptp = self.points[i][class_value].ptp()
ptp = np.ptp(self.points[i][class_value])
else:
coef = np.abs(self.log_reg_coeffs_orig[i][class_value]).mean()
ptp = coef * np.ptp(self.log_reg_cont_data_extremes[i][class_value])
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/visualize/owradviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def _manual_move(self, anchor_idx, x, y):
def _send_components_x(self):
components_ = super()._send_components_x()
angle = np.arctan2(*components_[::-1])
return np.row_stack((components_, angle))
return np.vstack((components_, angle))

def _send_components_metas(self):
return np.vstack((super()._send_components_metas(), ["angle"]))
Expand Down
4 changes: 2 additions & 2 deletions Orange/widgets/visualize/owsilhouetteplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,15 +1074,15 @@ def __itemDataAtPos(self, pos) -> Optional[Tuple[namespace, int, QRectF]]:
def __selectionChanged(self, selected, deselected):
for item, grp in zip(self.__plotItems(), self.__groups):
select = np.flatnonzero(
np.in1d(grp.indices, selected, assume_unique=True))
np.isin(grp.indices, selected, assume_unique=True))
items = item.items()
if select.size:
for i in select:
color = np.hstack((grp.color, np.array([130])))
items[i].setBrush(QBrush(QColor(*color)))

deselect = np.flatnonzero(
np.in1d(grp.indices, deselected, assume_unique=True))
np.isin(grp.indices, deselected, assume_unique=True))
if deselect.size:
for i in deselect:
items[i].setBrush(QBrush(QColor(*grp.color)))
Expand Down
6 changes: 3 additions & 3 deletions Orange/widgets/visualize/tests/test_owmosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,9 @@ def test_nan_column(self):
Domain(
[ContinuousVariable("a"), ContinuousVariable("b"), ContinuousVariable("c")]),
np.array([
[0, np.NaN, 0],
[0, np.NaN, 0],
[0, np.NaN, 0]
[0, np.nan, 0],
[0, np.nan, 0],
[0, np.nan, 0]
])
)
self.send_signal(self.widget.Inputs.data, table)
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/visualize/tests/test_owsilhouetteplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test_bad_data_range(self):
Silhouette Plot now sets axis range properly.
GH-2377
"""
nan = np.NaN
nan = np.nan
table = Table.from_list(
Domain(
[ContinuousVariable("a"), ContinuousVariable("b"), ContinuousVariable("c")],
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/visualize/utils/heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ def selectRows(self, selection: Sequence[int]):
indices = np.hstack([r.normalized_indices for r in self.parts.rows])
else:
indices = []
condition = np.in1d(indices, selection)
condition = np.isin(indices, selection)
visual_indices = np.flatnonzero(condition)
self.__selection_manager.select_rows(visual_indices.tolist())

Expand Down
5 changes: 2 additions & 3 deletions conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ requirements:
- python
- pip
- cython
- numpy
- numpy >= 2
- recommonmark
- setuptools
- sphinx >=4.2.0,<8
Expand All @@ -43,14 +43,13 @@ requirements:
# core requirements
- baycomp >=1.0.2
- bottleneck >=1.3.4
- catboost >=1.0.1
- chardet >=3.0.2
- httpx >=0.21
- joblib >=1.2.0
- keyring
- keyrings.alt
- networkx
- numpy >=1.20.0,<2
- numpy >=1.20.0
- openpyxl >=3.1.3
- openTSNE >=0.6.1,!=0.7.0
- pandas >=1.4.0,!=1.5.0,!=2.0.0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build-system]
requires = [
"cython>=3.0",
"oldest-supported-numpy",
"numpy>=2.0",
"recommonmark",
"setuptools>=51.0",
"sphinx",
Expand Down
3 changes: 1 addition & 2 deletions requirements-core.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
baycomp>=1.0.2
bottleneck>=1.3.4
catboost>=1.0.1
# Encoding detection
chardet>=3.0.2
httpx>=0.21.0
Expand All @@ -9,7 +8,7 @@ joblib>=1.2.0
keyring
keyrings.alt # for alternative keyring implementations
networkx
numpy>=1.20.0,<2
numpy>=1.20.0
openpyxl>=3.1.3
openTSNE>=0.6.1,!=0.7.0 # 0.7.0 segfaults
packaging
Expand Down

0 comments on commit 416fd82

Please sign in to comment.