Пример #1
0
    def _createEmptyDataset_(self, parent, dataset_name, shape, dtype,
                             **kwargs):
        """ Creates an empty dataset in the data file and returns a pointer
        to it. Raises IOError exception if the dataset already exists.
        """
        dataset_key = self.hdfObjectKey(dataset_name)
        if dataset_key in parent.keys():
            errmsg = "'%s' dataset already exists in current data file."
            raise IOError, errmsg % dataset_name

        create_args = {}
        attributes = {}
        for name in kwargs:
            if name in DATASET_CREATE_ARGS:
                create_args[safestring(name)] = safevalue(kwargs[name])
            else:
                attributes[safestring(name)] = safevalue(kwargs[name])

        if 'created' not in attributes:
            attributes['created'] = self._timestamp_()

        if dtype == N.dtype(object):
            create_args['dtype'] = h5py.new_vlen(str)
        else:
            create_args['dtype'] = dtype

        dataset = parent.create_dataset(dataset_key, shape, **create_args)

        for attr_name, attr_value in attributes.items():
            dataset.attrs[attr_name] = attr_value

        return dataset
Пример #2
0
 def _setObjectAttribute_(self, _object, attr_name, attr_value):
     """ Returns a dictionary of attr_name/attr_value pairs for all
     setable attributes of the dataset .
     """
     try:
         _object.attrs[safeDataKey(attr_name)] = safestring(attr_value)
     except Exception as e:
         errmsg = "Could not set attribute '%s' to '%s' for object '%s'"
         errmsg = errmsg % (attr_name, str(attr_value), _object.name)
         e.args = (errmsg, ) + e.args
Пример #3
0
    def _createDataset_(self, parent, dataset_name, numpy_array_or_shape,
                        **kwargs):
        """ Creates a new dataset in the data file and returns a pointer to
        it. Raises IOError exception if the dataset already exists.
        """
        dataset_key = self.hdfObjectKey(dataset_name)
        if dataset_key in parent.keys():
            errmsg = "'%s' dataset already exists in current data file."
            raise IOError, errmsg % dataset_name

        create_args = {}
        attributes = {}
        for name in kwargs:
            if name in DATASET_CREATE_ARGS:
                create_args[safestring(name)] = safevalue(kwargs[name])
            else:
                attributes[safestring(name)] = safevalue(kwargs[name])

        if 'created' not in attributes:
            attributes['created'] = self._timestamp_()

        if 'fillvalue' in create_args:
            if 'dtype' not in create_args:
                errmsg = "'dtype' is required for empty or extendable datasets."
                raise IOError, errmsg

            shape = numpy_array_or_shape
            dataset = parent.create_dataset(dataset_key, shape, **create_args)

        else:  #TODO: need to have a better set of checks here
            if 'dtype' not in create_args\
            and numpy_array_or_shape.dtype == N.dtype(object):
                create_args['dtype'] = h5py.new_vlen(str)

            dataset = parent.create_dataset(dataset_key,
                                            data=numpy_array_or_shape,
                                            **create_args)

        for attr_name, attr_value in attributes.items():
            dataset.attrs[attr_name] = attr_value

        return dataset