Skip to content

Commit 5b7ab7a

Browse files
authored
Merge pull request #180 from mulimoen/feature/chunking
Add example for chunking of data
2 parents 334da51 + 2791ae0 commit 5b7ab7a

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Added
6+
7+
- `Error` now implements `From<Infallible>`, which allows passing convertible
8+
extents (like tuples of integers) where `impl TryInto<Extents>` is required.
9+
310
## 0.8.0
411

512
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+
3+
use hdf5::{File, Result};
4+
use ndarray::Array2;
5+
6+
fn main() -> Result<()> {
7+
let file = File::create("chunking.h5")?;
8+
9+
let (ny, nx) = (100, 100);
10+
let arr = Array2::from_shape_fn((ny, nx), |(j, i)| (1000 * j + i) as f32);
11+
12+
let ds = file
13+
.new_dataset::<f32>()
14+
.chunk((1, ny, nx)) // each chunk contains ny * nx elements
15+
.shape((1.., ny, nx)) // first axis is unlimited with initial size of 1
16+
.deflate(3)
17+
.create("variable")?;
18+
19+
// writing one chunk at a time is the most efficient
20+
ds.write_slice(&arr, (0, .., ..))?;
21+
22+
// dataset can be resized along an unlimited dimension
23+
ds.resize((10, ny, nx))?;
24+
ds.write_slice(&arr, (1, .., ..))?;
25+
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+
// it's best to read from a chunked dataset in a chunk-wise fashion
33+
for k in 0..shape[0] {
34+
let _arr: Array2<f32> = ds.read_slice((k, .., ..))?;
35+
}
36+
37+
Ok(())
38+
}

src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::convert::Infallible;
12
use std::error::Error as StdError;
23
use std::fmt;
34
use std::ops::Deref;
@@ -229,6 +230,12 @@ impl From<String> for Error {
229230
}
230231
}
231232

233+
impl From<Infallible> for Error {
234+
fn from(_: Infallible) -> Self {
235+
unreachable!("Infallible error can never be constructed")
236+
}
237+
}
238+
232239
impl fmt::Debug for Error {
233240
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234241
match *self {

0 commit comments

Comments
 (0)