hdf5 0.8.1

Thread-safe Rust bindings for the HDF5 library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use std::fmt::{self, Debug};
use std::ops::Deref;

use hdf5_sys::{
    h5::{H5_index_t, H5_iter_order_t},
    h5a::{H5A_info_t, H5A_operator2_t, H5Acreate2, H5Adelete, H5Aiterate2},
};
use hdf5_types::TypeDescriptor;
use ndarray::ArrayView;

use crate::internal_prelude::*;

/// Represents the HDF5 attribute object.
#[repr(transparent)]
#[derive(Clone)]
pub struct Attribute(Handle);

impl ObjectClass for Attribute {
    const NAME: &'static str = "attribute";
    const VALID_TYPES: &'static [H5I_type_t] = &[H5I_ATTR];

    fn from_handle(handle: Handle) -> Self {
        Self(handle)
    }

    fn handle(&self) -> &Handle {
        &self.0
    }

    // TODO: short_repr()
}

impl Debug for Attribute {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.debug_fmt(f)
    }
}

impl Deref for Attribute {
    type Target = Container;

    fn deref(&self) -> &Container {
        unsafe { self.transmute() }
    }
}

impl Attribute {
    /// Returns names of all the members in the group, non-recursively.
    pub fn attr_names(obj: &Location) -> Result<Vec<String>> {
        extern "C" fn attributes_callback(
            _id: hid_t, attr_name: *const c_char, _info: *const H5A_info_t, op_data: *mut c_void,
        ) -> herr_t {
            std::panic::catch_unwind(|| {
                let other_data: &mut Vec<String> =
                    unsafe { &mut *(op_data.cast::<std::vec::Vec<std::string::String>>()) };
                other_data.push(string_from_cstr(attr_name));
                0 // Continue iteration
            })
            .unwrap_or(-1)
        }

        let callback_fn: H5A_operator2_t = Some(attributes_callback);
        let iteration_position: *mut hsize_t = &mut { 0_u64 };
        let mut result: Vec<String> = Vec::new();
        let other_data: *mut c_void = &mut result as *const _ as *mut c_void;

        h5call!(H5Aiterate2(
            obj.handle().id(),
            H5_index_t::H5_INDEX_NAME,
            H5_iter_order_t::H5_ITER_INC,
            iteration_position,
            callback_fn,
            other_data
        ))?;

        Ok(result)
    }
}

#[derive(Clone)]
/// An attribute builder
pub struct AttributeBuilder {
    builder: AttributeBuilderInner,
}

impl AttributeBuilder {
    pub fn new(parent: &Location) -> Self {
        Self { builder: AttributeBuilderInner::new(parent) }
    }

    pub fn empty<T: H5Type>(self) -> AttributeBuilderEmpty {
        self.empty_as(&T::type_descriptor())
    }

    pub fn empty_as(self, type_desc: &TypeDescriptor) -> AttributeBuilderEmpty {
        AttributeBuilderEmpty { builder: self.builder, type_desc: type_desc.clone() }
    }

    pub fn with_data<'d, A, T, D>(self, data: A) -> AttributeBuilderData<'d, T, D>
    where
        A: Into<ArrayView<'d, T, D>>,
        T: H5Type,
        D: ndarray::Dimension,
    {
        self.with_data_as::<A, T, D>(data, &T::type_descriptor())
    }

    pub fn with_data_as<'d, A, T, D>(
        self, data: A, type_desc: &TypeDescriptor,
    ) -> AttributeBuilderData<'d, T, D>
    where
        A: Into<ArrayView<'d, T, D>>,
        T: H5Type,
        D: ndarray::Dimension,
    {
        AttributeBuilderData {
            builder: self.builder,
            data: data.into(),
            type_desc: type_desc.clone(),
            conv: Conversion::Soft,
        }
    }

    #[inline]
    #[must_use]
    pub fn packed(mut self, packed: bool) -> Self {
        self.builder.packed(packed);
        self
    }
}

#[derive(Clone)]
/// An attribute builder with the type known
pub struct AttributeBuilderEmpty {
    builder: AttributeBuilderInner,
    type_desc: TypeDescriptor,
}

impl AttributeBuilderEmpty {
    pub fn shape<S: Into<Extents>>(self, extents: S) -> AttributeBuilderEmptyShape {
        AttributeBuilderEmptyShape {
            builder: self.builder,
            type_desc: self.type_desc,
            extents: extents.into(),
        }
    }
    pub fn create<'n, T: Into<&'n str>>(self, name: T) -> Result<Attribute> {
        self.shape(()).create(name)
    }

    #[inline]
    #[must_use]
    pub fn packed(mut self, packed: bool) -> Self {
        self.builder.packed(packed);
        self
    }
}

#[derive(Clone)]
/// An attribute builder with type and shape known
pub struct AttributeBuilderEmptyShape {
    builder: AttributeBuilderInner,
    type_desc: TypeDescriptor,
    extents: Extents,
}

impl AttributeBuilderEmptyShape {
    pub fn create<'n, T: Into<&'n str>>(&self, name: T) -> Result<Attribute> {
        h5lock!(self.builder.create(&self.type_desc, name.into(), &self.extents))
    }

    #[inline]
    #[must_use]
    pub fn packed(mut self, packed: bool) -> Self {
        self.builder.packed(packed);
        self
    }
}

#[derive(Clone)]
/// An attribute builder with type, shape, and data known
pub struct AttributeBuilderData<'d, T, D> {
    builder: AttributeBuilderInner,
    data: ArrayView<'d, T, D>,
    type_desc: TypeDescriptor,
    conv: Conversion,
}

impl<'d, T, D> AttributeBuilderData<'d, T, D>
where
    T: H5Type,
    D: ndarray::Dimension,
{
    /// Set maximum allowed conversion level.
    pub fn conversion(mut self, conv: Conversion) -> Self {
        self.conv = conv;
        self
    }

    /// Disallow all conversions.
    pub fn no_convert(mut self) -> Self {
        self.conv = Conversion::NoOp;
        self
    }

    pub fn create<'n, N: Into<&'n str>>(&self, name: N) -> Result<Attribute> {
        ensure!(
            self.data.is_standard_layout(),
            "input array is not in standard layout or is not contiguous"
        ); // TODO: relax this when it's supported in the writer
        let extents = Extents::from(self.data.shape());
        let name = name.into();

        h5lock!({
            let dtype_src = Datatype::from_type::<T>()?;
            let dtype_dst = Datatype::from_descriptor(&self.type_desc)?;
            dtype_src.ensure_convertible(&dtype_dst, self.conv)?;
            let ds = self.builder.create(&self.type_desc, name, &extents)?;
            if let Err(err) = ds.write(self.data.view()) {
                self.builder.try_unlink(name);
                Err(err)
            } else {
                Ok(ds)
            }
        })
    }

    #[inline]
    #[must_use]
    pub fn packed(mut self, packed: bool) -> Self {
        self.builder.packed(packed);
        self
    }
}

#[derive(Clone)]
/// The true internal dataset builder
struct AttributeBuilderInner {
    parent: Result<Handle>,
    packed: bool,
}

impl AttributeBuilderInner {
    pub fn new(parent: &Location) -> Self {
        Self { parent: parent.try_borrow(), packed: false }
    }

    pub fn packed(&mut self, packed: bool) {
        self.packed = packed;
    }

    unsafe fn create(
        &self, desc: &TypeDescriptor, name: &str, extents: &Extents,
    ) -> Result<Attribute> {
        // construct in-file type descriptor; convert to packed representation if needed
        let desc = if self.packed { desc.to_packed_repr() } else { desc.to_c_repr() };

        let datatype = Datatype::from_descriptor(&desc)?;
        let parent = try_ref_clone!(self.parent);

        let dataspace = Dataspace::try_new(extents)?;

        let name = to_cstring(name)?;
        Attribute::from_id(h5try!(H5Acreate2(
            parent.id(),
            name.as_ptr(),
            datatype.id(),
            dataspace.id(),
            // these args are currently unused as if HDF5 1.12
            // see details: https://portal.hdfgroup.org/display/HDF5/H5A_CREATE2
            H5P_DEFAULT,
            H5P_DEFAULT,
        )))
    }

    fn try_unlink(&self, name: &str) {
        let name = to_cstring(name).unwrap();
        if let Ok(parent) = &self.parent {
            h5lock!(H5Adelete(parent.id(), name.as_ptr()));
        }
    }
}

#[cfg(test)]
pub mod attribute_tests {
    use crate::internal_prelude::*;
    use ndarray::{arr2, Array2};
    use std::str::FromStr;
    use types::VarLenUnicode;

    #[test]
    pub fn test_shape_ndim_size() {
        with_tmp_file(|file| {
            let d = file.new_attr::<f32>().shape((2, 3)).create("name1").unwrap();
            assert_eq!(d.shape(), vec![2, 3]);
            assert_eq!(d.size(), 6);
            assert_eq!(d.ndim(), 2);
            assert_eq!(d.is_scalar(), false);

            let d = file.new_attr::<u8>().shape(()).create("name2").unwrap();
            assert_eq!(d.shape(), vec![]);
            assert_eq!(d.size(), 1);
            assert_eq!(d.ndim(), 0);
            assert_eq!(d.is_scalar(), true);
        })
    }

    #[test]
    pub fn test_get_file_attr_names() {
        with_tmp_file(|file| {
            let _ = file.new_attr::<f32>().shape((2, 3)).create("name1").unwrap();
            let _ = file.new_attr::<u8>().shape(()).create("name2").unwrap();

            let attr_names = file.attr_names().unwrap();
            assert_eq!(attr_names.len(), 2);
            assert!(attr_names.contains(&"name1".to_string()));
            assert!(attr_names.contains(&"name2".to_string()));
        })
    }

    #[test]
    pub fn test_get_dataset_attr_names() {
        with_tmp_file(|file| {
            let ds = file.new_dataset::<u32>().shape((10, 10)).create("d1").unwrap();

            let _ = ds.new_attr::<f32>().shape((2, 3)).create("name1").unwrap();
            let _ = ds.new_attr::<u8>().shape(()).create("name2").unwrap();

            let attr_names = ds.attr_names().unwrap();
            assert_eq!(attr_names.len(), 2);
            assert!(attr_names.contains(&"name1".to_string()));
            assert!(attr_names.contains(&"name2".to_string()));
        })
    }

    #[test]
    pub fn test_datatype() {
        with_tmp_file(|file| {
            assert_eq!(
                file.new_attr::<f32>().shape(1).create("name").unwrap().dtype().unwrap(),
                Datatype::from_type::<f32>().unwrap()
            );
        })
    }

    #[test]
    pub fn test_read_write() {
        with_tmp_file(|file| {
            let arr = arr2(&[[1, 2, 3], [4, 5, 6]]);

            let attr = file.new_attr::<f32>().shape((2, 3)).create("foo").unwrap();
            attr.as_writer().write(&arr).unwrap();

            let read_attr = file.attr("foo").unwrap();
            assert_eq!(read_attr.shape(), vec![2, 3]);

            let arr_dyn: Array2<_> = read_attr.as_reader().read().unwrap();

            assert_eq!(arr, arr_dyn.into_dimensionality().unwrap());
        })
    }

    #[test]
    pub fn test_create() {
        with_tmp_file(|file| {
            let attr = file.new_attr::<u32>().shape((1, 2)).create("foo").unwrap();
            assert!(attr.is_valid());
            assert_eq!(attr.shape(), vec![1, 2]);
            // FIXME - attr.name() returns "/" here, which is the name the attribute is connected to,
            // not the name of the attribute.
            //assert_eq!(attr.name(), "foo");
            assert_eq!(file.attr("foo").unwrap().shape(), vec![1, 2]);
        })
    }

    #[test]
    pub fn test_create_with_data() {
        with_tmp_file(|file| {
            let arr = arr2(&[[1, 2, 3], [4, 5, 6]]);

            let attr = file.new_attr_builder().with_data(&arr).create("foo").unwrap();
            assert!(attr.is_valid());
            assert_eq!(attr.shape(), vec![2, 3]);
            // FIXME - attr.name() returns "/" here, which is the name the attribute is connected to,
            // not the name of the attribute.
            //assert_eq!(attr.name(), "foo");
            assert_eq!(file.attr("foo").unwrap().shape(), vec![2, 3]);

            let read_attr = file.attr("foo").unwrap();
            assert_eq!(read_attr.shape(), vec![2, 3]);
            let arr_dyn: Array2<_> = read_attr.as_reader().read().unwrap();
            assert_eq!(arr, arr_dyn.into_dimensionality().unwrap());
        })
    }

    #[test]
    pub fn test_missing() {
        with_tmp_file(|file| {
            let _ = file.new_attr::<u32>().shape((1, 2)).create("foo").unwrap();
            let missing_result = file.attr("bar");
            assert!(missing_result.is_err());
        })
    }

    #[test]
    pub fn test_write_read_str() {
        with_tmp_file(|file| {
            let s = VarLenUnicode::from_str("var len foo").unwrap();
            let attr = file.new_attr::<VarLenUnicode>().shape(()).create("foo").unwrap();
            attr.as_writer().write_scalar(&s).unwrap();
            let read_attr = file.attr("foo").unwrap();
            assert_eq!(read_attr.shape(), []);
            let r: VarLenUnicode = read_attr.as_reader().read_scalar().unwrap();
            assert_eq!(r, s);
        })
    }

    #[test]
    pub fn test_list_names() {
        with_tmp_file(|file| {
            let arr1 = arr2(&[[123], [456]]);
            let _attr1 = file.new_attr_builder().with_data(&arr1).create("foo").unwrap();
            let _attr2 = file.new_attr_builder().with_data("string").create("bar").unwrap();
            let attr_names = file.attr_names().unwrap();
            assert_eq!(attr_names.len(), 2);
            assert!(attr_names.contains(&"foo".to_string()));
            assert!(attr_names.contains(&"bar".to_string()));
        })
    }
}