def test_equality(self):
        user_strat = placer()
        default_strat = placer()
        pylogging.set_loglevel(pylogging.get("marocco"),
                               pylogging.LogLevel.TRACE)

        self.assertEqual(user_strat, default_strat)

        user_strat.m_spiral_center = user_strat.spiral_neighbours
        self.assertEqual(user_strat, default_strat)
 def test_defaults(self):
     user_strat = placer()
     self.assertEqual(user_strat.m_neuron_block_on_hicann_ordering,
                      user_strat.merger_tree_friendly)
     self.assertEqual(
         user_strat.m_neuron_block_on_wafer_ordering,
         user_strat.hicann_on_wafer_then_neuron_block_on_hicann)
     self.assertEqual(user_strat.m_hicann_on_wafer_ordering,
                      user_strat.spiral)
     self.assertEqual(user_strat.m_spiral_center,
                      user_strat.spiral_neighbours)
     self.assertEqual(user_strat.SortPriorityTargets, 1)
     self.assertEqual(user_strat.SortPrioritySources, 1)
     self.assertEqual(user_strat.m_population_placement_priority,
                      user_strat.target_and_source)
    def test_vertical(self):
        pylogging.set_loglevel(pylogging.get("marocco"),
                               pylogging.LogLevel.TRACE)

        marocco = self.marocco

        user_strat = placer()
        user_strat.m_hicann_on_wafer_ordering = user_strat.vertical
        user_strat.m_spiral_center = user_strat.spiral_neighbours
        marocco.neuron_placement.default_placement_strategy(user_strat)

        pynn.setup(marocco=marocco)

        pops = {}
        pops[0] = pynn.Population(128, pynn.IF_cond_exp, {})
        pops[1] = pynn.Population(128, pynn.IF_cond_exp, {})
        pops[2] = pynn.Population(128, pynn.IF_cond_exp, {})

        proj1 = pynn.Projection(pops[0], pops[1],
                                pynn.OneToOneConnector(weights=0.01))
        proj2 = pynn.Projection(pops[1], pops[2],
                                pynn.OneToOneConnector(weights=0.01))

        h = {}
        h[pops[0]] = C.HICANNOnWafer(Enum(100))
        # the next free hicann (vertical order)
        h[pops[1]] = C.HICANNOnWafer(Enum(72))
        h[pops[2]] = C.HICANNOnWafer(Enum(48))
        marocco.manual_placement.on_hicann(pops[0], h[pops[0]])

        pynn.run(0)
        pynn.end()

        result = self.load_results()
        for key in pops:
            pop = pops[key]
            for nrn in pop:
                placement_item, = result.placement.find(nrn)
                logical_neuron = placement_item.logical_neuron()
                for denmem in logical_neuron:
                    self.assertEqual(h[pop].toEnum(),
                                     denmem.toHICANNOnWafer().toEnum())
    def test_python_interface_nb_enum_increase(self):
        """tests to set switches of the class"""

        marocco = self.marocco
        user_strat = placer()
        user_strat.m_neuron_block_on_hicann_ordering = placer.neuron_block_on_hicann_enum_increasing
        marocco.neuron_placement.default_placement_strategy(user_strat)

        pynn.setup(marocco=marocco)

        self.run_placement()

        result = self.load_results()
        nb = C.NeuronBlockOnHICANN(Enum(0))
        for pop in self.pops:
            for nrn in pop:
                placement_item, = result.placement.find(nrn)
                logical_neuron = placement_item.logical_neuron()
                for denmem in logical_neuron:
                    self.assertEqual(nb.toEnum(),
                                     denmem.toNeuronBlockOnHICANN().toEnum())
    def test_cluster_target(self):
        marocco = self.marocco

        user_strat = placer()
        user_strat.m_spiral_center = user_strat.spiral_neighbours_target
        marocco.neuron_placement.default_placement_strategy(user_strat)

        pynn.setup(marocco=marocco)

        pops = {}
        pops[0] = pynn.Population(1, pynn.IF_cond_exp, {})
        pops[1] = pynn.Population(1, pynn.IF_cond_exp, {})
        pops[2] = pynn.Population(1, pynn.IF_cond_exp, {})

        proj1 = pynn.Projection(pops[0], pops[1],
                                pynn.AllToAllConnector(weights=0.01))
        proj2 = pynn.Projection(pops[1], pops[2],
                                pynn.AllToAllConnector(weights=0.01))

        h = {}
        h[pops[0]] = C.HICANNOnWafer(Enum(100))
        # average positon of target
        h[pops[1]] = C.HICANNOnWafer(Enum(102))
        h[pops[2]] = C.HICANNOnWafer(Enum(102))
        marocco.manual_placement.on_hicann(pops[0], h[pops[0]])
        marocco.manual_placement.on_hicann(pops[2], h[pops[2]])

        pynn.run(0)
        pynn.end()

        result = self.load_results()
        for key in pops:
            pop = pops[key]
            for nrn in pop:
                placement_item, = result.placement.find(nrn)
                logical_neuron = placement_item.logical_neuron()
                for denmem in logical_neuron:
                    self.assertEqual(h[pop].toEnum(),
                                     denmem.toHICANNOnWafer().toEnum())
    def test_python_interface_merger_friendly(self):
        """tests to set switches of the class"""

        marocco = self.marocco
        user_strat = placer()
        user_strat.m_neuron_block_on_hicann_ordering = placer.merger_tree_friendly
        marocco.neuron_placement.default_placement_strategy(user_strat)

        pynn.setup(marocco=marocco)

        self.run_placement()

        result = self.load_results()

        # the first NB for merger friendly placemnt.
        nb = C.NeuronBlockOnHICANN(Enum(3))

        for pop in self.pops:
            for nrn in pop:
                placement_item, = result.placement.find(nrn)
                logical_neuron = placement_item.logical_neuron()
                for denmem in logical_neuron:
                    self.assertEqual(nb, denmem.toNeuronBlockOnHICANN())
 def test_equality(self):
     user_strat = placer()
     default_strat = placer()
     self.assertTrue(user_strat == default_strat)
 def test_unequality(self, derived_strat):
     base_strat = placer()
     self.assertFalse(base_strat == derived_strat)
class PlacementAlgorithms(utils.TestWithResults):
    """
    Tests python instantiation of placement algorithms.
    """
    pops = []

    def network(self):
        pylogging.set_loglevel(pylogging.get("marocco"),
                               pylogging.LogLevel.TRACE)
        pylogging.set_loglevel(pylogging.get("Calibtic"),
                               pylogging.LogLevel.ERROR)

        self.pops = []
        for i in range(10):
            pop = pynn.Population(1, pynn.IF_cond_exp, {})
            self.pops.append(pop)
            if (i > 1):
                proj = pynn.Projection(self.pops[i - 1], self.pops[i],
                                       pynn.AllToAllConnector(weights=0.01))
                proj  # prevent pep8 warning
        pynn.run(0)
        pynn.end()

    def test_get_strategy(self):
        """tests acquisiton of a default placement strategy"""
        p = self.marocco.neuron_placement
        p_strategy = p.default_placement_strategy()
        self.assertTrue(p_strategy is not None)

    @utils.parametrize([
        placer(),
        placer_linear(),
        placer_linear_asc(),
        placer_smallNB(),
        placer_pop_cluster(),
        placer_neuron_cluster(),
    ])
    def test_creation(self, strategy):
        self.marocco.neuron_placement.default_placement_strategy(strategy)

        pynn.setup(marocco=self.marocco)

        self.network()

        self.assertTrue(1 == 1)

    @utils.parametrize([
        placer_linear(),
        placer_linear_asc(),
        placer_smallNB(),
        placer_pop_cluster(),
        placer_neuron_cluster(),
    ])
    def test_unequality(self, derived_strat):
        base_strat = placer()
        self.assertFalse(base_strat == derived_strat)

    def test_equality(self):
        user_strat = placer()
        default_strat = placer()
        self.assertTrue(user_strat == default_strat)

    def test_hook_modularity_nb(self):
        """tests to override some hooks of the Placement Base class"""
        class myPlacer(placer):
            def initialise(self):
                print("initialise in python,\
                      remove all but NeuronBlock 4 on HICANN 42")

                # use one of the two ways to access the value
                a = self.m_neuron_blocks.access
                b = self.m_neuron_blocks.value()
                assert (a == b)

                b = sorted(
                    b,
                    key=lambda nb: int(nb.toHICANNOnWafer().toEnum().value()))

                c = []
                for nb in b:
                    if nb.toHICANNOnWafer().toEnum().value() == 42:
                        if (nb.toNeuronBlockOnHICANN().toEnum().value() == 4):
                            c.append(nb)

                # smaller values are in the front.
                # the c++ code pops from the back.
                c = sorted(c,
                           key=lambda nb: int(nb.toNeuronBlockOnHICANN().
                                              toEnum().value()),
                           reverse=False)

                for nb in c:
                    print(("will use : {}".format(nb)))

                # There are two options to set it again:
                # use access to set a full vector
                self.m_neuron_blocks.access = c

                # or use the value() to access single elements
                for i in range(len(self.m_neuron_blocks.value())):
                    del (self.m_neuron_blocks.value()[0])
                for nb in c:
                    self.m_neuron_blocks.value().append(nb)

            def loop(self):
                print("loop of the custom placer has been called")
                # OPTIONAL do some stuff, shuffle pops, etc.

            def finalise(self):
                print("finalise of the custom placer has been called")
                # OPTIONAL do some stuff, put pops back to original place etc.

        marocco = self.marocco
        user_strat = myPlacer()

        marocco.neuron_placement.default_placement_strategy(user_strat)

        pynn.setup(marocco=marocco)

        self.network()

        result = self.load_results()
        hicann = C.HICANNOnWafer(Enum(42))
        nb = C.NeuronBlockOnHICANN(Enum(4))
        for pop in self.pops:
            for nrn in pop:
                placement_item, = result.placement.find(nrn)
                logical_neuron = placement_item.logical_neuron()
                for denmem in logical_neuron:
                    self.assertEqual(hicann, denmem.toHICANNOnWafer())
                    self.assertEqual(nb, denmem.toNeuronBlockOnHICANN())

    def test_loop_modularity_nb(self):
        """tests to override the loop hook with NB handling"""
        class myPlacer(placer):
            def initialise(self):
                b = sorted(
                    self.m_neuron_blocks.access,
                    key=lambda nb: int(nb.toHICANNOnWafer().toEnum().value()))
                self.m_neuron_blocks.access = b

            def loop(self):
                print("removing the last NB")

                b = self.m_neuron_blocks.access  # use this or the following
                b = self.m_neuron_blocks.value()

                b = sorted(
                    b,
                    key=lambda nb: int(nb.toHICANNOnWafer().toEnum().value()))

                c = []
                for i in range(len(b) - 1):
                    c.append(b[i])

                # use access to set full vector
                self.m_neuron_blocks.access = c

                # or use the value() to access single elements
                for i in range(len(self.m_neuron_blocks.value())):
                    del (self.m_neuron_blocks.value()[0])
                for nb in c:
                    self.m_neuron_blocks.value().append(nb)

        marocco = self.marocco
        user_strat = myPlacer()

        marocco.neuron_placement.default_placement_strategy(user_strat)

        pynn.setup(marocco=marocco)

        self.network()

        result = self.load_results()
        hicann = C.HICANNOnWafer(Enum(42))
        nb = C.NeuronBlockOnHICANN(Enum(4))
        for pop in self.pops:
            for nrn in pop:
                placement_item, = result.placement.find(nrn)
                logical_neuron = placement_item.logical_neuron()
                for denmem in logical_neuron:
                    # all pops must be on different NBs
                    self.assertFalse(nb == denmem.toNeuronBlockOnHICANN()
                                     and hicann == denmem.toHICANNOnWafer())
            nb = denmem.toNeuronBlockOnHICANN()
            hicann = denmem.toHICANNOnWafer()

    @unittest.skip("disabled until m_queue can be used in python missing \
                   class registration to python for C++: \
                   `std::vector<NeuronPlacementRequest>` ")
    def test_access_queue(self):
        """use queue handling"""
        class myPlacer(placer):
            def loop(self):
                print("reversing populations")

                # b = self.m_queue.access;  # use this or the following
                print((dir(self.m_queue.value()[0])))
                b = self.m_queue.value()
                print((dir(b)))

        marocco = self.marocco
        user_strat = myPlacer()

        marocco.neuron_placement.default_placement_strategy(user_strat)

        pynn.setup(marocco=marocco)

        self.network()

        result = self.load_results()
        hicann = C.HICANNOnWafer(Enum(42))
        nb = C.NeuronBlockOnHICANN(Enum(4))
        for pop in self.pops:
            for nrn in pop:
                placement_item, = result.placement.find(nrn)
                logical_neuron = placement_item.logical_neuron()
                for denmem in logical_neuron:
                    # all pops shall be on different NBs
                    self.assertFalse(nb == denmem.toNeuronBlockOnHICANN()
                                     and hicann == denmem.toHICANNOnWafer())
            nb = denmem.toNeuronBlockOnHICANN()
            hicann = denmem.toHICANNOnWafer()