Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

guess_signed #1128

Merged
merged 19 commits into from
Jan 30, 2024
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
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)
Loading