def modify(self, name, value): """ Change the value of an attribute while preserving its type. Differs from __setitem__ in that if the attribute already exists, its type is preserved. This can be very useful for interacting with externally generated files. If the attribute doesn't exist, it will be automatically created. """ with phil: if not name in self: self[name] = value else: value = numpy.asarray(value, order="C") attr = h5a.open(self._id, self._e(name)) if attr.get_space().get_simple_extent_type() == h5s.NULL: raise IOError("Empty attributes can't be modified") # Allow the case of () <-> (1,) if (value.shape != attr.shape) and not ( numpy.product(value.shape) == 1 and numpy.product(attr.shape) == 1 ): raise TypeError("Shape of data is incompatible with existing attribute") attr.write(value)
def modify(self, name, value): """ Change the value of an attribute while preserving its type. Differs from __setitem__ in that if the attribute already exists, its type is preserved. This can be very useful for interacting with externally generated files. If the attribute doesn't exist, it will be automatically created. """ if not name in self: self[name] = value else: value = numpy.asarray(value, order='C') attr = h5a.open(self._id, self._e(name)) if attr.get_space().get_simple_extent_type() == h5s.NULL: raise IOError("Empty attributes can't be modified") # Allow the case of () <-> (1,) if (value.shape != attr.shape) and not \ (numpy.product(value.shape) == 1 and numpy.product(attr.shape) == 1): raise TypeError( "Shape of data is incompatible with existing attribute") attr.write(value)
def __init__(self, _id): # super __init__ is handled by DatasetID.__cinit__ automatically self._data_dict = None with phil: sid = self.get_space() self._shape = sid.get_simple_extent_dims() self._reshaped = False attr = h5a.open(self, b'raw_data') htype = h5t.py_create(attr.dtype) _arr = np.ndarray(attr.shape, dtype=attr.dtype, order='C') attr.read(_arr, mtype=htype) raw_data_name = _arr[()] if isinstance(raw_data_name, bytes): raw_data_name = raw_data_name.decode('utf-8') fid = h5i.get_file_id(self) g = Group(fid) self.raw_data = g[raw_data_name] self.chunks = tuple(self.raw_data.attrs['chunks']) fillvalue_a = np.empty((1,), dtype=self.dtype) dcpl = self.get_create_plist() dcpl.get_fill_value(fillvalue_a) self.fillvalue = fillvalue_a[0]
def test_named(self): """ Attributes created from named types link to the source type object """ self.f['type'] = np.dtype('u8') self.f.attrs.create('x', 42, dtype=self.f['type']) self.assertEqual(self.f.attrs['x'], 42) aid = h5a.open(self.f.id, b'x') htype = aid.get_type() htype2 = self.f['type'].id self.assertEqual(htype, htype2) self.assertTrue(htype.committed())
def __getitem__(self, name): """ Read the value of an attribute. """ attr = h5a.open(self._id, self._e(name)) dt = readtime_dtype(attr.dtype, []) arr = numpy.ndarray(attr.shape, dtype=dt, order='C') attr.read(arr) if len(arr.shape) == 0: return arr[()] return arr
def __getitem__(self, name): """ Read the value of an attribute. """ attr = h5a.open(self._id, self._e(name)) if attr.get_space().get_simple_extent_type() == h5s.NULL: raise IOError("Empty attributes cannot be read") tid = attr.get_type() rtdt = readtime_dtype(attr.dtype, []) arr = numpy.ndarray(attr.shape, dtype=rtdt, order="C") attr.read(arr) if len(arr.shape) == 0: return arr[()] return arr
def __getitem__(self, name): """ Read the value of an attribute. """ attr = h5a.open(self._id, self._e(name)) if attr.get_space().get_simple_extent_type() == h5s.NULL: raise IOError("Empty attributes cannot be read") tid = attr.get_type() rtdt = readtime_dtype(attr.dtype, []) arr = numpy.ndarray(attr.shape, dtype=rtdt, order='C') attr.read(arr) if len(arr.shape) == 0: return arr[()] return arr
def test_write(self): self.f.attrs["y"] = self.empty_obj self.assertTrue(is_empty_dataspace(h5a.open(self.f.id, b'y')))