Skip to content

Commit 2aa6d81

Browse files
committed
Add example scripts for HDF5 file operations and error handling
1 parent ab8ceeb commit 2aa6d81

6 files changed

Lines changed: 82 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
/pkg/
77
/spec/reports/
88
/tmp/
9+
examples/*.h5
910

1011
*.lock

examples/01_read_fixture.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'hdf5'
2+
3+
fixture = File.expand_path('../test/fixtures/example.h5', __dir__)
4+
5+
HDF5::File.open(fixture) do |file|
6+
puts "Entries: #{file.list_entries.inspect}"
7+
8+
group = file['foo']
9+
puts "Datasets in /foo: #{group.list_datasets.inspect}"
10+
11+
int_ds = group['bar_int']
12+
puts "bar_int shape: #{int_ds.shape.inspect}"
13+
puts "bar_int dtype: #{int_ds.dtype}"
14+
puts "bar_int values: #{int_ds.read.inspect}"
15+
end

examples/02_create_numeric_file.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'hdf5'
2+
3+
path = File.expand_path('numeric_example.h5', __dir__)
4+
5+
HDF5::File.create(path) do |file|
6+
file.create_group('values') do |group|
7+
group.create_dataset('ints', [1, 2, 3, 4, 5])
8+
group.create_dataset('floats', [1.25, 2.5, 3.75])
9+
end
10+
end
11+
12+
HDF5::File.open(path) do |file|
13+
puts "Created file: #{path}"
14+
puts "Root entries: #{file.list_entries.inspect}"
15+
16+
values = file['values']
17+
puts "ints: #{values['ints'].read.inspect}"
18+
puts "floats: #{values['floats'].read.inspect}"
19+
end

examples/03_nested_groups.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'hdf5'
2+
3+
path = File.expand_path('nested_groups.h5', __dir__)
4+
5+
HDF5::File.create(path) do |file|
6+
file.create_group('experiments') do |experiments|
7+
experiments.create_group('run_001') do |run|
8+
run.create_dataset('temperatures', [21.1, 21.3, 21.7, 22.0])
9+
end
10+
end
11+
end
12+
13+
HDF5::File.open(path) do |file|
14+
run = file['experiments']['run_001']
15+
ds = run['temperatures']
16+
17+
puts "Dataset shape: #{ds.shape.inspect}"
18+
puts "Dataset dtype: #{ds.dtype}"
19+
puts "Dataset values: #{ds.read.inspect}"
20+
end

examples/04_error_handling.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'hdf5'
2+
3+
begin
4+
HDF5::File.open('/tmp/does-not-exist-ruby-hdf5.h5')
5+
rescue HDF5::Error => e
6+
warn "HDF5::Error: #{e.message}"
7+
end
8+
9+
begin
10+
HDF5::File.create(File.expand_path('bad_data.h5', __dir__)) do |file|
11+
file.create_dataset('invalid', %w[a b c])
12+
end
13+
rescue HDF5::Error => e
14+
warn "HDF5::Error: #{e.message}"
15+
end

examples/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Examples
2+
3+
Run examples from the repository root.
4+
5+
```sh
6+
bundle exec ruby examples/01_read_fixture.rb
7+
bundle exec ruby examples/02_create_numeric_file.rb
8+
bundle exec ruby examples/03_nested_groups.rb
9+
bundle exec ruby examples/04_error_handling.rb
10+
```
11+
12+
If the HDF5 shared library cannot be found, set `HDF5_LIB_PATH` before running examples.

0 commit comments

Comments
 (0)