h5netcdf 1.7.1__py3-none-any.whl → 1.7.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of h5netcdf might be problematic. Click here for more details.

h5netcdf/_version.py CHANGED
@@ -28,7 +28,7 @@ version_tuple: VERSION_TUPLE
28
28
  commit_id: COMMIT_ID
29
29
  __commit_id__: COMMIT_ID
30
30
 
31
- __version__ = version = '1.7.1'
32
- __version_tuple__ = version_tuple = (1, 7, 1)
31
+ __version__ = version = '1.7.3'
32
+ __version_tuple__ = version_tuple = (1, 7, 3)
33
33
 
34
34
  __commit_id__ = commit_id = None
h5netcdf/core.py CHANGED
@@ -12,7 +12,7 @@ from packaging import version
12
12
 
13
13
  from . import __version__
14
14
  from .attrs import Attributes
15
- from .dimensions import Dimension, Dimensions
15
+ from .dimensions import Dimension, Dimensions, _check_classic_unlimited
16
16
  from .utils import (
17
17
  CompatibilityError,
18
18
  Frozen,
@@ -55,20 +55,13 @@ def _invalid_netcdf_feature(feature, allow):
55
55
 
56
56
  def _transform_1d_boolean_indexers(key):
57
57
  """Find and transform 1D boolean indexers to int"""
58
- # return key, if not iterable
59
- try:
60
- key = [
61
- (
62
- np.asanyarray(k).nonzero()[0]
63
- if isinstance(k, (np.ndarray, list)) and type(k[0]) in (bool, np.bool_)
64
- else k
65
- )
66
- for k in key
67
- ]
68
- except TypeError:
69
- return key
70
-
71
- return tuple(key)
58
+ # Convert 1D boolean arrays/lists to integer indices,
59
+ # leaving all other types unchanged
60
+ return tuple(
61
+ np.flatnonzero(arr) if arr.dtype == bool else k
62
+ for k in key
63
+ for arr in [np.asanyarray(k)]
64
+ )
72
65
 
73
66
 
74
67
  def _expanded_indexer(key, ndim):
@@ -526,13 +519,27 @@ class BaseVariable(BaseObject):
526
519
  h5ds_shape = self._h5ds.shape
527
520
  shape = self.shape
528
521
 
529
- # check for ndarray and list
530
522
  # see https://github.com/pydata/xarray/issues/7154
523
+ # see https://github.com/pydata/xarray/issues/10867
531
524
  # first get maximum index
532
- max_index = [
533
- max(k) + 1 if isinstance(k, (np.ndarray, list)) else k.stop
534
- for k in key0
535
- ]
525
+ def _get_max_index(k):
526
+ # Return the maximum index for ndarray, list, slice, or int,
527
+ # handling empty arrays/lists safely
528
+ if isinstance(k, np.ndarray):
529
+ if k.size == 0:
530
+ return None
531
+ return k.max() + 1
532
+ elif isinstance(k, list):
533
+ return max(k) + 1 if k else None
534
+ elif isinstance(k, slice):
535
+ return k.stop
536
+ elif isinstance(k, int):
537
+ return k + 1
538
+ else:
539
+ return None
540
+
541
+ max_index = [_get_max_index(k) for k in key0]
542
+
536
543
  # second convert to max shape
537
544
  # we take the minimum of shape vs max_index to not return
538
545
  # slices larger than expected data
@@ -1030,12 +1037,8 @@ class Group(Mapping):
1030
1037
 
1031
1038
  @dimensions.setter
1032
1039
  def dimensions(self, value):
1033
- if self._format == "NETCDF4_CLASSIC":
1034
- unlimited_dims = list(filter(lambda s: s in [None, 0], value.values()))
1035
- if len(unlimited_dims) > 1:
1036
- raise CompatibilityError(
1037
- "NETCDF4_CLASSIC format only allows one unlimited dimension."
1038
- )
1040
+ if self._root._format == "NETCDF4_CLASSIC":
1041
+ _check_classic_unlimited(value)
1039
1042
 
1040
1043
  for k, v in self._all_dimensions.maps[0].items():
1041
1044
  if k in value:
h5netcdf/dimensions.py CHANGED
@@ -1,12 +1,24 @@
1
1
  import weakref
2
2
  from collections import OrderedDict
3
- from collections.abc import MutableMapping
3
+ from collections.abc import Mapping, MutableMapping
4
4
 
5
5
  import numpy as np
6
6
 
7
7
  from .utils import CompatibilityError
8
8
 
9
9
 
10
+ def _check_classic_unlimited(value, unlimited=None):
11
+ if isinstance(value, Mapping):
12
+ multiple_unlimited_dimensions = sum(v in (None, 0) for v in value.values()) > 1
13
+ else:
14
+ multiple_unlimited_dimensions = unlimited and value in (None, 0)
15
+
16
+ if multiple_unlimited_dimensions:
17
+ raise CompatibilityError(
18
+ "Only one unlimited dimension allowed in the NETCDF4_CLASSIC format."
19
+ )
20
+
21
+
10
22
  class Dimensions(MutableMapping):
11
23
  def __init__(self, group):
12
24
  self._group_ref = weakref.ref(group)
@@ -25,14 +37,8 @@ class Dimensions(MutableMapping):
25
37
  raise RuntimeError("H5NetCDF: Write to read only")
26
38
  if name in self._objects:
27
39
  raise ValueError(f"dimension {name!r} already exists")
28
- if (
29
- size in [0, None]
30
- and self._unlimited()
31
- and self._group._format == "NETCDF4_CLASSIC"
32
- ):
33
- raise CompatibilityError(
34
- "Only one unlimited dimension allowed in the NETCDF4_CLASSIC format."
35
- )
40
+ if self._group._root._format == "NETCDF4_CLASSIC":
41
+ _check_classic_unlimited(size, self._unlimited())
36
42
 
37
43
  self._objects[name] = Dimension(self._group, name, size, create_h5ds=True)
38
44
 
@@ -211,7 +211,7 @@ def write_h5netcdf(tmp_netcdf, compression="gzip", format="NETCDF4"):
211
211
  if ds.data_model == "NETCDF4_CLASSIC":
212
212
  with raises(
213
213
  CompatibilityError,
214
- match="NETCDF4_CLASSIC format only allows one unlimited dimension.",
214
+ match="Only one unlimited dimension allowed",
215
215
  ):
216
216
  ds.dimensions = {"x": 4, "y": 5, "z": 6, "unlimited": None, "empty": 0}
217
217
 
@@ -1973,6 +1973,13 @@ def test_fancy_indexing(tmp_local_or_remote_netcdf):
1973
1973
  np.testing.assert_array_equal(ds["hello"][[4, 5, 6], 1], [41, 0, 0])
1974
1974
  np.testing.assert_array_equal(ds["hello"][slice(4, 7), 1], [41, 0, 0])
1975
1975
 
1976
+ # test empty slices
1977
+ # regression test for https://github.com/pydata/xarray/pull/10870
1978
+ empty = np.empty(0, dtype="int64")
1979
+ np.testing.assert_array_equal(ds["hello"][1, []], empty)
1980
+ np.testing.assert_array_equal(ds["hello"][1, np.array([], dtype="int")], empty)
1981
+ np.testing.assert_array_equal(ds["hello"][1, slice(0, 0)], empty)
1982
+
1976
1983
 
1977
1984
  def test_h5py_chunking(tmp_local_netcdf):
1978
1985
  with h5netcdf.File(tmp_local_netcdf, "w") as ds:
@@ -2903,7 +2910,7 @@ def test_raise_on_closed_file(tmp_local_netcdf):
2903
2910
  v = f.create_variable("hello", ("x",), float)
2904
2911
  v[:] = np.ones(5)
2905
2912
  f.close()
2906
- with pytest.raises(
2913
+ with raises(
2907
2914
  ValueError,
2908
2915
  match=f"I/O operation on <Closed h5netcdf.File>: '{tmp_local_netcdf}'",
2909
2916
  ):
@@ -3013,3 +3020,30 @@ def test_attributes_list(tmp_local_netcdf, attr):
3013
3020
  assert hf.attrs["foo"][0] == attr[0]
3014
3021
  assert hf.attrs["foo"][1] == attr[1]
3015
3022
  assert isinstance(hf.attrs["foo"], list)
3023
+
3024
+
3025
+ def test_group_dimensions(tmp_local_netcdf):
3026
+ # regression test for https://github.com/h5netcdf/h5netcdf/issues/293
3027
+ with h5netcdf.File(tmp_local_netcdf, mode="w") as f:
3028
+ group = f.create_group("data")
3029
+ dims = {"y": 3, "x": 3, "z": None, "z1": None}
3030
+ group.dimensions = dims
3031
+ assert list(group.dimensions) == ["y", "x", "z", "z1"]
3032
+ group.dimensions["z2"] = None
3033
+ assert list(group.dimensions) == ["y", "x", "z", "z1", "z2"]
3034
+
3035
+
3036
+ def test_group_dimensions_classic(tmp_local_netcdf):
3037
+ # regression test for https://github.com/h5netcdf/h5netcdf/issues/293
3038
+ with h5netcdf.File(tmp_local_netcdf, mode="w", format="NETCDF4_CLASSIC") as f:
3039
+ group = f.create_group("data")
3040
+ dims = {"y": 3, "x": 3, "z": None, "z1": None}
3041
+ with raises(CompatibilityError, match=r"Only one unlimited dimension allowed"):
3042
+ group.dimensions = dims
3043
+ assert list(group.dimensions) == []
3044
+ dims = {"y": 3, "x": 3, "z": None}
3045
+ group.dimensions = dims
3046
+ assert list(group.dimensions) == ["y", "x", "z"]
3047
+ with raises(CompatibilityError, match=r"Only one unlimited dimension allowed"):
3048
+ group.dimensions["z1"] = None
3049
+ assert list(group.dimensions) == ["y", "x", "z"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: h5netcdf
3
- Version: 1.7.1
3
+ Version: 1.7.3
4
4
  Summary: netCDF4 via h5py
5
5
  Author-email: Stephan Hoyer <shoyer@gmail.com>, Kai Mühlbauer <kmuehlbauer@wradlib.org>
6
6
  Maintainer-email: h5netcdf developers <devteam@h5netcdf.org>
@@ -0,0 +1,16 @@
1
+ h5netcdf/__init__.py,sha256=Y0EBCcmlJctwl1kCmj7yLijTVy9AioBTr2091vInAtw,456
2
+ h5netcdf/_version.py,sha256=7eQx5zuwit_DANpeuU5CY0KHuDtyAFB3ZgRhOo7lx8Y,704
3
+ h5netcdf/attrs.py,sha256=GSq2lzv9q0tEkJBqr2Hyrwo0YJnWZHxtLRhhG9NVf4w,4575
4
+ h5netcdf/core.py,sha256=jElZOQqCNE27hZytIGBzqkwZryc6rfhEJglu5N2jXsw,69961
5
+ h5netcdf/dimensions.py,sha256=BO126-7r9B0sF_bbcCSzTMnQY5wH7e_DSkbUO4F9q_o,8609
6
+ h5netcdf/legacyapi.py,sha256=MIZlht5Ad4hDFF1Slz2vXmKkgbv7Fhhf2YwNIe16Lfk,7682
7
+ h5netcdf/utils.py,sha256=btxKI-VZP-Wn0Rk_wmnhnUls-mrxO42w0s_uX_su4FI,6528
8
+ h5netcdf/tests/conftest.py,sha256=qS7XTZxos0NIRFtMCJwVEyx0paZw8Le1loPK1MtoQ_0,2350
9
+ h5netcdf/tests/pytest.ini,sha256=ruJxrLdCIA4bCPVuPQjxsLSlvVxuIsIakK6iQOmz-ak,107
10
+ h5netcdf/tests/test_h5netcdf.py,sha256=TriQc5J4oSCTIijgwhTf6bkVzzvwub6s-6eHfYn1IyI,118780
11
+ h5netcdf-1.7.3.dist-info/licenses/AUTHORS.txt,sha256=LTKzUh9o4Wc_oT3aFC48cyDCCP6tdm6VEV_6RrNy4uo,272
12
+ h5netcdf-1.7.3.dist-info/licenses/LICENSE,sha256=Xer1Jg8iL_n9Da0xt0S99blk6tsg9tee_JdgH1rWTjs,1505
13
+ h5netcdf-1.7.3.dist-info/METADATA,sha256=H1kMZigWMuWwgy_Np2u9u_Pp-7i6BHhMBk1N0JY1lDY,13396
14
+ h5netcdf-1.7.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
15
+ h5netcdf-1.7.3.dist-info/top_level.txt,sha256=Fb_KIpOE6MBqjSvxV1Ay7oYce1mdmQ1pO9JQJPDeGqg,9
16
+ h5netcdf-1.7.3.dist-info/RECORD,,
@@ -1,16 +0,0 @@
1
- h5netcdf/__init__.py,sha256=Y0EBCcmlJctwl1kCmj7yLijTVy9AioBTr2091vInAtw,456
2
- h5netcdf/_version.py,sha256=wcauG7D_isNqKUiP6L0WuH_5Dwe0oyUJa9LG3cC4WHk,704
3
- h5netcdf/attrs.py,sha256=GSq2lzv9q0tEkJBqr2Hyrwo0YJnWZHxtLRhhG9NVf4w,4575
4
- h5netcdf/core.py,sha256=Fg_68fF9vcyozlk-2vO1Qa0U_oRbvWfE976ax--CvxM,69698
5
- h5netcdf/dimensions.py,sha256=pxtt3ID55bTMp6djIpAka3RgXzioIUs9qBBgMqd9HhU,8336
6
- h5netcdf/legacyapi.py,sha256=MIZlht5Ad4hDFF1Slz2vXmKkgbv7Fhhf2YwNIe16Lfk,7682
7
- h5netcdf/utils.py,sha256=btxKI-VZP-Wn0Rk_wmnhnUls-mrxO42w0s_uX_su4FI,6528
8
- h5netcdf/tests/conftest.py,sha256=qS7XTZxos0NIRFtMCJwVEyx0paZw8Le1loPK1MtoQ_0,2350
9
- h5netcdf/tests/pytest.ini,sha256=ruJxrLdCIA4bCPVuPQjxsLSlvVxuIsIakK6iQOmz-ak,107
10
- h5netcdf/tests/test_h5netcdf.py,sha256=FWH2xJfIrBkUg7QEqOTvhrJYVvMU6flkDdkX-NWjI7s,117170
11
- h5netcdf-1.7.1.dist-info/licenses/AUTHORS.txt,sha256=LTKzUh9o4Wc_oT3aFC48cyDCCP6tdm6VEV_6RrNy4uo,272
12
- h5netcdf-1.7.1.dist-info/licenses/LICENSE,sha256=Xer1Jg8iL_n9Da0xt0S99blk6tsg9tee_JdgH1rWTjs,1505
13
- h5netcdf-1.7.1.dist-info/METADATA,sha256=tqEPGLxlIdLwfJjNy2NV60F_5jz95xr5YQ13KzfZa6I,13396
14
- h5netcdf-1.7.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
15
- h5netcdf-1.7.1.dist-info/top_level.txt,sha256=Fb_KIpOE6MBqjSvxV1Ay7oYce1mdmQ1pO9JQJPDeGqg,9
16
- h5netcdf-1.7.1.dist-info/RECORD,,