Ejemplo n.º 1
0
    def test_new(self):
        av = 1
        bv = 1

        a = Payload(av)
        self.assertEqual(a.value, av)

        b = Payload(bv)
        self.assertEqual(b.value, bv)
Ejemplo n.º 2
0
    def test_minus(self):
        av = 1
        bv = 1

        a = Payload(av)
        b = Payload(bv)

        a_sub_1 = a - 1
        self.assertEqual(a_sub_1.value, av - 1)

        a_sub_b = a - b
        self.assertEqual(a_sub_b.value, av - bv)
Ejemplo n.º 3
0
    def _replayChanges(self):
        """replayChanges """

        if len(self.log) == 0:
            return

        points = self.log[0].points
        values = self.log[0].values

        for shadow, point_list, value_list in zip(self.shadow_tensors, points,
                                                  values):

            if shadow.isMutable():
                for point, value in zip(point_list, value_list):

                    if Payload.isEmpty(value):
                        continue

                    ref = shadow.getPayloadRef(*point)
                    ref <<= value

        del self.log[0]

        #
        # Increment cycle
        #
        if not self.using_spacetimestamp:
            self.cycle += 1
Ejemplo n.º 4
0
    def __init__(self, object, highlights={}, extent=(30, 200)):
        """__init__"""

        #
        # Set up logging
        #
        self.logger = logging.getLogger('fibertree.graphics.tree_image')

        #
        # Record parameters
        #
        # Note: We conditionally unwrap Payload objects
        #
        self.object = Payload.get(object)
        self.row_extent = extent[0]
        self.col_extent = extent[1]

        level = self.object.getDepth() - 1
        self.highlight_manager = HighlightManager(highlights, level=level)

        #
        # Cache worker colors
        #
        worker_color = {}

        for n, worker in enumerate(highlights.keys()):
            worker_color[worker] = ImageUtils.getColor(worker)

        self.worker_color = worker_color

        #
        # Create the tree image
        #
        self._create_tree()
Ejemplo n.º 5
0
    def test_union_2x_1d2d(self):
        """Test union 2-way for 1d/2d fibers"""

        ans = Fiber([0, 2, 4, 6],
                    [('AB', 1, Fiber([0, 2, 3], [2, 4, 5])),
                     ('AB', 3, Fiber([0, 1, 2], [3, 4, 6])),
                     ('AB', 5, Fiber([0, 1, 2, 3], [1, 2, 3, 4])),
                     ('A', 7, Fiber([], []))])

        a_m = self.input["a1_m"]
        b_m = self.input["b2_m"]

        z_m1 = a_m | b_m
        z_m2 = Fiber.union(a_m, b_m)

        for test, z_m in enumerate([z_m1, z_m2]):
            with self.subTest(test=test):
                # Check for right answer
                self.assertEqual(z_m, ans)

                # Check that payloads are of correct type
                self.assertIsInstance(z_m[0].payload.value[1], Payload)
                self.assertIsInstance(z_m[0].payload.value[2], Fiber)
                self.assertIsInstance(z_m[2].payload.value[1], Payload)
                self.assertIsInstance(z_m[3].payload.value[2], Fiber)

                # Check that default was set properly
                z_m_default = z_m.getDefault()
                self.assertEqual(z_m_default, Payload(('', 0, Fiber)))
                self.assertIsInstance(z_m_default, Payload)

                # Check final shape is correct (note it is 1-D)
                z_m_shape = z_m.getShape()
                self.assertEqual(z_m_shape, [7])
Ejemplo n.º 6
0
    def test_plus(self):
        av = 1
        bv = 1

        a = Payload(av)
        b = Payload(bv)

        a_plus_1 = a + 1
        self.assertEqual(a_plus_1.value, av + 1)

        a_plus_b = a + b
        self.assertEqual(a_plus_b.value, av + bv)

        a += 1
        self.assertEqual(a.value, av + 1)

        a += b
        self.assertEqual(a.value, av + 1 + bv)
Ejemplo n.º 7
0
    def __init__(self, object, *args, highlights={}, style='tree', **kwargs):
        """__init__"""

        #
        # Set up logging
        #
        self.logger = logging.getLogger('fibertree.graphics.tensor_image')


        highlights = HighlightManager.canonicalizeHighlights(highlights)

        #
        # Conditionally unwrap Payload objects
        #
        object = Payload.get(object)

        #
        # Create the subimages
        #
        if "tree" in style:
            im1 = TreeImage(object, *args, highlights=highlights, **kwargs).im

        if "uncompressed" in style:
            im2 = UncompressedImage(object, *args, highlights=highlights, **kwargs).im

        #
        # Create the final image 
        #
        # TBD: Allow style to be a list
        #
        if style == "tree":
            self.im = im1
        elif style == "uncompressed":
            self.im = im2
        elif style == "tree+uncompressed":
            color="wheat"
            im = Image.new('RGB', (max(im1.width, im2.width), im1.height + im2.height), color)

            diff = im1.width - im2.width

            if diff > 0:
                # im1 is bigger
                im1_xoffset = 0
                im2_xoffset = diff//2
            else:
                # im2 is bigger
                im1_xoffset = -diff//2
                im2_xoffset = 0

            im.paste(im1, (im1_xoffset, 0))
            im.paste(im2, (im2_xoffset, im1.height))

            self.im = im
        else:
            print(f"TensorImage: Unsupported image style - {style}")
Ejemplo n.º 8
0
    def test_multiply(self):
        av = 1
        bv = 2

        a = Payload(av)
        b = Payload(bv)

        a_mul_2 = a * 2
        self.assertEqual(a_mul_2.value, av * 2)

        two_mul_a = 2 * a
        self.assertEqual(two_mul_a.value, 2 * av)

        a_mul_b = a * b
        self.assertEqual(a_mul_b.value, av * bv)

        a *= 2
        self.assertEqual(a.value, av * 2)

        a *= b
        self.assertEqual(a.value, av * 2 * b)
Ejemplo n.º 9
0
    def __init__(self, *tensors):
        """__init__"""

        #
        # Set up logging
        #
        self.logger = logging.getLogger('fibertree.graphics.spacetime_canvas')

        #
        # Structures to hold infomation about each tracked tensor
        #
        self.tensors = []
        self.spacetime = []
        self.highlights = []

        for tensor in tensors:
            #
            # Append each tensor being tracked, conditionally
            # unwraping it if it is a Payload object
            #
            self.tensors.append(Payload.get(tensor))

            #
            # Create a "spacetime" tensor to hold the spacetime
            # information for this tracked tensor
            #
            if isinstance(tensor, Tensor):
                assert tensor.getShape() != [], "No support for 0-D tensors"

                spacetime = Tensor(rank_ids=["T"] + tensor.getRankIds())
                spacetime.setName(tensor.getName())
                spacetime.setColor(tensor.getColor())
            else:
                assert tensor.getDepth() == 1, "Only 1-D fibers are supported"

                spacetime = Tensor(rank_ids=["T", "S"])

            #
            # Append the "spacetime" tensor to hold this tracked
            # tensor's spacetime information
            #
            self.spacetime.append(spacetime)
            #
            # Append an empty highlight object to hold the highlighting
            # information for this tracked tensor
            #
            self.highlights.append({})

        self.frame_num = 0
Ejemplo n.º 10
0
    def test_equality(self):
        cv = 8
        dv = 8
        ev = 1

        c = Payload(cv)
        d = Payload(dv)
        e = Payload(ev)

        self.assertTrue(c == d)
        self.assertFalse(c == e)

        self.assertFalse(c != d)
        self.assertTrue(c != e)

        self.assertTrue(c == 8)
        self.assertFalse(c == 1)
        self.assertFalse(c != 8)
        self.assertTrue(c != 1)

        self.assertTrue(8 == c)
        self.assertFalse(1 == c)
        self.assertFalse(8 != c)
        self.assertTrue(1 != c)
Ejemplo n.º 11
0
    def test_multiply(self):

        cp1 = CoordPayload(5, 4)
        cp2 = CoordPayload(6, 5)

        payload_ref = Payload(20)

        self.assertEqual(cp1 * 5, payload_ref)
        self.assertEqual(cp1 * cp2, payload_ref)

        self.assertEqual(5 * cp1, payload_ref)

        cp1 *= cp2
        self.assertEqual(cp1, payload_ref)

        cp2 *= 4
        self.assertEqual(cp2, payload_ref)
Ejemplo n.º 12
0
    def test_sub(self):

        cp1 = CoordPayload(5, 12)
        cp2 = CoordPayload(6, 10)

        payload_ref = Payload(2)

        self.assertEqual(cp1 - 10, payload_ref)
        self.assertEqual(cp1 - cp2, payload_ref)

        self.assertEqual(14 - cp1, payload_ref)

        cp1 -= cp2
        self.assertEqual(cp1, payload_ref)

        cp2 -= 8
        self.assertEqual(cp2, payload_ref)
Ejemplo n.º 13
0
    def test_add(self):

        cp1 = CoordPayload(5, 11)
        cp2 = CoordPayload(6, 12)

        payload_ref = Payload(23)

        self.assertEqual(cp1 + 12, payload_ref)
        self.assertEqual(cp1 + cp2, payload_ref)

        self.assertEqual(12 + cp1, payload_ref)

        cp1 += cp2
        self.assertEqual(cp1, payload_ref)

        cp2 += 11
        self.assertEqual(cp2, payload_ref)
    def test_mul_payload(self):
        """Test __mul__ payload"""

        f_in = Fiber.fromUncompressed([1, 2, 3, 0, 0, 6])
        f_ref = Fiber([0, 1, 2, 5], [2, 4, 6, 12])
        two = Payload(2)

        with self.subTest("f_in * 2"):
            f_out = f_in * two
            self.assertEqual(f_ref, f_out)

        with self.subTest("2*f_in"):
            f_out = two * f_in
            self.assertEqual(f_ref, f_out)

        with self.subTest("f_in *=2"):
            # f_in gets clobbered!
            f_in *= two
            self.assertEqual(f_ref, f_in)
    def test_add_payload(self):
        """Test __add__ payload"""

        f_in = Fiber.fromUncompressed([1, 2, 3, 0, 0, 6])
        f_ref = Fiber.fromUncompressed([3, 4, 5, 2, 2, 8])
        two = Payload(2)

        with self.subTest("f_in + 2"):
            f_out = f_in + two
            self.assertEqual(f_ref, f_out)

        with self.subTest("2 + f_in"):
            f_out = two + f_in
            self.assertEqual(f_ref, f_out)

        with self.subTest("f_in += 2"):
            # f_in gets clobbered!
            f_in += two
            self.assertEqual(f_ref, f_in)
    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
Ejemplo n.º 17
0
    def __init__(self, *tensors, style='tree', progress=True):
        """__init__"""

        #
        # Set up logging
        #
        self.logger = logging.getLogger('fibertree.graphics.movie_canvas')

        #
        # Set image type
        #
        self.style = style

        #
        # Set tqdm control
        #
        self.use_tqdm = progress

        #
        # Set up tensor class variables
        #
        # Note: We conditionally unwrap Payload objects
        #
        self.tensors = []
        self.image_list_per_tensor = []
        for tensor in tensors:
            self.tensors.append(Payload.get(tensor))
            self.image_list_per_tensor.append([])

        #
        # Font to use for text
        #
        self.font = ImageFont.truetype('Pillow/Tests/fonts/DejaVuSans.ttf', 16)

        #
        # Add an initial frame with nothing highlighted (it looks good)
        #
        self.addFrame()
Ejemplo n.º 18
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))
    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))
Ejemplo n.º 20
0
    print(f"b = CoordPayload(6, 2) -> {b}")

    print("")

    z = a + b
    print(f"a+b -> {z}")

    z = a + 1
    print(f"a+1 -> {z}")

    z = 1 + a
    print(f"1+a -> {z}")

    print("")

    p = Payload(4)
    print(f"p = Payload(4) -> {p}")

    z = a + p
    print(f"a+p -> {z}")

    z = p + a
    print(f"p+a -> {z}")

    print("")

    a += b
    print(f"a+=b -> {a}")
    a = CoordPayload(5, 4)

    a += 2
Ejemplo n.º 21
0
    def test_union_2x_1d(self):
        """Test union 2-way for 1d fibers"""

        ans = Fiber([0, 2, 3, 4, 6], [('AB', Payload(1), Payload(2)),
                                      ('AB', Payload(3), Payload(4)),
                                      ('B', Payload(0), Payload(5)),
                                      ('A', Payload(5), Payload(0)),
                                      ('A', Payload(7), Payload(0))])

        a_m = self.input["a1_m"]
        b_m = self.input["b1_m"]

        z_m1 = a_m | b_m
        z_m2 = Fiber.union(a_m, b_m)

        for test, z_m in enumerate([z_m1, z_m2]):
            with self.subTest(test=test):
                # Check for right answer
                self.assertEqual(z_m, ans)

                # Check that payloads are of correct type
                self.assertIsInstance(z_m[0].payload.value[1], Payload)
                self.assertIsInstance(z_m[2].payload.value[1], Payload)
                self.assertIsInstance(z_m[3].payload.value[2], Payload)

                # Check that default was set properly
                z_m_default = z_m.getDefault()
                self.assertEqual(z_m_default, Payload(('', 0, 0)))
                self.assertIsInstance(z_m_default, Payload)

                # Check final shape is correct
                z_m_shape = z_m.getShape()
                self.assertEqual(z_m_shape, [7])
Ejemplo n.º 22
0
        #        print(f"\na_d:\n{a_d}")
        #        print(f"\nd_d:\n{d_d})")

        #        a_less_d = a_d - d_d
        #        print(f"\na_less_d:\n{a_less_d})")

        #        assignment1 = d_d << a_less_d
        #        print(f"\nd_d << (a_d - d_d):\n{assignment1}")

        #        assignment2 = f1_d << assignment1
        #        print(f"\nf1_d << (d_d << (a_d - d_d)):\n{assignment2}")

        for d, (f1_ref, (d_ref, _)) in f1_d << (d_d << a_d):
            print(f"  Processing destination {d} = {d_ref}")

            if Payload.isEmpty(d_ref):
                print(f"Adding destination {d}")

                f1_ref += 1
                d_ref += level

    level += 1
    f0 = f1
    f0_d = f0.getRoot()

d_d.print("\nDistance Tensor")

print("")
print("--------------------------------------")
print("")
Ejemplo n.º 23
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