예제 #1
0
    def test_enumeration_values(self):
        self.assertEqual(
            self.test_obj.get_attribute("E-mapping-good",
                                        Selection([(0, 1), (2, 3)])).tolist(),
            ["C", "C"])

        self.assertEqual(
            self.test_obj.get_attribute("E-mapping-good",
                                        Selection([(0, 1), (2, 3)])).tolist(),
            ["C", "C"])

        self.assertEqual(
            self.test_obj.get_enumeration("E-mapping-good",
                                          Selection([(0, 1),
                                                     (2, 3)])).tolist(),
            [2, 2])

        self.assertEqual(self.test_obj.enumeration_values("E-mapping-good"),
                         ["A", "B", "C"])

        self.assertRaises(SonataError, self.test_obj.get_attribute,
                          "E-mapping-bad", 1)

        self.assertRaises(SonataError, self.test_obj.get_enumeration, "attr-X",
                          0)
예제 #2
0
    def test_dtypes(self):
        for dtype in (
                np.uint,
                np.uint16,
                np.uint32,
                np.uint64,
        ):
            self.assertEqual(
                Selection(np.array([1, 0, 1, 2],
                                   dtype=dtype)).flatten().tolist(),
                [1, 0, 1, 2])

        # these work due to forcecast
        for dtype in (
                np.int,
                np.int16,
                np.int32,
                np.int64,
        ):
            self.assertEqual(
                Selection(np.array([1, 0, 1, 2],
                                   dtype=dtype)).flatten().tolist(),
                [1, 0, 1, 2])

        # these fail due to being negative node ids
        self.assertRaises(SonataError, Selection, [-1, 0, 1, 2])
        self.assertRaises(SonataError, Selection, [1, 0, -1, 2])
        Selection(((0, 1), (1, 2)))
        self.assertRaises(SonataError, Selection, ((-2, -1), (1, 2)))

        self.assertRaises(SonataError, Selection, [[-1, 1], [3, 4], [5, 6]])
예제 #3
0
 def test_basic(self):
     ranges = [(3, 5), (0, 3)]
     selection = Selection(ranges)
     self.assertTrue(selection)
     self.assertEqual(selection.ranges, ranges)
     self.assertEqual(selection.flat_size, 5)
     self.assertEqual(selection.flatten().tolist(), [3, 4, 0, 1, 2])
예제 #4
0
    def test_union_intersection(self):
        empty = Selection([])
        self.assertEqual(empty, empty & empty)
        self.assertEqual(empty, empty | empty)

        even = Selection(list(range(0, 10, 2)))
        odd = Selection(list(range(1, 10, 2)))
        self.assertEqual(empty, empty & even)
        self.assertEqual(even, empty | even)
        self.assertEqual(empty, odd & even)
        self.assertEqual(Selection(list(range(10))), odd | even)
예제 #5
0
    def test_comparison(self):
        empty = Selection([])
        range_selection = Selection(((0, 2), (3, 4)))
        values_selection = Selection([1, 3, 4, 1])

        self.assertEqual(empty, empty)
        self.assertNotEqual(empty, range_selection)
        self.assertNotEqual(empty, values_selection)

        self.assertEqual(range_selection, range_selection)
        self.assertNotEqual(range_selection, values_selection)

        self.assertEqual(values_selection, values_selection)

        values_selection1 = Selection([1, 3, 4, 1])
        self.assertEqual(values_selection, values_selection1)
예제 #6
0
 def test_from_values(self):
     values = [
         [1, 3, 4, 1],
         [0, 0, 0, 0],
     ]
     expected = [(1, 2), (3, 5), (1, 2)]
     self.assertEqual(Selection(values[0]).ranges, expected)
     self.assertEqual(
         Selection(np.array(values, dtype=np.uint64, order='C')[0]).ranges,
         expected)
     self.assertEqual(
         Selection(np.array(values, dtype=np.uint32, order='C')[0]).ranges,
         expected)
     self.assertEqual(
         Selection(np.array(values, dtype=np.uint64, order='F')[0]).ranges,
         expected)
예제 #7
0
 def test_BasicScalarAnded(self):
     j = '''{"NodeSet0": {"E-mapping-good": "C",
                          "attr-Y": [21, 22]
                         }
         }'''
     sel = NodeSets(j).materialize("NodeSet0", self.population)
     self.assertEqual(sel, Selection(((0, 1), )))
예제 #8
0
    def test_get_attribute(self):
        self.assertEqual(self.test_obj.get_attribute('attr-X', 0), 11.)
        self.assertEqual(
            self.test_obj.get_attribute('attr-X', Selection([0, 5])).tolist(),
            [11., 16.])

        # different dtypes
        self.assertEqual(self.test_obj.get_attribute('attr-Y', 0), 21)
        self.assertEqual(self.test_obj.get_attribute('attr-Z', 0), 'aa')

        # default value
        self.assertEqual(
            self.test_obj.get_attribute('attr-X', Selection([0, 5]),
                                        42.).tolist(), [11., 16.])

        self.assertRaises(SonataError, self.test_obj.get_attribute,
                          'no-such-attribute', 0)
예제 #9
0
 def test_NodeSetCompound(self):
     j = '''{"NodeSet0": { "node_id": [1] },
             "NodeSet1": { "node_id": [2] },
             "NodeSetCompound0": ["NodeSet0", "NodeSet1"],
             "NodeSetCompound1": ["NodeSetCompound0", "NodeSet2"],
             "NodeSet2": { "node_id": [3] }
     }'''
     sel = NodeSets(j).materialize("NodeSetCompound1", self.population)
     self.assertEqual(sel, Selection(((1, 4), )))
예제 #10
0
 def test_BasicScalarEnum(self):
     sel = NodeSets(
         '{ "NodeSet0": { "E-mapping-good": "C" } }').materialize(
             "NodeSet0", self.population)
     self.assertEqual(sel, Selection((
         (0, 1),
         (2, 3),
         (4, 6),
     )))
예제 #11
0
    def test_from_values(self):
        values = [
            [1, 3, 4, 1],
            [0, 0, 0, 0],
        ]
        expected = [(1, 2), (3, 5), (1, 2)]
        self.assertEqual(Selection(values[0]).ranges, expected)
        self.assertEqual(
            Selection(np.array(values, dtype=np.uint64, order='C')[0]).ranges,
            expected)
        self.assertEqual(
            Selection(np.array(values, dtype=np.uint32, order='C')[0]).ranges,
            expected)
        self.assertEqual(
            Selection(np.array(values, dtype=np.uint64, order='F')[0]).ranges,
            expected)

        self.assertRaises(ValueError, Selection,
                          np.zeros((3, 3, 3), dtype=np.uint64))

        self.assertRaises(SonataError, Selection, ((2, 1), (1, 2)))
예제 #12
0
    def test_get_dynamics_attribute(self):
        self.assertEqual(self.test_obj.get_dynamics_attribute('dparam-X', 0),
                         1011.)
        self.assertEqual(
            self.test_obj.get_dynamics_attribute('dparam-X',
                                                 Selection([0, 5])).tolist(),
            [1011., 1016.])

        # different dtypes
        self.assertEqual(self.test_obj.get_dynamics_attribute('dparam-Y', 0),
                         1021)
        self.assertEqual(self.test_obj.get_dynamics_attribute('dparam-Z', 0),
                         'd-aa')

        # default value
        self.assertEqual(
            self.test_obj.get_dynamics_attribute('dparam-X', Selection([0, 5]),
                                                 42.).tolist(), [1011., 1016.])

        self.assertRaises(SonataError, self.test_obj.get_dynamics_attribute,
                          'no-such-attribute', 0)
예제 #13
0
    def test_BasicScalarPopulation(self):
        sel = NodeSets(
            '{ "NodeSet0": { "population": "nodes-A" } }').materialize(
                "NodeSet0", self.population)
        self.assertEqual(sel, self.population.select_all())

        sel = NodeSets('{ "NodeSet0": { "population": ["nodes-A",  "FAKE"] } }'
                       ).materialize("NodeSet0", self.population)
        self.assertEqual(sel, self.population.select_all())

        sel = NodeSets(
            '{ "NodeSet0": { "population": "NOT_A_POP" } }').materialize(
                "NodeSet0", self.population)
        self.assertEqual(sel, Selection([]))
예제 #14
0
 def test_BasicScalarString(self):
     sel = NodeSets(
         '{ "NodeSet0": { "attr-Z": ["aa", "cc"] } }').materialize(
             "NodeSet0", self.population)
     self.assertEqual(sel, Selection(((0, 1), (2, 3))))
예제 #15
0
 def test_from_values_empty(self):
     self.assertFalse(Selection([]))
     self.assertFalse(Selection(np.array([], dtype=np.uint64)))
예제 #16
0
 def test_from_ranges(self):
     Selection(((0, 1), (1, 2)))
     self.assertRaises(SonataError, Selection, ((2, 1), (1, 2)))
예제 #17
0
 def test_BasicScalarNodeId(self):
     sel = NodeSets('{ "NodeSet0": { "node_id": [1, 3, 5] } }').materialize(
         "NodeSet0", self.population)
     self.assertEqual(sel, Selection(((1, 2), (3, 4), (5, 6))))
예제 #18
0
 def test_BasicScalarInt(self):
     sel = NodeSets('{ "NodeSet0": { "attr-Y": 21 } }').materialize(
         "NodeSet0", self.population)
     self.assertEqual(sel, Selection(((0, 1), )))
예제 #19
0
 def test_target_nodes(self):
     self.assertEqual(self.test_obj.target_node(1), 2)
     self.assertEqual(
         self.test_obj.target_nodes(Selection([0, 1, 2, 4])).tolist(),
         [1, 2, 1, 0])
예제 #20
0
 def test_source_nodes(self):
     self.assertEqual(self.test_obj.source_node(1), 1)
     self.assertEqual(
         self.test_obj.source_nodes(Selection([0, 1, 2, 4])).tolist(),
         [1, 1, 2, 3])