Skip to content

Commit

Permalink
Merge pull request #6 from ConorMacBride/use-numpy-integer
Browse files Browse the repository at this point in the history
Check isinstance np.integer as well as int
  • Loading branch information
ConorMacBride committed Jan 5, 2021
2 parents b28d860 + 05b1faf commit 88d4c59
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/mcalf/models/ibis.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def _get_sigma(self, classification=None, sigma=None):
else:
return self.sigma[1]
else:
if isinstance(sigma, int):
if isinstance(sigma, (int, np.integer)):
return self.sigma[sigma]
else:
return np.asarray(sigma, dtype=np.float64)
Expand Down Expand Up @@ -479,7 +479,7 @@ def _fit(self, spectrum, profile=None, sigma=None, classification=None, spectrum
profile = 'absorption'
elif classification is None:
raise ValueError("classification must be specified if profile is not specified")
elif not isinstance(classification, int):
elif not isinstance(classification, (int, np.integer)):
raise TypeError("classification must be an integer")
else:
raise ValueError("unexpected classification, got %s" % classification)
Expand Down Expand Up @@ -597,13 +597,14 @@ def fit(self, time=None, row=None, column=None, spectrum=None, profile=None, sig
raise ValueError("number of spectra, number of recorded indices and number of classifications"
"are not the same (impossible error)")

if n_pools is None or (isinstance(n_pools, int) and n_pools <= 0): # Multiprocessing not required
# Multiprocessing not required
if n_pools is None or (isinstance(n_pools, (int, np.integer)) and n_pools <= 0):

print("Processing {} spectra".format(n_valid))
results = [self._fit(spectra[i], profile=profile, sigma=sigma, classification=classifications[i],
spectrum_index=indices[i]) for i in range(len(spectra))]

elif isinstance(n_pools, int) and n_pools >= 1: # Use multiprocessing
elif isinstance(n_pools, (int, np.integer)) and n_pools >= 1: # Use multiprocessing

# Define single argument function that can be evaluated in the pools
def func(data, profile=profile, sigma=sigma):
Expand Down Expand Up @@ -711,7 +712,7 @@ def plot(self, fit=None, time=None, row=None, column=None, spectrum=None, classi
if background is None and fit is not None:
if not explicit_spectrum: # Take from loaded background if using indices
time, row, column = self._get_time_row_column(time=time, row=row, column=column)
if sum([isinstance(i, (int, np.int64, np.int32, np.int16, np.int8)) for i in [time, row, column]]) != 3:
if sum([isinstance(i, (int, np.integer)) for i in [time, row, column]]) != 3:
raise TypeError("plot only accepts integer values for time, row and column")
background = self.background[time, row, column]
else: # Otherwise assume to be zero
Expand Down
2 changes: 1 addition & 1 deletion src/mcalf/models/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(self, shape, n_parameters, time=None):
if not isinstance(shape, tuple) or len(shape) != 2:
raise TypeError("`shape` must be a tuple of length 2, got %s" % type(shape))

if not isinstance(n_parameters, int) or n_parameters < 1:
if not isinstance(n_parameters, (int, np.integer)) or n_parameters < 1:
raise ValueError("`n_parameters` must be an integer greater than zero, got %s" % n_parameters)
parameters_shape = tuple(list(shape) + [n_parameters])
self.parameters = np.full(parameters_shape, np.nan, dtype=float)
Expand Down
4 changes: 2 additions & 2 deletions src/mcalf/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ def merge_results(filenames, output):
test_function = _nan_test
elif isinstance(invalid, bool) and not invalid: # bool (can only overwrite False)
test_function = _false_test
elif isinstance(invalid, int) and invalid == -1:
elif isinstance(invalid, (int, np.integer)) and invalid == -1:
test_function = _minus_one_test
elif isinstance(invalid, int) and invalid == 0:
elif isinstance(invalid, (int, np.integer)) and invalid == 0:
test_function = _zero_test
else:
raise ValueError(f"Unexpected invalid value {invalid}.")
Expand Down
2 changes: 1 addition & 1 deletion src/mcalf/utils/smooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def moving_average(array, width):
array([1. , 1.5, 2.5, 3.5, 4.5])
"""

if not isinstance(width, int) or width <= 0 or width > len(array):
if not isinstance(width, (int, np.integer)) or width <= 0 or width > len(array):
raise ValueError("`width` must be a positive integer less than the length of `array`, got %s." % width)

kernel = np.ones(width) / width
Expand Down

0 comments on commit 88d4c59

Please sign in to comment.