Skip to content

Commit 05b968b

Browse files
committed
Add example for chunking of data
1 parent 334da51 commit 05b968b

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ jobs:
134134
- name: Build and test with filters
135135
run: cargo test --workspace -v --features hdf5-sys/static,hdf5-sys/zlib,lzf,blosc --exclude hdf5-derive
136136
if: matrix.rust != 'stable-gnu'
137-
- name: Run example
138-
run: cargo r --example simple --features hdf5-sys/static,hdf5-sys/zlib,lzf,blosc
137+
- name: Run examples
138+
run: |
139+
cargo r --example simple --features hdf5-sys/static,hdf5-sys/zlib,lzf,blosc
140+
cargo r --example chunking --features hdf5-sys/static,hdf5-sys/zlib,lzf,blosc
139141
if: matrix.rust != 'stable-gnu'
140142

141143
apt:

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Added
6+
- Conversion from `Infallible` to `Error` to allow infallible conversion to
7+
extents.
8+
39
## 0.8.0
410

511
Release date: Oct 23, 2021.

examples/chunking.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//! Create, write, and read a chunked dataset
2+
fn main() -> hdf5::Result<()> {
3+
let file = hdf5::File::create("chunking.h5")?;
4+
5+
let ny = 100;
6+
let nx = 100;
7+
let arr = ndarray::Array2::from_shape_fn((ny, nx), |(j, i)| (1000 * j + i) as f32);
8+
9+
{
10+
let ds = file
11+
.new_dataset::<f32>()
12+
.chunk((1, ny, nx)) // nx*ny elements will be compressed as a single chunk
13+
.shape((1.., ny, nx)) // Initial size of 1 on the unlimited dimension
14+
.deflate(3)
15+
.create("variable")?;
16+
17+
// Writing a chunk at a time will be most efficient
18+
ds.write_slice(&arr, (0, .., ..))?;
19+
20+
// Dataset can be resized along an unlimited dimension
21+
ds.resize((10, ny, nx))?;
22+
ds.write_slice(&arr, (1, .., ..))?;
23+
}
24+
25+
let ds = file.dataset("variable")?;
26+
let chunksize = ds.chunk().unwrap();
27+
assert_eq!(chunksize, &[1, ny, nx]);
28+
29+
let shape = ds.shape();
30+
assert_eq!(shape, &[10, ny, nx]);
31+
32+
// Reading from a chunked dataset should be done in a chunk-wise order
33+
for k in 0..shape[0] {
34+
let _arr: ndarray::Array2<f32> = ds.read_slice((k, .., ..))?;
35+
}
36+
37+
Ok(())
38+
}

src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ impl From<String> for Error {
229229
}
230230
}
231231

232+
impl From<core::convert::Infallible> for Error {
233+
fn from(_v: core::convert::Infallible) -> Self {
234+
unreachable!("Infallible error can never be constructed")
235+
}
236+
}
237+
232238
impl fmt::Debug for Error {
233239
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234240
match *self {

0 commit comments

Comments
 (0)