def graph_property(self, property_name):
     """todo"""
     try:
         return self._graph_property[property_name]
     except KeyError:
         raise PropertyError("property %s is undefined on graph" %
                             property_name)
Beispiel #2
0
 def remove_vertex_property(self, property_name):
     """todo"""
     try:
         del self._vertex_property[property_name]
     except KeyError:
         raise PropertyError("property %s is undefined on vertices" 
                             % property_name)
 def add_graph_property(self, property_name, values=None):
     """todo"""
     if property_name in self._graph_property:
         raise PropertyError("property %s is already defined on graph" %
                             property_name)
     if values is None: values = {}
     self._graph_property[property_name] = values
 def edge_property(self, property_name):
     """todo"""
     try:
         return self._edge_property[property_name]
     except KeyError:
         raise PropertyError("property %s is undefined on edges" %
                             property_name)
Beispiel #5
0
 def update_interface_property(self,
                               property_name,
                               degree,
                               values,
                               keys=None):
     """todo"""
     if not (isinstance(values, np.ndarray) or isinstance(values, dict)
             or isinstance(values, array_dict)):
         raise TypeError(
             "Values are not in an acceptable type (array, dict, array_dict)"
         )
     if property_name not in self._interface_properties[degree]:
         print PropertyError("property " + property_name +
                             " is undefined on elements of degree " +
                             str(degree))
         print "Creating property ", property_name, " for degree ", degree
         self._interface_properties[degree][property_name] = array_dict()
     if isinstance(values, np.ndarray):
         if keys is None:
             keys = np.array(self._interface.keys())
         self._interface_properties[degree][property_name].update(
             values, keys=keys, ignore_missing_keys=False)
     elif isinstance(values, dict):
         if keys is None:
             keys = np.array(values.keys())
         self._interface_properties[degree][property_name].update(
             np.array(values.values()),
             keys=keys,
             ignore_missing_keys=False)
     elif isinstance(values, array_dict):
         if keys is None:
             keys = values.keys()
         self._interface_properties[degree][property_name].update(
             values.values(), keys=keys, ignore_missing_keys=False)
    def add_domains_from_dict(self, dict_domains, domain_names):
        """ If one already posses a dict indicating for a list of vertex which domain they belong to, it can be given to the graph directly.
        
        :Parameters:
        - `dict_domains` (dict) - *keys = ids (SpatialImage); *values = intergers indicating the domain(s)
        - `domain_name` (list) - a list containing the name of the domain(s)
        """
        list_domains = np.unique(dict_domains.values())
        if len(domain_names) != len(list_domains):
            warnings.warn(
                "You didn't provided the same number of domains and domain names."
            )
            pass

        if not "domains" in self._vertex_property.keys():
            self.add_vertex_property("domains")

        for domain, domain_name in enumerate(domain_names):
            if domain_name in self._graph_property:
                raise PropertyError(
                    "property {} is already defined on graph".format(
                        domain_name))
            self._graph_property[domain_name] = []
            for vid in dict_domains:
                if dict_domains[vid] == list_domains[domain]:
                    self._add_vertex_to_domain(set([vid]), domain_name)
Beispiel #7
0
 def interface_property(self, property_name, degree, wids=None):
     """todo"""
     try:
         return self._interface_properties[degree][property_name]
     except KeyError:
         raise PropertyError("property " + property_name +
                             " is undefined on interfaces of degree " +
                             str(degree))
 def is_connected_domain(self, domain_name, edge_type=None):
     """Return True if a domain is connected."""
     if not domain_name in self._graph_property:
         raise PropertyError("property %s is not defined on graph" %
                             domain_name)
     domain_sub_graph = Graph.sub_graph(self,
                                        self._graph_property[domain_name])
     distances = domain_sub_graph.topological_distance(
         domain_sub_graph._vertices.keys()[0], edge_type=edge_type)
     return not float('inf') in distances.values()
 def remove_graph_property(self, property_name):
     """todo"""
     try:
         del self._graph_property[property_name]
     except KeyError:
         raise PropertyError("property %s is undefined on graph" %
                             property_name)
     try:
         del self._graph_property['units'][property_name]
     except KeyError:
         pass
 def vertex_property(self, property_name, vids=None):
     """todo"""
     try:
         if vids is not None:
             return dict([(k, v) for k, v in
                          self._vertex_property[property_name].iteritems()
                          if k in vids])
         else:
             return self._vertex_property[property_name]
     except KeyError:
         raise PropertyError("property %s is undefined on vertices" %
                             property_name)
    def remove_domain(self, domain_name):
        """Remove a domain `domain_name`."""
        if not domain_name in self._graph_property:
            raise PropertyError(
                "property {} is not defined on graph".format(domain_name))

        for vid in self.iter_domain(domain_name):
            self._vertex_property["domains"][vid].remove(domain_name)
            if self._vertex_property["domains"][vid] == []:
                self._vertex_property["domains"].pop(vid)

        return self._graph_property.pop(domain_name)
Beispiel #12
0
 def wisp_property(self, property_name, degree, wids=None):
     """todo"""
     try:
         if wids is not None:
             return array_dict(
                 self._wisp_properties[degree][property_name].values(wids),
                 wids)
         else:
             return self._wisp_properties[degree][property_name]
     except KeyError:
         raise PropertyError("property " + property_name +
                             " is undefined on wisps of degree " +
                             str(degree))
 def add_domain_from_func(self, func, domain_name):
     """ Create a domain `domain_name` of vertices according to a function `func`.
     
     :Parameters:
     - `func` : the function to make the domain (might return True or False)
     - `domain_name` : the name of the domain
     """
     if domain_name in self._graph_property:
         raise PropertyError(
             "property {} is already defined on graph".format(domain_name))
     self._graph_property[domain_name] = []
     if not "domains" in self._vertex_property.keys():
         self.add_vertex_property("domains")
     for vid in self._vertices.keys():
         if func(self, vid):
             self._add_vertex_to_domain(set([vid]), domain_name)
Beispiel #14
0
 def add_interface_property(self, property_name, degree, values=None):
     """todo"""
     if property_name in self._interface_properties[degree]:
         raise PropertyError(
             "property " + property_name +
             " is already defined on interfaces of degree " + str(degree))
     if values is None: values = array_dict()
     elif isinstance(values, np.ndarray):
         keys = np.array(self._interface.keys())
         values = array_dict(values, keys)
     elif isinstance(values, dict):
         values = array_dict(values)
     if not isinstance(values, array_dict):
         raise TypeError(
             "Values are not in an acceptable type (array, dict, array_dict)"
         )
     self._interface_properties[degree][property_name] = values
    def extend_graph_property(self, property_name, values):
        """todo"""
        assert values is not None
        if property_name not in self._graph_property:
            raise PropertyError("property %s is not defined on graph" %
                                property_name)
        #~ if self.graph_property(property_name) is not None or self.graph_property(property_name) != []:
        #~ assert isinstance(values, type(self.graph_property(property_name)))

        if isinstance(self.graph_property(property_name), list):
            self._graph_property[property_name].extend(values)
        elif isinstance(self.graph_property(property_name), dict):
            self._graph_property[property_name].update(
                dict([(k, v) for k, v in values.iteritems()
                      if k not in self.graph_property(property_name).keys()]))
        else:
            print "Unable to extend 'graph_property' with this type of data: {}".format(
                type(values))
    def extend_vertex_property(self, property_name, values):
        """todo AND TO CHECK AND TEST !!"""
        if not isinstance(values, dict):
            raise TypeError("Values %s is not a type 'dict'" % values)
        if property_name not in self._vertex_property:
            print PropertyError("Property %s is not defined on vertices" %
                                property_name)
            print "Creating vertex property %s" % property_name
            self._vertex_property[property_name] = {}

        for k, v in values.iteritems():
            if k in self.vertices():
                if not self._vertex_property[property_name].has_key(k):
                    self._vertex_property[property_name][k] = v
                else:
                    print "Vertex id {} already has a value for vertex property {}".format(
                        k, property_name)
            else:
                print "Vertex id {} doesn't exist in the graph !!".format(k)
 def iter_domain(self, domain_name):
     if not domain_name in self._graph_property:
         raise PropertyError(
             "property {} is not defined on graph".format(domain_name))
     return iter(self._graph_property[domain_name])
 def remove_vertex_from_domain(self, vids, domain_name):
     """Remove a set of vertices `vids` from a domain `domain_name`."""
     if not domain_name in self._graph_property:
         raise PropertyError(
             "property {} is not defined on graph".format(domain_name))
     self._remove_vertex_from_domain(self.__to_set(vids), domain_name)
Beispiel #19
0
 def add_edge_property(self, property_name):
     """todo"""
     if property_name in self._edge_property:
         raise PropertyError("property %s is already defined on edges" 
                             % property_name)
     self._edge_property[property_name] = {}