… slice a dataset based on a condition?

… slice a dataset based on a condition?#

Good practice is to have dimension attached to the datasets. The dimension scales can be used to decide what to slice exactly. The following example first generates a dataset and the slices it depending on the value in the first dimension, in this case the time:

import h5rdmtoolbox as h5tbx
import numpy as np

h5tbx.use(None)

with h5tbx.File() as h5:
    h5.create_dataset('time', data=range(0, 100), make_scale=True)
    h5.create_dataset('x', data=range(0, 100), make_scale=True)
    h5.create_dataset('y', data=range(0, 200), make_scale=True)
    h5.create_dataset('data', np.random.rand(100, 200, 100), attach_scale=('time', 'y', 'x'))
    data = h5.data[:]
    h5.dump()
      (time: 100, y: 200, x: 100) [float64]
      (100) [int64]
      (100) [int64]
      (200) [int64]
with h5tbx.File(h5.hdf_filename) as h5:
    h5.data[h5.data.time > 5.4, :, :].plot()
Matplotlib is building the font cache; this may take a moment.
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Cell In[2], line 2
      1 with h5tbx.File(h5.hdf_filename) as h5:
----> 2     h5.data[h5.data.time > 5.4, :, :].plot()

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/xarray/plot/accessor.py:48, in DataArrayPlotAccessor.__call__(self, **kwargs)
     46 @functools.wraps(dataarray_plot.plot, assigned=("__doc__", "__annotations__"))
     47 def __call__(self, **kwargs) -> Any:
---> 48     return dataarray_plot.plot(self._da, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/xarray/plot/dataarray_plot.py:310, in plot(darray, row, col, col_wrap, ax, hue, subplot_kws, **kwargs)
    306     plotfunc = hist
    308 kwargs["ax"] = ax
--> 310 return plotfunc(darray, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/xarray/plot/dataarray_plot.py:698, in hist(darray, figsize, size, aspect, ax, xincrease, yincrease, xscale, yscale, xticks, yticks, xlim, ylim, *args, **kwargs)
    694 if darray.ndim == 0 or darray.size == 0:
    695     # TypeError to be consistent with pandas
    696     raise TypeError("No numeric data to plot.")
--> 698 ax = get_axis(figsize, size, aspect, ax)
    700 no_nan_arr = np.ravel(darray.to_numpy())
    701 no_nan = no_nan_arr[pd.notnull(no_nan_arr)]

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/xarray/plot/utils.py:469, in get_axis(figsize, size, aspect, ax, **subplot_kws)
    467 else:
    468     mpl = attempt_import("matplotlib")
--> 469     plt = attempt_import("matplotlib.pyplot")
    471 if figsize is not None:
    472     if ax is not None:

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/xarray/core/utils.py:1294, in attempt_import(module)
   1292 reason = package_purpose.get(package_name, "")
   1293 try:
-> 1294     return importlib.import_module(module)
   1295 except (ImportError, ModuleNotFoundError) as e:
   1296     raise ImportError(
   1297         f"The {install_name} package is required {reason}"
   1298         " but could not be imported."
   1299         " Please install it with your package manager (e.g. conda or pip)."
   1300     ) from e

File ~/.asdf/installs/python/3.10.17/lib/python3.10/importlib/__init__.py:126, in import_module(name, package)
    124             break
    125         level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1050, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:1027, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:1006, in _find_and_load_unlocked(name, import_)

File <frozen importlib._bootstrap>:688, in _load_unlocked(spec)

File <frozen importlib._bootstrap_external>:883, in exec_module(self, module)

File <frozen importlib._bootstrap>:241, in _call_with_frames_removed(f, *args, **kwds)

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/matplotlib/pyplot.py:57
     55 from cycler import cycler  # noqa: F401
     56 import matplotlib
---> 57 import matplotlib.colorbar
     58 import matplotlib.image
     59 from matplotlib import _api

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/matplotlib/colorbar.py:19
     16 import numpy as np
     18 import matplotlib as mpl
---> 19 from matplotlib import _api, cbook, collections, cm, colors, contour, ticker
     20 import matplotlib.artist as martist
     21 import matplotlib.patches as mpatches

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/matplotlib/contour.py:15
     13 import matplotlib as mpl
     14 from matplotlib import _api, _docstring
---> 15 from matplotlib.backend_bases import MouseButton
     16 from matplotlib.lines import Line2D
     17 from matplotlib.path import Path

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/matplotlib/backend_bases.py:49
     46 import numpy as np
     48 import matplotlib as mpl
---> 49 from matplotlib import (
     50     _api, backend_tools as tools, cbook, colors, _docstring, text,
     51     _tight_bbox, transforms, widgets, is_interactive, rcParams)
     52 from matplotlib._pylab_helpers import Gcf
     53 from matplotlib.backend_managers import ToolManager

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/matplotlib/text.py:16
     14 from . import _api, artist, cbook, _docstring
     15 from .artist import Artist
---> 16 from .font_manager import FontProperties
     17 from .patches import FancyArrowPatch, FancyBboxPatch, Rectangle
     18 from .textpath import TextPath, TextToPath  # noqa # Logically located here

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/matplotlib/font_manager.py:1643
   1639     _log.info("generated new fontManager")
   1640     return fm
-> 1643 fontManager = _load_fontmanager()
   1644 findfont = fontManager.findfont
   1645 get_font_names = fontManager.get_font_names

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/matplotlib/font_manager.py:1637, in _load_fontmanager(try_read_cache)
   1635             _log.debug("Using fontManager instance from %s", fm_path)
   1636             return fm
-> 1637 fm = FontManager()
   1638 json_dump(fm, fm_path)
   1639 _log.info("generated new fontManager")

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/matplotlib/font_manager.py:1103, in FontManager.__init__(self, size, weight)
   1100 for path in [*findSystemFonts(paths, fontext=fontext),
   1101              *findSystemFonts(fontext=fontext)]:
   1102     try:
-> 1103         self.addfont(path)
   1104     except OSError as exc:
   1105         _log.info("Failed to open font file %s: %s", path, exc)

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/envs/v2.0.0/lib/python3.10/site-packages/matplotlib/font_manager.py:1136, in FontManager.addfont(self, path)
   1134     self.afmlist.append(prop)
   1135 else:
-> 1136     font = ft2font.FT2Font(path)
   1137     prop = ttfFontProperty(font)
   1138     self.ttflist.append(prop)

KeyboardInterrupt: