From 27c658c03be61679ae2c9deb3e66f7cbc420efa3 Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sat, 23 Mar 2024 07:31:27 -0400 Subject: [PATCH 1/4] Use shape attribute to determine ndarray-like attribute --- cheta/fetch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cheta/fetch.py b/cheta/fetch.py index 7088f994..551de9cf 100644 --- a/cheta/fetch.py +++ b/cheta/fetch.py @@ -719,7 +719,7 @@ def _get_comp_data(self, comp_cls): self.colnames = [ attr for attr, val in attrs.items() - if (isinstance(val, np.ndarray) and len(val) == len(attrs["times"])) + if (hasattr(val, "shape") and len(val) == len(attrs["times"])) ] # Apply attributes to self From 2fc6807f337ef4f37e4744525798cc75585ff492 Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sat, 23 Mar 2024 07:31:56 -0400 Subject: [PATCH 2/4] Filter out Quat normalization warning --- cheta/derived/comps.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cheta/derived/comps.py b/cheta/derived/comps.py index a1e8579e..332276ea 100644 --- a/cheta/derived/comps.py +++ b/cheta/derived/comps.py @@ -22,6 +22,7 @@ import functools import re +import warnings import astropy.table as tbl import numpy as np @@ -428,7 +429,11 @@ def get_msid_attrs(self, tstart: float, tstop: float, msid: str, msid_args: tupl q4 = np.sqrt((1.0 - q1**2 - q2**2 - q3**2).clip(0.0)) q = np.array([q1, q2, q3, q4]).transpose() - quat = Quat(q=normalize(q)) + with warnings.catch_warnings(): + warnings.filterwarnings( + "ignore", message="Normalizing quaternion with zero norm" + ) + quat = Quat(q=normalize(q)) bads = np.zeros_like(q1, dtype=bool) for msid in msids: bads |= dat[msid].bads From 927be989da0f4044f7b0c2cb41a05243e2c0e622 Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sat, 23 Mar 2024 10:12:18 -0400 Subject: [PATCH 3/4] Avoid user-facing resource warning for unclosed file pickle.load(open(file, "rb")) does not close the file --- cheta/fetch.py | 3 ++- cheta/units.py | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cheta/fetch.py b/cheta/fetch.py index 551de9cf..1333a088 100644 --- a/cheta/fetch.py +++ b/cheta/fetch.py @@ -295,7 +295,8 @@ def load_msid_names(all_msid_names_files): all_colnames = dict() for k, msid_names_file in all_msid_names_files.items(): try: - all_colnames[k] = pickle.load(open(os.path.join(*msid_names_file), "rb")) + with open(os.path.join(*msid_names_file), "rb") as fh: + all_colnames[k] = pickle.load(fh) except IOError: pass return all_colnames diff --git a/cheta/units.py b/cheta/units.py index 2de722c6..4b7137dd 100644 --- a/cheta/units.py +++ b/cheta/units.py @@ -61,7 +61,8 @@ def emit(self, record): units = {} units["system"] = "cxc" -units["cxc"] = pickle.load(open(os.path.join(module_dir, "units_cxc.pkl"), "rb")) +with open(os.path.join(module_dir, "units_cxc.pkl"), "rb") as fh: + units["cxc"] = pickle.load(fh) # Equivalent unit descriptors used in 'eng' and 'cxc' units @@ -222,7 +223,8 @@ def load_units(unit_system): if unit_system not in units: filename = os.path.join(module_dir, "units_{0}.pkl".format(unit_system)) - units[unit_system] = pickle.load(open(filename, "rb")) + with open(filename, "rb") as fh: + units[unit_system] = pickle.load(fh) def set_units(unit_system): From 33660ad6828420e3bbf3dbef97522e6d4aeefe20 Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sat, 23 Mar 2024 10:13:08 -0400 Subject: [PATCH 4/4] Add a test for computed quat bad vals issue --- cheta/tests/test_comps.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cheta/tests/test_comps.py b/cheta/tests/test_comps.py index c9109648..e28aa7f3 100644 --- a/cheta/tests/test_comps.py +++ b/cheta/tests/test_comps.py @@ -2,6 +2,8 @@ """Test that computed MSIDs work as expected.""" +import warnings + import astropy.units as u import numpy as np import pytest @@ -269,6 +271,28 @@ def test_quat_comp(msid, maude, offset): assert isinstance(datq.vals, Quat) +def test_quat_comp_bad_times(): + """Test bad time data on 2024:264. All four quats have zero value and are bad. + + The bad sample times are ['2024:064:09:27:02.652' '2024:064:09:27:03.677']. + """ + start = "2024:064:09:26:00" + stop = "2024:064:09:28:00" + # Assert no warnings despite quat with zero normalization. The zero-norm samples are + # marked bad. + with warnings.catch_warnings(): + warnings.simplefilter("error") # Assert no warnings + dat = fetch_eng.MSID("quat_aoattqt", start, stop) + + assert np.count_nonzero(dat.bads) == 2 + assert len(dat.vals) == len(dat.times) + + dat2 = fetch_eng.Msid("quat_aoattqt", start, stop) + assert dat2.bads is None # After Msid filtering + assert len(dat2.vals) == len(dat2.times) + assert len(dat2.vals) == len(dat.vals) - 2 + + def test_pitch_comp(): """Test pitch_comp during a time with NPNT, NMAN, NSUN and Safe Sun""" start = "2022:293"