Пример #1
0
    def convert_from_h5_model(self, obj=dict(), children_dict=class_dict):

        children_dict.update(getattr(obj, "children_dict", {}))

        output = obj.__class__.__name__
        if np.in1d(output, ["tuple", "list"]):
            obj = list_or_tuple_to_dict(obj)

        data = dict()
        data.update(self.datasets_dict)
        data.update(self.metadata_dict)

        data = sort_dict(data)

        for key, value in data.iteritems():

            if key[0] == "/":
                key = key.split('/', 1)[1]

            build_hierarchical_object_recursively(obj, key, value,
                                                  children_dict)

        if np.in1d(output, ["tuple", "list"]):
            obj = dict_to_list_or_tuple(obj, output)

        return obj
Пример #2
0
 def __repr__(self):
     d = {
         "a. sensors type": self.s_type,
         "b. labels": reg_dict(self.labels),
         "c. locations": reg_dict(self.locations, self.labels),
         "d. orientations": reg_dict(self.orientations, self.labels)
     }
     return formal_repr(self, sort_dict(d))
Пример #3
0
 def __repr__(self):
     d = {
         "a. vertices": self.vertices,
         "b. triangles": self.triangles,
         "c. vertex_normals": self.vertex_normals,
         "d. triangle_normals": self.triangle_normals
     }
     return formal_repr(self, sort_dict(d))
Пример #4
0
 def __repr__(self):
     d = {"1. name": self.name,
          "2. low": self.low,
          "3. high": self.high,
          "4. location": self.loc,
          "5. scale": self.scale,
          "6. distribution": self.distribution,
          "7. shape": self.shape}
     return formal_repr(self, sort_dict(d))
Пример #5
0
def read_h5_model(path):

    h5_file = h5py.File(path, 'r', libver='latest')

    datasets_dict = dict()
    metadata_dict = dict()

    for key, value in h5_file.attrs.iteritems():
        metadata_dict.update({key: value})

    datasets_keys = return_h5_dataset_paths_recursively(h5_file)

    for key in datasets_keys:
        datasets_dict.update({key: h5_file[key][()]})

    datasets_dict = sort_dict(datasets_dict)
    metadata_dict = sort_dict(metadata_dict)

    return H5Model(datasets_dict, metadata_dict)
Пример #6
0
 def __repr__(self):
     d = {"f. normalized weights": reg_dict(self.normalized_weights, self.region_labels),
          "g. weights": reg_dict(self.weights, self.region_labels),
          "h. tract_lengths": reg_dict(self.tract_lengths, self.region_labels),
          "a. region_labels": reg_dict(self.region_labels),
          "b. centers": reg_dict(self.centers, self.region_labels),
          "c. hemispheres": reg_dict(self.hemispheres, self.region_labels),
          "d. orientations": reg_dict(self.orientations, self.region_labels),
          "e. areas": reg_dict(self.areas, self.region_labels)}
     return formal_repr(self, sort_dict(d))
Пример #7
0
 def __repr__(self):
     d = {"1. name": self.name,
          "2. connectivity": self.connectivity,
          "3. RM": reg_dict(self.region_mapping, self.connectivity.region_labels),
          "4. VM": reg_dict(self.volume_mapping, self.connectivity.region_labels),
          "5. surface": self.cortical_surface,
          "6. T1": self.t1_background,
          "7. SEEG": self.sensorsSEEG,
          "8. EEG": self.sensorsEEG,
          "9. MEG": self.sensorsMEG }
     return formal_repr(self, sort_dict(d))
Пример #8
0
 def __repr__(self):
     d = {"1. name": self.name,
          "2. type": self.type,
          "3. number of regions": self.n_regions,
          "4. number of active regions": self.n_active_regions,
          "5. number of nonactive regions": self.n_nonactive_regions,
          "6. number of observation signals": self.n_signals,
          "7. number of time points": self.n_times,
          "8. euler_method": self.euler_method,
          "9. observation_expression": self.observation_expression,
          "10. observation_model": self.observation_model,
          "11. number of parameters": self.n_parameters,
          "12. parameters": [p.__str__ for p in self.parameters.items()]}
     return formal_repr(self, sort_dict(d))
Пример #9
0
    def convert_from_h5_model(self,
                              obj=dict(),
                              children_dict=class_dict,
                              hypothesis=False):

        children_dict.update(getattr(obj, "children_dict", {}))

        output = obj.__class__.__name__
        if np.in1d(output, ["tuple", "list"]):
            obj = list_or_tuple_to_dict(obj)

        data = dict()
        data.update(self.datasets_dict)
        data.update(self.metadata_dict)

        data = sort_dict(data)

        for key, value in data.iteritems():

            if key[0] == "/":
                key = key.split('/', 1)[1]

            build_hierarchical_object_recursively(obj, key, value,
                                                  children_dict)

        if np.in1d(output, ["tuple", "list"]):
            obj = dict_to_list_or_tuple(obj, output)

        if hypothesis:
            # TODO: make this hack work also recursively, if needed...
            if isinstance(obj, dict):
                set_field = lambda obj, key, value: obj.update({key: value})
                get_field = lambda obj, key: obj.get(key, None)
            else:
                set_field = lambda obj, attribute, value: setattr(
                    obj, attribute, value)
                get_field = lambda obj, attribute: getattr(
                    obj, attribute, None)

            for var_str in ["x0_values", "e", "w"]:
                ind = get_field(obj, var_str + "_indices")
                var_key = var_str + "_values"
                var = np.array(get_field(obj, var_key))
                if len(ind) > 0:
                    set_field(obj, var_key, var[ind])
                else:
                    set_field(obj, var_key, np.array([]))

        return obj
Пример #10
0
def object_to_h5_model_recursively(h5_model, obj, name=""):

    # Use in some cases the name of the class as key, when name is empty string. Otherwise, class_name = name.
    name_empty = (len(name) == 0)
    class_name = name_empty * obj.__class__.__name__ + name

    if obj is None:
        h5_model.add_or_update_metadata_attribute(class_name, "None")

    if isinstance(obj, (float, int, long, complex, str, np.ndarray)):

        if isinstance(obj, (float, int, long, complex, str)):
            h5_model.add_or_update_metadata_attribute(class_name, obj)

        elif isinstance(obj, np.ndarray):
            h5_model.add_or_update_datasets_attribute(class_name, obj)

    else:

        for key, val in bool_inf_nan_empty.iteritems():
            # Bool, inf, nan, or empty list/tuple/dict/str
            try:
                if all(obj == val):
                    h5_model.add_or_update_metadata_attribute(class_name, key)
                    return
            except:
                continue

        # In any other case, make sure object is/becomes an alphabetically ordered dictionary:
        if not (isinstance(obj, dict)):

            try:

                for class_type in class_list:
                    if obj.__class__.__name__ == class_type:
                        name = name + ":" + obj.__class__.__name__
                        break

                if isinstance(obj, (list, tuple)):
                    try:
                        # empty list or tuple get into metadata
                        if len(obj) == 0:
                            h5_model.add_or_update_metadata_attribute(
                                name, key)
                            return
                        temp = np.array(obj)
                        # those that can be converted to np arrays get in datasets
                        if temp.dtype != "O":
                            h5_model.add_or_update_datasets_attribute(
                                name, temp)
                            return
                    except:
                        pass
                    # the rest are converted to dict
                    obj = list_or_tuple_to_dict(obj)

                elif isinstance(obj, dict):
                    obj = sort_dict(obj)

                else:
                    obj = sort_dict(vars(obj))

            except:
                logger.info("Object " + name + (len(name) > 0) * "/" + key +
                            "cannot be assigned to h5_model because it"
                            "is has no __dict__ property")
                return

        for key, value in obj.iteritems():

            key = name + (len(name) > 0) * "/" + key

            if key.find("children_dict") < 0:
                # call recursively...
                object_to_h5_model_recursively(h5_model, value, key)