Beispiel #1
0
    def test_dimension(self):
        """
        Change the dimension of a layout.
        """
        dim = random.randint(3, 10)

        # Pass in dim as an argument
        layout_pre = mml.Layout(self.S, dim=dim)
        self.assertEqual(layout_pre.dim, dim)
        self.assertIsLayout(self.S, layout_pre)

        # Change the layout to have the dim
        layout_post = mml.Layout(self.S)
        layout_post.dim = dim
        self.assertEqual(layout_post.dim, dim)
        self.assertIsLayout(self.S, layout_post)

        # Change the dimension without changing the object,
        layout = mml.Layout(self.S, dim=2)
        new_layout_array = _dimension_layout(layout.layout_array, dim)
        self.assertEqual(layout.dim, 2)

        # The layout_arrays changed after the fact should match each other
        self.assertArrayEqual(new_layout_array, layout_post.layout_array)

        # Test dimension too small
        layout = mml.Layout(self.S, dim=2)
        self.assertRaises(ValueError, _dimension_layout, layout.layout_array,
                          1)
Beispiel #2
0
    def test_pnorm(self):
        """
        Test the p_norm layout algorithm.
        """
        # Some specs to test with
        low_dim = random.randint(3, 9)
        high_dim = len(self.S_small)
        center = low_dim * (1, )
        scale = random.random() * random.randint(1, 10)

        # Default behavior
        mml.p_norm(self.S_small)

        # Using a starting_layout
        mml.p_norm(self.S_small,
                   starting_layout=nx.random_layout(self.S_small))

        # Passing in G_distances
        mml.p_norm(self.S_small,
                   G_distances=nx.all_pairs_shortest_path_length(self.S_small))

        # Passing in dim
        mml.p_norm(self.S_small, dim=low_dim)
        mml.p_norm(self.S_small, dim=high_dim)

        # Passing in center
        mml.p_norm(self.S_small, center=center)

        # Passing in scale
        mml.p_norm(self.S_small, scale=scale)

        # Different p-norms
        mml.p_norm(self.S_small, p=1)
        mml.p_norm(self.S_small, p=3)
        mml.p_norm(self.S_small, p=float("inf"))

        # Test through the Layout object
        layout = mml.Layout(self.S_small,
                            mml.p_norm,
                            dim=low_dim,
                            center=center,
                            scale=scale)

        self.assertArrayEqual(layout.center, center)
        self.assertAlmostEqual(layout.scale, scale)

        # Ensure that the rectangle packer works
        # TODO fix p_norm's issues with disconnected graphs
        layout = mml.Layout(self.S_components,
                            mml.p_norm,
                            dim=2,
                            center=[12, 20],
                            scale=scale)

        self.assertArrayEqual(layout.center, [12, 20])
        self.assertAlmostEqual(layout.scale, scale)
Beispiel #3
0
    def test_silly_graphs(self):
        """
        Make sure things don't break for trivial graphs.
        """
        # Empty graph
        layout = mml.Layout(self.G)
        self.assertIsLayout(self.G, layout)

        # Single vertex graph
        layout = mml.Layout(self.H)
        self.assertIsLayout(self.H, layout)
Beispiel #4
0
    def test_layout_functions(self):
        """
        Functions can be passed in to Layout objects.
        """
        # Circular
        layout = mml.Layout(self.S, nx.circular_layout)
        self.assertIsLayout(self.S, layout)

        # Random
        layout = mml.Layout(self.S, nx.random_layout)
        self.assertIsLayout(self.S, layout)
Beispiel #5
0
    def test_layout_functions(self):
        """
        Functions can be passed in to Layout objects.
        """
        # Circular
        layout = mml.Layout(self.S, nx.circular_layout)
        self.assertIsLayout(self.S, layout)

        # Random -- nx.random_layout doesn't accept a "scale" parameter, so we
        # need to disable component packing
        layout = mml.Layout(self.S, nx.random_layout, pack_components=False)
        self.assertIsLayout(self.S, layout)
    def test_scale_ratio(self):
        """
        Make sure filling works correctly.
        """
        # Make C_layout bigger than S_layout
        S_layout = mml.Layout(self.S, scale=1)
        C_layout = mml.Layout(self.C, scale=10)

        # Have S_layout scale to various ratios
        placement = mml.Placement(S_layout, C_layout, scale_ratio=1)
        self.assertAlmostEqual(placement.S_layout.scale, 10)

        placement = mml.Placement(S_layout, C_layout, scale_ratio=.5)
        self.assertAlmostEqual(placement.S_layout.scale, 5)
Beispiel #7
0
    def __init__(self, *args, **kwargs):
        super(TestLayoutPlacement, self).__init__(*args, **kwargs)

        # Graphs for testing
        self.S = nx.random_regular_graph(3, 50)
        self.S_small = nx.random_regular_graph(3, 10)
        self.G = nx.Graph()
        self.H = nx.complete_graph(1)
        self.C = dnx.chimera_graph(4)
        self.P = dnx.pegasus_graph(4)

        # Compute some layouts
        self.S_layout = mml.Layout(self.S)
        self.S_small_layout = mml.Layout(self.S_small)
        self.G_layout = mml.Layout(self.G)
        self.C_layout = mml.Layout(self.C)
        self.C_layout_3 = mml.Layout(self.C, dim=3)
Beispiel #8
0
    def test_precomputed_layout(self):
        """
        Pass in a precomputed layout to the Layout class.
        """
        # Pick an arbitrary layout to precompute
        layout = nx.random_layout(self.S)

        # Initialize the layout object
        layout_obj = mml.Layout(self.S, layout)

        self.assertLayoutEqual(self.S, layout, layout_obj)
        self.assertIsLayout(self.S, layout_obj)
Beispiel #9
0
    def test_intersection(self):
        """
        Tests the intersection placement strategy.
        """
        # Default behavior
        S_layout = mml.Layout(self.S)
        placement = mml.intersection(S_layout, self.C_layout)
        self.assertIsPlacement(self.S, self.C, placement)

        # Test different scale ratios
        placement_1 = mml.intersection(S_layout, self.C_layout, scale_ratio=.5)
        placement_2 = mml.intersection(S_layout, self.C_layout, scale_ratio=1)
        placement_3 = mml.intersection(S_layout,
                                       self.C_layout,
                                       scale_ratio=1.5)
        self.assertIsPlacement(self.S, self.C, placement_1)
        self.assertIsPlacement(self.S, self.C, placement_2)
        self.assertIsPlacement(self.S, self.C, placement_3)

        # Test a coordinate variations of chimera
        placement = mml.intersection(S_layout, self.C_coord_layout)
        self.assertIsPlacement(self.S, self.C_coord, placement)

        placement = mml.intersection(S_layout, self.C_blank_layout)
        self.assertIsPlacement(self.S, self.C_blank, placement)

        # Test coordinate variations of pegasus
        placement = mml.intersection(S_layout, self.P_coord_layout)
        self.assertIsPlacement(self.S, self.P_coord, placement)

        placement = mml.intersection(S_layout, self.P_layout)
        self.assertIsPlacement(self.S, self.P, placement)

        placement = mml.intersection(S_layout, self.P_nice_layout)
        self.assertIsPlacement(self.S, self.P_nice, placement)

        placement = mml.intersection(S_layout, self.P_nice_layout)
        self.assertIsPlacement(self.S, self.P_nice, placement)

        placement = mml.intersection(S_layout, self.P_blank_layout)
        self.assertIsPlacement(self.S, self.P_blank, placement)

        # Test coordinate variations of zephyr
        placement = mml.intersection(S_layout, self.Z_coord_layout)
        self.assertIsPlacement(self.S, self.Z_coord, placement)

        placement = mml.intersection(S_layout, self.Z_layout)
        self.assertIsPlacement(self.S, self.Z, placement)

        placement = mml.intersection(S_layout, self.Z_blank_layout)
        self.assertIsPlacement(self.S, self.Z_blank, placement)
Beispiel #10
0
    def test_intersection(self):
        """
        Tests the intersection placement strategy.
        """
        # Default behavior
        S_layout = mml.Layout(self.S)
        placement = mml.intersection(S_layout, self.C_layout)
        self.assertIsPlacement(self.S, self.C, placement)

        # Test different scale ratios
        S_layout = mml.Layout(self.S)
        placement_1 = mml.intersection(S_layout, self.C_layout, scale_ratio=.5)
        S_layout = mml.Layout(self.S)
        placement_2 = mml.intersection(S_layout, self.C_layout, scale_ratio=1)
        S_layout = mml.Layout(self.S)
        placement_3 = mml.intersection(S_layout,
                                       self.C_layout,
                                       scale_ratio=1.5)
        self.assertIsPlacement(self.S, self.C, placement_1)
        self.assertIsPlacement(self.S, self.C, placement_2)
        self.assertIsPlacement(self.S, self.C, placement_3)

        # Test a coordinate version of chimera
        S_layout = mml.Layout(self.S)
        placement = mml.intersection(S_layout, self.C_coord_layout)
        self.assertIsPlacement(self.S, self.C_coord, placement)

        # Test bad inputs
        # Pegasus is not allowed
        self.assertRaises(NotImplementedError, mml.intersection, self.S_layout,
                          self.P_layout)
        # Chimera must have data or coordinates
        self.assertRaises(NotImplementedError, mml.intersection, self.S_layout,
                          self.C_blank_layout)
        # Pegasus coordinates not implemented
        self.assertRaises(NotImplementedError,
                          _lookup_intersection_coordinates, self.P)
Beispiel #11
0
    def test_scale(self):
        """
        Rescale a layout.
        """
        scale = random.random() * random.randint(1, 10)

        # Pass in scale as an argument
        layout_pre = mml.Layout(self.S, scale=scale)
        self.assertAlmostEqual(layout_pre.scale, scale)

        # Change the layout to have the scale
        layout_post = mml.Layout(self.S)
        layout_post.scale = scale
        self.assertAlmostEqual(layout_post.scale, scale)

        # Change the scale without changing the object,
        layout = mml.Layout(self.S, scale=1)
        new_layout_array = _scale_layout(layout.layout_array, scale)
        self.assertAlmostEqual(layout.scale, 1)

        # The layouts should match each other
        self.assertLayoutEqual(self.S, layout_pre, layout_post)
        self.assertIsLayout(self.S, layout_pre)
        self.assertArrayEqual(new_layout_array, layout_pre.layout_array)
Beispiel #12
0
    def test_center(self):
        """
        Recenter a layout.
        """
        center = (random.randint(-10, 10), random.randint(-10, 10))

        # Pass in center as an argument
        layout_pre = mml.Layout(self.S, center=center)
        self.assertArrayEqual(layout_pre.center, center)

        # Change the layout to have the center
        layout_post = mml.Layout(self.S)
        layout_post.center = center
        self.assertArrayEqual(layout_post.center, center)

        # Change the center without changing the object,
        layout = mml.Layout(self.S, center=(0, 0))
        new_layout_array = _center_layout(layout.layout_array, center)
        self.assertArrayEqual(layout.center, (0, 0))

        # The layouts should match each other
        self.assertLayoutEqual(self.S, layout_pre, layout_post)
        self.assertIsLayout(self.S, layout_pre)
        self.assertArrayEqual(new_layout_array, layout_pre.layout_array)
Beispiel #13
0
    def test_dnx(self):
        """
        Test the dnx layout.
        """
        # Some specs to test with
        dim = random.randint(3, 9)
        center = dim * (1, )
        scale = random.random() * random.randint(1, 10)

        # Default behavior
        # Chimera
        mml.dnx_layout(self.C)
        # Pegasus
        mml.dnx_layout(self.P)
        # Zephyr
        mml.dnx_layout(self.Z)

        # Passing in dim
        mml.dnx_layout(self.C, dim=dim)

        # Passing in center
        mml.dnx_layout(self.C, center=center)

        # Passing in scale
        mml.dnx_layout(self.C, scale=scale)

        # Test through the Layout object
        layout = mml.Layout(self.C,
                            mml.dnx_layout,
                            dim=dim,
                            center=center,
                            scale=scale)
        self.assertArrayEqual(layout.center, center)
        self.assertAlmostEqual(layout.scale, scale)

        # Test non-dnx_graph
        self.assertRaises(ValueError, mml.dnx_layout, self.S)

        # Test dim and center mismatch
        self.assertRaises(ValueError,
                          mml.dnx_layout,
                          self.C,
                          dim=3,
                          center=(0, 0))
Beispiel #14
0
    def test_layout_class(self):
        """
        Test the layout mutable mapping behavior.
        """
        L = mml.Layout(nx.Graph())

        # Test __setitem__
        L['a'] = 1

        # Test __iter__ and __getitem__
        for k, v in L.items():
            self.assertEqual(k, 'a')
            self.assertEqual(v, 1)

        # Test __len__
        self.assertEqual(len(L), 1)

        # Test __del__
        del L['a']

        # Test __repr__
        self.assertEqual(repr(L), "{}")
Beispiel #15
0
    def __init__(self, *args, **kwargs):
        super(TestLayoutPlacement, self).__init__(*args, **kwargs)

        # Graphs for testing
        self.S = nx.random_regular_graph(3, 50)
        self.S_small = nx.random_regular_graph(3, 10)
        self.S_components = nx.Graph([(2*i, 2*i+1) for i in range(10)])
        self.G = nx.Graph()
        self.H = nx.complete_graph(1)
        self.C = dnx.chimera_graph(4)
        self.C_coord = dnx.chimera_graph(4, coordinates=True)
        self.C_blank = dnx.chimera_graph(4, data=False)
        self.P = dnx.pegasus_graph(4)
        self.P_coord = dnx.pegasus_graph(4, coordinates=True)
        self.P_nice = dnx.pegasus_graph(4, nice_coordinates=True)
        self.P_blank = dnx.pegasus_graph(4, data=False)
        self.Z = dnx.zephyr_graph(4)
        self.Z_coord = dnx.zephyr_graph(4, coordinates=True)
        self.Z_blank = dnx.pegasus_graph(4, data=False)

        # Compute some layouts
        self.S_layout = mml.Layout(self.S)
        self.S_small_layout = mml.Layout(self.S_small)
        self.S_components_layout = mml.Layout(self.S_small)
        self.G_layout = mml.Layout(self.G)
        self.C_layout = mml.Layout(self.C)
        self.C_coord_layout = mml.Layout(self.C_coord)
        self.C_blank_layout = mml.Layout(self.C_blank)
        self.C_layout_3 = mml.Layout(self.C, dim=3)
        self.P_layout = mml.Layout(self.P)
        self.P_coord_layout = mml.Layout(self.P_coord)
        self.P_nice_layout = mml.Layout(self.P_nice)
        self.P_blank_layout = mml.Layout(self.P_blank)
        self.Z_layout = mml.Layout(self.Z)
        self.Z_coord_layout = mml.Layout(self.Z_coord)
        self.Z_blank_layout = mml.Layout(self.Z_blank)
Beispiel #16
0
 def test_edge_input(self):
     """
     Layouts can be computed with edges instead of graph objects.
     """
     layout = mml.Layout(self.S.edges)
     self.assertIsLayout(self.S, layout)