Skip to content

Commit

Permalink
set default masked_value to None for integer types
Browse files Browse the repository at this point in the history
- add integer test
- default masked value to None if integer type given
  • Loading branch information
observingClouds authored Aug 4, 2024
1 parent 914c086 commit cc661d3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
X.X.X (unreleased)
------------------

* Set default masked value to None for integers (:pr:`289`) `Hauke Schulz`_.
* Add basic filter to remove artificial information from bitinformation (:pr:`280`, :issue:`209`) `Ishaan Jain`_.
* Add support for additional datatypes in :py:func:`xbitinfo.xbitinfo.plot_bitinformation` (:pr:`218`, :issue:`168`) `Hauke Schulz`_.
* Drop python 3.8 support and add python 3.11 (:pr:`175`) `Hauke Schulz`_.
Expand Down
13 changes: 9 additions & 4 deletions tests/test_get_bitinformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,19 @@ def test_get_bitinformation_label(rasm, implementation):


@pytest.mark.parametrize("implementation", ["julia", "python"])
@pytest.mark.parametrize("dtype", ["float64", "float32", "float16"])
@pytest.mark.parametrize("dtype", ["float64", "float32", "float16", "int16"])
def test_get_bitinformation_dtype(rasm, dtype, implementation):
"""Test xb.get_bitinformation returns correct number of bits depending on dtype."""
dtype = np.dtype(dtype)
ds = rasm.astype(dtype)
v = list(ds.data_vars)[0]
dtype_bits = dtype.replace("float", "")
assert len(xb.get_bitinformation(ds, dim="x")[v].coords["bit" + dtype]) == int(
dtype_bits
if dtype.kind == "f":
dtype_bits = np.finfo(dtype).bits
elif dtype.kind == "i" or dtype.kind == "u":
dtype_bits = np.iinfo(dtype).bits
assert (
len(xb.get_bitinformation(ds, dim="x")[v].coords["bit" + str(dtype)])
== dtype_bits
)


Expand Down
10 changes: 9 additions & 1 deletion xbitinfo/xbitinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,15 @@ def _get_bitinformation_kwargs_handler(da, kwargs):
"""Helper function to preprocess kwargs args of :py:func:`xbitinfo.xbitinfo.get_bitinformation`."""
kwargs_var = kwargs.copy()
if "masked_value" not in kwargs_var:
kwargs_var["masked_value"] = f"convert({str(da.dtype).capitalize()},NaN)"
if da.dtype.kind == "i" or da.dtype.kind == "u":
logging.warning(
"No masked value given for integer type variable. Assuming no mask to apply."
)
kwargs_var["masked_value"] = "nothing"
elif da.dtype.kind == "f":
kwargs_var["masked_value"] = f"convert({str(da.dtype).capitalize()},NaN)"
else:
raise ValueError(f"Dtype kind ({da.dtype.kind}) not supported.")
elif kwargs_var["masked_value"] is None:
kwargs_var["masked_value"] = "nothing"
if "set_zero_insignificant" not in kwargs_var:
Expand Down

0 comments on commit cc661d3

Please sign in to comment.