Skip to content

Commit

Permalink
Further improve bin1d_vec (4): Avoid 3x try/except cases by always …
Browse files Browse the repository at this point in the history
…operating on arrays

... by simply assuring that `idx` is an array.
  • Loading branch information
mherrmann3 committed Feb 6, 2025
1 parent 22aac12 commit 1fac3bf
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions csep/utils/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,15 @@ def bin1d_vec(p, bins, tol=None, right_continuous=False):
p_tol = tol or _get_tolerance(p)

idx = numpy.floor((p - a0 + p_tol + a0_tol) / (h - h_tol))
idx = numpy.asarray(idx) # assure idx is an array

if right_continuous:
# set upper bin index to last
try:
idx[(idx < 0)] = -1
idx[(idx >= len(bins) - 1)] = len(bins) - 1
except TypeError:
if idx >= len(bins) - 1:
idx = len(bins) - 1
if idx < 0:
idx = -1
idx[idx < 0] = -1
idx[idx >= len(bins) - 1] = len(bins) - 1
else:
try:
idx[((idx < 0) | (idx >= len(bins)))] = -1
except TypeError:
if idx < 0 or idx >= len(bins):
idx = -1
try:
idx = idx.astype(numpy.int64)
except AttributeError:
idx = int(idx)
idx[(idx < 0) | (idx >= len(bins))] = -1
idx = idx.astype(numpy.int64)
return idx

def _compute_likelihood(gridded_data, apprx_rate_density, expected_cond_count, n_obs):
Expand Down

0 comments on commit 1fac3bf

Please sign in to comment.