Skip to content

Commit

Permalink
Work around imports shadowing built ins
Browse files Browse the repository at this point in the history
Use alternate/alias name.
Use method instead of function.
Change some logic to use different functions with same result.
  • Loading branch information
153957 committed Dec 16, 2024
1 parent 5eee0eb commit 7d14a56
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
12 changes: 6 additions & 6 deletions sapphire/analysis/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from datetime import datetime, timedelta
from itertools import chain, combinations, tee

from numpy import abs, arange, histogram, isnan, linspace, nan, percentile, sqrt, std, sum
from numpy import absolute, arange, histogram, isnan, linspace, nan, percentile, sqrt, std
from scipy.optimize import curve_fit

from ..api import Station
Expand Down Expand Up @@ -60,7 +60,7 @@ def determine_detector_timing_offsets(events, station=None):
offsets[detector_id], _ = determine_detector_timing_offset(dt, dz)

# If all except reference are nan, make reference nan.
if sum(isnan(offsets)) == 3:
if sum(isnan(offset) for offset in offsets) == 3:
offsets = [nan, nan, nan, nan]

# Try to make detector 2 the reference point, if it is not nan.
Expand All @@ -80,8 +80,8 @@ def determine_detector_timing_offset(dt, dz=0):
the error of the mean.
"""
dt_filter = abs(dt + dz / c) < 100
if not sum(dt_filter):
dt_filter = absolute(dt + dz / c) < 100
if not dt_filter.sum():
return nan, nan
p = round_in_base(percentile(dt.compress(dt_filter), [0.5, 99.5]), 2.5)
bins = arange(p[0] + 1.25, p[1], 2.5)
Expand Down Expand Up @@ -369,7 +369,7 @@ def fit_timing_offset(dt, bins):
popt, pcov = curve_fit(gauss, x, y, p0=(len(dt), 0.0, std(dt)), sigma=sigma, absolute_sigma=False)
offset = popt[1]
width = popt[2]
offset_error = width / sqrt(sum(y))
offset_error = width / sqrt(y.sum())
except (RuntimeError, TypeError):
offset, offset_error = nan, nan
return offset, offset_error
Expand All @@ -389,7 +389,7 @@ def determine_best_reference(filters):

for detector_id in ids:
idx = [j for j in ids if j != detector_id]
lengths.append(sum(filters[detector_id] & (filters[idx[0]] | filters[idx[1]] | filters[idx[2]])))
lengths.append((filters[detector_id] & (filters[idx[0]] | filters[idx[1]] | filters[idx[2]])).sum())
return lengths.index(max(lengths))


Expand Down
12 changes: 7 additions & 5 deletions sapphire/tests/analysis/test_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import tables

from numpy import all, array, isnan, nan, random, std
from numpy import array, isnan, nan, random, std

from sapphire import HiSPARCNetwork, HiSPARCStations
from sapphire.analysis import calibration
Expand All @@ -33,8 +33,9 @@ def test_determine_detector_timing_offsets(self):
@patch.object(calibration, 'fit_timing_offset')
def test_determine_detector_timing_offset(self, mock_fit):
# Empty list
offset = calibration.determine_detector_timing_offset(array([]))
self.assertTrue(all(isnan(offset)))
offset, error = calibration.determine_detector_timing_offset(array([]))
self.assertTrue(isnan(offset))
self.assertTrue(isnan(error))

dt = array([-10, 0, 10])
dz = 0.6
Expand Down Expand Up @@ -74,8 +75,9 @@ def test_determine_station_timing_offset(self, mock_fit, mock_percentile):
dzc = dz / c

# Empty list
offset = calibration.determine_station_timing_offset([])
self.assertTrue(all(isnan(offset)))
offset, error = calibration.determine_station_timing_offset([])
self.assertTrue(isnan(offset))
self.assertTrue(isnan(error))

# Good result
mock_fit.return_value = (1.0, 5.0)
Expand Down
6 changes: 3 additions & 3 deletions sapphire/tests/validate_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import tables

from numpy import all, array
from numpy.testing import assert_array_almost_equal
from numpy import array
from numpy.testing import assert_array_almost_equal, assert_equal


def validate_results(test, expected_path, actual_path):
Expand Down Expand Up @@ -92,7 +92,7 @@ def validate_vlarrays(test, expected_node, actual_node):
f"VLArrays '{expected_node._v_pathname}' do not have the same shape.",
)
for expected_array, actual_array in zip(expected_node, actual_node):
test.assertTrue(all(expected_array == actual_array), f"VLArrays '{expected_node._v_pathname}' do not match.")
assert_equal(actual_array, expected_array, f"VLArrays '{expected_node._v_pathname}' do not match.")


def validate_arrays(test, expected_node, actual_node):
Expand Down
4 changes: 2 additions & 2 deletions sapphire/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from functools import wraps
from os import environ

from numpy import arcsin, ceil, floor, pi, round, sin, sqrt
from numpy import arcsin, around, ceil, floor, pi, sin, sqrt
from progressbar import ETA, Bar, Percentage, ProgressBar
from scipy.stats import norm

Expand Down Expand Up @@ -73,7 +73,7 @@ def floor_in_base(value, base):
def round_in_base(value, base):
"""Get nearest multiple of base to the value"""

return base * round(value / base)
return base * around(value / base)


def closest_in_list(value, items):
Expand Down

0 comments on commit 7d14a56

Please sign in to comment.