Skip to main content

H5Attribute

Struct H5Attribute 

Source
pub struct H5Attribute { /* private fields */ }
Expand description

A handle to an HDF5 attribute.

After creating an attribute via AttrBuilder::create, use write_scalar or write_string to set its value.

In read mode, use read_string to read string attributes.

Implementations§

Source§

impl H5Attribute

Source

pub fn name(&self) -> &str

Return the attribute name.

Source

pub fn write_scalar(&self, value: &VarLenUnicode) -> Result<()>

Write a scalar value to the attribute.

For VarLenUnicode, this writes a variable-length UTF-8 string attribute — the string is stored in a global heap and h5py reads it back as a Python str (matching the VarLenUnicode type name).

Source

pub fn write_string(&self, value: &str) -> Result<()>

Write a string value to the attribute (convenience method).

Source

pub fn write_string_array(&self, values: &[&str]) -> Result<()>

Write a variable-length UTF-8 string array attribute.

The attribute is given its shape via AttrBuilder::shape (any rank; empty = scalar) and values must hold exactly the product of those dimensions, supplied in row-major order. Every element is stored in one shared global heap collection; h5py reads the attribute back as a numpy array of Python str with that shape. The string-array counterpart of write_string / write_array.

Source

pub fn write_object_references(&self, paths: &[&str]) -> Result<()>

Write an object-reference attribute — h5py’s ds.attrs['source'] = f['/data'].ref.

The attribute is given its shape via AttrBuilder::shape (empty = the scalar shape a single reference takes) and paths must hold exactly the product of those dimensions, in row-major order. Each path names a dataset or a group (/ is the root group) and must already exist; what reaches the file is the target’s object header address, which is assigned when the file is finalized.

let file = H5File::create("refs.h5").unwrap();
let ds = file.new_dataset::<f32>().shape(&[4]).create("data").unwrap();
let attr = ds.new_attr::<u64>().shape(()).create("self").unwrap();
attr.write_object_references(&["/data"]).unwrap();
Source

pub fn write_numeric<T: H5Type>(&self, value: &T) -> Result<()>

Write a numeric scalar attribute.

let file = H5File::create("num_attr.h5").unwrap();
let ds = file.new_dataset::<f32>().shape(&[10]).create("data").unwrap();
ds.write_raw(&[0.0f32; 10]).unwrap();
let attr = ds.new_attr::<f64>().shape(()).create("scale").unwrap();
attr.write_numeric(&3.14f64).unwrap();
Source

pub fn write_array<T: H5Type>(&self, values: &[T]) -> Result<()>

Write a numeric array attribute.

The number of values must equal the product of the dimensions set via AttrBuilder::shape; if no shape was set the attribute is a scalar and exactly one value is required. The on-disk datatype is T::hdf5_type() and the dataspace is the simple dataspace described by the shape — matching the 1-D int32 array attributes AreaDetector writes (e.g. NDArrayDimOffset, Binning, Reverse).

let file = H5File::create("arr_attr.h5").unwrap();
let ds = file.new_dataset::<f32>().shape(&[10]).create("data").unwrap();
ds.write_raw(&[0.0f32; 10]).unwrap();
let attr = ds.new_attr::<i32>().shape([3]).create("dim_offset").unwrap();
attr.write_array(&[0i32, 4, 8]).unwrap();
Source

pub fn read_numeric<T: H5Type>(&self) -> Result<T>

Read a numeric scalar attribute.

let file = H5File::open("num_attr.h5").unwrap();
let ds = file.dataset("data").unwrap();
let attr = ds.attr("scale").unwrap();
let val: f64 = attr.read_numeric().unwrap();
Source

pub fn read_numeric_as<T: ReadNumeric>(&self) -> Result<Vec<T>>

Read a numeric attribute as T, converting each element from the stored datatype — the attribute counterpart of H5Dataset::read_numeric_as.

Returns every element (one for a scalar attribute), with the same policy: integer → integer is checked and errors with the element index and value instead of wrapping, f32 → f64 widens exactly, and f64 → f32, float ↔ integer, and non-numeric datatypes are rejected. Big-endian sources are decoded per the stored byte order, which read_numeric refuses.

Source

pub fn read_string(&self) -> Result<String>

Read the attribute value as a string.

Handles both fixed-length string attributes and variable-length string attributes (h5py’s default), resolving a vlen value through the global heap.

Source

pub fn datatype(&self) -> Result<DatatypeMessage>

Return the attribute datatype as parsed from the file (read mode only).

Mirrors H5Dataset::datatype: it exposes the full datatype — class (integer vs floating-point vs string vs compound …), signedness, byte order and bit precision — so callers mapping an attribute to a NumPy / Arrow dtype need not infer a type from the byte width, which cannot distinguish u8 from i8 (both 1 byte) or i32 from f32 (both 4 bytes).

§Errors

Returns an error for a write-mode handle, which carries no decoded attribute message.

let file = H5File::open("data.h5").unwrap();
let ds = file.dataset("image").unwrap();
let attr = ds.attr("scale").unwrap();
match attr.datatype().unwrap() {
    DatatypeMessage::FloatingPoint { size, .. } => println!("float: {size} bytes"),
    other => println!("other type: {other}"),
}
Source

pub fn read_references(&self) -> Result<Vec<Reference>>

Read a reference attribute’s elements, each resolved to the object it names — the attribute counterpart of H5Dataset::read_references.

let file = H5File::open("refs.h5").unwrap();
let ds = file.dataset("image").unwrap();
let target = ds.attr("source").unwrap().read_references().unwrap();
println!("{:?}", target[0].path());
Source

pub fn read_raw(&self) -> Result<Vec<u8>>

Read the raw attribute data bytes.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.