def _traverse(self, fiber):
        """traverse"""

        #
        # Assume this is a rank-3 or less tensor
        #
        if not Payload.contains(fiber, Fiber):
            #
            # Draw a 0-D tensor, i.e., a value (NOT a fiber)
            #

            # TBD
            region_size = [1, 1]
        else:
            #
            # Recursively draw the fibers of a non-0-D tensor
            #
            shape = fiber.getShape(all_ranks=True)
            dimensions = len(shape)

            hl_manager = self.highlight_manager

            if dimensions == 4:
                region_size = self._traverse_hypercube(
                    shape, fiber, highlight_manager=hl_manager)
            elif dimensions == 3:
                region_size = self._traverse_cube(shape,
                                                  fiber,
                                                  highlight_manager=hl_manager)
            elif dimensions == 2:
                region_size = self._traverse_matrix(
                    shape, fiber, highlight_manager=hl_manager)
            elif dimensions == 1:
                region_size = self._traverse_vector(
                    shape, fiber, highlight_manager=hl_manager)
            else:
                self.logger.info(
                    f"Unsupported number of ranks for uncompressed image ({dimensions})"
                )
                region_size = [1, 1]

        return region_size
    def _create_uncompressed(self):
        """Create uncompressed image

        Create an image of a tensor or fiber tree

        Notes
        ------

        The drawing is made in a coordinate space where the X
        and Y are the positions in the tensor.
        Translation to pixels happens in the draw_*() methods.

        """

        object = self.object

        #
        # Create the objects for the image
        #
        self._image_setup()

        #
        # Display either the root of a tensor or a raw fiber
        #
        if isinstance(object, Tensor):
            #
            # Handle a tensor
            #
            root = object.getRoot()
            self._color = object.getColor()
            #
            # Print tensor name
            #
            name = object.getName()
            if not name:
                name = "unknown"

            ranks = ", ".join([str(r) for r in object.getRankIds()])

            self._draw_label(0, 0, f"Tensor: {name}[{ranks}]")

        elif isinstance(object, Fiber):
            #
            # Handle a fiber
            #
            root = object
            self._color = "red"
        else:
            #
            # Handle a scalar
            #
            root = None
            self._color = "red"

        #
        # Process appropriately if root has 0 dimensions or more
        #
        if not Payload.contains(root, Fiber):
            # Draw a 0-D tensor, i.e., a value

            # TBD
            region_size = [1, 1]

        else:
            # Draw a non-0-D tensor or a fiber, i.e., the fiber tree
            region_size = self._traverse(root)

        #
        # Crop the image
        #
        if region_size[0] > self.row_extent or region_size[1] > self.col_extent:
            msg = f"Uncompressed image too large [ {region_size[0]}, {region_size[1]}"
            self.logger.info(msg)
            return

        right = 200 + self._col2x(region_size[1])
        lower = 20 + self._row2y(region_size[0])

        self.logger.debug(
            f"right: {region_size[1]}/{right}, lower: {region_size[0]}/{lower}"
        )
        self.im = self.im.crop((0, 0, right, lower))
Exemplo n.º 3
0
    def _create_tree(self):
        """create_tree

        Create an image of a tensor or fiber tree

        Notes
        ------

        The drawing is made in a coordinate space where the X
        dimension is measured in the number of non-empty fiber
        coordinates being displayed and the Y dimension is measured in
        layers of the tree. Translation to pixels happens in the
        draw_*() methods.

        """

        object = self.object

        #
        # Create the objects for the image
        #
        self._image_setup()

        #
        # Display either the root of a tensor or a raw fiber
        #
        if isinstance(object, Tensor):
            #
            # Displaying a tensor
            #
            root = object.getRoot()
            #
            # Get tensor's name
            #
            name = object.getName()
            #
            # Get tensor's color
            #
            self._color = object.getColor()
            #
            # Create rank_id string
            #
            # Note: if rank_id is a list, convert to a string
            #
            ranks = ", ".join([str(r) for r in object.getRankIds()])

            if name:
                self._draw_rank(0, f"Tensor: {name}[{ranks}]")
            else:
                self._draw_rank(0, f"File: {object.yamlfile}")
        elif isinstance(object, Fiber):
            #
            # Displaying a fiber
            #
            root = object
            self._color = "red"
        else:
            #
            # Displaying nothing?
            #
            root = None
            self._color = "red"

        #
        # Process appropriately if root has 0 dimensions or more
        #
        if not Payload.contains(root, Fiber):
            #
            # Draw a 0-D tensor, i.e., a value
            #
            hlm = self.highlight_manager
            hl = hlm.getColorCoord(0)

            self._draw_coord(0, 0, "R")
            self._draw_line(0, 1 / 2, 1, 1 / 2)
            self._draw_value(1, 0, Payload.get(root), hl)
            region_end = 1
        else:
            #
            # Draw a non-0-D tensor or a fiber, i.e., the fiber tree
            #
            region_end = self._traverse(
                root, highlight_manager=self.highlight_manager)

        #
        # Crop the image
        #
        right = 200 + self._offset2x(region_end)
        lower = 20 + self.max_y

        self.im = self.im.crop((0, 0, right, lower))
Exemplo n.º 4
0
    def _traverse(self, fiber, level=0, offset=0, highlight_manager=None):
        """traverse"""

        #
        # Check if this is level0, which may just be a payload
        #
        if level == 0:
            region_start = 0

            if not Payload.contains(fiber, Fiber):
                #
                # Draw a 0-D tensor, i.e., a value (NOT a fiber)
                #
                self._draw_coord(0, 0, "R")
                self._draw_line(0, 1 / 2, 1, 1 / 2)
                self._draw_value(1, 0, Payload.get(fiber))
                region_end = 1
            else:
                #
                # Recursively traverse and draw the fibers of a non-0-D tensor
                #
                region_end = self._traverse(
                    fiber,
                    level=1,
                    offset=offset,
                    highlight_manager=highlight_manager)

                region_size = region_end - region_start
                #
                # Draw root of tree
                #
                fiber_size = 1
                fiber_start = region_start + (region_size - fiber_size) / 2
                self._draw_coord(0, fiber_start, "R")
                self._draw_line(0, region_size / 2, 1, region_size / 2)

            return region_end

        #
        # Process the fibers of the tree (level > 0)
        #

        #
        # Print out the rank information (if available)
        #
        if offset == 0 and not fiber.getOwner() is None:
            self._draw_rank(level, "Rank: %s " % fiber.getOwner().getId())

        #
        # Initialize drawing region information
        #
        region_start = offset
        region_end = offset

        #
        # Figure out space of region below this fiber
        #
        targets = []
        coordinate_start = region_start

        #
        # Traverse the fiber at this level
        #
        for n, (c, p) in enumerate(fiber):
            #
            # TBD: Truncate fibers with too many elements
            #
            # if n > 10: break

            if Payload.contains(p, Fiber):
                #
                # Configure highlights for this fiber
                #
                next_highlight_manager = highlight_manager.addFiber(c)

                #
                # Draw the object below this coordinate (in "c")
                #
                region_end = self._traverse(
                    Payload.get(p),
                    level=level + 1,
                    offset=region_end,
                    highlight_manager=next_highlight_manager)

            else:
                region_end += 1

            #
            # Record (in "targets") the middle of the object below
            # this coordinate to draw a line to it later, and
            # calculate where the next object starts ("coordinate_start")
            #
            targets.append(coordinate_start +
                           (region_end - coordinate_start) / 2)
            coordinate_start = region_end

        #
        # If the fiber was empty we still occupy a single space
        #
        if len(fiber) == 0:
            region_end += 1

        region_size = region_end - region_start

        #
        # Set up the highlighting for this level
        #
        highlight_subtensor = highlight_manager.highlight_subtensor

        #
        # Display fiber for this level
        #
        fiber_size = len(fiber)
        fiber_start = region_start + (region_size - fiber_size) / 2

        self._draw_fiber(level, fiber_start, fiber_start + fiber_size,
                         highlight_subtensor)

        pos = fiber_start

        for c, p in fiber:
            #
            # Gets sets of workers to be colored
            #
            color_coord = highlight_manager.getColorCoord(c)
            color_subtensor = highlight_manager.getColorSubtensor()
            color_coord_or_subtensor = color_coord | color_subtensor

            #
            # Draw the coordinates, lines and maybe values
            #
            self._draw_coord(level, pos, c, color_coord_or_subtensor)

            if len(color_coord - color_subtensor):
                self._draw_intra_line(level, fiber_start + fiber_size / 2,
                                      pos + 0.5, True)

            #
            # Draw the line if the next level will actually draw something.
            #
            if not Payload.contains(p, Fiber) or len(p.coords) > 0:
                self._draw_line(level, pos + 0.5, level + 1, targets.pop(0),
                                len(color_coord_or_subtensor) > 0)
            else:
                #
                # Nothing to connect a line to so pop target
                #
                targets.pop(0)

            if not Payload.contains(p, Fiber):
                #
                # How could this not be the leaf ---
                # "and rest_of_highlighting == []"
                #
                self._draw_value(level + 1, pos, Payload.get(p),
                                 color_coord_or_subtensor)

            pos += 1

        return region_end