コード例 #1
0
class TestEntities(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        import nest

        nest.ResetKernel()
        super(TestEntities, self).setUpClass()
        config = JSONConfig(file=minimal_config_entities)
        self.scaffold = Scaffold(config)
        self.scaffold.compile_network()
        hdf_config = _from_hdf5("minimal_entities.hdf5")
        self.scaffold_fresh = Scaffold(hdf_config,
                                       from_file="minimal_entities.hdf5")

    def test_placement_statistics(self):
        self.assertEqual(self.scaffold.statistics.cells_placed["entity_type"],
                         100)

    @unittest.skipIf(
        importlib.util.find_spec("nest") is None, "NEST is not importable.")
    def test_creation_in_nest(self):

        f = h5py.File("minimal_entities.hdf5", "r")
        ids = list(f["entities"]["entity_type"])
        self.assertEqual(ids, list(range(100)))
        f.close()

        # Try to load the network directly from the hdf5 file
        nest_adapter = self.scaffold_fresh.get_simulation("test")
        simulator = nest_adapter.prepare()
        nest_adapter.simulate(simulator)
        nest_ids = nest_adapter.entities["entity_type"].nest_identifiers
        self.assertEqual(list(nest_ids), list(range(1, 101)))
コード例 #2
0
ファイル: test_nest.py プロジェクト: francesshei/bsb
class TestMultiInstance(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        super(TestMultiInstance, self).setUpClass()
        import nest

        self.nest = nest
        config = JSONConfig(file=single_neuron_config)
        self.scaffold = Scaffold(config)
        self.scaffold.compile_network()
        self.hdf5 = self.scaffold.output_formatter.file
        self.nest_adapter_0 = self.scaffold.get_simulation(
            "test_single_neuron")
        # When another test errors, the lock might remain, and all locking tests fail
        self.nest_adapter_0.delete_lock()
        self.nest_adapter_1 = self.scaffold.create_adapter(
            "test_single_neuron")
        self.nest_adapter_2 = self.scaffold.create_adapter(
            "test_single_neuron")
        self.nest_adapter_multi_1 = self.scaffold.create_adapter(
            "test_single_neuron")
        self.nest_adapter_multi_1.enable_multi("first")
        self.nest_adapter_multi_1b = self.scaffold.create_adapter(
            "test_single_neuron")
        self.nest_adapter_multi_1b.enable_multi("first")
        self.nest_adapter_multi_2 = self.scaffold.create_adapter(
            "test_single_neuron")
        self.nest_adapter_multi_2.enable_multi("second")

    def tearDown(self):
        # Clean up any remaining locks to keep the test functions independent.
        # Otherwise a chain reaction of failures is evoked.
        self.nest_adapter_0.delete_lock()

    def test_single_instance_unwanted_usage(self):
        # Test AdapterError when trying to unlock unlocked adapter
        self.assertRaises(AdapterError, self.nest_adapter_0.release_lock)
        # Test whether the scaffold throws an AdapterError when the same
        # adapter is prepared twice.
        self.nest_adapter_0.prepare()
        self.assertRaises(AdapterError, self.nest_adapter_0.prepare)

        self.nest_adapter_0.release_lock()
        self.nest_adapter_0.reset()

    def test_single_instance_single_lock(self):
        self.nest_adapter_1.reset()
        # Lock kernel. Prepare adapter and thereby lock NEST kernel
        self.nest_adapter_1.prepare()
        lock_data = self.nest_adapter_1.read_lock()
        self.assertEqual(lock_data["multi"], False)
        self.assertEqual(self.nest_adapter_1.multi, False)
        self.assertEqual(self.nest_adapter_1.has_lock, True)

        # Release lock.
        self.nest_adapter_1.release_lock()
        self.assertEqual(self.nest_adapter_1.read_lock(), None)
        self.assertEqual(self.nest_adapter_1.has_lock, False)
        self.nest_adapter_1.reset()

    def test_multi_instance_single_lock(self):
        # Test that a 2nd single-instance adapter can't manage a locked kernel.
        self.nest_adapter_1.prepare()

        self.assertRaises(KernelLockedError, self.nest_adapter_2.prepare)
        self.assertEqual(self.nest_adapter_2.is_prepared, False)

        self.nest_adapter_1.release_lock()
        self.nest_adapter_1.reset()
        self.nest_adapter_2.reset()

    def test_single_instance_multi_lock(self):
        self.nest_adapter_multi_1.reset()
        # Test functionality of the multi lock.
        self.nest_adapter_multi_1.prepare()
        lock_data = self.nest_adapter_multi_1.read_lock()
        self.assertEqual(self.nest_adapter_multi_1.suffix, "first")
        self.assertEqual(lock_data["multi"], True)
        self.assertEqual(lock_data["suffixes"][0],
                         self.nest_adapter_multi_1.suffix)
        self.assertEqual(self.nest_adapter_multi_1.multi, True)
        self.assertEqual(self.nest_adapter_multi_1.is_prepared, True)
        self.assertEqual(self.nest_adapter_multi_1.has_lock, True)

        self.nest_adapter_multi_1.release_lock()
        self.assertEqual(self.nest_adapter_multi_1.multi, True)
        self.assertEqual(self.nest_adapter_multi_1.has_lock, False)
        self.nest_adapter_multi_1.reset()
        self.assertEqual(self.nest_adapter_multi_1.is_prepared, False)

    def test_multi_instance_multi_lock(self):
        # Test functionality of the multi lock.
        self.nest_adapter_multi_1.prepare()
        # Test that we have 1 lock.
        lock_data = self.nest_adapter_multi_1.read_lock()
        # Check multi instance multi lock
        self.nest_adapter_multi_2.cell_models["test_cell"].parameters[
            "t_ref"] = 3.0
        self.nest_adapter_multi_2.prepare()
        # Check that we have 2 locks
        lock_data = self.nest_adapter_multi_1.read_lock()
        self.assertEqual(len(lock_data["suffixes"]), 2)

        # Test that we set the right parameters on the right classes.
        try:
            params = self.nest.GetDefaults("test_cell_first")
        except Exception as e:
            self.fail("First suffixed NEST models not found")
        try:
            params = self.nest.GetDefaults("test_cell_second")
        except Exception as e:
            self.fail("Second suffixed NEST models not found")

        # Test that the adapters have the correct nest_identifiers
        id1 = self.nest_adapter_multi_1.cell_models[
            "test_cell"].nest_identifiers
        id2 = self.nest_adapter_multi_2.cell_models[
            "test_cell"].nest_identifiers
        self.assertEqual(id1, [1, 2, 3, 4])
        self.assertEqual(id2, [5, 6, 7, 8])

        # Test that the adapter nodes have the right model
        self.assertTrue(
            all(
                map(
                    lambda x: str(x["model"]) == "test_cell_first",
                    self.nest.GetStatus(id1),
                )))
        self.assertTrue(
            all(
                map(
                    lambda x: str(x["model"]) == "test_cell_second",
                    self.nest.GetStatus(id2),
                )))

        # Test that the adapter nodes have the right differential parameter t_ref
        self.assertTrue(
            all(map(lambda x: x["t_ref"] == 1.5, self.nest.GetStatus(id1))))
        self.assertTrue(
            all(map(lambda x: x["t_ref"] == 3.0, self.nest.GetStatus(id2))))

        # Check duplicate suffixes
        self.assertRaises(SuffixTakenError, self.nest_adapter_multi_1b.prepare)

        self.nest_adapter_multi_1.release_lock()
        self.nest_adapter_multi_1.reset()
        # Test that we have 1 lock again after release.
        lock_data = self.nest_adapter_multi_1.read_lock()
        self.assertEqual(lock_data["suffixes"][0], "second")
        self.nest_adapter_multi_2.release_lock()
        self.nest_adapter_multi_2.reset()