Skip to content

Commit 1fac3bf

Browse files
committed
Further improve bin1d_vec (4): Avoid 3x try/except cases by always operating on arrays
... by simply assuring that `idx` is an array.
1 parent 22aac12 commit 1fac3bf

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

csep/utils/calc.py

+5-17
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,15 @@ def bin1d_vec(p, bins, tol=None, right_continuous=False):
101101
p_tol = tol or _get_tolerance(p)
102102

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

105106
if right_continuous:
106107
# set upper bin index to last
107-
try:
108-
idx[(idx < 0)] = -1
109-
idx[(idx >= len(bins) - 1)] = len(bins) - 1
110-
except TypeError:
111-
if idx >= len(bins) - 1:
112-
idx = len(bins) - 1
113-
if idx < 0:
114-
idx = -1
108+
idx[idx < 0] = -1
109+
idx[idx >= len(bins) - 1] = len(bins) - 1
115110
else:
116-
try:
117-
idx[((idx < 0) | (idx >= len(bins)))] = -1
118-
except TypeError:
119-
if idx < 0 or idx >= len(bins):
120-
idx = -1
121-
try:
122-
idx = idx.astype(numpy.int64)
123-
except AttributeError:
124-
idx = int(idx)
111+
idx[(idx < 0) | (idx >= len(bins))] = -1
112+
idx = idx.astype(numpy.int64)
125113
return idx
126114

127115
def _compute_likelihood(gridded_data, apprx_rate_density, expected_cond_count, n_obs):

0 commit comments

Comments
 (0)