Skip to content

Commit

Permalink
Replaced asserts by proper checks (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zwicker authored Mar 3, 2024
1 parent 47f04ef commit 886524a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions droplets/droplets.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ def get_dtype(cls, **kwargs) -> DTypeList:
:class:`numpy.dtype`: the (structured) dtype associated with this class
"""
position = np.atleast_1d(kwargs.pop("position"))
assert not kwargs # no more keyword arguments
if kwargs:
raise ValueError(f"Leftover keyword arguments: {kwargs}")
dim = len(position)
return [("position", float, (dim,)), ("radius", float)]

Expand Down Expand Up @@ -789,7 +790,8 @@ def amplitudes(self) -> np.ndarray:
@amplitudes.setter
def amplitudes(self, value: np.ndarray | None = None) -> None:
if value is None:
assert self.modes == 0
if self.modes != 0:
raise ValueError("Require values for amplitudes")
self.data["amplitudes"] = np.broadcast_to(0.0, (0,))
else:
self.data["amplitudes"] = np.broadcast_to(value, (self.modes,))
Expand Down
3 changes: 2 additions & 1 deletion droplets/emulsions.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ def get_position():

else:
bnds = np.atleast_2d(grid_or_bounds)
assert bnds.ndim == 2 and bnds.shape[0] > 0 and bnds.shape[1] == 2
if bnds.ndim != 2 or bnds.shape[0] == 0 or bnds.shape[1] != 2:
raise ValueError(f"Bounds must be array of shape (n, 2), got {bnds}")

def get_position():
return rng.uniform(bnds[:, 0], bnds[:, 1])
Expand Down
6 changes: 4 additions & 2 deletions droplets/image_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ def locate_droplets(
Returns:
:class:`~droplets.emulsions.Emulsion`: All detected droplets
"""
assert isinstance(phase_field, ScalarField)
if not isinstance(phase_field, ScalarField):
raise TypeError("`phase_field` must be ScalarField")
dim = phase_field.grid.dim # dimensionality of the space

if modes > 0 and dim not in [2, 3]:
Expand Down Expand Up @@ -581,7 +582,8 @@ def refine_droplet(
:class:`~droplets.droplets.DiffuseDroplet`:
The refined droplet as an instance of the argument `droplet`
"""
assert isinstance(phase_field, ScalarField)
if not isinstance(phase_field, ScalarField):
raise TypeError("`phase_field` must be ScalarField")
if least_squares_params is None:
least_squares_params = {}
if tolerance is not None:
Expand Down
6 changes: 4 additions & 2 deletions droplets/tools/spherical.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ def points_cartesian_to_spherical(points: np.ndarray) -> np.ndarray:
:class:`~numpy.ndarray`: Points (r, θ, φ) in spherical coordinates
"""
points = np.atleast_1d(points)
assert points.shape[-1] == 3, "Points must have 3 coordinates"
if points.shape[-1] != 3:
raise DimensionError("Points must have 3 coordinates")

ps_spherical = np.empty(points.shape)
# calculate radius in [0, infinity]
Expand All @@ -343,7 +344,8 @@ def points_spherical_to_cartesian(points: np.ndarray) -> np.ndarray:
:class:`~numpy.ndarray`: Points in Cartesian coordinates
"""
points = np.atleast_1d(points)
assert points.shape[-1] == 3, "Points must have 3 coordinates"
if points.shape[-1] != 3:
raise DimensionError("Points must have 3 coordinates")

sin_θ = np.sin(points[..., 1])
ps_cartesian = np.empty(points.shape)
Expand Down

0 comments on commit 886524a

Please sign in to comment.