Skip to content

Commit

Permalink
Add nan integer (ARM-DOE#842)
Browse files Browse the repository at this point in the history
* Checking data type befor adding in NaN since will need the data type to be float. Upconverting if data is type integer.

* Testing for upconverting data in add_in_nan() when data is not type float.

* PEP8 for Black Linting.
  • Loading branch information
kenkehoe authored Jul 3, 2024
1 parent bc3cf0b commit 2722d32
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions act/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,14 @@ def add_in_nan(time, data):
mode = stats.mode(diff, keepdims=True).mode[0]
except TypeError:
mode = stats.mode(diff).mode[0]

index = np.where(diff > (2.0 * mode))

# If the data is not float time and we try to insert a NaN it will
# not auto upconvert the data. Need to convert before inserting NaN.
if len(index) > 0 and np.issubdtype(data.dtype, np.integer):
data = data.astype('float32')

offset = 0
for i in index[0]:
corr_i = i + offset
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions tests/plotting/test_timeseriesdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,34 @@ def test_add_nan_line():
matplotlib.pyplot.close(display.fig)


@pytest.mark.mpl_image_compare(tolerance=10)
def test_add_nan_line_integer():
data = np.arange(100, dtype=np.int32)
time = np.array('2019-11-01T00:00:00', dtype='datetime64[m]') + np.arange(data.size)
time = time.astype('datetime64[ns]') # Only done to stop a warning appearing

# Remove data to produce a gap
data = np.delete(data, np.arange(50, 60), axis=0)
time = np.delete(time, np.arange(50, 60), axis=0)
data = np.delete(data, np.arange(70, 75), axis=0)
time = np.delete(time, np.arange(70, 75), axis=0)

ds = xr.Dataset(
data_vars={'data': ('time', data, {'long_name': 'Data values', 'units': 'degC'})},
coords={'time': ('time', time, {'long_name': 'Time in UTC'})},
)

display = TimeSeriesDisplay({'test_datastream': ds}, figsize=(15, 10), subplot_shape=(1,))
display.plot('data', subplot_index=(0,), add_nan=True, marker='.', markersize=20, linewidth=5)

assert np.issubdtype(ds['data'].dtype, np.integer)

try:
return display.fig
finally:
matplotlib.pyplot.close(display.fig)


@pytest.mark.mpl_image_compare(tolerance=10)
def test_timeseries_invert():
ds = act.io.arm.read_arm_netcdf(sample_files.EXAMPLE_IRT25m20s)
Expand Down

0 comments on commit 2722d32

Please sign in to comment.