Skip to content

Commit

Permalink
Fixes to get it to work for NumPy 2. (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA authored Oct 23, 2024
1 parent 2fc84bb commit c69d777
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
5 changes: 2 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ url = https://github.com/ArtifactDB/dolomite-base
# Add here related links, for example:
project_urls =
Documentation = https://github.com/ArtifactDB/dolomite-base
# Source = https://github.com/pyscaffold/pyscaffold/
Source = https://github.com/ArtifactDB/dolomite-base
# Changelog = https://pyscaffold.org/en/latest/changelog.html
# Tracker = https://github.com/pyscaffold/pyscaffold/issues
# Conda-Forge = https://anaconda.org/conda-forge/pyscaffold
Expand Down Expand Up @@ -50,13 +50,12 @@ python_requires = >=3.8
install_requires =
importlib-metadata; python_version<"3.8"
biocutils>=0.1.5
biocframe>=0.5.10
biocframe>=0.5.11
numpy
jsonschema
dolomite-schemas>=0.0.8
h5py


[options.packages.find]
where = src
exclude =
Expand Down
2 changes: 1 addition & 1 deletion src/dolomite_base/choose_missing_placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def choose_missing_float_placeholder(x: Sequence[float], dtype: type = numpy.flo
can_nan = False
break
if can_nan:
return dtype(numpy.NaN)
return dtype(numpy.nan)

if not numpy.inf in x:
return dtype(numpy.inf)
Expand Down
6 changes: 3 additions & 3 deletions src/dolomite_base/save_simple_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def _save_simple_list_recursive_MaskedConstant(x: np.ma.core.MaskedConstant, ext
if handle is None:
return { "type": "number", "values": None}
else:
_save_scalar_hdf5(handle, x=np.NaN, dtype=float, missing_placeholder=np.NaN)
_save_scalar_hdf5(handle, x=np.nan, dtype=float, missing_placeholder=np.nan)
return


Expand Down Expand Up @@ -431,9 +431,9 @@ def _save_simple_list_recursive_none(x: None, externals: list, handle):
def _sanitize_float_json(x):
if np.isnan(x):
return "NaN"
elif x == np.Inf:
elif x == np.inf:
return "Inf"
elif x == -np.Inf:
elif x == -np.inf:
return "-Inf"
return float(x)

Expand Down
4 changes: 2 additions & 2 deletions src/dolomite_base/write_vector_to_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def write_integer_vector_to_hdf5(
if not allow_float_promotion:
raise ValueError("cannot save out-of-range integers without type promotion")
if missed:
placeholder = numpy.NaN
placeholder = numpy.nan
x = _fill_with_placeholder(x, numpy.float64, placeholder)
else:
if missed:
Expand All @@ -142,7 +142,7 @@ def write_integer_vector_to_hdf5(
exceeds = True
if not allow_float_promotion:
raise ValueError("cannot find a suitable missing value placeholder without type promotion")
placeholder = numpy.NaN
placeholder = numpy.nan
x = _fill_with_placeholder(x, numpy.float64, placeholder)
else:
x = _fill_with_placeholder(x, placeholder.dtype.type, placeholder)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_data_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,15 @@ def test_data_frame_large_integers():

def test_data_frame_special_floats():
df = BiocFrame({
"sumire": [ np.NaN, np.Inf, -np.Inf ],
"kanon": np.array([ np.NaN, np.Inf, -np.Inf ]),
"chisato": np.ma.array([ np.NaN, 4, 5 ], mask=[0, 1, 1]) # distinguish NaN from missing.
"sumire": [ np.nan, np.inf, -np.inf ],
"kanon": np.array([ np.nan, np.inf, -np.inf ]),
"chisato": np.ma.array([ np.nan, 4, 5 ], mask=[0, 1, 1]) # distinguish NaN from missing.
})

def as_expected(x):
assert np.isnan(x[0])
assert x[1] == np.Inf
assert x[2] == -np.Inf
assert x[1] == np.inf
assert x[2] == -np.inf

def as_expected_masked(x):
assert np.isnan(x[0])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_load_vector_from_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_load_vector_from_hdf5_floats():
ghandle = handle.create_group("yourmom")
dl.write_float_vector_to_hdf5(ghandle, "FOO1", [1.1, 2.2, 3.3, 4.4])
dl.write_float_vector_to_hdf5(ghandle, "FOO2", [1.1, 2.2, 4.4, None])
dl.write_float_vector_to_hdf5(ghandle, "FOO3", [1.1, 2.2, 4.4, None, numpy.NaN]) # check for correct behavior with non-NaN placeholders
dl.write_float_vector_to_hdf5(ghandle, "FOO3", [1.1, 2.2, 4.4, None, numpy.nan]) # check for correct behavior with non-NaN placeholders
dl.write_integer_vector_to_hdf5(ghandle, "FOO4", [1, 2, 3, 2**32], h5type="u8") # check for correct promotion of integer storage types.

with h5py.File(path, "r") as handle:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_simple_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ def test_simple_list_large_integers():

def test_simple_list_special_float():
everything = {
"a": np.NaN,
"b": FloatList([np.Inf, -np.Inf, np.NaN])
"a": np.nan,
"b": FloatList([np.inf, -np.inf, np.nan])
}

# Stage as JSON.
Expand All @@ -202,8 +202,8 @@ def test_simple_list_special_float():

roundtrip = dl.read_object(dir)
assert np.isnan(roundtrip["a"])
assert roundtrip["b"][0] == np.Inf
assert roundtrip["b"][1] == -np.Inf
assert roundtrip["b"][0] == np.inf
assert roundtrip["b"][1] == -np.inf
assert np.isnan(roundtrip["b"][2])

# Stage as HDF5.
Expand All @@ -212,8 +212,8 @@ def test_simple_list_special_float():

roundtrip = dl.read_object(dir)
assert np.isnan(roundtrip["a"])
assert roundtrip["b"][0] == np.Inf
assert roundtrip["b"][1] == -np.Inf
assert roundtrip["b"][0] == np.inf
assert roundtrip["b"][1] == -np.inf
assert np.isnan(roundtrip["b"][2])


Expand Down

0 comments on commit c69d777

Please sign in to comment.