From 6130b33d9ad586b8afcb2b49fb9d58578544824c Mon Sep 17 00:00:00 2001 From: rpm4 Date: Wed, 21 Jun 2023 14:20:04 -0500 Subject: [PATCH 01/15] Update _array.py --- WrightTools/kit/_array.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index 51971cc2..3377f930 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -27,7 +27,8 @@ "unique", "valid_index", "mask_reduce", - "enforce_mask_shape", + "enforce_mask_shape" + "signed", ] @@ -433,3 +434,22 @@ 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 signed(d0): + """Tells bluesky whether to sign data. We have a 7.5 % tolerance right now.%""" + + + maxd0 = np.max(d0) + mind0 = np.min(d0) + + tolerance = (maxd0 - np.absolute(mind0))/(maxd0 + np.absolute(mind0)) + + if tolerance < 7.5*10**(-2): + if min(d0) > 0: + return False + else: return True + + else: + return False From ba3987c8e2ae2413ceffb71aefa946a77f9d4941 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 21 Jun 2023 19:22:33 +0000 Subject: [PATCH 02/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- WrightTools/kit/_array.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index 3377f930..143bc6d6 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -27,8 +27,7 @@ "unique", "valid_index", "mask_reduce", - "enforce_mask_shape" - "signed", + "enforce_mask_shape" "signed", ] @@ -436,20 +435,19 @@ def enforce_mask_shape(mask, shape): return mask.max(axis=red, keepdims=True) - def signed(d0): """Tells bluesky whether to sign data. We have a 7.5 % tolerance right now.%""" - maxd0 = np.max(d0) mind0 = np.min(d0) - - tolerance = (maxd0 - np.absolute(mind0))/(maxd0 + np.absolute(mind0)) - - if tolerance < 7.5*10**(-2): + + tolerance = (maxd0 - np.absolute(mind0)) / (maxd0 + np.absolute(mind0)) + + if tolerance < 7.5 * 10 ** (-2): if min(d0) > 0: return False - else: return True - + else: + return True + else: return False From 2a2245d73688445eba6885a87c16b9d48fbfed09 Mon Sep 17 00:00:00 2001 From: rpm4 Date: Wed, 21 Jun 2023 14:29:58 -0500 Subject: [PATCH 03/15] Update _array.py --- WrightTools/kit/_array.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index 3377f930..39055140 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -27,7 +27,7 @@ "unique", "valid_index", "mask_reduce", - "enforce_mask_shape" + "enforce_mask_shape", "signed", ] @@ -203,7 +203,7 @@ def orthogonal(*args) -> bool: if hasattr(arg, "shape"): args[i] = arg.shape for s in zip(*args): - if np.prod(s) != max(s): + if np.product(s) != max(s): return False return True From 34beb5689c593d307625d4785a8e06bdc51fe83e Mon Sep 17 00:00:00 2001 From: rpm4 Date: Wed, 21 Jun 2023 14:47:51 -0500 Subject: [PATCH 04/15] Create signed.py --- tests/kit/signed.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/kit/signed.py diff --git a/tests/kit/signed.py b/tests/kit/signed.py new file mode 100644 index 00000000..1bb38952 --- /dev/null +++ b/tests/kit/signed.py @@ -0,0 +1,26 @@ +"""Test signed data.""" + + +# --- import ------------------------------------------------------------------------------------- + + +import numpy as np + +import WrightTools as wt + + +# --- test --------------------------------------------------------------------------------------- + + +def test_5(): + arr = np.array([-1, 0, 1]) + assert wt.kit.signed(arr) == True + + + +def test_5_multiple(): + arr = np.array([1, 3, 4, 11, 12]) + assert wt.kit.signed(arr) == False + + + From e729f97ad0c599579d42e9ff934a35a703f5ee87 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 21 Jun 2023 19:48:25 +0000 Subject: [PATCH 05/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/kit/signed.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/kit/signed.py b/tests/kit/signed.py index 1bb38952..10a9b3f9 100644 --- a/tests/kit/signed.py +++ b/tests/kit/signed.py @@ -17,10 +17,6 @@ def test_5(): assert wt.kit.signed(arr) == True - def test_5_multiple(): arr = np.array([1, 3, 4, 11, 12]) assert wt.kit.signed(arr) == False - - - From 091c1fcbcda25ec9b5f7e96ea3b33bc4d54f06cc Mon Sep 17 00:00:00 2001 From: rpm4 Date: Wed, 21 Jun 2023 15:13:52 -0500 Subject: [PATCH 06/15] Update _array.py --- WrightTools/kit/_array.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index 35059241..6d650634 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -437,7 +437,22 @@ def enforce_mask_shape(mask, shape): def signed(d0): - """Tells bluesky whether to sign data. We have a 7.5 % tolerance right now.%""" + """Tells bluesky whether to sign data. We have a 7.5 % tolerance right now.% + + Parameters + ------------- + d0: An array + maxd0: number + Maximum value in the array. + + mind0: number + Minimum value in the array. + + + Returns + ------- + A boolean; True if the data is signed and False if the data is not. + """ maxd0 = np.max(d0) mind0 = np.min(d0) From 0035d240535dd55e59ec0d007518c1816406d9dc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 21 Jun 2023 20:14:28 +0000 Subject: [PATCH 07/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- WrightTools/kit/_array.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index 6d650634..e333e0c9 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -438,20 +438,20 @@ def enforce_mask_shape(mask, shape): def signed(d0): """Tells bluesky whether to sign data. We have a 7.5 % tolerance right now.% - + Parameters ------------- d0: An array maxd0: number Maximum value in the array. - + mind0: number Minimum value in the array. - + Returns ------- - A boolean; True if the data is signed and False if the data is not. + A boolean; True if the data is signed and False if the data is not. """ maxd0 = np.max(d0) From 1adf1a02316dfa1f7580c804e9ff3fda2b33f5e4 Mon Sep 17 00:00:00 2001 From: rpm4 Date: Wed, 21 Jun 2023 15:46:38 -0500 Subject: [PATCH 08/15] Update _array.py --- WrightTools/kit/_array.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index e333e0c9..f8a4322c 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -441,12 +441,8 @@ def signed(d0): Parameters ------------- - d0: An array - maxd0: number - Maximum value in the array. + d0: Input data array - mind0: number - Minimum value in the array. Returns From ef2d9731803374b1195bbc353825e27652cfea28 Mon Sep 17 00:00:00 2001 From: Daniel Kohler <11864045+ddkohler@users.noreply.github.com> Date: Fri, 26 Jan 2024 10:09:00 -0600 Subject: [PATCH 09/15] signed -> guess_signed make function more robust as well --- WrightTools/kit/_array.py | 43 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index f8a4322c..8c625964 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -28,7 +28,7 @@ "valid_index", "mask_reduce", "enforce_mask_shape", - "signed", + "guess_signed", ] @@ -436,30 +436,35 @@ def enforce_mask_shape(mask, shape): return mask.max(axis=red, keepdims=True) -def signed(d0): - """Tells bluesky whether to sign data. We have a 7.5 % tolerance right now.% - +def guess_signed(chan, tol=7.5e-2): + """guess whether or not a channel is signed by comparing range to min and max values. + Parameters ------------- - d0: Input data array - - + 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 ------- - A boolean; True if the data is signed and False if the data is not. + guess : bool + True if the data seems signed and False otherwise. """ - maxd0 = np.max(d0) - mind0 = np.min(d0) - - tolerance = (maxd0 - np.absolute(mind0)) / (maxd0 + np.absolute(mind0)) + maxc = chan.max() + minc = chan.min() + from ..data import Channel + if isinstance(chan, Channel): + null = chan.null + else: + null = 0 - if tolerance < 7.5 * 10 ** (-2): - if min(d0) > 0: - return False - else: - return True + # avoid zero division for comparison + bottom = np.abs(maxc + minc - 2 * null) + if not bottom: # (maxc-null)=-(minc-null), so probably signed + return maxc != null - else: - return False + # should be <~ 1 for unsigned data + comparison = np.abs(maxc - minc) / np.abs(maxc + minc - 2 * null) + return comparison < 1 + tol From 1386acc8fc803f1b225941e18fbb2bea9d8ee749 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Jan 2024 16:09:15 +0000 Subject: [PATCH 10/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- WrightTools/kit/_array.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index 8c625964..400679c0 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -438,7 +438,7 @@ def enforce_mask_shape(mask, shape): def guess_signed(chan, tol=7.5e-2): """guess whether or not a channel is signed by comparing range to min and max values. - + Parameters ------------- chan : array-like @@ -455,6 +455,7 @@ def guess_signed(chan, tol=7.5e-2): maxc = chan.max() minc = chan.min() from ..data import Channel + if isinstance(chan, Channel): null = chan.null else: @@ -462,7 +463,7 @@ def guess_signed(chan, tol=7.5e-2): # avoid zero division for comparison bottom = np.abs(maxc + minc - 2 * null) - if not bottom: # (maxc-null)=-(minc-null), so probably signed + if not bottom: # (maxc-null)=-(minc-null), so probably signed return maxc != null # should be <~ 1 for unsigned data From 5617a2ab352467b68a700a61e07bdf7f4ac94881 Mon Sep 17 00:00:00 2001 From: Daniel Kohler <11864045+ddkohler@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:25:45 -0600 Subject: [PATCH 11/15] tests pass correct the guess function --- WrightTools/kit/_array.py | 16 ++++++++-------- tests/kit/signed.py | 26 ++++++++++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index 400679c0..7f6c00c2 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -436,8 +436,8 @@ def enforce_mask_shape(mask, shape): return mask.max(axis=red, keepdims=True) -def guess_signed(chan, tol=7.5e-2): - """guess whether or not a channel is signed by comparing range to min and max values. +def guess_signed(chan, tol=1e-1): + """guess whether or not a channel is signed by examining min and max values. Parameters ------------- @@ -462,10 +462,10 @@ def guess_signed(chan, tol=7.5e-2): null = 0 # avoid zero division for comparison - bottom = np.abs(maxc + minc - 2 * null) - if not bottom: # (maxc-null)=-(minc-null), so probably signed - return maxc != null + bottom = np.abs(maxc-null) + np.abs(minc-null) + if not bottom: # (maxc-null)=-(minc-null) + return True - # should be <~ 1 for unsigned data - comparison = np.abs(maxc - minc) / np.abs(maxc + minc - 2 * null) - return comparison < 1 + tol + comparison = np.abs(maxc + minc - 2*null) / bottom + # should be < 1 if signed + return comparison < 1 - tol diff --git a/tests/kit/signed.py b/tests/kit/signed.py index 10a9b3f9..0ca7a7c6 100644 --- a/tests/kit/signed.py +++ b/tests/kit/signed.py @@ -1,22 +1,32 @@ -"""Test signed data.""" +"""Test guess_signed""" # --- import ------------------------------------------------------------------------------------- import numpy as np - import WrightTools as wt # --- test --------------------------------------------------------------------------------------- -def test_5(): - arr = np.array([-1, 0, 1]) - assert wt.kit.signed(arr) == True +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) -def test_5_multiple(): - arr = np.array([1, 3, 4, 11, 12]) - assert wt.kit.signed(arr) == False From 70fd692b4167baf3fadbb6e5be023021d0b5d7ce Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Jan 2024 17:28:20 +0000 Subject: [PATCH 12/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- WrightTools/kit/_array.py | 4 ++-- tests/kit/signed.py | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index 7f6c00c2..973257f8 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -462,10 +462,10 @@ def guess_signed(chan, tol=1e-1): null = 0 # avoid zero division for comparison - bottom = np.abs(maxc-null) + np.abs(minc-null) + 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 + comparison = np.abs(maxc + minc - 2 * null) / bottom # should be < 1 if signed return comparison < 1 - tol diff --git a/tests/kit/signed.py b/tests/kit/signed.py index 0ca7a7c6..d40de21e 100644 --- a/tests/kit/signed.py +++ b/tests/kit/signed.py @@ -12,9 +12,9 @@ def test_many_ranges(): - for (minmax, signed) in [ + for minmax, signed in [ ([-0.05, 1], False), - ([-1, 0.05], False), + ([-1, 0.05], False), ([-1, 1], True), ([0, 0], True), ([0, -1], False), @@ -25,8 +25,7 @@ def test_many_ranges(): def test_channel(): d = wt.Data() - chan = d.create_channel("chan", values=np.linspace(3,4,16).reshape(4,4)) + 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) - From 70980207d23fa2f1ceb1602af515bca870c5120f Mon Sep 17 00:00:00 2001 From: Daniel Kohler <11864045+ddkohler@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:32:04 -0600 Subject: [PATCH 13/15] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 443acc80..cffa47e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ 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) + ### Fixed - numpy deprecated the `np.float` alias, so use `np.float64` to be more precise From 89e5b60d69258696793fb4530d2b37af76b8bb5f Mon Sep 17 00:00:00 2001 From: Daniel Kohler <11864045+ddkohler@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:48:58 -0600 Subject: [PATCH 14/15] Update WrightTools/kit/_array.py --- WrightTools/kit/_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WrightTools/kit/_array.py b/WrightTools/kit/_array.py index 973257f8..15634cc8 100644 --- a/WrightTools/kit/_array.py +++ b/WrightTools/kit/_array.py @@ -203,7 +203,7 @@ def orthogonal(*args) -> bool: if hasattr(arg, "shape"): args[i] = arg.shape for s in zip(*args): - if np.product(s) != max(s): + if np.prod(s) != max(s): return False return True From afdbed2432aee262abb37ea1f60c59b3440d8c92 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 30 Jan 2024 19:21:26 +0000 Subject: [PATCH 15/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/kit/signed.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/kit/signed.py b/tests/kit/signed.py index d40de21e..f56f52d0 100644 --- a/tests/kit/signed.py +++ b/tests/kit/signed.py @@ -1,6 +1,5 @@ """Test guess_signed""" - # --- import -------------------------------------------------------------------------------------