Skip to content

Commit

Permalink
guess_signed (#1128)
Browse files Browse the repository at this point in the history
* Update _array.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update _array.py

* Create signed.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update _array.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update _array.py

* signed -> guess_signed

make function more robust as well

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* tests pass

correct the guess function

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update CHANGELOG.md

* Update WrightTools/kit/_array.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Kohler <11864045+ddkohler@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 30, 2024
1 parent f772f2c commit 1a2fbb9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
## [Unreleased]

### Added
- `kit.guess_signed` for empirically guessing channel sign (useful for automated workflows)
- `Data.squeeze`: squeezing the data object to the shape of the axes.

### Fixed
Expand Down
36 changes: 36 additions & 0 deletions WrightTools/kit/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"valid_index",
"mask_reduce",
"enforce_mask_shape",
"guess_signed",
]


Expand Down Expand Up @@ -432,3 +433,38 @@ def enforce_mask_shape(mask, shape):
"""
red = tuple([i for i in range(len(shape)) if shape[i] == 1])
return mask.max(axis=red, keepdims=True)


def guess_signed(chan, tol=1e-1):
"""guess whether or not a channel is signed by examining min and max values.
Parameters
-------------
chan : array-like
Input channel or array
tol : float (optional)
Tolerance value used to judge signed. `tol` should be much less than 1. To prefer signed guesses, use negative tolerance values.
Returns
-------
guess : bool
True if the data seems signed and False otherwise.
"""

maxc = chan.max()
minc = chan.min()
from ..data import Channel

if isinstance(chan, Channel):
null = chan.null
else:
null = 0

# avoid zero division for comparison
bottom = np.abs(maxc - null) + np.abs(minc - null)
if not bottom: # (maxc-null)=-(minc-null)
return True

comparison = np.abs(maxc + minc - 2 * null) / bottom
# should be < 1 if signed
return comparison < 1 - tol
30 changes: 30 additions & 0 deletions tests/kit/signed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Test guess_signed"""

# --- import -------------------------------------------------------------------------------------


import numpy as np
import WrightTools as wt


# --- test ---------------------------------------------------------------------------------------


def test_many_ranges():
for minmax, signed in [
([-0.05, 1], False),
([-1, 0.05], False),
([-1, 1], True),
([0, 0], True),
([0, -1], False),
([-1, 0.5], True),
]:
assert wt.kit.guess_signed(np.array(minmax)) == signed


def test_channel():
d = wt.Data()
chan = d.create_channel("chan", values=np.linspace(3, 4, 16).reshape(4, 4))
assert wt.kit.guess_signed(chan) == False
chan.null = 3.5
assert wt.kit.guess_signed(chan)

0 comments on commit 1a2fbb9

Please sign in to comment.