Skip to content

Commit 71da959

Browse files
authored
Merge pull request #178 from aldanor/feature/update-example
2 parents cad6b72 + 2048764 commit 71da959

File tree

8 files changed

+302
-484
lines changed

8 files changed

+302
-484
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ 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
139+
if: matrix.rust != 'stable-gnu'
137140

138141
apt:
139142
name: apt

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
- Globals no longer creates a `lazy_static` per global.
9494
- Unsafe `ObjectClass::cast()` has been renamed to `ObjectClass::cast_unchecked()`.
9595
- Bump `winreg` (Windows only) to 0.10, `pretty_assertions` (dev) to 1.0.
96+
- Updated the example in the readme to showcase the new features.
9697

9798
### Fixed
9899

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ lazy_static = "1.4"
2727
libc = "0.2"
2828
parking_lot = "0.11"
2929
ndarray = "0.15"
30+
paste = "1.0"
3031
mpi-sys = { version = "0.1", optional = true }
3132
errno = { version = "0.2", optional = true }
3233
hdf5-sys = { path = "hdf5-sys", version = "0.7.1" } # !V

README.md

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ HDF5 for Rust.
77
[![Documentation](https://docs.rs/hdf5/badge.svg)](https://docs.rs/hdf5)
88
[![Changelog](https://img.shields.io/github/v/release/aldanor/hdf5-rust)](https://github.com/aldanor/hdf5-rust/blob/master/CHANGELOG.md)
99
![hdf5: rustc 1.51+](https://img.shields.io/badge/hdf5-rustc_1.51+-lightblue.svg)
10+
[![Total Lines](https://tokei.rs/b1/github/aldanor/hdf5-rust)](https://github.com/aldanor/hdf5-rust)
1011
[![Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
1112
[![MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
1213

@@ -25,53 +26,77 @@ Requires HDF5 library of version 1.8.4 or later.
2526
## Example
2627

2728
```rust
28-
#[derive(hdf5::H5Type, Clone, PartialEq, Debug)]
29+
#[cfg(feature = "blosc")]
30+
use hdf5::filters::blosc_set_nthreads;
31+
use hdf5::{File, H5Type, Result};
32+
use ndarray::{arr2, s};
33+
34+
#[derive(H5Type, Clone, PartialEq, Debug)] // register with HDF5
2935
#[repr(u8)]
3036
pub enum Color {
31-
RED = 1,
32-
GREEN = 2,
33-
BLUE = 3,
37+
R = 1,
38+
G = 2,
39+
B = 3,
3440
}
3541

36-
#[derive(hdf5::H5Type, Clone, PartialEq, Debug)]
42+
#[derive(H5Type, Clone, PartialEq, Debug)] // register with HDF5
3743
#[repr(C)]
3844
pub struct Pixel {
3945
xy: (i64, i64),
4046
color: Color,
4147
}
4248

43-
fn main() -> hdf5::Result<()> {
44-
use self::Color::*;
45-
use ndarray::{arr1, arr2};
46-
47-
{
48-
// write
49-
let file = hdf5::File::create("pixels.h5")?;
50-
let colors = file.new_dataset::<Color>().shape(2).create("colors")?;
51-
colors.write(&[RED, BLUE])?;
52-
let group = file.create_group("dir")?;
53-
let pixels = group.new_dataset::<Pixel>().shape((2, 2)).create("pixels")?;
54-
pixels.write(&arr2(&[
55-
[Pixel { xy: (1, 2), color: RED }, Pixel { xy: (3, 4), color: BLUE }],
56-
[Pixel { xy: (5, 6), color: GREEN }, Pixel { xy: (7, 8), color: RED }],
57-
]))?;
58-
}
59-
{
60-
// read
61-
let file = hdf5::File::open("pixels.h5")?;
62-
let colors = file.dataset("colors")?;
63-
assert_eq!(colors.read_1d::<Color>()?, arr1(&[RED, BLUE]));
64-
let pixels = file.dataset("dir/pixels")?;
65-
assert_eq!(
66-
pixels.read_raw::<Pixel>()?,
67-
vec![
68-
Pixel { xy: (1, 2), color: RED },
69-
Pixel { xy: (3, 4), color: BLUE },
70-
Pixel { xy: (5, 6), color: GREEN },
71-
Pixel { xy: (7, 8), color: RED },
72-
]
73-
);
49+
impl Pixel {
50+
pub fn new(x: i64, y: i64, color: Color) -> Self {
51+
Self { xy: (x, y), color }
7452
}
53+
}
54+
55+
fn write_hdf5() -> Result<()> {
56+
use Color::*;
57+
let file = File::create("pixels.h5")?; // open for writing
58+
let group = file.create_group("dir")?; // create a group
59+
#[cfg(feature = "blosc")]
60+
blosc_set_nthreads(2); // set number of blosc threads
61+
let builder = group.new_dataset_builder();
62+
#[cfg(feature = "blosc")]
63+
let builder = builder.blosc_zstd(9, true); // zstd + shuffle
64+
let ds = builder
65+
.with_data(&arr2(&[
66+
// write a 2-D array of data
67+
[Pixel::new(1, 2, R), Pixel::new(2, 3, B)],
68+
[Pixel::new(3, 4, G), Pixel::new(4, 5, R)],
69+
[Pixel::new(5, 6, B), Pixel::new(6, 7, G)],
70+
]))
71+
// finalize and write the dataset
72+
.create("pixels")?;
73+
// create an attr with fixed shape but don't write the data
74+
let attr = ds.new_attr::<Color>().shape([3]).create("colors")?;
75+
// write the attr data
76+
attr.write(&[R, G, B])?;
77+
Ok(())
78+
}
79+
80+
fn read_hdf5() -> Result<()> {
81+
use Color::*;
82+
let file = File::open("pixels.h5")?; // open for reading
83+
let ds = file.dataset("dir/pixels")?; // open the dataset
84+
assert_eq!(
85+
// read a slice of the 2-D dataset and verify it
86+
ds.read_slice::<Pixel, _, _>(s![1.., ..])?,
87+
arr2(&[
88+
[Pixel::new(3, 4, G), Pixel::new(4, 5, R)],
89+
[Pixel::new(5, 6, B), Pixel::new(6, 7, G)],
90+
])
91+
);
92+
let attr = ds.attr("colors")?; // open the attribute
93+
assert_eq!(attr.read_1d::<Color>()?.as_slice().unwrap(), &[R, G, B]);
94+
Ok(())
95+
}
96+
97+
fn main() -> Result<()> {
98+
write_hdf5()?;
99+
read_hdf5()?;
75100
Ok(())
76101
}
77102
```

examples/simple.rs

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,73 @@
1-
#[derive(hdf5::H5Type, Clone, PartialEq, Debug)]
1+
#[cfg(feature = "blosc")]
2+
use hdf5::filters::blosc_set_nthreads;
3+
use hdf5::{File, H5Type, Result};
4+
use ndarray::{arr2, s};
5+
6+
#[derive(H5Type, Clone, PartialEq, Debug)] // register with HDF5
27
#[repr(u8)]
38
pub enum Color {
4-
RED = 1,
5-
GREEN = 2,
6-
BLUE = 3,
9+
R = 1,
10+
G = 2,
11+
B = 3,
712
}
813

9-
#[derive(hdf5::H5Type, Clone, PartialEq, Debug)]
14+
#[derive(H5Type, Clone, PartialEq, Debug)] // register with HDF5
1015
#[repr(C)]
1116
pub struct Pixel {
1217
xy: (i64, i64),
1318
color: Color,
1419
}
1520

16-
fn main() -> hdf5::Result<()> {
17-
use self::Color::*;
18-
use ndarray::{arr1, arr2};
19-
20-
{
21-
// write
22-
let file = hdf5::File::create("pixels.h5")?;
23-
let colors = file.new_dataset::<Color>().shape(2).create("colors")?;
24-
colors.write(&[RED, BLUE])?;
25-
let group = file.create_group("dir")?;
26-
let pixels = group.new_dataset::<Pixel>().shape((2, 2)).create("pixels")?;
27-
pixels.write(&arr2(&[
28-
[Pixel { xy: (1, 2), color: RED }, Pixel { xy: (3, 4), color: BLUE }],
29-
[Pixel { xy: (5, 6), color: GREEN }, Pixel { xy: (7, 8), color: RED }],
30-
]))?;
31-
}
32-
{
33-
// read
34-
let file = hdf5::File::open("pixels.h5")?;
35-
let colors = file.dataset("colors")?;
36-
assert_eq!(colors.read_1d::<Color>()?, arr1(&[RED, BLUE]));
37-
let pixels = file.dataset("dir/pixels")?;
38-
assert_eq!(
39-
pixels.read_raw::<Pixel>()?,
40-
vec![
41-
Pixel { xy: (1, 2), color: RED },
42-
Pixel { xy: (3, 4), color: BLUE },
43-
Pixel { xy: (5, 6), color: GREEN },
44-
Pixel { xy: (7, 8), color: RED },
45-
]
46-
);
21+
impl Pixel {
22+
pub fn new(x: i64, y: i64, color: Color) -> Self {
23+
Self { xy: (x, y), color }
4724
}
25+
}
26+
27+
fn write_hdf5() -> Result<()> {
28+
use Color::*;
29+
let file = File::create("pixels.h5")?; // open for writing
30+
let group = file.create_group("dir")?; // create a group
31+
#[cfg(feature = "blosc")]
32+
blosc_set_nthreads(2); // set number of blosc threads
33+
let builder = group.new_dataset_builder();
34+
#[cfg(feature = "blosc")]
35+
let builder = builder.blosc_zstd(9, true); // zstd + shuffle
36+
let ds = builder
37+
.with_data(&arr2(&[
38+
// write a 2-D array of data
39+
[Pixel::new(1, 2, R), Pixel::new(2, 3, B)],
40+
[Pixel::new(3, 4, G), Pixel::new(4, 5, R)],
41+
[Pixel::new(5, 6, B), Pixel::new(6, 7, G)],
42+
]))
43+
// finalize and write the dataset
44+
.create("pixels")?;
45+
// create an attr with fixed shape but don't write the data
46+
let attr = ds.new_attr::<Color>().shape([3]).create("colors")?;
47+
// write the attr data
48+
attr.write(&[R, G, B])?;
49+
Ok(())
50+
}
51+
52+
fn read_hdf5() -> Result<()> {
53+
use Color::*;
54+
let file = File::open("pixels.h5")?; // open for reading
55+
let ds = file.dataset("dir/pixels")?; // open the dataset
56+
assert_eq!(
57+
// read a slice of the 2-D dataset and verify it
58+
ds.read_slice::<Pixel, _, _>(s![1.., ..])?,
59+
arr2(&[
60+
[Pixel::new(3, 4, G), Pixel::new(4, 5, R)],
61+
[Pixel::new(5, 6, B), Pixel::new(6, 7, G)],
62+
])
63+
);
64+
let attr = ds.attr("colors")?; // open the attribute
65+
assert_eq!(attr.read_1d::<Color>()?.as_slice().unwrap(), &[R, G, B]);
66+
Ok(())
67+
}
68+
69+
fn main() -> Result<()> {
70+
write_hdf5()?;
71+
read_hdf5()?;
4872
Ok(())
4973
}

0 commit comments

Comments
 (0)