Skip to content
Draft
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
Empty file added src/array/create.rs
Empty file.
1 change: 1 addition & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod r#async;
mod create;
mod selection;
mod shared;
mod sync;
Expand Down
56 changes: 54 additions & 2 deletions src/array/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ use std::sync::Arc;
use crate::array::selection::PySelection;
use crate::array::shared::array_metadata_accessors;
use crate::array::util::PyChunkIndices;
use crate::codec::PyCodecOptions;
use crate::chunks::PyChunkGrid;
use crate::codec::{PyArrayToArrayCodec, PyBytesToBytesCodec, PyCodecChain, PyCodecOptions};
use crate::decoded_array::DecodedArray;
use crate::dtype::PyDataType;
use crate::error::ZarristaResult;
use crate::fill_value::PyFillValue;
use crate::node::PyNodePath;
use crate::storage::PySyncStorage;
use pyo3::prelude::*;
use pyo3_bytes::PyBytes;
use zarrs::array::Array;
use zarrs::array::{Array, ArrayBuilder};
use zarrs::storage::ReadableWritableListableStorageTraits;

/// A Zarr array.
Expand Down Expand Up @@ -45,6 +48,55 @@ impl PyArray {
)
}

/// Create a new array
#[staticmethod]
#[pyo3(
signature = (store, dtype, chunk_grid, fill_value, *, path="/", subchunk_shape=None, array_to_array_codecs=None, bytes_to_bytes_codecs=None),
text_signature = "(store, dtype, chunk_grid, fill_value, *, path='/', subchunk_shape=None, array_to_array_codecs=None, bytes_to_bytes_codecs=None)"
)]
#[expect(clippy::too_many_arguments)]
fn create(
store: PySyncStorage,
dtype: PyDataType,
chunk_grid: PyChunkGrid,
fill_value: PyFillValue,
path: &str,
subchunk_shape: Option<Vec<u64>>,
array_to_array_codecs: Option<Vec<PyArrayToArrayCodec>>,
bytes_to_bytes_codecs: Option<Vec<PyBytesToBytesCodec>>,
) -> ZarristaResult<Self> {
let store = store.into_inner();
let mut builder = ArrayBuilder::new_with_chunk_grid(
chunk_grid,
dtype.into_inner(),
fill_value.into_inner(),
);

if let Some(subchunk_shape) = subchunk_shape {
builder.subchunk_shape(subchunk_shape);
}
if let Some(array_to_array_codecs) = array_to_array_codecs {
builder.array_to_array_codecs(
array_to_array_codecs
.into_iter()
.map(|c| c.into_inner())
.collect(),
);
}
if let Some(bytes_to_bytes_codecs) = bytes_to_bytes_codecs {
builder.bytes_to_bytes_codecs(
bytes_to_bytes_codecs
.into_iter()
.map(|c| c.into_inner())
.collect(),
);
}

Ok(Self {
inner: builder.build(store, path)?,
})
}

/// Open the array stored at `path` in `store`.
#[staticmethod]
#[pyo3(
Expand Down
16 changes: 15 additions & 1 deletion src/chunks.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
use pyo3::prelude::*;
use zarrs::array::ChunkGrid;

#[pyclass(module = "zarrista", frozen, name = "ChunkGrid")]
use crate::error::ZarristaResult;
use crate::metadata::PyMetadataV3;

#[derive(Debug, Clone)]
#[pyclass(module = "zarrista", frozen, name = "ChunkGrid", from_py_object)]
pub struct PyChunkGrid(ChunkGrid);

#[pymethods]
impl PyChunkGrid {
#[staticmethod]
fn from_metadata(metadata: PyMetadataV3, shape: Vec<u64>) -> ZarristaResult<Self> {
Ok(Self(ChunkGrid::from_metadata(metadata.as_ref(), &shape)?))
}

#[getter]
fn metadata(&self) -> PyMetadataV3 {
self.0.metadata().into()
}

#[getter]
fn ndim(&self) -> usize {
self.0.dimensionality()
Expand Down
12 changes: 11 additions & 1 deletion src/codec/array_to_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ pub fn bitround(keepbits: u32) -> PyArrayToArrayCodec {
PyArrayToArrayCodec(Arc::new(codec))
}

#[pyclass(module = "zarrista.codec", frozen, name = "ArrayToArrayCodec")]
#[derive(Debug, Clone)]
#[pyclass(
module = "zarrista.codec",
frozen,
name = "ArrayToArrayCodec",
from_py_object
)]
pub struct PyArrayToArrayCodec(Arc<dyn ArrayToArrayCodecTraits>);

impl PyArrayToArrayCodec {
pub fn into_inner(self) -> Arc<dyn ArrayToArrayCodecTraits> {
self.0
}

pub fn new(codec: Arc<dyn ArrayToArrayCodecTraits>) -> Self {
Self(codec)
}
Expand Down
12 changes: 11 additions & 1 deletion src/codec/bytes_to_bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ use zarrs::array::{BytesToBytesCodecTraits, Codec, CodecOptions};
use crate::error::ZarristaResult;
use crate::metadata::{PyConfiguration, PyMetadataV3};

#[pyclass(module = "zarrista.codec", frozen, name = "BytesToBytesCodec")]
#[derive(Debug, Clone)]
#[pyclass(
module = "zarrista.codec",
frozen,
name = "BytesToBytesCodec",
from_py_object
)]
pub struct PyBytesToBytesCodec(Arc<dyn BytesToBytesCodecTraits>);

impl PyBytesToBytesCodec {
pub fn new(codec: Arc<dyn BytesToBytesCodecTraits>) -> Self {
Self(codec)
}

pub fn into_inner(self) -> Arc<dyn BytesToBytesCodecTraits> {
self.0
}
}

#[pymethods]
Expand Down
6 changes: 5 additions & 1 deletion src/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use zarrs::array::{DataType, DataTypeSize};
use zarrs::metadata::v3::MetadataV3;

#[derive(Debug, Clone)]
#[pyclass(module = "zarrista", frozen, name = "DataType", skip_from_py_object)]
#[pyclass(module = "zarrista", frozen, name = "DataType", from_py_object)]
pub struct PyDataType {
inner: DataType,
}
Expand All @@ -19,6 +19,10 @@ impl PyDataType {
pub(crate) fn inner(&self) -> &DataType {
&self.inner
}

pub fn into_inner(self) -> DataType {
self.inner
}
}

#[pymethods]
Expand Down
7 changes: 6 additions & 1 deletion src/fill_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ use pyo3::prelude::*;
use pyo3_bytes::PyBytes;
use zarrs::array::FillValue;

#[pyclass(module = "zarrista", frozen, name = "FillValue")]
#[derive(Debug, Clone)]
#[pyclass(module = "zarrista", frozen, name = "FillValue", from_py_object)]
pub struct PyFillValue(FillValue);

impl PyFillValue {
pub(crate) fn inner(&self) -> &FillValue {
&self.0
}

pub fn into_inner(self) -> FillValue {
self.0
}
}

#[pymethods]
Expand Down
Loading