Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions geoapps_utils/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import annotations

import logging
from contextlib import contextmanager


def get_logger(
Expand Down Expand Up @@ -61,3 +62,18 @@ def get_logger(
log.setLevel(level)

return log


@contextmanager
def suppress_logging(level=logging.WARNING):
"""
Temporarily disable logging records at or below the given level.

:param level: Logging level to suppress (default: logging.WARNING).
"""
previous_disable_level = logging.root.manager.disable
logging.disable(level)
try:
yield
finally:
logging.disable(previous_disable_level)
Comment thread
domfournier marked this conversation as resolved.
11 changes: 7 additions & 4 deletions geoapps_utils/utils/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ def cartesian_to_polar(

local_xyz = locations - np.asarray(origin)
distances = np.linalg.norm(local_xyz[:, :2], axis=1)
azimuths = 90 - np.rad2deg(np.arctan2(local_xyz[:, 0], local_xyz[:, 1]))

# Deal with points close to the origin with nearest neighbor azimuth
# Compute azimuth from the delta between points and the origin
azimuths = 90 - np.rad2deg(np.arctan2(local_xyz[:, 1], local_xyz[:, 0]))
azimuths = azimuths % 360
Comment thread
domfournier marked this conversation as resolved.
Comment thread
domfournier marked this conversation as resolved.

# Deal with locations at the origin (0 distance)
if np.any(distances < 1e-8):
indices = np.where(distances >= 1e-8)[0]
nearest = indices[np.argsort(distances[indices])]
neighbours = np.where(distances >= 1e-8)[0]
nearest = neighbours[np.argsort(distances[neighbours])]
azimuths[distances < 1e-8] = azimuths[nearest[0]]

return np.c_[distances, azimuths, locations[:, 2]]
Expand Down
18 changes: 18 additions & 0 deletions tests/driver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from geoapps_utils.driver.driver import BaseDriver, Driver
from geoapps_utils.driver.params import BaseParams
from geoapps_utils.run import fetch_driver_class
from geoapps_utils.utils.logger import suppress_logging

from .dummy_driver_test import (
TestOptions,
Expand Down Expand Up @@ -219,3 +220,20 @@ def test_logger(caplog):
assert "my-app" in caplog.text
assert caplog.records[0].levelname == "INFO"
assert caplog.records[0].name == "my-app"


def test_suppress_logging(caplog):
"""
Test that the logger suppression occurs, then restored after the context is closed.
"""
logger = get_logger("my-app")
with caplog.at_level("INFO"):
with suppress_logging(level=logging.INFO):
logger.info("Test log message")

assert "Test log message" not in caplog.text

# Check if logging is reinstated after the context
with caplog.at_level("INFO"):
logger.info("Test log message")
assert "Test log message" in caplog.text
4 changes: 2 additions & 2 deletions tests/transformations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def test_cartesian_to_polar():
polar = cartesian_to_polar(locations)
np.testing.assert_almost_equal(polar[0, 0], 0.0) # First point at zero distance
np.testing.assert_allclose(
polar[:, 1], np.rad2deg(azm)
polar[:, 1], 90 - np.rad2deg(azm)
) # All other distances positive

with pytest.raises(ValueError, match=r"Origin must be an iterable of length 3\."):
Expand All @@ -225,5 +225,5 @@ def test_cartesian_to_polar():
# End reference
polar = cartesian_to_polar(locations, origin=locations[-1, :])
np.testing.assert_allclose(
polar[:, 1], np.rad2deg(azm) + 180
polar[:, 1], 90 - np.rad2deg(azm) + 180
) # All other distances positive
Loading