Skip to content

Commit

Permalink
Use numbers types when checking arguments (#419)
Browse files Browse the repository at this point in the history
## Description

<!-- Provide a brief description of the PR's purpose here. -->

Previously, we were using `isinstance(x, (int, np.integer))`; however,
we can be more general and succinct by using `isinstance(x,
numbers.Integral)` (the `numbers` module is part of the standard
library).

## TODO

<!-- Notable points that this PR has either accomplished or will
accomplish. -->

## Questions

<!-- Any concerns or points of confusion? -->

## Status

- [x] I have read the guidelines in

[CONTRIBUTING.md](https://github.com/icaros-usc/pyribs/blob/master/CONTRIBUTING.md)
- [x] I have formatted my code using `yapf`
- [x] I have tested my code by running `pytest`
- [x] I have linted my code with `pylint`
- [x] I have added a one-line description of my change to the changelog
in
      `HISTORY.md`
- [x] This PR is ready to go
  • Loading branch information
btjanaka committed Nov 14, 2023
1 parent ee3878b commit ca3eaf2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#### Improvements

- Use numbers types when checking arguments ({pr}`419`)
- Reimplement ArchiveBase using ArrayStore ({pr}`399`)
- Use chunk computation in CVT brute force calculation to reduce memory usage
({pr}`394`)
Expand Down
3 changes: 2 additions & 1 deletion ribs/archives/_array_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Provides ArrayStore."""
import itertools
import numbers
from enum import IntEnum
from functools import cached_property

Expand Down Expand Up @@ -125,7 +126,7 @@ def __init__(self, field_desc, capacity):
raise ValueError(
f"Field names must be valid identifiers: `{name}`")

if isinstance(field_shape, (int, np.integer)):
if isinstance(field_shape, numbers.Integral):
field_shape = (field_shape,)

array_shape = (capacity,) + tuple(field_shape)
Expand Down
4 changes: 3 additions & 1 deletion ribs/emitters/_evolution_strategy_emitter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Provides the EvolutionStrategyEmitter."""
import numbers

import numpy as np

from ribs._utils import check_1d_shape, validate_batch_args
Expand Down Expand Up @@ -173,7 +175,7 @@ def _check_restart(self, num_parents):
Raises:
ValueError: If :attr:`restart_rule` is invalid.
"""
if isinstance(self._restart_rule, (int, np.integer)):
if isinstance(self._restart_rule, numbers.Integral):
return self._itrs % self._restart_rule == 0
if self._restart_rule == "no_improvement":
return num_parents == 0
Expand Down
4 changes: 3 additions & 1 deletion ribs/emitters/_gradient_arborescence_emitter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Provides the GradientArborescenceEmitter."""
import numbers

import numpy as np

from ribs._utils import check_1d_shape, validate_batch_args
Expand Down Expand Up @@ -301,7 +303,7 @@ def _check_restart(self, num_parents):
Raises:
ValueError: If :attr:`restart_rule` is invalid.
"""
if isinstance(self._restart_rule, (int, np.integer)):
if isinstance(self._restart_rule, numbers.Integral):
return self._itrs % self._restart_rule == 0
if self._restart_rule == "no_improvement":
return num_parents == 0
Expand Down
3 changes: 2 additions & 1 deletion ribs/emitters/_gradient_operator_emitter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provides the GradientOperatorEmitter."""
import numbers

import numpy as np

Expand Down Expand Up @@ -140,7 +141,7 @@ def __init__(self,

self._rng = np.random.default_rng(seed)
self._sigma = archive.dtype(sigma) if isinstance(
sigma, (float, np.floating)) else np.array(sigma)
sigma, numbers.Real) else np.array(sigma)
self._sigma_g = archive.dtype(sigma_g)
self._line_sigma = line_sigma
self._use_isolinedd = operator_type != 'isotropic'
Expand Down

0 comments on commit ca3eaf2

Please sign in to comment.