Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nan warning in get_bitinformation; Closes #200 #260

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGELOG

X.X.X (unreleased)
------------------

* Add nan warning in :py:func:`xbitinfo.xbitinfo.get_bitinformation` (:issue:`200`, :pr:`260`) `Timothy Hodson` _.
* 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`_.
* Implement basic retrieval of bitinformation in python as alternative to julia implementation (:pr:`156`, :issue:`155`, :pr:`126`, :issue:`125`) `Hauke Schulz`_ with helpful comments from `Milan Klöwer`_.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_get_bitinformation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for `xbitinfo` package."""
import os
import warnings

import numpy as np
import pytest
Expand Down Expand Up @@ -215,6 +216,14 @@ def test_get_bitinformation_dim_list(rasm, implementation):
assert (bi.dim == ["x", "y"]).all()


def test_get_bitinformation_nan_warning():
data = xr.Dataset({"var": ("x", np.array([1, 2, np.nan, 4, 5]))})

with warnings.catch_warnings(record=True) as w:
xb.get_bitinformation(data, implementation="python")
assert len(w) >= 1


def test_get_bitinformation_keep_attrs(rasm):
bi = xb.get_bitinformation(rasm, dim=["x", "y"]).Tair
assert "units" in bi.attrs
Expand Down
3 changes: 3 additions & 0 deletions xbitinfo/xbitinfo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import os
import warnings

import numpy as np
import xarray as xr
Expand Down Expand Up @@ -184,6 +185,8 @@ def get_bitinformation( # noqa: C901
"""
if implementation == "julia" and not julia_installed:
raise ImportError('Please install julia or use implementation="python".')
if ds.isnull().any():
warnings.warn("This dataset contains NaNs, which can yield unexpected results.")
if dim is None and axis is None:
# gather bitinformation on all axis
return _get_bitinformation_along_dims(
Expand Down
Loading