Skip to content

Commit

Permalink
log clip_zero default revert to False, and only apply if data dtype s…
Browse files Browse the repository at this point in the history
…upports it.
  • Loading branch information
cboulay committed Nov 1, 2024
1 parent 95c9b6b commit 3c947ea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/ezmsg/sigproc/math/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@consumer
def log(
base: float = 10.0,
clip_zero: bool = True,
clip_zero: bool = False,
) -> typing.Generator[AxisArray, AxisArray, None]:
"""
Take the logarithm of the data. See :obj:`np.log` for more details.
Expand All @@ -29,7 +29,11 @@ def log(
log_base = np.log(base)
while True:
msg_in: AxisArray = yield msg_out
if clip_zero:
if (
clip_zero
and np.any(msg_in.data <= 0)
and np.issubdtype(msg_in.data.dtype, np.floating)
):
msg_in.data = np.clip(
msg_in.data, a_min=np.finfo(msg_in.data.dtype).tiny, a_max=None
)
Expand Down
10 changes: 7 additions & 3 deletions tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ def test_invert():


@pytest.mark.parametrize("base", [np.e, 2, 10])
def test_log(base: float):
@pytest.mark.parametrize("dtype", [int, float])
@pytest.mark.parametrize("clip", [False, True])
def test_log(base: float, dtype, clip: bool):
n_times = 130
n_chans = 255
in_dat = np.arange(n_times * n_chans).reshape(n_times, n_chans)
in_dat = np.arange(n_times * n_chans).reshape(n_times, n_chans).astype(dtype)
msg_in = AxisArray(in_dat, dims=["time", "ch"])
proc = log(base)
proc = log(base, clip_zero=clip)
msg_out = proc.send(msg_in)
if clip and dtype is float:
in_dat = np.clip(in_dat, a_min=np.finfo(msg_in.data.dtype).tiny, a_max=None)
assert np.array_equal(msg_out.data, np.log(in_dat) / np.log(base))


Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3c947ea

Please sign in to comment.