Exemplo n.º 1
0
def list_or_tuple_to_h5_model(h5_model, obj, path, container_path, obj_type):
    # empty list or tuple get into metadata
    if len(obj) == 0:
        h5_model.add_or_update_metadata_attribute(path + "/type_str", obj_type)
        if isinstance(obj, list):
            h5_model.add_or_update_datasets_attribute(path, "[]")
        else:
            h5_model.add_or_update_datasets_attribute(path, "()")
        return h5_model, None
    # Try to store it as a ndarray of numbers or strings but not objects...
    temp = np.array(obj)
    if not (isequal_string(str(temp.dtype)[0], "O")):
        h5_model.add_or_update_metadata_attribute(path + "/type_str",
                                                  obj.__class__.__name__)
        if isinstance(obj, tuple):
            h5_model.add_or_update_metadata_attribute(path + "/transform_str",
                                                      "tuple(obj)")
        else:
            h5_model.add_or_update_metadata_attribute(path + "/transform_str",
                                                      "obj.tolist()")
        h5_model.add_or_update_datasets_attribute(path, temp)
        return h5_model, None
    else:
        h5_model.add_or_update_metadata_attribute(
            os.path.join(container_path[1:], "create_str"), "list()")
        if isinstance(obj, tuple):
            h5_model.add_or_update_metadata_attribute(
                os.path.join(container_path[1:], "transform_str"),
                "tuple(obj))")
        return h5_model, iterable_to_dict(obj)
Exemplo n.º 2
0
 def convert_from_h5_model(self, obj=None, output_shape=None):
     output_type = obj.__class__.__name__
     if isinstance(obj, dict):
         obj = sort_dict(obj)
     elif np.in1d(output_type, ["tuple", "list"]):
         obj = iterable_to_dict(obj)
     elif isequal_string(output_type, "numpy.ndarray"):
         if isequal_string(obj.dtype, "numpy.ndarray"):
             obj = iterable_to_dict(obj.tolist())
     else:
         obj, output_type = create_object("/", self.metadata_dict)[:2]
     if obj is None:
         obj = OrderedDict()
     if output_type is None:
         output_type = obj.__class__.__name__
     for abs_path in self.datasets_dict.keys():
         child_obj = self.datasets_dict.pop(abs_path)
         rel_path = abs_path.split("/", 1)[1]
         build_hierarchical_object_recursively(obj, rel_path, child_obj,
                                               "/", abs_path,
                                               self.metadata_dict)
     if np.in1d(output_type, ["tuple", "list"]):
         obj = dict_to_list_or_tuple(obj, output_type)
     elif isequal_string(output_type, "numpy.ndarray"):
         obj = np.array(dict.values())
         if isinstance(output_shape, tuple):
             try:
                 obj = np.reshape(obj, output_shape)
             except:
                 logger.warning(
                     "Failed to reshape read object to target shape " +
                     str(output_shape) + "!" +
                     "\nReturning array of shape " + str(obj.shape) + "!")
     else:
         obj = update_object(obj, "/", self.metadata_dict,
                             getORpop="pop")[0]
     if isinstance(obj, dict) and output_type.lower().find("dict") < 0:
         return OrderedDictDot(obj)
     else:
         return obj
Exemplo n.º 3
0
def array_to_h5_model(h5_model, obj, path, container_path, obj_type):
    if isequal_string(str(obj.dtype)[0], "O"):
        h5_model.add_or_update_metadata_attribute(
            os.path.join(container_path, "create_str"), "list()")
        h5_model.add_or_update_metadata_attribute(
            os.path.join(container_path, "transform_str"),
            "np.reshape(obj, " + str(obj.shape) + ")")
        return h5_model, iterable_to_dict(obj)
    else:
        h5_model.add_or_update_metadata_attribute(path + "/type_str", obj_type)
        h5_model.add_or_update_metadata_attribute(
            path + "/transform_str", "np.reshape(obj, " + str(obj.shape) + ")")
        h5_model.add_or_update_datasets_attribute(path, obj)
        return h5_model, None