Ejemplo n.º 1
0
def dual_contour_2d(f, f_normal, xmin=XMIN, xmax=XMAX, ymin=YMIN, ymax=YMAX):
    """Iterates over a cells of size one between the specified range, and evaluates f and f_normal to produce
    a boundary by Dual Contouring. Returns an unordered list of Edge objects."""
    # For each cell, find the the best vertex for fitting f
    verts = {}
    for x in range(xmin, xmax):
        for y in range(ymin, ymax):
            verts[(x, y)] = dual_contour_2d_find_best_vertex(f, f_normal, x, y)
    # For each cell edge, emit an edge between the center of the adjacent cells if it is a sign changing edge
    edges = []
    for x in range(xmin + 1, xmax):
        for y in range(ymin, ymax):
            y0 = y
            y1 = y + 1
            y0_solid = f(x, y0) > 0
            y1_solid = f(x, y1) > 0
            if y0_solid != y1_solid:
                edges.append(
                    Edge(verts[(x - 1, y)], verts[x, y]).swap(y0_solid))
    for x in range(xmin, xmax):
        for y in range(ymin + 1, ymax):
            x0 = x
            x1 = x + 1
            x0_solid = f(x0, y) > 0
            x1_solid = f(x1, y) > 0
            if x0_solid != x1_solid:
                edges.append(
                    Edge(verts[(x, y - 1)], verts[x, y]).swap(x0_solid))
    return edges
    def convert(self, tensor):
        """Function to create an Edge object representing given Tensor.

        Creates a new Edge object to represent the given tensor and populates
        its attributes.

        Args:
            tensor (TF tensor object) : The tensor to create an edge for.

        Returns:
            The created Edge object intance representing the tensor.
        """
        # Creating edge and extracting features
        new_edge = Edge.Edge(label=tensor.name,
                             tensor_label=tensor.name,
                             value=None)

        new_edge.tensor_type = str(tensor.dtype)[9:-2].upper()
        if tensor.shape:
            new_edge.tensor_shape = tensor.get_shape().as_list()
        return new_edge
    def convert(self, tensor):
        """Function to create an Edge object representing given Tensor.

        Creates a new Edge object to represent the given tensor and populates
        its attributes.

        Args:
            tensor (tflite/Tensor object) : The tensor to create an edge for.

        Returns:
            The created Edge object instance representing the tensor.
        """

        new_edge = Edge.Edge(label=tensor.Name(),
                             tensor_label=tensor.Name(),
                             value=tensor)
        new_edge.tensor_type = self._tensor_type[tensor.Type()]

        shape = list()
        for i in range(tensor.ShapeLength()):
            shape.append(tensor.Shape(i))
        new_edge.tensor_shape = shape

        return new_edge
Ejemplo n.º 4
0
 def connect(self, id1, id2, wt=None, cap=None):
     edge = Edge(id1, id2, self.attrs['directed'], wt, cap)
     self.add_edge(edge)
Ejemplo n.º 5
0
    case = ((1 if x0y0 > 0 else 0) + (2 if x0y1 > 0 else 0) +
            (4 if x1y0 > 0 else 0) + (8 if x1y1 > 0 else 0))

    # Several of the cases are inverses of each other where solid is non solid and visa versa
    # They have the same boundary, which cuts down the cases a bit.
    # But we swap the direction of the boundary, so that edges are always winding clockwise around the solid.
    # Getting those swaps correct isn't needed for our simple visualizations, but is important in other uses cases
    # particularly in 3d.

    if case is 0 or case is 15:
        # All outside / inside
        return []
    if case is 1 or case is 14:
        # Single corner
        return [
            Edge(V2(x + 0 + adapt(x0y0, x1y0), y),
                 V2(x + 0, y + adapt(x0y0, x0y1))).swap(case is 14)
        ]
    if case is 2 or case is 13:
        # Single corner
        return [
            Edge(V2(x + 0, y + adapt(x0y0, x0y1)),
                 V2(x + adapt(x0y1, x1y1), y + 1)).swap(case is 11)
        ]
    if case is 4 or case is 11:
        # Single corner
        return [
            Edge(V2(x + 1, y + adapt(x1y0, x1y1)),
                 V2(x + adapt(x0y0, x1y0), y + 0)).swap(case is 13)
        ]
    if case is 8 or case is 7:
        # Single corner
Ejemplo n.º 6
0
    def parse_models(self, parse_stateful = False):
        """Method to query and read data from database.

        Method to query database and read models into Graph objects.

        Args:
            parse_stateful (bool) : Boolean to indicate whether graphs with 
                stateful partitioned call should be parsed, these graphs do not
                contain a graph structure or tensors. Defaults to False.

        Returns:
            List of Graph objects corresponding to the graph objects the models
            in the spanner database have been parsed into.
        """

        model_graphs = list()
        
        # Query to get all models from Models table
        with self.database.snapshot() as snapshot:
            qresult_models = snapshot.execute_sql(
                "SELECT model_name, category, sub_category, source, num_inputs"
                " FROM Models"
                )

        for row in qresult_models:

            # Checking num_inputs for presence of graph structure
            if row[4] == 0 and not parse_stateful:
                continue

            # Extracting model attributes
            model_name = row[0]
            category = row[1]
            sub_category = row[2]
            source = row[3]

            nodes = list()
            edges = list()
            start_node_indices = list()

            adj_list = dict()

            # Querying Operators of model_name
            with self.database.snapshot() as snapshot:
                qresult_operators = snapshot.execute_sql(
                    "SELECT * from Models JOIN Operators"
                    " ON Models.model_name = Operators.model_name"
                    " WHERE Models.model_name = '" + model_name + "'"
                    " ORDER BY operator_id"
                )
            
            # Dictionary to hold which field is in which index of query results
            field_to_index = dict()

            # Boolean to check if field_to_dict needs to be populated
            populate_dicts = True

            # Extracting Node attributes
            for row in qresult_operators:
                if populate_dicts:
                    for index in range(len(qresult_operators.metadata.row_type.fields)):
                        field_name = qresult_operators.metadata.row_type.fields[index].name
                        field_to_index[field_name] = index
                    
                    populate_dicts = False

                new_node = Node.Node(None, None)

                for attr in vars(new_node).keys():
                    if attr in field_to_index:
                        setattr(new_node, attr, row[field_to_index[attr]])

                nodes.append(new_node)

                # populating start_node_indices using is_input field
                if row[field_to_index['is_input']]:
                    start_node_indices.append(len(nodes) - 1)
            
            # Querying Tensors of model_name
            with self.database.snapshot() as snapshot:
                qresult_tensors = snapshot.execute_sql(
                    "SELECT * from Models JOIN Tensors"
                    " ON Models.model_name = Tensors.model_name"
                    " WHERE Models.model_name = '" + model_name + "'"
                    " ORDER BY tensor_id"
                )

            # Dictionary to hold which field is in which index of query results
            field_to_index.clear()

            # Boolean to check if field_to_dict needs to be populated
            populate_dicts = True

            # Extracting Edge attributes
            for row in qresult_tensors:
                if populate_dicts:
                    for index in range(len(qresult_tensors.metadata.row_type.fields)):
                        field_name = qresult_tensors.metadata.row_type.fields[index].name
                        field_to_index[field_name] = index
                    
                    populate_dicts = False

                new_edge = Edge.Edge(None, None)

                for attr in vars(new_edge).keys():
                    if attr in field_to_index:
                        setattr(new_edge, attr, row[field_to_index[attr]])

                edges.append(new_edge)

                to_operator_ids = row[field_to_index['to_operator_ids']]
                from_operator_ids = row[field_to_index['from_operator_ids']]

                edge_index = len(edges) - 1

                for src_node_index in from_operator_ids:
                    src_node_index -= 1
                    for dest_node_index in to_operator_ids:
                        dest_node_index -= 1

                        if src_node_index not in adj_list:
                            adj_list.update({src_node_index : []})
                        
                        adj_list[src_node_index].append([edge_index, 
                                                            dest_node_index])

            new_graph = Graph.Graph(nodes, start_node_indices, edges, adj_list, 
                                    model_name, category, sub_category)
            new_graph.source = source

            model_graphs.append(new_graph)

        return model_graphs