Skip to content

Commit a836927

Browse files
committed
Fix imprecise bin edge creation in utils.calc.cleaner_range
Spotted by running unit tests for NZ region (the end-corner-based unit test `test_bin2d_regions_endcorner` failed). Those imprecise bin edges were originated in `utils.calc.cleaner_range`. The crucial modification is using `round` instead of `floor`. The other change – replacing the hard-coded `const` with a flexible `scale` parameter – is to account for decimal places of bin edges _and_ stepping (`const = 100000` would lead to imprecise bin edges if `h < 0.00001`). + simplified `core.regions.magnitude_bins()` to just call (this improved) `cleaner_range()`; it was essentially a copy of the former.
1 parent 92e33f0 commit a836927

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

csep/core/regions.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,7 @@ def magnitude_bins(start_magnitude, end_magnitude, dmw):
313313
Returns:
314314
bin_edges (numpy.ndarray)
315315
"""
316-
# convert to integers to prevent accumulating floating point errors
317-
const = 10000
318-
start = numpy.floor(const * start_magnitude)
319-
end = numpy.floor(const * end_magnitude)
320-
d = const * dmw
321-
return numpy.arange(start, end + d / 2, d) / const
316+
return cleaner_range(start_magnitude, end_magnitude, dmw)
322317

323318
def create_space_magnitude_region(region, magnitudes):
324319
"""Simple wrapper to create space-magnitude region """

csep/utils/calc.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,13 @@ def cleaner_range(start, end, h):
213213
Returns:
214214
bin_edges (numpy.ndarray)
215215
"""
216-
# convert to integers to prevent accumulating floating point errors
217-
const = 100000
218-
start = numpy.floor(const * start)
219-
end = numpy.floor(const * end)
220-
d = const * h
221-
return numpy.arange(start, end + d / 2, d) / const
216+
# determine required scaling to account for decimal places of bin edges and stepping
217+
num_decimals_bins = len(str(float(start)).split('.')[1])
218+
scale = max(10**num_decimals_bins, 1 / h)
219+
start = numpy.round(scale * start)
220+
end = numpy.round(scale * end)
221+
d = scale * h
222+
return numpy.arange(start, end + d / 2, d) / scale
222223

223224
def first_nonnan(arr, axis=0, invalid_val=-1):
224225
mask = arr==arr

0 commit comments

Comments
 (0)