Skip to content

Commit

Permalink
Replaced asserts by proper checks
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zwicker committed Mar 2, 2024
1 parent 47f04ef commit 6f651e6
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}")

Check warning on line 284 in droplets/droplets.py

View check run for this annotation

Codecov / codecov/patch

droplets/droplets.py#L284

Added line #L284 was not covered by tests
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")

Check warning on line 794 in droplets/droplets.py

View check run for this annotation

Codecov / codecov/patch

droplets/droplets.py#L794

Added line #L794 was not covered by tests
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}")

Check warning on line 157 in droplets/emulsions.py

View check run for this annotation

Codecov / codecov/patch

droplets/emulsions.py#L157

Added line #L157 was not covered by tests

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")

Check warning on line 421 in droplets/image_analysis.py

View check run for this annotation

Codecov / codecov/patch

droplets/image_analysis.py#L421

Added line #L421 was not covered by tests
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")

Check warning on line 586 in droplets/image_analysis.py

View check run for this annotation

Codecov / codecov/patch

droplets/image_analysis.py#L586

Added line #L586 was not covered by tests
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")

Check warning on line 324 in droplets/tools/spherical.py

View check run for this annotation

Codecov / codecov/patch

droplets/tools/spherical.py#L324

Added line #L324 was not covered by tests

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")

Check warning on line 348 in droplets/tools/spherical.py

View check run for this annotation

Codecov / codecov/patch

droplets/tools/spherical.py#L348

Added line #L348 was not covered by tests

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

0 comments on commit 6f651e6

Please sign in to comment.