Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add data type to sparse #783

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Write the date in place of the "Unreleased" in the case a new version is release

# Changelog

## v0.1.0b9 (Unreleased)

### Added

- `dtype` property for sparse arrays in `COOAdapter` and `COOStructure`.

## v0.1.0b8 (2024-09-06)

### Fixed
Expand Down
8 changes: 6 additions & 2 deletions tiled/_tests/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,13 @@ def test_sparseadapter_protocol(mocker: MockFixture) -> None:
mock_call4 = mocker.patch.object(CustomSparseAdapter, "specs")
mock_call5 = mocker.patch.object(CustomSparseAdapter, "metadata")

structure = COOStructure(shape=(2 * 5,), chunks=((5, 5),))

array = numpy.random.rand(2, 512, 512)

structure = COOStructure(
shape=(2 * 5,),
chunks=((5, 5),),
data_type=BuiltinDtype.from_numpy_dtype(array.dtype),
)
blocks: Dict[Tuple[int, ...], Tuple[NDArray[Any], Any]] = {(1,): (array, (1,))}
metadata: JSON = {"foo": "bar"}
anyslice = (1, 1, 1)
Expand Down
9 changes: 8 additions & 1 deletion tiled/_tests/test_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ..mimetypes import PARQUET_MIMETYPE
from ..queries import Key
from ..server.app import build_app
from ..structures.array import BuiltinDtype
from ..structures.core import Spec, StructureFamily
from ..structures.data_source import DataSource
from ..structures.sparse import COOStructure
Expand Down Expand Up @@ -245,7 +246,13 @@ def test_write_sparse_chunked(tree):
"sparse",
[
DataSource(
structure=COOStructure(shape=(2 * N,), chunks=((N, N),)),
structure=COOStructure(
shape=(2 * N,),
chunks=((N, N),),
data_type=BuiltinDtype.from_numpy_dtype(
numpy.dtype("float64")
),
),
structure_family="sparse",
)
],
Expand Down
3 changes: 3 additions & 0 deletions tiled/adapters/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sparse
from numpy._typing import NDArray

from ..structures.array import BuiltinDtype
from ..structures.core import Spec, StructureFamily
from ..structures.sparse import COOStructure
from .array import slice_and_shape_from_block_and_chunks
Expand Down Expand Up @@ -49,6 +50,7 @@ def from_arrays(
dims=dims,
shape=shape,
chunks=tuple((dim,) for dim in shape),
data_type=BuiltinDtype.from_numpy_dtype(data.dtype),
resizable=False,
)
return cls(
Expand Down Expand Up @@ -133,6 +135,7 @@ def from_global_ref(
dims=dims,
shape=shape,
chunks=chunks,
data_type=BuiltinDtype.from_numpy_dtype(data.dtype),
resizable=False,
)
return cls(
Expand Down
2 changes: 2 additions & 0 deletions tiled/client/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,13 +886,15 @@ def write_sparse(
>>> x.write_block(coords=[[2, 4]], data=[3.1, 2.8], block=(0,))
>>> x.write_block(coords=[[0, 1]], data=[6.7, 1.2], block=(1,))
"""
from ..structures.array import BuiltinDtype
from ..structures.sparse import COOStructure

structure = COOStructure(
shape=shape,
# This method only supports single-chunk COO arrays.
chunks=tuple((dim,) for dim in shape),
dims=dims,
data_type=BuiltinDtype.from_numpy_dtype(data.dtype),
)
client = self.new(
StructureFamily.sparse,
Expand Down
2 changes: 2 additions & 0 deletions tiled/server/pydantic_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import pydantic

from ..structures.array import BuiltinDtype, StructDtype
from ..structures.sparse import SparseLayout


class COOStructure(pydantic.BaseModel):
shape: Tuple[int, ...] # tuple of ints like (3, 3)
chunks: Tuple[Tuple[int, ...], ...] # tuple-of-tuples-of-ints like ((3,), (3,))
data_type: Optional[Union[BuiltinDtype, StructDtype]] = None
dims: Optional[Tuple[str, ...]] = None # None or tuple of names like ("x", "y")
resizable: Union[bool, Tuple[bool, ...]] = False
layout: SparseLayout = SparseLayout.COO
Expand Down
9 changes: 9 additions & 0 deletions tiled/structures/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from dataclasses import dataclass
from typing import Optional, Tuple, Union

from .array import BuiltinDtype, StructDtype


class SparseLayout(str, enum.Enum):
# Only COO is currently supported, but this lays a path
Expand All @@ -13,14 +15,21 @@ class SparseLayout(str, enum.Enum):
class COOStructure:
chunks: Tuple[Tuple[int, ...], ...] # tuple-of-tuples-of-ints like ((3,), (3,))
shape: Tuple[int, ...] # tuple of ints like (3, 3)
data_type: Optional[Union[BuiltinDtype, StructDtype]] = None
dims: Optional[Tuple[str, ...]] = None # None or tuple of names like ("x", "y")
resizable: Union[bool, Tuple[bool, ...]] = False
layout: SparseLayout = SparseLayout.COO
# TODO Include fill_value?

@classmethod
def from_json(cls, structure):
data_type = structure.get("data_type", None)
if data_type is not None and "fields" in data_type:
data_type = StructDtype.from_json(data_type)
else:
data_type = BuiltinDtype.from_json(data_type)
return cls(
data_type=data_type,
chunks=tuple(map(tuple, structure["chunks"])),
shape=tuple(structure["shape"]),
dims=structure["dims"],
Expand Down
Loading