Skip to content

Commit aabc9ca

Browse files
authored
fix: put back patches to prevent underflows from being cast to zero (#514)
1 parent d33f801 commit aabc9ca

4 files changed

Lines changed: 104 additions & 2 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Changes
77
- Added development dependencies to the `pyproject.toml`.
88
- Reformatted `CHANGES.md` for clarity and added linting
99
to ensure consistent formatting in the future.
10+
- Added back patch of `cfitsio` to prevent subnormal floats from being
11+
cast to zero for bundled builds.
1012

1113
Bug Fixes
1214

fitsio/tests/test_image_compression.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,9 @@ def test_image_compression_nulls_patches_with_subnormal(
792792
# however, on read, we send nullcheck=NAN and as a side
793793
# effect the subnormal float value in this row gets truncated
794794
# to zero
795-
np.testing.assert_array_equal(read_data[1, 1:], 0.0)
796-
np.testing.assert_array_equal(read_slice1[1:], 0.0)
795+
if not cfitsio_is_bundled():
796+
np.testing.assert_array_equal(read_data[1, 1:], 0.0)
797+
np.testing.assert_array_equal(read_slice1[1:], 0.0)
797798

798799
# this does not happen for row index 2 which doesn't have
799800
# subnormal float values
@@ -887,6 +888,27 @@ def test_image_compression_read_from_osx_arm64():
887888
np.testing.assert_array_equal(data, cdata)
888889

889890

891+
def test_image_compression_gzip_subnormal_cast_to_zero():
892+
# test code from astrofrog in https://github.com/esheldon/fitsio/issues/513
893+
data = np.zeros(
894+
(5, 1), dtype='>f4'
895+
) # a single 5x1 tile, big-endian float32
896+
data[:, 0] = [134.97459, 248.02034, 183.40105, 57.59670, 216.31425]
897+
898+
with tempfile.TemporaryDirectory() as tmpdir:
899+
fn = os.path.join(tmpdir, "mini.fit")
900+
with FITS(fn, "rw") as f:
901+
f.write(
902+
data, compress="GZIP_1", tile_dims=(5, 1), qlevel=5, qmethod=-1
903+
)
904+
905+
back = read(fn)
906+
if cfitsio_is_bundled():
907+
assert not back.ravel()[0] == 0, back.ravel()
908+
else:
909+
assert back.ravel()[0] == 0, back.ravel()
910+
911+
890912
if __name__ == '__main__':
891913
test_compressed_seed(
892914
compress='rice',

patches/getcold.c.patch

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--- a/getcold.c
2+
+++ b/getcold.c
3+
@@ -1418,7 +1418,7 @@ int fffr4r8(float *input, /* I - array of values to be converted */
4+
nullarray[ii] = 1;
5+
}
6+
else /* it's an underflow */
7+
- output[ii] = 0;
8+
+ output[ii] = (double) input[ii];
9+
}
10+
else
11+
output[ii] = (double) input[ii];
12+
@@ -1439,7 +1439,7 @@ int fffr4r8(float *input, /* I - array of values to be converted */
13+
nullarray[ii] = 1;
14+
}
15+
else /* it's an underflow */
16+
- output[ii] = zero;
17+
+ output[ii] = input[ii] * scale + zero;
18+
}
19+
else
20+
output[ii] = input[ii] * scale + zero;
21+
@@ -1519,7 +1519,7 @@ int fffr8r8(double *input, /* I - array of values to be converted */
22+
}
23+
}
24+
else /* it's an underflow */
25+
- output[ii] = 0;
26+
+ output[ii] = input[ii];
27+
}
28+
else
29+
output[ii] = input[ii];
30+
@@ -1544,7 +1544,7 @@ int fffr8r8(double *input, /* I - array of values to be converted */
31+
}
32+
}
33+
else /* it's an underflow */
34+
- output[ii] = zero;
35+
+ output[ii] = input[ii] * scale + zero;
36+
}
37+
else
38+
output[ii] = input[ii] * scale + zero;

patches/getcole.c.patch

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--- a/getcole.c
2+
+++ b/getcole.c
3+
@@ -1425,7 +1425,7 @@ int fffr4r4(float *input, /* I - array of values to be converted */
4+
}
5+
}
6+
else /* it's an underflow */
7+
- output[ii] = 0;
8+
+ output[ii] = input[ii];
9+
}
10+
else
11+
output[ii] = input[ii];
12+
@@ -1450,7 +1450,7 @@ int fffr4r4(float *input, /* I - array of values to be converted */
13+
}
14+
}
15+
else /* it's an underflow */
16+
- output[ii] = (float) zero;
17+
+ output[ii] = (float) (input[ii] * scale + zero);
18+
}
19+
else
20+
output[ii] = (float) (input[ii] * scale + zero);
21+
@@ -1549,8 +1549,8 @@ int fffr8r4(double *input, /* I - array of values to be converted */
22+
else
23+
nullarray[ii] = 1;
24+
}
25+
- else /* it's an underflow */
26+
- output[ii] = 0;
27+
+ else
28+
+ output[ii] = (float) input[ii];
29+
}
30+
else
31+
{
32+
@@ -1596,7 +1596,7 @@ int fffr8r4(double *input, /* I - array of values to be converted */
33+
output[ii] = FLT_MAX;
34+
}
35+
else
36+
- output[ii] = (float) zero;
37+
+ output[ii] = (float) (input[ii] * scale + zero);
38+
}
39+
}
40+
else

0 commit comments

Comments
 (0)