def test_subnet_mplex_to_mlayer(self):
        mplex = net.MultiplexNetwork([('categorical', 1.0)],
                                     fullyInterconnected=False)

        mplex[0, 1, 0] = 1
        mplex[0, 2, 1] = 1

        mlayer = transforms.subnet(mplex, [0, 1, 2], [0, 1],
                                   newNet=net.MultilayerNetwork(
                                       aspects=1, fullyInterconnected=False))

        self.assertEqual(
            set(mlayer.edges),
            set([(0, 0, 0, 1, 1.0), (0, 1, 0, 0, 1), (0, 2, 1, 1, 1)]))
        self.assertTrue(isinstance(mlayer, net.MultilayerNetwork))

        self.assertRaises(
            TypeError, lambda: transforms.subnet(mlayer, [0, 1, 2], [0, 1],
                                                 newNet=net.MultiplexNetwork([(
                                                     'categorical', 1.0)])))

        mplex = net.MultiplexNetwork([('categorical', 1.0)],
                                     fullyInterconnected=True)

        mplex[0, 1, 0] = 1
        mplex[0, 2, 1] = 1

        mlayer = transforms.subnet(mplex, [0, 1, 2], [0, 1],
                                   newNet=net.MultilayerNetwork(aspects=1))

        self.assertEqual(
            set(mlayer.edges),
            set([(0, 0, 0, 1, 1.0), (0, 1, 0, 0, 1), (0, 2, 1, 1, 1),
                 (1, 1, 0, 1, 1.0), (2, 2, 0, 1, 1.0)]))
        self.assertTrue(isinstance(mlayer, net.MultilayerNetwork))
    def test_subnet_mlayer_example(self):
        #monoplex
        copynet = transforms.subnet(self.mlayer_example_monoplex, [1, 2, 3, 4])
        self.assertEqual(copynet, self.mlayer_example_monoplex)
        copynet = transforms.subnet(self.mlayer_example_monoplex, None)
        self.assertEqual(copynet, self.mlayer_example_monoplex)
        import copy
        copynet2 = copy.deepcopy(self.mlayer_example_monoplex)
        self.assertEqual(copynet2, self.mlayer_example_monoplex)

        n = net.MultilayerNetwork(aspects=0, fullyInterconnected=True)
        n[2, 3] = 1
        n[3, 4] = 2
        self.assertEqual(
            n, transforms.subnet(self.mlayer_example_monoplex, [2, 3, 4]))

        #1-aspect
        copynet = transforms.subnet(self.mlayer_example_1d, [1, 2, 3, 4],
                                    ['A', 'B'])
        self.assertEqual(copynet, self.mlayer_example_1d)
        copynet = transforms.subnet(self.mlayer_example_1d, None, ['A', 'B'])
        self.assertEqual(copynet, self.mlayer_example_1d)
        copynet = transforms.subnet(self.mlayer_example_1d, None, None)
        self.assertEqual(copynet, self.mlayer_example_1d)
        copynet = transforms.subnet(self.mlayer_example_1d, [1, 2, 3, 4], None)
        self.assertEqual(copynet, self.mlayer_example_1d)
        import copy
        copynet2 = copy.deepcopy(self.mlayer_example_1d)
        self.assertEqual(copynet2, self.mlayer_example_1d)

        n = net.MultilayerNetwork(aspects=1, fullyInterconnected=False)
        n[2, 3, 'A', 'A'] = 1
        self.assertEqual(
            n, transforms.subnet(self.mlayer_example_1d, [2, 3, 4], ['A']))

        #2-aspect
        copynet = transforms.subnet(self.mlayer_example_2d, [1, 2, 3, 4],
                                    ['A', 'B'], ['X', 'Y'])
        self.assertEqual(copynet, self.mlayer_example_2d)
        import copy
        copynet2 = copy.deepcopy(self.mlayer_example_2d)
        self.assertEqual(copynet2, self.mlayer_example_2d)

        n = net.MultilayerNetwork(aspects=2, fullyInterconnected=False)
        n[3, 4, 'B', 'B', 'X', 'X'] = 1
        n.add_node(2, layer=('A', 'X'))
        n.add_node(3, layer=('A', 'X'))
        self.assertEqual(
            n,
            transforms.subnet(self.mlayer_example_2d, [2, 3, 4], None,
                              ['X', 'dummy']))
    def test_subnet_different_interconnectivities(self):
        fully_interc = net.MultilayerNetwork(aspects=1,
                                             fullyInterconnected=True)
        fully_interc[1, 'X'][1, 'Y'] = 1
        fully_interc[1, 'X'][2, 'X'] = 1
        other_fully_interc = transforms.subnet(
            fully_interc,
            fully_interc.get_layers(aspect=0),
            fully_interc.get_layers(aspect=1),
            newNet=net.MultilayerNetwork(aspects=1, fullyInterconnected=True))
        self.assertEqual(set(other_fully_interc.iter_node_layers()),
                         set([(1, 'X'), (1, 'Y'), (2, 'X'), (2, 'Y')]))

        fully_interc = net.MultilayerNetwork(aspects=1,
                                             fullyInterconnected=True)
        fully_interc[1, 'X'][1, 'Y'] = 1
        fully_interc[1, 'X'][2, 'X'] = 1
        non_fully_interc = transforms.subnet(fully_interc,
                                             fully_interc.get_layers(aspect=0),
                                             fully_interc.get_layers(aspect=1),
                                             newNet=net.MultilayerNetwork(
                                                 aspects=1,
                                                 fullyInterconnected=False))
        self.assertEqual(set(non_fully_interc.iter_node_layers()),
                         set([(1, 'X'), (1, 'Y'), (2, 'X'), (2, 'Y')]))

        non_fully_interc = net.MultilayerNetwork(aspects=1,
                                                 fullyInterconnected=False)
        non_fully_interc[1, 'X'][1, 'Y'] = 1
        non_fully_interc[1, 'X'][2, 'X'] = 1
        non_fully_interc.add_node(2, layer='Y')
        other_non_fully_interc = transforms.subnet(
            non_fully_interc,
            non_fully_interc.get_layers(aspect=0),
            non_fully_interc.get_layers(aspect=1),
            newNet=net.MultilayerNetwork(aspects=1, fullyInterconnected=False))
        self.assertEqual(set(other_non_fully_interc.iter_node_layers()),
                         set([(1, 'X'), (1, 'Y'), (2, 'X'), (2, 'Y')]))

        non_fully_interc = net.MultilayerNetwork(aspects=1,
                                                 fullyInterconnected=False)
        non_fully_interc[1, 'X'][1, 'Y'] = 1
        non_fully_interc[1, 'X'][2, 'X'] = 1
        self.assertRaises(
            TypeError, lambda: transforms.subnet(
                non_fully_interc,
                non_fully_interc.get_layers(aspect=0),
                non_fully_interc.get_layers(aspect=1),
                newNet=net.MultilayerNetwork(aspects=1,
                                             fullyInterconnected=True)))
 def test_subnet_mplex_simple(self):
     copynet = transforms.subnet(self.mplex_simple, [1, 2, 3, 4], [1, 2, 3])
     self.assertEqual(copynet, self.mplex_simple)
     import copy
     copynet2 = copy.deepcopy(self.mplex_simple)
     self.assertEqual(copynet2, self.mplex_simple)