Skip to content

Commit

Permalink
Refactor droplet classes to improve code readability and consistency
Browse files Browse the repository at this point in the history
* Fixed bug in displaying perturbed droplets
* Improved testing with bare py.test
  • Loading branch information
david-zwicker committed Aug 15, 2024
1 parent 9f73852 commit 5254f7e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions droplets/droplets.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ def _get_mpl_patch(self, dim=None, *, color=None, **kwargs):


class PerturbedDroplet2D(PerturbedDropletBase):
r"""Represents a single droplet in two dimensions with a perturbed shape.
r"""Represents a single 2D droplet with a perturbed shape.
The shape is described using the distance :math:`R(\phi)` of the interface from the
`position`, which is a function of the polar angle :math:`\phi`. This function is
Expand Down Expand Up @@ -946,7 +946,7 @@ def interface_position(self, φ: np.ndarray) -> np.ndarray:
Array with coordinates of interfacial points associated with each angle φ
"""
dist = self.interface_distance(φ)
pos = dist[:, None] * np.transpose([np.sin(φ), np.cos(φ)])
pos = dist[:, None] * np.transpose([np.cos(φ), np.sin(φ)])
return self.position[None, :] + pos # type: ignore

@preserve_scalars
Expand Down Expand Up @@ -1044,7 +1044,7 @@ def _get_mpl_patch(self, dim=2, **kwargs):


class PerturbedDroplet3D(PerturbedDropletBase):
r"""Represents a single droplet in three dimensions with a perturbed shape.
r"""Represents a single 3D droplet with a perturbed shape.
The shape is described using the distance :math:`R(\theta, \phi)` of the interface
from the origin as a function of the azimuthal angle :math:`\theta` and the polar
Expand Down Expand Up @@ -1205,7 +1205,7 @@ def volume_approx(self) -> float:


class PerturbedDroplet3DAxisSym(PerturbedDropletBase):
r"""Represents a droplet axisymmetrically perturbed shape in three dimensions.
r"""Represents a 3D droplet with axisymmetrically perturbed shape.
The shape is described using the distance :math:`R(\theta)` of the interface from
the origin as a function of the azimuthal angle :math:`\theta`, while polar symmetry
Expand Down
8 changes: 4 additions & 4 deletions droplets/image_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ def refine_droplet(
phase_field: ScalarField,
droplet: DiffuseDroplet,
*,
vmin: float = 0.0,
vmax: float = 1.0,
vmin: float | None = 0.0,
vmax: float | None = 1.0,
adjust_values: bool = False,
tolerance: float | None = None,
least_squares_params: dict[str, Any] | None = None,
Expand All @@ -565,7 +565,7 @@ def refine_droplet(
vmax (float):
The intensity value inside the droplet. If `None`, the value will be
determined automatically.
adjust_value (bool):
adjust_values (bool):
Flag determining whether the intensity values will be included in the
fitting procedure. The default value `False` implies that the intensity
values are regarded fixed.
Expand All @@ -590,7 +590,7 @@ def refine_droplet(
least_squares_params.setdefault(key, tolerance)

if not isinstance(droplet, DiffuseDroplet):
droplet = DiffuseDroplet.from_droplet(droplet)
droplet_obj = DiffuseDroplet.from_droplet(droplet)
if droplet.interface_width is None:
droplet.interface_width = phase_field.grid.typical_discretization

Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def init_random_number_generators():
@pytest.fixture(scope="function", autouse=True)
def setup_and_teardown():
"""Helper function adjusting environment before and after tests."""
# ensure we use the Agg backend, so figures are not displayed
plt.switch_backend("agg")
# raise all underflow errors
np.seterr(all="raise", under="ignore")

Expand Down
1 change: 1 addition & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_example(path):

env = os.environ.copy()
env["PYTHONPATH"] = str(PACKAGE_PATH) + ":" + env.get("PYTHONPATH", "")
env["MPLBACKEND"] = "agg"
proc = sp.Popen([sys.executable, path], env=env, stdout=sp.PIPE, stderr=sp.PIPE)
try:
outs, errs = proc.communicate(timeout=30)
Expand Down

0 comments on commit 5254f7e

Please sign in to comment.