Skip to content

Commit b202ad6

Browse files
committed
FIX: avoid warnings for (fully) non-numeric chunks (fixes #311)
1 parent 655fa93 commit b202ad6

3 files changed

Lines changed: 61 additions & 42 deletions

File tree

‎doc/source/changes/version_0_35_1.rst.inc‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Fixes
4040
their length and/or iterating them, such as scalar Groups (closes
4141
:editor_issue:`313`).
4242

43+
* avoid warnings when displaying data with any column entirely non-numeric
44+
(including NaN). Closes :editor_issue:`311`.
45+
4346
* fixed the mechanism writing warning/error messages happening during the
4447
editor initialization. The errors are now correctly written in the user
4548
TEMP directory / larray-editor-stderr.log

‎larray_editor/arrayadapter.py‎

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
# are probably the most likely to be re-visited) and read from the file
3535
# if the user requests some data outside of those chunks
3636
import collections.abc
37+
import warnings
38+
3739
import logging
3840
import sys
3941
import os
@@ -615,14 +617,16 @@ def update_finite_min_max_values(self, finite_values: np.ndarray,
615617
h_stop: int, v_stop: int):
616618
"""can return either two floats or two arrays"""
617619

618-
# we need initial to support empty arrays
619-
vmin = np.nanmin(finite_values, initial=np.nan)
620-
vmax = np.nanmax(finite_values, initial=np.nan)
620+
with warnings.catch_warnings():
621+
warnings.simplefilter("ignore", category=RuntimeWarning)
622+
# we need initial to support empty arrays
623+
vmin = np.nanmin(finite_values, initial=np.nan)
624+
vmax = np.nanmax(finite_values, initial=np.nan)
621625

622-
self.vmin = (
623-
np.nanmin([self.vmin, vmin]) if self.vmin is not None else vmin)
624-
self.vmax = (
625-
np.nanmax([self.vmax, vmax]) if self.vmax is not None else vmax)
626+
self.vmin = (
627+
np.nanmin([self.vmin, vmin]) if self.vmin is not None else vmin)
628+
self.vmax = (
629+
np.nanmax([self.vmax, vmax]) if self.vmax is not None else vmax)
626630
return self.vmin, self.vmax
627631

628632
def get_axes_area(self):
@@ -969,33 +973,38 @@ def update_finite_min_max_values(self, finite_values: np.ndarray,
969973
assert isinstance(self.vmin, dict) and isinstance(self.vmax, dict)
970974
assert h_stop >= h_start
971975

972-
# per column => axis=0
973-
local_vmin = np.nanmin(finite_values, axis=0, initial=np.nan)
974-
local_vmax = np.nanmax(finite_values, axis=0, initial=np.nan)
975976
num_cols = h_stop - h_start
976-
assert local_vmin.shape == (num_cols,), \
977-
(f"unexpected shape: {local_vmin.shape} ({finite_values.shape=}) vs "
978-
f"{(num_cols,)} ({h_start=} {h_stop=})")
979977
# vmin or self.vmin can both be nan (if the whole section data
980978
# is/was nan)
981979
global_vmin = self.vmin
982980
global_vmax = self.vmax
983981
vmin_slice = np.empty(num_cols, dtype=np.float64)
984982
vmax_slice = np.empty(num_cols, dtype=np.float64)
985-
for global_col_idx in range(h_start, h_stop):
986-
local_col_idx = global_col_idx - h_start
987-
988-
col_min = np.nanmin([global_vmin.get(global_col_idx, np.nan),
989-
local_vmin[local_col_idx]])
990-
# update the global vmin dict inplace
991-
global_vmin[global_col_idx] = col_min
992-
vmin_slice[local_col_idx] = col_min
993-
994-
col_max = np.nanmax([global_vmax.get(global_col_idx, np.nan),
995-
local_vmax[local_col_idx]])
996-
# update the global vmax dict inplace
997-
global_vmax[global_col_idx] = col_max
998-
vmax_slice[local_col_idx] = col_max
983+
984+
# per column => axis=0
985+
with warnings.catch_warnings():
986+
warnings.simplefilter("ignore", category=RuntimeWarning)
987+
988+
local_vmin = np.nanmin(finite_values, axis=0, initial=np.nan)
989+
local_vmax = np.nanmax(finite_values, axis=0, initial=np.nan)
990+
991+
assert local_vmin.shape == (num_cols,), \
992+
(f"unexpected shape: {local_vmin.shape} ({finite_values.shape=}) vs "
993+
f"{(num_cols,)} ({h_start=} {h_stop=})")
994+
for global_col_idx in range(h_start, h_stop):
995+
local_col_idx = global_col_idx - h_start
996+
997+
col_min = np.nanmin([global_vmin.get(global_col_idx, np.nan),
998+
local_vmin[local_col_idx]])
999+
# update the global vmin dict inplace
1000+
global_vmin[global_col_idx] = col_min
1001+
vmin_slice[local_col_idx] = col_min
1002+
1003+
col_max = np.nanmax([global_vmax.get(global_col_idx, np.nan),
1004+
local_vmax[local_col_idx]])
1005+
# update the global vmax dict inplace
1006+
global_vmax[global_col_idx] = col_max
1007+
vmax_slice[local_col_idx] = col_max
9991008
return vmin_slice, vmax_slice
10001009

10011010

@@ -1354,18 +1363,20 @@ def get_color_value(array, global_vmin, global_vmax, axis=None):
13541363
try:
13551364
finite_value = get_finite_numeric_values(array)
13561365

1357-
vmin = np.nanmin(finite_value, axis=axis)
1358-
if global_vmin is not None:
1359-
# vmin or global_vmin can both be nan (if the whole section data is/was nan)
1360-
global_vmin = np.nanmin([global_vmin, vmin], axis=axis)
1361-
else:
1362-
global_vmin = vmin
1363-
vmax = np.nanmax(finite_value, axis=axis)
1364-
if global_vmax is not None:
1365-
# vmax or global_vmax can both be nan (if the whole section data is/was nan)
1366-
global_vmax = np.nanmax([global_vmax, vmax], axis=axis)
1367-
else:
1368-
global_vmax = vmax
1366+
with warnings.catch_warnings():
1367+
warnings.simplefilter("ignore", category=RuntimeWarning)
1368+
vmin = np.nanmin(finite_value, axis=axis)
1369+
if global_vmin is not None:
1370+
# vmin or global_vmin can both be nan (if the whole section data is/was nan)
1371+
global_vmin = np.nanmin([global_vmin, vmin], axis=axis)
1372+
else:
1373+
global_vmin = vmin
1374+
vmax = np.nanmax(finite_value, axis=axis)
1375+
if global_vmax is not None:
1376+
# vmax or global_vmax can both be nan (if the whole section data is/was nan)
1377+
global_vmax = np.nanmax([global_vmax, vmax], axis=axis)
1378+
else:
1379+
global_vmax = vmax
13691380
color_value = scale_to_01range(finite_value, global_vmin, global_vmax)
13701381
except (ValueError, TypeError):
13711382
global_vmin = None

‎larray_editor/utils.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,17 +793,22 @@ def num_int_digits(value):
793793
794794
Examples
795795
--------
796+
>>> num_int_digits(0)
797+
1
796798
>>> num_int_digits(1)
797799
1
798800
>>> num_int_digits(99)
799801
2
800802
>>> num_int_digits(-99.1)
801803
2
802-
>>> num_int_digits(np.array([1, 99, -99.1]))
803-
array([1, 2, 2])
804+
>>> num_int_digits(np.array([1, 99, 0, -99.1]))
805+
array([1, 2, 1, 2])
804806
"""
805807
value = abs(value)
806-
log10 = np.where(value > 0, np.log10(value), 0)
808+
# avoid a warning for log10(0) and log10(negative) because we will handle
809+
# these cases with np.where anyway
810+
with np.errstate(divide='ignore'):
811+
log10 = np.where(value > 0, np.log10(value), 0)
807812
res = np.where(np.isinf(log10), MAX_INT_DIGITS,
808813
# maximum(..., 1) because there must be at least one
809814
# integer digit (the 0 in 0.00..X)

0 commit comments

Comments
 (0)