Skip to content

Commit

Permalink
Merge pull request #8 from LDeakin/dimension_names
Browse files Browse the repository at this point in the history
Dimension names cleanup
  • Loading branch information
LDeakin authored Feb 13, 2024
2 parents 7005b2a + 6df10db commit 4d068bf
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **Breaking** Existing `Array` `_opt` use new encode/decode options insted of `parallel: bool`
- Implement `DoubleEndedIterator` for `{Indices,LinearisedIndices,ContiguousIndices,ContiguousLinearisedIndicesIterator}Iterator`
- Add `ParIndicesIterator` and `ParChunksIterator`
- Implement `From<String>` for `DimensionName`

### Changed
- Dependency bumps
Expand All @@ -51,6 +52,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Indices` and `Chunks` implement `IntoParallelIter`
- **Breaking**: array subset iterators are moved into public `array_subset::iterators` and no longer in the `array_subset` namespace
- Add a fast path to `Array::retrieve_chunk_subset{_opt}` if the entire chunk is requested
- `DimensionName::new()` generalised to accept a name implementing `Into<String>`
- **Breaking**: `ArrayBuilder::dimension_names()` generalised to accept `Option<I>` where `I: IntoIterator<Item = D>` and `D: Into<DimensionName>`
- Can now write
`builder.dimension_names(["y", "x"].into())` instead of `builder.dimension_names(vec!["y".into(), "x".into()].into())`

### Removed
- **Breaking**: remove `InvalidArraySubsetError` and `ArrayExtractElementsError`
Expand Down
2 changes: 1 addition & 1 deletion examples/array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn array_write_read() -> Result<(), Box<dyn std::error::Error>> {
FillValue::from(ZARR_NAN_F32),
)
// .bytes_to_bytes_codecs(vec![]) // uncompressed
.dimension_names(Some(vec!["y".into(), "x".into()]))
.dimension_names(["y", "x"].into())
// .storage_transformers(vec![].into())
.build(store.clone(), array_path)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/async_array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn async_array_write_read() -> Result<(), Box<dyn std::error::Error>> {
FillValue::from(ZARR_NAN_F32),
)
// .bytes_to_bytes_codecs(vec![]) // uncompressed
.dimension_names(Some(vec!["y".into(), "x".into()]))
.dimension_names(["y", "x"].into())
// .storage_transformers(vec![].into())
.build(store.clone(), array_path)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/rectangular_array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn rectangular_array_write_read() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "gzip")]
Box::new(codec::GzipCodec::new(5)?),
])
.dimension_names(Some(vec!["y".into(), "x".into()]))
.dimension_names(["y", "x"].into())
// .storage_transformers(vec![].into())
.build(store.clone(), array_path)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/sharded_array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn sharded_array_write_read() -> Result<(), Box<dyn std::error::Error>> {
FillValue::from(0u16),
)
.array_to_bytes_codec(Box::new(sharding_codec_builder.build()))
.dimension_names(Some(vec!["y".into(), "x".into()]))
.dimension_names(["y", "x"].into())
// .storage_transformers(vec![].into())
.build(store.clone(), array_path)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/zip_array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn write_array_to_storage<TStorage: ReadableWritableStorageTraits + 'static>(
#[cfg(feature = "gzip")]
Box::new(codec::GzipCodec::new(5)?),
])
.dimension_names(Some(vec!["y".into(), "x".into()]))
.dimension_names(["y", "x"].into())
// .storage_transformers(vec![].into())
.build(storage, ARRAY_PATH)?;

Expand Down
23 changes: 18 additions & 5 deletions src/array/array_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use super::{
/// #[cfg(feature = "gzip")]
/// Box::new(zarrs::array::codec::GzipCodec::new(5)?),
/// ])
/// .dimension_names(Some(vec!["y".into(), "x".into()]))
/// .dimension_names(["y", "x"].into())
/// .build(store.clone(), "/group/array")?;
/// array.store_metadata()?; // write metadata to the store
///
Expand Down Expand Up @@ -231,8 +231,21 @@ impl ArrayBuilder {
/// Set the dimension names.
///
/// If left unmodified, all dimension names are "unnamed".
pub fn dimension_names(&mut self, dimension_names: Option<Vec<DimensionName>>) -> &mut Self {
self.dimension_names = dimension_names;
pub fn dimension_names<I, D>(&mut self, dimension_names: Option<I>) -> &mut Self
where
I: IntoIterator<Item = D>,
D: Into<DimensionName>,
{
if let Some(dimension_names) = dimension_names {
self.dimension_names = Some(
dimension_names
.into_iter()
.map(Into::<DimensionName>::into)
.collect(),
);
} else {
self.dimension_names = None;
}
self
}

Expand Down Expand Up @@ -332,7 +345,7 @@ mod tests {
)));
builder.fill_value(FillValue::from(0i8));

builder.dimension_names(Some(vec!["y".into(), "x".into()]));
builder.dimension_names(["y", "x"].into());

let mut attributes = serde_json::Map::new();
attributes.insert("key".to_string(), "value".into());
Expand Down Expand Up @@ -400,7 +413,7 @@ mod tests {
vec![2, 2].try_into().unwrap(),
FillValue::from(0i8),
);
builder.dimension_names(Some(vec!["z".into(), "y".into(), "x".into()]));
builder.dimension_names(["z", "y", "x"].into());
assert!(builder.build(storage.clone(), "/").is_err());
}
}
12 changes: 9 additions & 3 deletions src/array/dimension_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Default for DimensionName {
impl DimensionName {
/// Create a new dimension with `name`. Use [`default`](DimensionName::default) to create a dimension with no name.
#[must_use]
pub fn new(name: &str) -> Self {
pub fn new<T: Into<String>>(name: T) -> Self {
Self(Some(name.into()))
}

Expand All @@ -27,8 +27,14 @@ impl DimensionName {
}

impl From<&str> for DimensionName {
fn from(value: &str) -> Self {
Self(Some(value.into()))
fn from(name: &str) -> Self {
Self::new(name)
}
}

impl From<String> for DimensionName {
fn from(name: String) -> Self {
Self::new(name)
}
}

Expand Down

0 comments on commit 4d068bf

Please sign in to comment.