Skip to content

Commit

Permalink
compatibility with numpy2 (#1613)
Browse files Browse the repository at this point in the history
* numpy2

* repl

* cev (#1610)

* fix

* fix

* multiceviche

* fix

* nocevicheincircleci

* manyfixed

* manyfixed

* ola

* po

* po

* fix

* asarray

* complex

* trapz

* noz

* numpy2black

* BICUBIC

* additionalfixes

* po

* fix

* fix

* fix

* fix

* fix

* po

* po

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* Update config.yml (#1615)

* fix

* fix

* fix

* fix

* fix

* Update bench.txt

* Update config.yml

* fix

* Update bench.txt

* Update test_optimizerlib.py

* Update __init__.py
  • Loading branch information
teytaud authored Jun 21, 2024
1 parent 1338ea1 commit c23f204
Show file tree
Hide file tree
Showing 38 changed files with 79 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ commands:
pip install --progress-bar off -U pip setuptools
pip install --progress-bar off -e .[all]
#pip install --progress-bar off --use-deprecated=legacy-resolver -e .[all]
pip install --progress-bar off -U numpy>=1.20.0
#pip install --progress-bar off -U numpy==1.24.0
pip install --progress-bar off -U numpy==1.24.0 matplotlib==2.2.3 pyomo==5.7.1
pip install keras==2.6.0 # issue on Nov 4th 2021
jobs:
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
__all__ = ["optimizers", "families", "callbacks", "p", "typing", "errors", "ops"]


__version__ = "1.0.2"
__version__ = "1.0.3"
7 changes: 4 additions & 3 deletions nevergrad/benchmark/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.legend import Legend
from matplotlib import cm

# from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
import nevergrad.common.typing as tp
from . import utils
Expand Down Expand Up @@ -611,7 +612,7 @@ def __init__(
all_x = [v for vals in optim_vals.values() for v in vals[xaxis]]
try:
all_x = [float(a_) for a_ in all_x]
self._ax.set_xlim([min(all_x), max(all_x)])
self._ax.set_xlim([min(all_x), max(all_x)]) # type: ignore
except TypeError:
print(f"TypeError for minimum or maximum or {all_x}")
self.add_legends(legend_infos)
Expand Down Expand Up @@ -798,7 +799,7 @@ def __init__(self, winrates_df: pd.DataFrame) -> None:
max_cols = 25
self._cax = self._ax.imshow(
100 * np.array(self.winrates)[:, :max_cols],
cmap=cm.seismic,
cmap="seismic",
interpolation="none",
vmin=0,
vmax=100,
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/benchmark/xpbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def create_seed_generator(seed: tp.Optional[int]) -> tp.Iterator[tp.Optional[int
"""
generator = None if seed is None else np.random.RandomState(seed=seed)
while True:
yield None if generator is None else generator.randint(2**32, dtype=np.uint32)
yield None if generator is None else generator.randint(2**32, dtype=np.uint32) # type: ignore


class Experiment:
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/common/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
PathLike = Union[str, Path]
FloatLoss = float
Loss = Union[float, ArrayLike]
BoundValue = Optional[Union[float, int, _np.int_, _np.float_, ArrayLike]]
BoundValue = Optional[Union[float, int, _np.int_, _np.float64, ArrayLike]]


# %% Protocol definitions for executor typing
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/functions/arcoating/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self, nbslab: int = 10, d_ar: int = 400, bounding_method: str = "bo
super().__init__(self._get_minimum_average_reflexion, array)

def _get_minimum_average_reflexion(self, x: np.ndarray) -> float:
x = np.array(x, copy=False).ravel()
x = np.asarray(x).ravel()
assert len(x) == self.dimension, f"Expected dimension {self.dimension}, got {len(x)}"
if np.min(x) < self.epmin or np.max(x) > self.epf: # acceptability
return float("inf")
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/functions/functionlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def process( # pylint: disable=unused-argument
self.random_state.seed(int(hashlib.md5(str(y).encode()).hexdigest(), 16) % 500000)
data2[i] = self.random_state.normal(0.0, 1.0)
data = data2
data = np.array(data, copy=False)
data = np.asarray(data)
output = []
for transform in self._transforms:
output.append(data[transform.indices] if self.only_index_transform else transform(data))
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/functions/gym/multigym.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def get_env_names() -> tp.List[str]:

gym_env_names = []
max_displays = 10
for e in gym.envs.registry.all():
for e in gym.envs.registry.values(): # .all():
try:
assert not any(
x in str(e.id)
Expand Down
4 changes: 3 additions & 1 deletion nevergrad/functions/images/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def __init__(
assert index == 0 # For the moment only 1 target.
# path = os.path.dirname(__file__) + "/headrgb_olivier.png"
path = Path(__file__).with_name("headrgb_olivier.png")
image = PIL.Image.open(path).resize((self.domain_shape[0], self.domain_shape[1]), PIL.Image.BICUBIC)
image = PIL.Image.open(path).resize(
(self.domain_shape[0], self.domain_shape[1])
) # , PIL.Image.BICUBIC)
self.data = np.asarray(image)[:, :, :3] # 4th Channel is pointless here, only 255.
# parametrization
if not with_pgan:
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/functions/images/test_imagelosses.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_l1_loss() -> None:
@pytest.mark.parametrize("loss_name", imagelosses.registry) # type: ignore
def test_consistency_losses_with_oteytaud(loss_name: str) -> None:
path = Path(__file__).with_name("headrgb_olivier.png")
image = PIL.Image.open(path).resize((256, 256), PIL.Image.BICUBIC)
image = PIL.Image.open(path).resize((256, 256)) # , PIL.Image.BICUBIC)
data = np.asarray(image)[:, :, :3] # 4th Channel is pointless here, only 255.

data_flip = np.flip(data, 0).copy() # Copy necessary as some nets do not support negative stride.
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/functions/mlda/problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def apply(self, parameters: tp.ArrayLike) -> np.ndarray:
np.ndarray
transformed data
"""
parameters = np.array(parameters, copy=False)
parameters = np.asarray(parameters)
assert parameters.shape == (10,)
tmp = np.tanh(self._x[:, None] * parameters[None, :3] + parameters[None, 3:6])
tmp *= parameters[None, 6:9]
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/functions/photonics/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def to_array(self, *args: tp.Any, **kwargs: tp.Any) -> np.ndarray:
assert not kwargs
data = np.concatenate(args).T if self._as_tuple else args[0]
assert data.size == self.dimension
return np.array(data, copy=False).ravel()
return np.asarray(data).ravel()

def evaluation_function(self, *recommendations: ng.p.Parameter) -> float:
assert len(recommendations) == 1, "Should not be a pareto set for a singleobjective function"
Expand Down
25 changes: 16 additions & 9 deletions nevergrad/functions/photonics/photonics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
# pylint: disable=blacklisted-name,too-many-locals,too-many-arguments


def trapezoid(a, b): # type: ignore
try:
return np.trapz(a, b) # numpy < 2.0
except: # type: ignore
return np.trapezoid(a, b) # numpy 2.0


def bragg(X: np.ndarray) -> float:
"""
Cost function for the Bragg mirror problem: maximizing the reflection
Expand Down Expand Up @@ -109,8 +116,8 @@ def c_bas(A: np.ndarray, V: np.ndarray, h: float) -> np.ndarray:


def marche(a: float, b: float, p: float, n: int, x: float) -> np.ndarray:
l = np.zeros(n, dtype=np.complex_) # noqa
m = np.zeros(n, dtype=np.complex_)
l = np.zeros(n, dtype=np.complex128) # noqa
m = np.zeros(n, dtype=np.complex128)
tmp = (
1
/ (2 * np.pi * np.arange(1, n))
Expand Down Expand Up @@ -187,7 +194,7 @@ def morpho(X: np.ndarray) -> float:
l = lam / d # noqa
k0 = 2 * np.pi / l
P, V = homogene(k0, 0, pol, 1, n)
S = np.block([[np.zeros([n, n]), np.eye(n, dtype=np.complex_)], [np.eye(n), np.zeros([n, n])]])
S = np.block([[np.zeros([n, n]), np.eye(n, dtype=np.complex128)], [np.eye(n), np.zeros([n, n])]])
for j in range(0, n_motifs):
Pc, Vc = creneau(k0, 0, pol, e2, 1, a[j], n, x0[j])
S = cascade(S, interface(P, Pc))
Expand All @@ -196,7 +203,7 @@ def morpho(X: np.ndarray) -> float:
S = c_bas(S, V, spacers[j])
Pc, Vc = homogene(k0, 0, pol, e2, n)
S = cascade(S, interface(P, Pc))
R = np.zeros(3, dtype=np.float_)
R = np.zeros(3, dtype=np.float64)
for j in range(-1, 2):
R[j] = abs(S[j + nmod, nmod]) ** 2 * np.real(V[j + nmod]) / k0
cost: float = 1 - (R[-1] + R[1]) / 2 + R[0] / 2
Expand All @@ -208,7 +215,7 @@ def morpho(X: np.ndarray) -> float:
P, V = homogene(k0, 0, pol, 1, n)
S = np.block(
[
[np.zeros([n, n], dtype=np.complex_), np.eye(n)],
[np.zeros([n, n], dtype=np.complex128), np.eye(n)],
[np.eye(n), np.zeros([n, n])],
]
)
Expand Down Expand Up @@ -366,8 +373,8 @@ def cf_photosic_reference(X: np.ndarray) -> float:
absorb = absorption(lam, epsilon, mu, type_, hauteur, pol, theta)
scc[k] = solar(lam)
Ab[k] = absorb[len(absorb) - 1]
max_scc = np.trapz(scc, vlam)
j_sc = np.trapz(scc * Ab, vlam)
max_scc = trapezoid(scc, vlam)
j_sc = trapezoid(scc * Ab, vlam)
CE = j_sc / max_scc
cost = 1 - CE
return cost # type: ignore
Expand Down Expand Up @@ -404,8 +411,8 @@ def cf_photosic_realistic(eps_and_d: np.ndarray) -> float:
absorb = absorption(lam, epsilon, mu, type_, hauteur, pol, theta)
scc[k] = solar(lam)
Ab[k] = absorb[len(absorb) - 1]
max_scc = np.trapz(scc, vlam)
j_sc = np.trapz(scc * Ab, vlam)
max_scc = trapezoid(scc, vlam)
j_sc = trapezoid(scc * Ab, vlam)
CE = j_sc / max_scc
cost = 1 - CE
return cost # type: ignore
Expand Down
3 changes: 3 additions & 0 deletions nevergrad/functions/powersystems/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import typing as tp
from math import pi, cos, sin
import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
from nevergrad.parametrization import parameter as p
Expand Down
1 change: 1 addition & 0 deletions nevergrad/functions/pyomo/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import typing as tp
import os
import numpy as np

import pyomo.environ as pyomo
import nevergrad as ng
from . import core
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/functions/stsp/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ def make_plots(self, filename: str = "stsp.png") -> None:
order = self.order
x = self.x
y = self.y
ax.plot((x[o] for o in order), (y[o] for o in order))
ax.plot((x[o] for o in order), (y[o] for o in order)) # type: ignore
plt.savefig(filename)
2 changes: 1 addition & 1 deletion nevergrad/optimization/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def tell(
)
loss = 5.0e20 # sys.float_info.max leads to numerical problems so let us do this.
elif isinstance(loss, (tuple, list, np.ndarray)):
loss = np.array(loss, copy=False, dtype=float).ravel() if len(loss) != 1 else loss[0]
loss = np.asarray(loss, dtype=float).ravel() if len(loss) != 1 else loss[0]
elif not isinstance(loss, np.ndarray):
raise TypeError(
f'"tell" method only supports float values but the passed loss was: {loss} (type: {type(loss)}.'
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/optimization/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __call__(self, optimizer: base.Optimizer, candidate: p.Parameter, loss: tp.F
data["#parents_uids"] = candidate.parents_uids
for name, param in helpers.flatten(candidate, with_containers=False, order=1):
val = param.value
if isinstance(val, (np.float_, np.int_, np.bool_)):
if isinstance(val, (np.float64, np.int_, np.bool_)):
val = val.item()
if inspect.ismethod(val):
val = repr(val.__self__) # show mutation class
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/optimization/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(
if not (inspect.isclass(cls) and issubclass(cls, base.Optimizer)):
raise TypeError('"cls" must be a str, a ConfiguredOptimizer instance, or an Optimizer class')
args = (
ng.p.Array(init=np.array(p.data, dtype=np.float_)).set_bounds(-bound, bound, method="clipping")
ng.p.Array(init=np.array(p.data, dtype=np.float64)).set_bounds(-bound, bound, method="clipping")
for p in self.parameters
) # bounded to avoid overflows
self.optimizer = cls(ng.p.Tuple(*args), budget=None, num_workers=1)
Expand Down
4 changes: 1 addition & 3 deletions nevergrad/optimization/multiobjective/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ def __init__(
no_hypervolume: bool = False,
) -> None:
self._auto_bound = 0
self._upper_bounds = (
np.array([-float("inf")]) if upper_bounds is None else np.array(upper_bounds, copy=False)
)
self._upper_bounds = np.asarray([-float("inf")]) if upper_bounds is None else np.array(upper_bounds)
if upper_bounds is None:
self._auto_bound = auto_bound
# If we are yet to set the upper bounds, or yet to have an add since doing so, _best_volume is -inf.
Expand Down
4 changes: 2 additions & 2 deletions nevergrad/optimization/multiobjective/hypervolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(
self, dimension: int, coordinates: tp.Optional[tp.Union[np.ndarray, tp.List[float]]] = None
) -> None:
self.dimension = dimension
self.coordinates = np.array(coordinates, copy=False)
self.coordinates = np.asarray(coordinates)
self._next: tp.List["VectorNode"] = [self for _ in range(self.dimension)]
self._prev: tp.List["VectorNode"] = [self for _ in range(self.dimension)]
self.dominated_flag = 0
Expand Down Expand Up @@ -185,7 +185,7 @@ class HypervolumeIndicator:
"""

def __init__(self, reference_point: np.ndarray) -> None:
self.reference_point = np.array(reference_point, copy=False)
self.reference_point = np.asarray(reference_point)
self.dimension = self.reference_point.size
self.reference_bounds = np.full(self.dimension, -np.inf)
self._multilist: tp.Optional[VectorLinkedList] = None
Expand Down
8 changes: 4 additions & 4 deletions nevergrad/optimization/optimizerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ def es(self) -> tp.Any: # typing not possible since cmaes not imported :(
x0=(
self.parametrization.sample().get_standardized_data(reference=self.parametrization)
if self._config.random_init
else np.zeros(self.dimension, dtype=np.float_)
else np.zeros(self.dimension, dtype=np.float64)
),
sigma0=self._config.scale * scale_multiplier,
inopts=inopts,
Expand All @@ -765,7 +765,7 @@ def es(self) -> tp.Any: # typing not possible since cmaes not imported :(
"Please install fcmaes (pip install fcmaes) to use FCMA optimizers"
) from e
self._es = cmaes.Cmaes(
x0=np.zeros(self.dimension, dtype=np.float_),
x0=np.zeros(self.dimension, dtype=np.float64),
input_sigma=self._config.scale * scale_multiplier,
popsize=self._popsize,
randn=self._rng.randn,
Expand Down Expand Up @@ -2928,7 +2928,7 @@ def _internal_ask_candidate(self) -> p.Parameter:
else:
x_probe = self.bo.suggest(util) # this is time consuming
x_probe = [x_probe[self._fake_function.key(i)] for i in range(len(x_probe))]
data = self._normalizer.backward(np.array(x_probe, copy=False))
data = self._normalizer.backward(np.asarray(x_probe))
candidate = self.parametrization.spawn_child().set_standardized_data(data)
candidate._meta["x_probe"] = x_probe
return candidate
Expand Down Expand Up @@ -3067,7 +3067,7 @@ def _internal_ask_candidate(self) -> p.Parameter:
candidate = candidate.tolist()
self._buffer = candidate
x_probe = self._buffer.pop()
data = self._normalizer.backward(np.array(x_probe, copy=False))
data = self._normalizer.backward(np.array(x_probe))
candidate = self.parametrization.spawn_child().set_standardized_data(data)
candidate._meta["x_probe"] = x_probe
return candidate
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/optimization/recastlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _optimization_function(
def ax_obj(p):
data = [p["x" + str(i)] for i in range(weakself.dimension)] # type: ignore
if weakself._normalizer:
data = weakself._normalizer.backward(np.asarray(data, dtype=np.float_))
data = weakself._normalizer.backward(np.asarray(data, dtype=np.float64))
return objective_function(data)

while remaining > 0: # try to restart if budget is not elapsed
Expand Down
4 changes: 2 additions & 2 deletions nevergrad/optimization/sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def _internal_sampler(self) -> tp.ArrayLike:
class Rescaler:
def __init__(self, points: tp.Iterable[tp.ArrayLike]) -> None:
iterp = iter(points)
self.sample_mins = np.array(next(iterp), copy=False)
self.sample_mins = np.asarray(next(iterp))
self.sample_maxs = self.sample_mins
for point in iterp:
self.sample_mins = np.minimum(self.sample_mins, point)
Expand All @@ -217,6 +217,6 @@ def __init__(self, points: tp.Iterable[tp.ArrayLike]) -> None:
), f"Non-positive epsilon={self.epsilon} from mins {self.sample_mins} and maxs {self.sample_maxs}"

def apply(self, point: tp.ArrayLike) -> np.ndarray:
point = np.array(point, copy=False)
point = np.asarray(point)
factor = (1 - 2 * self.epsilon) / (self.sample_maxs - self.sample_mins)
return self.epsilon + factor * (point - self.sample_mins) # type: ignore
2 changes: 1 addition & 1 deletion nevergrad/optimization/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _func(x: tp.Any, y: tp.Any, blublu: str, array: tp.Any, multiobjective: bool

def test_log_parameters(tmp_path: Path) -> None:
filepath = tmp_path / "logs.txt"
cases = [0, np.int_(1), np.float_(2.0), np.nan, float("inf"), np.inf]
cases = [0, np.int_(1), np.float64(2.0), np.nan, float("inf"), np.inf]
instrum = ng.p.Instrumentation(
ng.ops.mutations.Translation()(ng.p.Array(shape=(1,))),
ng.p.Scalar(),
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/optimization/test_optimizerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, x0: tp.ArrayLike) -> None:
def __call__(self, x: tp.ArrayLike) -> float:
assert len(self.x0) == len(x)
self.call_times.append(time.time())
return float(np.sum((np.array(x, copy=False) - self.x0) ** 2))
return float(np.sum((np.asarray(x) - self.x0) ** 2))

def get_factors(self) -> tp.Tuple[float, float]:
logdiffs = np.log(np.maximum(1e-15, np.cumsum(np.diff(self.call_times))))
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/optimization/test_special.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, x0: tp.ArrayLike, x1: tp.ArrayLike) -> None:

def __call__(self, x: tp.ArrayLike) -> float:
assert len(self.x0) == len(x)
return float(np.sum(self.x1 * np.cos(np.array(x, copy=False) - self.x0) ** 2))
return float(np.sum(self.x1 * np.cos(np.asarray(x) - self.x0) ** 2))


@pytest.mark.parametrize("dim", [2, 10, 40, 200]) # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions nevergrad/optimization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ def submit(self, fn: tp.Callable[..., tp.Any], *args: tp.Any, **kwargs: tp.Any)


def _tobytes(x: tp.ArrayLike) -> bytes:
x = np.array(x, copy=False) # for compatibility
x = np.asarray(x) # for compatibility
assert x.ndim == 1, f"Input shape: {x.shape}"
assert x.dtype == np.float_, f"Incorrect type {x.dtype} is not float"
assert x.dtype == np.float64, f"Incorrect type {x.dtype} is not float"
return x.tobytes()


Expand Down
2 changes: 1 addition & 1 deletion nevergrad/parametrization/_datalayers.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Modulo(BoundLayer):

def __init__(self, module: tp.Any) -> None:
super().__init__(lower=0, upper=module)
if not isinstance(module, (np.ndarray, np.float_, np.int_, float, int)):
if not isinstance(module, (np.ndarray, np.float64, np.int_, float, int)):
raise TypeError(f"Unsupported type {type(module)} for module")
self._module = module

Expand Down
Loading

0 comments on commit c23f204

Please sign in to comment.