Skip to content
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 changes/738.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix handling of logical/int8 FITS_rec columns when provided boolean values.
12 changes: 11 additions & 1 deletion src/stdatamodels/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,17 @@ def _as_fitsrec(val):
else:
coldefs = fits.ColDefs(val)
uint = any(c._pseudo_unsigned_ints for c in coldefs)
fits_rec = fits.FITS_rec(val)
if any(c.format == "L" for c in coldefs):
# Copy so we can modify the values to match what astropy expects.
fits_rec = fits.FITS_rec(val.copy())
for c in coldefs:
if c.format == "L":
d = fits_rec[c.name]
m = d.astype(bool)
d[m] = ord("T")
d[~m] = ord("F")
else:
fits_rec = fits.FITS_rec(val)
fits_rec._coldefs = coldefs
# FITS_rec needs to know if it should be operating in pseudo-unsigned-ints mode,
# otherwise it won't properly convert integer columns with TZEROn before saving.
Expand Down
18 changes: 18 additions & 0 deletions tests/jwst/datamodels/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,24 @@ def test_amioi_model_extra_meta(tmp_path, oifits_ami_model):
oifits_ami_model.save(fn)


@pytest.mark.parametrize("value", [True, False, 0, 1, ord("T"), ord("F")])
def test_amioi_logical_flag(tmp_path, value, oifits_ami_model):
"""
Test that int8 stored "FLAG" columns accept bools.

This is a targeted test to reproduce a bug found in:
https://github.com/spacetelescope/stdatamodels/issues/735
"""
oifits_ami_model.t3[0][-1] = value
assert oifits_ami_model.t3[0][-1] == bool(value)

# make sure it round-trips
fn = tmp_path / "test.fits"
oifits_ami_model.save(fn)
with datamodels.open(fn) as model:
assert model.t3[0][-1] == bool(value)


def test_dq_def_roundtrip(tmp_path, test_data_path):
"""
Open a MaskModel with a defined DQ array and dq_def that modifies the
Expand Down
4 changes: 3 additions & 1 deletion tests/schemas/table_model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ allOf:
properties:
table:
fits_hdu: TABLE
default: [0, 42, NaN, "foo", 42.0, 0.0, 0.0, 0.0]
default: [False, 0, 42, NaN, "foo", 42.0, 0.0, 0.0, 0.0]
Comment thread
braingram marked this conversation as resolved.
datatype:
- name: int8_column
datatype: int8
- name: int16_column
datatype: int16
- name: uint16_column
Expand Down
12 changes: 12 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ def test_get_dtype_table():
assert isinstance(dtype, np.dtype)
expected_dtype = np.dtype(
[
("int8_column", "<i1"),
("int16_column", "<i2"),
("uint16_column", "<u2"),
("float32_column", "<f4"),
Expand Down Expand Up @@ -375,6 +376,17 @@ def test_multivalued_default_table_schema():
max_ndim_col = dm.table["float32_column_with_max_ndim"]
assert max_ndim_col.shape == (10, 0, 0)

# test logical type
assert np.all(dm.table["int8_column"] == False)


def test_logical_column():
dm = TableModel((10,))
dm.table["int8_column"][:] = True
assert np.all(dm.table["int8_column"] == True)
dm.table["int8_column"][:] = False
assert np.all(dm.table["int8_column"] == False)


def test_multivalued_default_table_schema_bad():
"""Test error raise if the default does not match the array type"""
Expand Down
4 changes: 4 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def test_table_array_shape_ndim(filename, tmp_path):
with TableModel() as x:
x.table = [
(
False,
-42,
42000,
37.5,
Expand All @@ -103,6 +104,7 @@ def test_table_array_shape_ndim(filename, tmp_path):
)
]
assert x.table.dtype == [
("int8_column", "=i1"),
("int16_column", "=i2"),
("uint16_column", "=u2"),
("float32_column", "=f4"),
Expand All @@ -119,6 +121,7 @@ def test_table_array_shape_ndim(filename, tmp_path):
assert np.can_cast(
x.table.dtype,
[
("int8_column", "=i1"),
("int16_column", "=i2"),
("uint16_column", "=u2"),
("float32_column", "=f4"),
Expand All @@ -135,6 +138,7 @@ def test_table_array_shape_ndim(filename, tmp_path):
with pytest.raises(ValueError):
x.table = [
(
False,
-42,
42000,
37.5,
Expand Down
Loading