Skip to content

Commit 2048764

Browse files
committed
Rewrite the example to fit better in the readme
1 parent aa6fbb5 commit 2048764

File tree

2 files changed

+103
-67
lines changed

2 files changed

+103
-67
lines changed

README.md

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,55 +31,73 @@ use hdf5::filters::blosc_set_nthreads;
3131
use hdf5::{File, H5Type, Result};
3232
use ndarray::{arr2, s};
3333

34-
#[derive(H5Type, Clone, PartialEq, Debug)] // map the HDF5 type for this enum
34+
#[derive(H5Type, Clone, PartialEq, Debug)] // register with HDF5
3535
#[repr(u8)]
3636
pub enum Color {
37-
R = 1,
38-
G = 2,
39-
B = 3,
37+
R = 1,
38+
G = 2,
39+
B = 3,
4040
}
4141

42-
#[derive(H5Type, Clone, PartialEq, Debug)] // register this struct with HDF5
42+
#[derive(H5Type, Clone, PartialEq, Debug)] // register with HDF5
4343
#[repr(C)]
4444
pub struct Pixel {
45-
xy: (i64, i64),
46-
color: Color,
45+
xy: (i64, i64),
46+
color: Color,
4747
}
4848

49-
fn main() -> Result<()> {
50-
{
51-
let file = File::create("pixels.h5")?; // open the file for writing
49+
impl Pixel {
50+
pub fn new(x: i64, y: i64, color: Color) -> Self {
51+
Self { xy: (x, y), color }
52+
}
53+
}
54+
55+
fn write_hdf5() -> Result<()> {
56+
use Color::*;
57+
let file = File::create("pixels.h5")?; // open for writing
5258
let group = file.create_group("dir")?; // create a group
5359
#[cfg(feature = "blosc")]
54-
blosc_set_nthreads(2); // set number of threads for compressing/decompressing chunks
60+
blosc_set_nthreads(2); // set number of blosc threads
5561
let builder = group.new_dataset_builder();
5662
#[cfg(feature = "blosc")]
57-
let builder = builder.blosc_zstd(9, true); // enable zstd compression with shuffling
63+
let builder = builder.blosc_zstd(9, true); // zstd + shuffle
5864
let ds = builder
59-
.with_data(&arr2(&[
60-
// write a 2-D array of data
61-
[Pixel { xy: (1, 2), color: Color::R }, Pixel { xy: (2, 3), color: Color::B }],
62-
[Pixel { xy: (3, 4), color: Color::G }, Pixel { xy: (4, 5), color: Color::R }],
63-
[Pixel { xy: (5, 6), color: Color::B }, Pixel { xy: (6, 7), color: Color::G }],
64-
]))
65-
.create("pixels")?; // finalize and write the dataset
66-
let attr = ds.new_attr::<Color>().shape([3]).create("colors")?; // create an attribute
67-
attr.write(&[Color::R, Color::G, Color::B])?;
68-
}
69-
{
70-
let file = File::open("pixels.h5")?; // open the file for reading
71-
let ds = file.dataset("dir/pixels")?; // open the dataset object
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
7284
assert_eq!(
73-
ds.read_slice::<Pixel, _, _>(s![1.., ..])?, // read a slice of the 2-D dataset
74-
arr2(&[
75-
[Pixel { xy: (3, 4), color: Color::G }, Pixel { xy: (4, 5), color: Color::R }],
76-
[Pixel { xy: (5, 6), color: Color::B }, Pixel { xy: (6, 7), color: Color::G }],
77-
])
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+
])
7891
);
7992
let attr = ds.attr("colors")?; // open the attribute
80-
assert_eq!(attr.read_1d::<Color>()?.as_slice().unwrap(), &[Color::R, Color::G, Color::B]);
81-
}
82-
Ok(())
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()?;
100+
Ok(())
83101
}
84102
```
85103

examples/simple.rs

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,71 @@ use hdf5::filters::blosc_set_nthreads;
33
use hdf5::{File, H5Type, Result};
44
use ndarray::{arr2, s};
55

6-
#[derive(H5Type, Clone, PartialEq, Debug)] // map the HDF5 type for this enum
6+
#[derive(H5Type, Clone, PartialEq, Debug)] // register with HDF5
77
#[repr(u8)]
88
pub enum Color {
99
R = 1,
1010
G = 2,
1111
B = 3,
1212
}
1313

14-
#[derive(H5Type, Clone, PartialEq, Debug)] // register this struct with HDF5
14+
#[derive(H5Type, Clone, PartialEq, Debug)] // register with HDF5
1515
#[repr(C)]
1616
pub struct Pixel {
1717
xy: (i64, i64),
1818
color: Color,
1919
}
2020

21-
fn main() -> Result<()> {
22-
{
23-
let file = File::create("pixels.h5")?; // open the file for writing
24-
let group = file.create_group("dir")?; // create a group
25-
#[cfg(feature = "blosc")]
26-
blosc_set_nthreads(2); // set number of threads for compressing/decompressing chunks
27-
let builder = group.new_dataset_builder();
28-
#[cfg(feature = "blosc")]
29-
let builder = builder.blosc_zstd(9, true); // enable zstd compression with shuffling
30-
let ds = builder
31-
.with_data(&arr2(&[
32-
// write a 2-D array of data
33-
[Pixel { xy: (1, 2), color: Color::R }, Pixel { xy: (2, 3), color: Color::B }],
34-
[Pixel { xy: (3, 4), color: Color::G }, Pixel { xy: (4, 5), color: Color::R }],
35-
[Pixel { xy: (5, 6), color: Color::B }, Pixel { xy: (6, 7), color: Color::G }],
36-
]))
37-
.create("pixels")?; // finalize and write the dataset
38-
let attr = ds.new_attr::<Color>().shape([3]).create("colors")?; // create an attribute
39-
attr.write(&[Color::R, Color::G, Color::B])?;
40-
}
41-
{
42-
let file = File::open("pixels.h5")?; // open the file for reading
43-
let ds = file.dataset("dir/pixels")?; // open the dataset object
44-
assert_eq!(
45-
ds.read_slice::<Pixel, _, _>(s![1.., ..])?, // read a slice of the 2-D dataset
46-
arr2(&[
47-
[Pixel { xy: (3, 4), color: Color::G }, Pixel { xy: (4, 5), color: Color::R }],
48-
[Pixel { xy: (5, 6), color: Color::B }, Pixel { xy: (6, 7), color: Color::G }],
49-
])
50-
);
51-
let attr = ds.attr("colors")?; // open the attribute
52-
assert_eq!(attr.read_1d::<Color>()?.as_slice().unwrap(), &[Color::R, Color::G, Color::B]);
21+
impl Pixel {
22+
pub fn new(x: i64, y: i64, color: Color) -> Self {
23+
Self { xy: (x, y), color }
5324
}
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()?;
5472
Ok(())
5573
}

0 commit comments

Comments
 (0)