Skip to content

Commit

Permalink
don't store empty columns in HDRTAB (#8958)
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram authored Nov 16, 2024
2 parents 315a2e5 + e6452da commit dfbb927
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions changes/8958.general.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When blending metadata don't store columns containing all missing value (nans).
12 changes: 10 additions & 2 deletions jwst/model_blender/_tablebuilder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import numpy as np


class _MissingValueType:
pass

_MISSING_VALUE = _MissingValueType()


def _convert_dtype(value):
"""Convert numarray column dtype into YAML-compatible format description"""
if 'U' in value:
Expand Down Expand Up @@ -99,7 +105,7 @@ def header_to_row(self, header):

def _add_row(self, row):
for attr, col in self.attr_to_column.items():
self.columns[col].append(row[attr] if attr in row else np.nan)
self.columns[col].append(row[attr] if attr in row else _MISSING_VALUE)

def build_table(self):
"""
Expand All @@ -113,6 +119,8 @@ def build_table(self):
arrays = []
table_dtype = []
for col, items in self.columns.items():
arrays.append(np.array(items))
if all((i is _MISSING_VALUE for i in items)):
continue
arrays.append(np.array([np.nan if i is _MISSING_VALUE else i for i in items]))
table_dtype.append((col, arrays[-1].dtype))
return np.rec.fromarrays(arrays, dtype=table_dtype)
6 changes: 6 additions & 0 deletions jwst/model_blender/tests/test_blend.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
# even if the metadata is defined in the schema
KNOWN_MISSING_FIRSTS = [None, '1', '2']

# None of the below test data defines this attribute.
# It is expected to be missing from the resulting
# table (as is checked below).
MISSING_COLUMN = "TIME-OBS"


def _make_data():
"""Create a set of input models to blendmeta
Expand Down Expand Up @@ -195,6 +200,7 @@ def test_blendtab(blend):
# Ensure all the expected FITS keywords are in the table.
colnames = set(newtab.dtype.fields)
assert not fits_expected.difference(colnames)
assert MISSING_COLUMN not in colnames
for col in colnames:
if col in input_values:
assert newtab[col] == input_values[col]
Expand Down

0 comments on commit dfbb927

Please sign in to comment.