-
Notifications
You must be signed in to change notification settings - Fork 12
fix: bring back zarr #21
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
Merged
+113
−52
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ce30286
fix: bring back zarr
ilan-gold c00314b
fix: update versions
ilan-gold da9dba1
fix: deprecated methods
ilan-gold 45c9a3d
fix: fill values
ilan-gold e1c982b
clear cache
ilan-gold 29a59e6
fix: more cache clearing
ilan-gold 41a9d34
fix: don't need to clear the cache on reshape
ilan-gold 561e1e0
fix: shorter types
ilan-gold b716b22
Update anndata-test-utils/tests/tests.rs
ilan-gold 3c8f334
fix: clarify cache clearing
ilan-gold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,17 +7,24 @@ use anyhow::{bail, Context, Result}; | |
| use ndarray::{Array, ArrayD, ArrayView, CowArray, Dimension, IxDyn, SliceInfoElem}; | ||
| use std::{ | ||
| borrow::Cow, | ||
| num::NonZeroU64, | ||
| ops::{Deref, Index}, | ||
| path::{Path, PathBuf}, | ||
| }; | ||
| use std::{sync::Arc, vec}; | ||
| use zarrs::array::codec::bytes_to_bytes::zstd::ZstdCodec; | ||
| use zarrs::array::{ | ||
| ZARR_NAN_F32, ZARR_NAN_F64, codec::bytes_to_bytes::zstd::ZstdCodec, data_type::{ | ||
| BoolDataType, Float32DataType, Float64DataType, Int8DataType, Int16DataType, Int32DataType, Int64DataType, StringDataType, UInt8DataType, UInt16DataType, UInt32DataType, UInt64DataType | ||
| } | ||
| }; | ||
| use zarrs::filesystem::FilesystemStore; | ||
| use zarrs::group::Group; | ||
| use zarrs::{array::ElementOwned, storage::ReadableWritableListableStorageTraits}; | ||
| use zarrs::{ | ||
| array::{codec::ShardingCodecBuilder, data_type::DataType, ArrayShardedReadableExt, Element}, | ||
| array_subset::ArraySubset, | ||
| array::{ | ||
| codec::ShardingCodecBuilder, data_type, ArrayShardedReadableExt, ArraySubset, Element, | ||
| FillValue, | ||
| }, | ||
| storage::StorePrefix, | ||
| }; | ||
|
|
||
|
|
@@ -344,20 +351,32 @@ impl AttributeOp<Zarr> for ZarrDataset { | |
|
|
||
| impl DatasetOp<Zarr> for ZarrDataset { | ||
| fn dtype(&self) -> Result<ScalarType> { | ||
| match self.dataset.data_type() { | ||
| DataType::UInt8 => Ok(ScalarType::U8), | ||
| DataType::UInt16 => Ok(ScalarType::U16), | ||
| DataType::UInt32 => Ok(ScalarType::U32), | ||
| DataType::UInt64 => Ok(ScalarType::U64), | ||
| DataType::Int8 => Ok(ScalarType::I8), | ||
| DataType::Int16 => Ok(ScalarType::I16), | ||
| DataType::Int32 => Ok(ScalarType::I32), | ||
| DataType::Int64 => Ok(ScalarType::I64), | ||
| DataType::Float32 => Ok(ScalarType::F32), | ||
| DataType::Float64 => Ok(ScalarType::F64), | ||
| DataType::Bool => Ok(ScalarType::Bool), | ||
| DataType::String => Ok(ScalarType::String), | ||
| ty => bail!("Unsupported type: {:?}", ty), | ||
| if self.dataset.data_type().is::<UInt8DataType>() { | ||
| Ok(ScalarType::U8) | ||
| } else if self.dataset.data_type().is::<UInt16DataType>() { | ||
| Ok(ScalarType::U16) | ||
| } else if self.dataset.data_type().is::<UInt32DataType>() { | ||
| Ok(ScalarType::U32) | ||
| } else if self.dataset.data_type().is::<UInt64DataType>() { | ||
| Ok(ScalarType::U64) | ||
| } else if self.dataset.data_type().is::<Int8DataType>() { | ||
| Ok(ScalarType::I8) | ||
| } else if self.dataset.data_type().is::<Int16DataType>() { | ||
| Ok(ScalarType::I16) | ||
| } else if self.dataset.data_type().is::<Int32DataType>() { | ||
| Ok(ScalarType::I32) | ||
| } else if self.dataset.data_type().is::<Int64DataType>() { | ||
| Ok(ScalarType::I64) | ||
| } else if self.dataset.data_type().is::<Float32DataType>() { | ||
| Ok(ScalarType::F32) | ||
| } else if self.dataset.data_type().is::<Float64DataType>() { | ||
| Ok(ScalarType::F64) | ||
| } else if self.dataset.data_type().is::<BoolDataType>() { | ||
| Ok(ScalarType::Bool) | ||
| } else if self.dataset.data_type().is::<StringDataType>() { | ||
| Ok(ScalarType::String) | ||
| } else { | ||
| bail!("Unsupported type: {:?}", self.dataset.data_type()) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -371,8 +390,10 @@ impl DatasetOp<Zarr> for ZarrDataset { | |
|
|
||
| fn reshape(&mut self, shape: &Shape) -> Result<()> { | ||
| self.dataset | ||
| .set_shape(shape.as_ref().iter().map(|x| *x as u64).collect()); | ||
| .set_shape(shape.as_ref().iter().map(|x| *x as u64).collect())?; | ||
| self.dataset.store_metadata()?; | ||
| // TODO: is this necessary? I think as long as no data is written, it should be fine to not clear. | ||
|
flying-sheep marked this conversation as resolved.
Outdated
|
||
| // self.cache.clear(); | ||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -392,21 +413,21 @@ impl DatasetOp<Zarr> for ZarrDataset { | |
| if let Some(subset) = to_array_subset(sel) { | ||
| let arr = dataset | ||
| .dataset | ||
| .retrieve_array_subset_ndarray_sharded_opt( | ||
| .retrieve_array_subset_sharded_opt::<ndarray::ArrayD<T>>( | ||
| &dataset.cache, | ||
| &subset, | ||
| &zarrs::array::codec::CodecOptions::default(), | ||
| &zarrs::array::CodecOptions::default(), | ||
| )? | ||
| .into_dimensionality::<D>()?; | ||
| Ok(arr) | ||
| } else { | ||
| // Read the entire array and then select the slice. | ||
| let arr = dataset | ||
| .dataset | ||
| .retrieve_array_subset_ndarray_sharded_opt( | ||
| .retrieve_array_subset_sharded_opt::<ndarray::ArrayD<T>>( | ||
| &dataset.cache, | ||
| &dataset.dataset.subset_all(), | ||
| &zarrs::array::codec::CodecOptions::default(), | ||
| &zarrs::array::CodecOptions::default(), | ||
| )? | ||
| .into_dimensionality::<D>()?; | ||
| Ok(select(arr.view(), selection)) | ||
|
|
@@ -461,9 +482,10 @@ impl DatasetOp<Zarr> for ZarrDataset { | |
| }) | ||
| .collect(); | ||
| if starts.len() == selection.ndim() { | ||
| container.cache.clear(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this was a bug because shards can become stale when writing. We have the cache with the old shards, but then write But the cache thinks its already up to date despite the newly written chunks. |
||
| container | ||
| .dataset | ||
| .store_array_subset_ndarray(starts.as_slice(), arr.into_owned())?; | ||
| .store_array_subset(&ArraySubset::new_with_start_shape(starts, arr.shape().iter().map(|x| *x as u64).collect())?, arr.to_owned())?; | ||
| } else { | ||
| panic!("Not implemented"); | ||
| } | ||
|
|
@@ -567,23 +589,27 @@ fn new_empty_dataset_helper<T: BackendData, S: ?Sized>( | |
| config: WriteConfig, | ||
| ) -> Result<zarrs::array::Array<S>> { | ||
| let (datatype, fill) = match T::DTYPE { | ||
| ScalarType::U8 => (DataType::UInt8, 0u8.into()), | ||
| ScalarType::U16 => (DataType::UInt16, 0u16.into()), | ||
| ScalarType::U32 => (DataType::UInt32, 0u32.into()), | ||
| ScalarType::U64 => (DataType::UInt64, 0u64.into()), | ||
| ScalarType::I8 => (DataType::Int8, 0i8.into()), | ||
| ScalarType::I16 => (DataType::Int16, 0i16.into()), | ||
| ScalarType::I32 => (DataType::Int32, 0i32.into()), | ||
| ScalarType::I64 => (DataType::Int64, 0i64.into()), | ||
| ScalarType::F32 => (DataType::Float32, zarrs::array::ZARR_NAN_F32.into()), | ||
| ScalarType::F64 => (DataType::Float64, zarrs::array::ZARR_NAN_F64.into()), | ||
| ScalarType::Bool => (DataType::Bool, false.into()), | ||
| ScalarType::String => (DataType::String, "".into()), | ||
| ScalarType::U8 => (data_type::uint8(), FillValue::from(0u8)), | ||
| ScalarType::U16 => (data_type::uint16(), FillValue::from(0u16)), | ||
| ScalarType::U32 => (data_type::uint32(), FillValue::from(0u32)), | ||
| ScalarType::U64 => (data_type::uint64(), FillValue::from(0u64)), | ||
| ScalarType::I8 => (data_type::int8(), FillValue::from(0i8)), | ||
| ScalarType::I16 => (data_type::int16(), FillValue::from(0i16)), | ||
| ScalarType::I32 => (data_type::int32(), FillValue::from(0i32)), | ||
| ScalarType::I64 => (data_type::int64(), FillValue::from(0i64)), | ||
| ScalarType::F32 => (data_type::float32(), FillValue::from(ZARR_NAN_F32)), | ||
| ScalarType::F64 => (data_type::float64(), FillValue::from(ZARR_NAN_F64)), | ||
| ScalarType::Bool => (data_type::bool(), FillValue::from(false)), | ||
| ScalarType::String => (data_type::string(), FillValue::from("")), | ||
| }; | ||
|
|
||
| let shape = shape.as_ref(); | ||
| let chunk_size: Vec<u64> = match config.block_size { | ||
| Some(s) => s.as_ref().into_iter().map(|x| (*x).max(1) as u64).collect(), | ||
| Some(s) => s | ||
| .as_ref() | ||
| .into_iter() | ||
| .map(|x| (*x).max(1) as u64) | ||
| .collect::<Vec<_>>(), | ||
| _ => { | ||
| if shape.len() == 1 { | ||
| vec![shape[0].min(16384).max(1) as u64] | ||
|
|
@@ -594,29 +620,35 @@ fn new_empty_dataset_helper<T: BackendData, S: ?Sized>( | |
| }; | ||
|
|
||
| let mut use_sharding = true; | ||
| if matches!(datatype, DataType::String) {//|| shape.iter().sum::<usize>() == 0 { | ||
| if datatype == data_type::string() { | ||
| //|| shape.iter().sum::<usize>() == 0 { | ||
| // Strings are not sharded, they are stored as a single chunk. | ||
| use_sharding = false; | ||
| } | ||
|
|
||
| let array = if use_sharding { | ||
| let shard_shape = chunk_size.iter().map(|&x| x * 8).collect::<Vec<_>>(); | ||
| let mut sharding_codec_builder = | ||
| ShardingCodecBuilder::new(chunk_size.try_into()?); | ||
| let mut sharding_codec_builder = ShardingCodecBuilder::new( | ||
| chunk_size | ||
| .iter() | ||
| .map(|e| NonZeroU64::try_from(*e)) | ||
| .collect::<Result<Vec<NonZeroU64>, _>>()?, | ||
| &datatype, | ||
| ); | ||
| sharding_codec_builder.bytes_to_bytes_codecs(vec![Arc::new(ZstdCodec::new(7, false))]); | ||
| zarrs::array::ArrayBuilder::new( | ||
| shape.iter().map(|x| *x as u64).collect(), | ||
| shape.iter().map(|x| *x as u64).collect::<Vec<_>>(), | ||
| shard_shape.as_slice(), | ||
| datatype, | ||
| shard_shape.try_into()?, | ||
| fill, | ||
| ) | ||
| .array_to_bytes_codec(sharding_codec_builder.build_arc()) | ||
| .build(store, path)? | ||
| } else { | ||
| zarrs::array::ArrayBuilder::new( | ||
| shape.iter().map(|x| *x as u64).collect(), | ||
| shape.iter().map(|x| *x as u64).collect::<Vec<_>>(), | ||
| chunk_size.as_slice(), | ||
| datatype, | ||
| chunk_size.try_into()?, | ||
| fill, | ||
| ) | ||
| .bytes_to_bytes_codecs(vec![Arc::new(ZstdCodec::new(7, false))]) | ||
|
|
@@ -710,15 +742,22 @@ mod tests { | |
| let mut dataset = | ||
| group.new_empty_dataset::<i32>("test", &[20, 50].as_slice().into(), config)?; | ||
|
|
||
| let arr = Array::random((10, 10), Uniform::new(0, 100)); | ||
| // Repeated writes force cache clearance | ||
| let arr: ndarray::Array2<i32> = Array::random((10, 10), Uniform::new(0, 100).unwrap()); | ||
| dataset.write_array_slice(arr.view().into(), s![5..15, 10..20].as_ref())?; | ||
| assert_eq!( | ||
| arr, | ||
| dataset.read_array_slice::<i32, _, _>(s![5..15, 10..20].as_ref())? | ||
| ); | ||
| let arr: ndarray::Array2<i32> = Array::random((10, 10), Uniform::new(0, 100).unwrap()); | ||
| dataset.write_array_slice(arr.view().into(), s![5..15, 10..20].as_ref())?; | ||
| assert_eq!( | ||
| arr, | ||
| dataset.read_array_slice::<i32, _, _>(s![5..15, 10..20].as_ref())? | ||
| ); | ||
|
|
||
| // Repeatitive writes | ||
| let arr = Array::random((20, 50), Uniform::new(0, 100)); | ||
| let arr = Array::random((20, 50), Uniform::new(0, 100).unwrap()); | ||
| dataset.write_array_slice(arr.view().into(), s![.., ..].as_ref())?; | ||
| dataset.write_array_slice(arr.view().into(), s![.., ..].as_ref())?; | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.