Ejemplo n.º 1
0
    def test_insert_interposers_ignore_sinkless(self):
        """Test that interposers are inserted correctly, ignoring those that
        don't connect to anything.
        """
        cm = model.ConnectionMap()

        # Add a connection from a node to a passthrough node to the model
        nodes = [object(), object()]
        ptn = model.PassthroughNode()

        for node in nodes:
            cm.add_connection(
                node, OutputPort.standard, model.SignalParameters(weight=1),
                NodeTransmissionParameters(Transform(1, 1, 1)),
                ptn, InputPort.standard,
                model.ReceptionParameters(None, 1, None)
            )

        # Connect the passthrough node to another passthrough node
        ptn2 = model.PassthroughNode()
        cm.add_connection(ptn, OutputPort.standard, model.SignalParameters(),
                          PassthroughNodeTransmissionParameters(
                            Transform(1, 70, transform=np.ones((70, 1)))),
                          ptn2, InputPort.standard,
                          model.ReceptionParameters(None, 70, None))

        # Connect the passthrough node to another passthrough node
        ptn3 = model.PassthroughNode()
        cm.add_connection(ptn2, OutputPort.standard, model.SignalParameters(),
                          PassthroughNodeTransmissionParameters(
                            Transform(70, 70, 1)),
                          ptn3, InputPort.standard,
                          model.ReceptionParameters(None, 70, None))

        # Insert interposers, getting a list of interposers and a new
        # connection map.
        interposers, new_cm = cm.insert_interposers()
        assert len(interposers) == 0  # No interposers

        # No signals at all
        assert len(list(new_cm.get_signals())) == 0
Ejemplo n.º 2
0
    def test_insert_interposers_simple(self):
        """Test that interposers are inserted correctly."""
        cm = model.ConnectionMap()

        # Add a connection from a node to a passthrough node to the model
        nodes = [object(), object()]
        ptn = model.PassthroughNode()

        for node in nodes:
            cm.add_connection(
                node, OutputPort.standard, model.SignalParameters(weight=1),
                NodeTransmissionParameters(Transform(1, 1, 1)),
                ptn, InputPort.standard,
                model.ReceptionParameters(None, 1, None)
            )

        # Add a connection from the passthrough node to another node
        sink = object()
        sink_port = object()
        cm.add_connection(
            ptn, OutputPort.standard, model.SignalParameters(weight=70),
            PassthroughNodeTransmissionParameters(
                Transform(1, 70, np.ones((70, 1)))
            ),
            sink, sink_port, model.ReceptionParameters(None, 70, None)
        )

        # Insert interposers, getting a list of interposers and a new
        # connection map.
        interposers, new_cm = cm.insert_interposers()
        assert len(interposers) == 1  # Should insert 1 interposer
        interposer = interposers[0]

        # Check that each of the nodes connects to the interposer
        for node in nodes:
            from_node = new_cm._connections[node][OutputPort.standard]
            assert len(from_node) == 1

            for sinks in itervalues(from_node):
                assert len(sinks) == 1
                for s in sinks:
                    assert s.sink_object is interposer
                    assert s.port is InputPort.standard

        # Check that the interposer connects to the sink
        from_interposer = new_cm._connections[interposer][OutputPort.standard]
        assert len(from_interposer) == 1

        for sinks in itervalues(from_interposer):
            assert len(sinks) == 1
            for s in sinks:
                assert s.sink_object is sink
                assert s.port is sink_port
Ejemplo n.º 3
0
    def test_insert_interposers_removes_passthrough_node(self):
        """Test that passthrough nodes are removed while inserting interposers.
        """
        cm = model.ConnectionMap()

        # Add a connection from a node to a passthrough node to the model
        node = object()
        ptn = model.PassthroughNode()
        cm.add_connection(
            node, OutputPort.standard, model.SignalParameters(weight=1),
            NodeTransmissionParameters(Transform(1, 1, 1)),
            ptn, InputPort.standard, model.ReceptionParameters(None, 1, None)
        )

        # Add a connection from the passthrough node to another node
        sink = object()
        cm.add_connection(
            ptn, OutputPort.standard, model.SignalParameters(weight=1),
            PassthroughNodeTransmissionParameters(Transform(1, 1, 1)),
            sink, InputPort.standard, model.ReceptionParameters(None, 1, None)
        )

        # Insert interposers, getting a list of interposers (empty) and a new
        # connection map.
        interposers, new_cm = cm.insert_interposers()
        assert len(interposers) == 0  # No interposers expected

        # Check that there is now just one connection from the node to the sink
        from_node = new_cm._connections[node]
        assert list(from_node) == [OutputPort.standard]

        for (signal_pars, transmission_pars), sinks in \
                iteritems(from_node[OutputPort.standard]):
            # Check the transmission parameters
            assert transmission_pars == NodeTransmissionParameters(
                Transform(1, 1, 1)
            )

            # Check that the sink is correct
            assert len(sinks) == 1
            for s in sinks:
                assert s.sink_object is sink
Ejemplo n.º 4
0
    def test_insert_interposers_earliest_interposer_only(self):
        """Test that only the first interposer in a network of possible
        interposers is inserted.
        """
        cm = model.ConnectionMap()

        node = object()
        ptn1 = model.PassthroughNode()
        ptn2 = model.PassthroughNode()
        sink = object()

        # Add connections
        cm.add_connection(
            node, OutputPort.standard, model.SignalParameters(),
            NodeTransmissionParameters(Transform(16, 512, 1,
                                                 slice_out=slice(16, 32))),
            ptn1, InputPort.standard,
            model.ReceptionParameters(None, 512, None)
        )
        cm.add_connection(
            ptn1, OutputPort.standard, model.SignalParameters(),
            PassthroughNodeTransmissionParameters(
                Transform(512, 512, np.ones((512, 512)))
            ),
            ptn2, InputPort.standard,
            model.ReceptionParameters(None, 512, None)
        )
        cm.add_connection(
            ptn2, OutputPort.standard, model.SignalParameters(),
            PassthroughNodeTransmissionParameters(
                Transform(512, 1024, np.ones((1024, 512)))
            ),
            sink, InputPort.standard,
            model.ReceptionParameters(None, 1024, None)
        )

        # Insert interposers, only one should be included
        interposers, new_cm = cm.insert_interposers()
        assert len(interposers) == 1
        interposer = interposers[0]

        # Check that the node connects to the interposer and that the
        # interposer was the first passthrough node.
        from_node = new_cm._connections[node][OutputPort.standard]
        for (_, transmission_pars), sinks in iteritems(from_node):
            assert transmission_pars == NodeTransmissionParameters(
                Transform(16, 512, 1, slice_out=slice(16, 32))
            )

            assert len(sinks) == 1
            for s in sinks:
                assert s.sink_object is interposer

        # Check that the interposer connects to the sink
        from_interposer = new_cm._connections[interposer][OutputPort.standard]
        for (_, transmission_pars), sinks in iteritems(from_interposer):
            assert transmission_pars.size_in == 512
            assert transmission_pars.size_out == 1024

            assert len(sinks) == 1
            for s in sinks:
                assert s.sink_object is sink
Ejemplo n.º 5
0
    def test_get_coarsened_graph_and_extract_cliques(self):
        """Check that a coarsened representation of the graph can be extracted.
        """
        # Construct a graph of the form:
        #
        #         /--<-------------<--\
        #         |                   |
        # E ->-\  v /->- E ->-\       |
        # E ->--> o -->- E ->--> o ->-/
        # E ->-/    \->- E ->-/
        #
        # Where E are ensembles and `o' is a passthrough node.
        cm = model.ConnectionMap()

        # Network objects
        ens_a = [object() for _ in range(3)]
        ens_b = [object() for _ in range(3)]
        ptns = [model.PassthroughNode(), model.PassthroughNode()]

        # Add connections to the network, excluding the feedback loop.
        for ens in ens_a:
            cm.add_connection(
                ens, OutputPort.standard, model.SignalParameters(),
                object(), ptns[0], InputPort.standard, None
            )

        for ens in ens_b:
            cm.add_connection(
                ptns[0], OutputPort.standard, model.SignalParameters(),
                object(), ens, InputPort.standard, None
            )
            cm.add_connection(
                ens, OutputPort.standard, model.SignalParameters(),
                object(), ptns[1], InputPort.standard, None
            )

        # Get a coarsened representation of the connectivity
        graph = cm.get_coarsened_graph()

        for ens in ens_a:
            assert graph[ens].inputs == set()
            assert graph[ens].outputs == {ptns[0]}

        for ens in ens_b:
            assert graph[ens].inputs == {ptns[0]}
            assert graph[ens].outputs == {ptns[1]}

        assert graph[ptns[0]].inputs == set(ens_a)
        assert graph[ptns[0]].outputs == set(ens_b)

        assert graph[ptns[1]].inputs == set(ens_b)
        assert graph[ptns[1]].outputs == set()

        # Extract cliques from this graph, without the feedback in place there
        # should be two cliques.
        for sources, nodes in cm.get_cliques():
            if nodes == {ptns[0]}:
                assert sources == set(ens_a)
            elif nodes == {ptns[1]}:
                assert sources == set(ens_b)
            else:
                assert False, "Unexpected clique."

        # Add a feedback connection to the graph, this should mean that only a
        # single clique exists.
        cm.add_connection(
            ptns[1], OutputPort.standard, model.SignalParameters(),
            object(), ptns[0], InputPort.standard, None
        )

        # Get a coarsened representation of the connectivity
        graph = cm.get_coarsened_graph()

        for ens in ens_a:
            assert graph[ens].inputs == set()
            assert graph[ens].outputs == {ptns[0]}

        for ens in ens_b:
            assert graph[ens].inputs == {ptns[0]}
            assert graph[ens].outputs == {ptns[1]}

        assert graph[ptns[0]].inputs == set(ens_a) | {ptns[1]}
        assert graph[ptns[0]].outputs == set(ens_b)

        assert graph[ptns[1]].inputs == set(ens_b)
        assert graph[ptns[1]].outputs == {ptns[0]}

        # Extract cliques from this graph, without the feedback in place there
        # should be two cliques.
        for sources, nodes in cm.get_cliques():
            assert nodes == set(ptns)
            assert sources == set(ens_a) | set(ens_b)