Skip to content

Commit

Permalink
[cocopp] support numpy 2.0
Browse files Browse the repository at this point in the history
addressing #2306
  • Loading branch information
nikohansen committed Jun 26, 2024
1 parent 3882ef6 commit af9e249
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion code-postprocessing/cocopp/bestalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ def extractBestAlgorithms(args=algs2009, f_factor=2,

# add second best or all algorithms that have an ERT
# within a factor of f_factor of the best:
secondbest_ERT = np.infty
secondbest_ERT = np.inf
secondbest_str = ''
secondbest_included = False
for astring in j:
Expand Down
17 changes: 8 additions & 9 deletions code-postprocessing/cocopp/pproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _DataSet_complement_data(self, step=10**0.2, final_target=1e-8):
newdat = []
warnings.warn("implementation has changed and was never used")
nb_columns = self._evals.shape[1]
self._evals = np.array(self._evals, copy=False)
self._evals = np.asarray(self._evals)
for i in range(len(self._evals) - 1):
newdat.append(self._evals[i])
target = self._evals[i][0] / step
Expand Down Expand Up @@ -413,7 +413,7 @@ def __call__(self, fun_dim=None, discretize=None):

if len(targets) > 1 and targets[-1] >= targets[-2] and self.force_different_targets_factor > 1 and targets[-1] > self.smallest_target:
targets[-1] = targets[-2] / self.force_different_targets_factor
targets = np.array(targets, copy=False)
targets = np.asarray(targets)
targets[targets < self.smallest_target] = self.smallest_target

# a few more sanity checks
Expand Down Expand Up @@ -445,7 +445,7 @@ def __call__(self, fun_dim=None, discretize=None):
if (len(targets) > 1 and targets[-1] >= targets[-2] and
self.force_different_targets_factor > 1):
targets[-1] = targets[-2] / self.force_different_targets_factor
targets = np.array(targets, copy=False)
targets = np.asarray(targets)
targets[targets < self.smallest_target] = self.smallest_target

# a few more sanity checks
Expand Down Expand Up @@ -1569,7 +1569,7 @@ def detAverageEvals(self, targets):
idxnan = np.isnan(evalrow)
evalsums.append(sum(evalrow[idxnan==False]) + sum(self.maxevals[idxnan]))

averages = np.array(evalsums, copy=False) / self.nbRuns()
averages = np.asarray(evalsums) / self.nbRuns()

if do_assertion:
assert all([(ert == np.inf and ps == 0) or toolsdivers.equals_approximately(ert, averages[i] / ps)
Expand Down Expand Up @@ -2914,9 +2914,8 @@ def run_length_distributions(self, dimension, target_values,
raise ValueError('reference_value is not finite')
# a possible solution would be to set ``val = 1``
reference_scores[ds.funcId] = \
np.array([data_per_target * [val]
for val in reference_scores[ds.funcId]],
copy=False)
np.asarray([data_per_target * [val]
for val in reference_scores[ds.funcId]])
for i, line in enumerate(reference_scores[ds.funcId]):
reference_scores[ds.funcId][i] = \
np.sort(np.asarray(line)[toolsstats.randint_derandomized(0, len(line), data_per_target)])
Expand Down Expand Up @@ -3088,8 +3087,8 @@ def det_best_data_lines(self, target_values, fct, dim,
if scoring_function is None or scoring_function == 'ERT':
current_scores = ds.detERT(target_values)
else:
current_scores = np.array([scoring_function(d)
for d in current_lines], copy=False)
current_scores = np.asarray([scoring_function(d)
for d in current_lines])
assert len(current_lines) == len(best_lines)
assert len(current_lines) == len(current_scores) \
== len(best_scores) == len(best_lines)
Expand Down
20 changes: 10 additions & 10 deletions code-postprocessing/cocopp/toolsstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def fix_data_number(data, ndata=15,
return data


def sp1(data, maxvalue=np.Inf, issuccessful=None):
def sp1(data, maxvalue=np.inf, issuccessful=None):
"""sp1(data, maxvalue=Inf, issuccessful=None) computes a
mean value over successful entries in data divided by
success rate, the so-called SP1
Expand All @@ -79,7 +79,7 @@ def sp1(data, maxvalue=np.Inf, issuccessful=None):
Returns: (SP1, success_rate, nb_of_successful_entries), where
SP1 is the mean over successful entries in data divided
by the success rate. SP1 equals np.Inf when the success
by the success rate. SP1 equals np.inf when the success
rate is zero.
"""

Expand Down Expand Up @@ -111,11 +111,11 @@ def sp1(data, maxvalue=np.Inf, issuccessful=None):

# return
if succ == 0:
return (np.Inf, 0., 0)
return (np.inf, 0., 0)
else:
return (np.mean(dat) / succ, succ, len(dat))

def sp(data, maxvalue=np.Inf, issuccessful=None, allowinf=True):
def sp(data, maxvalue=np.inf, issuccessful=None, allowinf=True):
"""sp(data, issuccessful=None) computes the sum of the function
evaluations over all runs divided by the number of success,
the so-called success performance which estimates the expected
Expand Down Expand Up @@ -245,7 +245,7 @@ def drawSP(runlengths_succ, runlengths_unsucc, percentiles,

if Nsucc == 0:
raise NotImplementedError('this code has been removed as it was not clear whether it makes sense')
# return (np.Inf*np.array(percentiles), )
# return (np.inf*np.array(percentiles), )
# TODO: the following line does not work because of the use of function sum which interface is different than that of sp or sp1
return (draw(runlengths_unsucc, percentiles, samplesize=samplesize,
func=sum
Expand Down Expand Up @@ -477,7 +477,7 @@ def prctile(x, arrprctiles, issorted=False, ignore_nan=True):
prctiles
.. note::
treats np.Inf and -np.Inf, np.NaN and None, the latter are
treats np.inf and -np.inf, np.nan and None, the latter are
simply disregarded
"""
Expand All @@ -492,7 +492,7 @@ def prctile(x, arrprctiles, issorted=False, ignore_nan=True):

N = float(len(x))
if N == 0:
return [np.NaN for a in arrprctiles]
return [np.nan for a in arrprctiles]

res = []
for p in arrprctiles:
Expand Down Expand Up @@ -609,7 +609,7 @@ def wfunc(w):
return x

Z_MAX = 6.0 # maximum meaningful z-value
x = np.zeros(z.shape, np.float_) # initialize
x = np.zeros(z.shape, np.float64) # initialize
y = 0.5 * np.fabs(z)
x = np.where(np.less(y, 1.0), wfunc(y * y), yfunc(y - 2.0)) # get x's
x = np.where(np.greater(y, Z_MAX * 0.5), 1.0, x) # kill those with big Z
Expand Down Expand Up @@ -942,7 +942,7 @@ def sliding_window_data(data, width=2, operator=np.median,
'the number of data %d' % int(width), len(data))
down = width // 2
up = width // 2 + (width % 2)
d = np.array(data, copy=False)
d = np.asarray(data)
smoothened_data = []
stats = []
stats_mod = len(d) // number_of_stats
Expand All @@ -967,7 +967,7 @@ def sliding_window_data(data, width=2, operator=np.median,
next = 0.1 + 1.8 * np.random.rand()
i_last_stats += 1

return (np.array(smoothened_data, copy=False)
return (np.asarray(smoothened_data)
if isinstance(data, np.ndarray) else smoothened_data, stats)

def equals_approximately(a, b, abs=1e-11, rel=1e-11):
Expand Down

0 comments on commit af9e249

Please sign in to comment.