Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ authors = [
{ name = "Alister Trabattoni", email = "alister.trabattoni@gmail.com" },
]
dependencies = [
"arro3.core",
"dask<2025.4.0",
"h5netcdf",
"h5py",
"hdf5plugin",
Expand Down
23 changes: 14 additions & 9 deletions xdas/coordinates/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ class Coordinates(dict):
interrogator: 'SRN'
"""

def __init__(self, coords=None, dims=None):
def __init__(self, coords=None, dims=None, parent=None):
super().__init__()
if isinstance(coords, Coordinates):
if dims is None:
dims = coords.dims
coords = dict(coords)
self._dims = () if dims is None else tuple(dims)
if parent is not None:
self._parent = weakref.ref(parent)
if coords is not None:
for name in coords:
self[name] = coords[name]
Expand All @@ -144,14 +146,17 @@ def __setitem__(self, key, value):
else:
if coord.dim is not None:
if coord.dim not in self.dims:
raise KeyError(
f"cannot add new dimension {coord.dim} to an existing DataArray"
)
size = self.parent.sizes[coord.dim]
if not len(coord) == size:
raise ValueError(
f"conflicting sizes for dimension {coord.dim}: size {len(coord)} "
f"in `coords` and size {size} in `data`"
pass
# warnings.warn(
# # raise KeyError(
# f"cannot add new dimension {coord.dim} to an existing DataArray"
# )
elif hasattr(self.parent, "sizes"):
size = self.parent.sizes[coord.dim]
if not len(coord) == size:
raise ValueError(
f"conflicting sizes for dimension {coord.dim}: size {len(coord)} "
f"in `coords` and size {size} in `data`"
)
coord._assign_parent(self)
return super().__setitem__(key, coord)
Expand Down
113 changes: 110 additions & 3 deletions xdas/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@
dims = coords.dims
if dims is not None and len(dims) != data.ndim:
raise ValueError("different number of dimensions on `data` and `dims`")
coords = Coordinates(coords, dims)
coords._assign_parent(self)
self._coords = coords
new_coords = Coordinates(coords, dims, self)
self._coords = new_coords

# metadata
self.name = name
Expand Down Expand Up @@ -1027,6 +1026,114 @@

return open_dataarray(fname, group)


def to_zarr(
self,
fname,
mode="w",
group=None,
virtual=None,
encoding=None,
create_dirs=False,
):
"""
Write DataArray contents to a zarr file.

Parameters
----------
fname : str
Path to which to save this dataset.
mode : {'w', 'a'}, optional
Write ('w') or append ('a') mode. If mode='a', the file must already exist.
group : str, optional
Path to the zarr4 group in the given file to open.
virtual : bool, optional
Weather to write a virtual dataset. The DataArray data must be a VirtualSource
or a VirtualLayout. Default (None) is to try to write a virtual dataset if
possible.
encoding : dict, optional
Dictionary of encoding attributes. Because a DataArray contains a unique
data variable, the encoding dictionary should not contain the variable name.
For more information on encoding, see the `xarray documentation
<http://xarray.pydata.org/en/stable/io.html#zarr>`_.
create_dirs : bool, optional
Whether to create parent directories if they do not exist. Default is False.

Examples
--------
>>> import os
>>> import tempfile

>>> import numpy as np
>>> import xdas as xd
>>> import hdf5plugin

Create some sample data array:

>>> da = xd.DataArray(np.random.rand(100, 100))

Save the dataset with ZFP compression:

>>> encoding = {"chunks": (10, 10), **hdf5plugin.Zfp(accuracy=0.001)}
>>> with tempfile.TemporaryDirectory() as tmpdir:
... tmpfile = os.path.join(tmpdir, "path.nc")
... da.to_zarr(tmpfile, encoding=encoding)

"""
from ..io.zarr import save_dataarray_zarr

save_dataarray_zarr(self, fname, mode, group, virtual, encoding, create_dirs)

@classmethod
def from_zarr(cls, fname, group=None):
"""
Read a data array from a zarr file.

Parameters
----------
fname: str
The path of the file to open.
group: str, optional
The location of the data array within the file. Root by default

Returns
-------
DataArray
The openend data array.
"""
from ..io.zarr import open_dataarray_zarr

return open_dataarray_zarr(fname, group)


def to_dict(self):
"""Convert the DataArray to a dictionary."""
if isinstance(self.data, VirtualArray):

Check failure on line 1111 in xdas/core/dataarray.py

View workflow job for this annotation

GitHub Actions / lint

ruff (F821)

xdas/core/dataarray.py:1111:34: F821 Undefined name `VirtualArray`
raise NotImplementedError("cannot convert a virtual array to a dictionary")
elif isinstance(self.data, np.ndarray):
data = self.data.tolist()
elif isinstance(self.data, DaskArray):

Check failure on line 1115 in xdas/core/dataarray.py

View workflow job for this annotation

GitHub Actions / lint

ruff (F821)

xdas/core/dataarray.py:1115:36: F821 Undefined name `DaskArray`
data = to_dict(self.data)

Check failure on line 1116 in xdas/core/dataarray.py

View workflow job for this annotation

GitHub Actions / lint

ruff (F821)

xdas/core/dataarray.py:1116:20: F821 Undefined name `to_dict`
return {
"data": data,
"coords": self.coords.to_dict()["coords"],
"dims": self.dims,
"name": self.name,
"attrs": self.attrs,
}

@classmethod
def from_dict(cls, dct):
"""Create a DataArray from a dictionary."""
if isinstance(dct["data"], list):
data = np.array(dct["data"])
elif isinstance(dct["data"], dict):
data = from_dict(dct["data"])

Check failure on line 1131 in xdas/core/dataarray.py

View workflow job for this annotation

GitHub Actions / lint

ruff (F821)

xdas/core/dataarray.py:1131:20: F821 Undefined name `from_dict`
else:
raise ValueError("data must be a list or a dictionary")
coords = Coordinates.from_dict({key: dct[key] for key in ["coords", "dims"]})
return cls(data, coords, dct["dims"], dct["name"], dct["attrs"])

def plot(self, *args, **kwargs):
"""
Plot a DataArray.
Expand Down
6 changes: 6 additions & 0 deletions xdas/core/routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,12 @@ def concat(
return objs[0] if objs else DataArray([])
objs = non_empty

if virtual is None:
virtual = all(isinstance(da.data, (VirtualSource, VirtualStack)) for da in objs)

if len(objs) == 0:
return DataArray() # return empty dataarray

if dim in objs[0].dims + ("first", "last"):
axis = objs[0].get_axis_num(dim)
dim = objs[0].dims[axis] # ensure not "first" or "last"
Expand Down
3 changes: 2 additions & 1 deletion xdas/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
"silixa",
"terra15",
"xdas",
"zarr",
]

from . import apsensing, aragon, asn, febus, prodml, silixa, terra15, xdas
from . import apsensing, aragon, asn, febus, prodml, silixa, terra15, xdas, zarr
from .core import AutoEngine, Engine, get_free_port

# isort: split
Expand Down
142 changes: 142 additions & 0 deletions xdas/io/zarr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import os

Check failure on line 1 in xdas/io/zarr.py

View workflow job for this annotation

GitHub Actions / lint

ruff (D100)

xdas/io/zarr.py:1:1: D100 Missing docstring in public module
from pathlib import Path

import zarr

Check failure on line 4 in xdas/io/zarr.py

View workflow job for this annotation

GitHub Actions / lint

ruff (F401)

xdas/io/zarr.py:4:8: F401 `zarr` imported but unused help: Remove unused import: `zarr`
import h5py
import xarray as xr

from ..core.dataarray import DataArray
from ..core.datacollection import DataCollection, DataMapping, DataSequence
from .core import Engine

Check failure on line 10 in xdas/io/zarr.py

View workflow job for this annotation

GitHub Actions / lint

ruff (I001)

xdas/io/zarr.py:1:1: I001 Import block is un-sorted or un-formatted help: Organize imports


class XdasZarrEngine(Engine, name="zarr"):

Check failure on line 13 in xdas/io/zarr.py

View workflow job for this annotation

GitHub Actions / lint

ruff (D101)

xdas/io/zarr.py:13:7: D101 Missing docstring in public class

def open_dataarray(self, fname, **kwargs):

Check failure on line 15 in xdas/io/zarr.py

View workflow job for this annotation

GitHub Actions / lint

ruff (D102)

xdas/io/zarr.py:15:9: D102 Missing docstring in public method
return open_dataarray_zarr(fname, **kwargs)

def save_dataarray(self, da, fname, **kwargs):

Check failure on line 18 in xdas/io/zarr.py

View workflow job for this annotation

GitHub Actions / lint

ruff (D102)

xdas/io/zarr.py:18:9: D102 Missing docstring in public method
return save_dataarray_zarr(da, fname, **kwargs)

def open_datacollection(self, fname, **kwargs):
return open_datamapping(fname, **kwargs)

def save_datacollection(self, dc, fname, **kwargs):
return save_datamapping(dc, fname, **kwargs)

def open_dataarray_zarr(fname, **kwargs):
xr_da = xr.open_zarr(fname, **kwargs)
xr_da["name"] = fname
return DataArray.from_xarray(xr_da)

def save_dataarray_zarr(
da, fname, mode="w", group=None, virtual=None, encoding=None, create_dirs=False
):
dx = da.to_xarray()
dx.name = "data"

### TODO: da coords != dx coords. Write without xarray?
# chunks=(1000,1000)
# codec = codecs.ZFPY(mode=4, tolerance=tolerance)
# z_write = zarr.array(da.data, chunks=chunks, codecs=[codec], store=fname, overwrite=True)

xr.DataArray.to_zarr(dx,fname, mode=mode, group=group, encoding=encoding)


def open_datacollection(fname, group=None):
if isinstance(fname, Path):
fname = str(fname)
dc = open_datamapping(fname, group)
try:
keys = [int(key) for key in dc.keys()]
if keys == list(range(len(keys))):
return DataSequence.from_mapping(dc)
else:
return dc
except ValueError:
return dc


def save_datacollection(
dc, fname, mode="w", group=None, virtual=None, encoding=None, create_dirs=False
):
if isinstance(fname, Path):
fname = str(fname)

if isinstance(dc, DataSequence):
save_datasequence(dc, fname, mode, group, virtual, encoding, create_dirs)
elif isinstance(dc, DataCollection):
save_datamapping(dc, fname, mode, group, virtual, encoding, create_dirs)
else:
raise ValueError("can only save a DataCollection or a DataSequence")


def open_datamapping(fname, group=None):
if isinstance(fname, Path):
fname = str(fname)

with h5py.File(fname, "r") as file:
if group is None:
group = file[list(file.keys())[0]]
else:
group = file[group]
name = group.name.split("/")[-1]
if isinstance(group, h5py.Dataset):
raise ValueError(
"it looks like you are trying to open a data array as a data collection."
)
else:
if not isinstance(group, h5py.Group):
raise RuntimeError(
"something went wrong while opening the data collection."
)
keys = list(group.keys())
dm = DataMapping({}, name=None if name == "collection" else name)
for key in keys:
subgroup = group[key]
if _get_depth(subgroup) == 0:
dm[key] = DataArray.from_zarr(fname, subgroup.name)
else:
raise NotImplementedError("zarr datacollection TBD")
subgroup = subgroup[list(subgroup.keys())[0]]
dm[key] = DataCollection.from_zarr(fname, subgroup.name)
return dm


def save_datamapping(
dm, fname, mode="w", group=None, virtual=None, encoding=None, create_dirs=False
):
if mode == "w" and group is None and os.path.exists(fname):
os.remove(fname)
for key in dm:
name = dm.name if dm.name is not None else "collection"
location = "/".join([name, str(key)])
if group is not None:
location = "/".join([group, location])
if create_dirs:
dirname = os.path.dirname(fname)
if dirname:
os.makedirs(dirname, exist_ok=True)
dm[key].to_zarr(
fname, mode="a", group=location, virtual=virtual, encoding=encoding
)


def open_datasequence(fname, group=None):
dm = open_datamapping(fname, group)
return DataSequence.from_mapping(dm)


def save_datasequence(
ds, fname, mode="w", group=None, virtual=None, encoding=None, create_dirs=False
):
dm = ds.to_mapping()
save_datamapping(dm, fname, mode, group, virtual, encoding, create_dirs)


def _get_depth(group):
if not isinstance(group, h5py.Group):
raise ValueError("not a group")
depths = []
group.visit(lambda name: depths.append(name.count("/")))
return max(depths)
Loading
Loading