Skip to content

Commit

Permalink
Update bindings for Zarr driver
Browse files Browse the repository at this point in the history
  • Loading branch information
aliddell committed Mar 8, 2024
1 parent 877b49d commit 0e8f8c5
Show file tree
Hide file tree
Showing 4 changed files with 327 additions and 43 deletions.
20 changes: 10 additions & 10 deletions python/acquire/acquire.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ class Capabilities:
def __init__(self, *args: None, **kwargs: Any) -> None: ...
def dict(self) -> Dict[str, Any]: ...

@final
class StorageDimension:
name: str
kind: DimensionType
array_size_px: int
chunk_size_px: int
shard_size_chunks: int

def dict(self) -> Dict[str, Any]: ...

@final
class DimensionType:
Space: ClassVar[DimensionType]
Expand Down Expand Up @@ -323,6 +313,16 @@ class StorageCapabilities:

def dict(self) -> Dict[str, Any]: ...

@final
class StorageDimension:
name: str
kind: DimensionType
array_size_px: int
chunk_size_px: int
shard_size_chunks: int

def dict(self) -> Dict[str, Any]: ...

@final
class StorageProperties:
external_metadata_json: Optional[str]
Expand Down
52 changes: 50 additions & 2 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl TryFrom<capi::StorageProperties> for StorageProperties {
};

let mut acquisition_dimensions: Vec<Py<StorageDimension>> = Default::default();
for i in 1..value.acquisition_dimensions.size {
for i in 0..value.acquisition_dimensions.size {
acquisition_dimensions.push(Python::with_gil(|py| {
Py::new(
py,
Expand Down Expand Up @@ -294,6 +294,27 @@ impl Default for capi::StorageProperties {
}
}

unsafe extern "C" fn init_storage_dimension_array(
data: *mut *mut capi::StorageDimension,
size: usize,
) {
if data.is_null() {
return;
}

*data = std::alloc::alloc(std::alloc::Layout::array::<capi::StorageDimension>(size).unwrap())
as *mut capi::StorageDimension;
}

unsafe extern "C" fn destroy_storage_dimension_array(data: *mut capi::StorageDimension) {
if !data.is_null() {
std::alloc::dealloc(
data as *mut u8,
std::alloc::Layout::array::<capi::StorageDimension>(1).unwrap(),
);
}
}

impl TryFrom<&StorageProperties> for capi::StorageProperties {
type Error = anyhow::Error;

Expand Down Expand Up @@ -338,13 +359,40 @@ impl TryFrom<&StorageProperties> for capi::StorageProperties {
},
) == 1
} {
Err(anyhow::anyhow!("Failed acquire api status check"))
Err(anyhow::anyhow!("Failed to initialize storage properties."))
} else if !unsafe {
capi::storage_properties_set_enable_multiscale(&mut out, value.enable_multiscale as u8)
== 1
} {
Err(anyhow::anyhow!("Failed acquire api status check"))
} else if !unsafe {
if value.acquisition_dimensions.is_empty() {
true
} else {
out.acquisition_dimensions.init = Some(init_storage_dimension_array);
out.acquisition_dimensions.destroy = Some(destroy_storage_dimension_array);
capi::storage_properties_dimensions_init(
&mut out,
value.acquisition_dimensions.len(),
) == 1
}
} {
Err(anyhow::anyhow!(
"Failed to initialize storage dimension array."
))
} else {
// initialize each dimension separately
for (i, pydim) in value.acquisition_dimensions.iter().enumerate() {
let dim = Python::with_gil(|py| -> PyResult<_> {
let storage_dim: StorageDimension = pydim.extract(py)?;
Ok(storage_dim)
})?;

unsafe {
*out.acquisition_dimensions.data.add(i) = (&dim).try_into()?;
}
}

Ok(out)
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def runtime():


def test_version():
assert isinstance(acquire.__version__, str)
# this will fail if pip install -e . has not been run
# so feel free to remove this line if it's not what you want to test
assert acquire.__version__ != "uninstalled"
assert isinstance(acquire.__version__, str)
# this will fail if pip install -e . has not been run
# so feel free to remove this line if it's not what you want to test
assert acquire.__version__ != "uninstalled"


def test_set():
Expand Down
Loading

0 comments on commit 0e8f8c5

Please sign in to comment.