Ejemplo n.º 1
0
    def test_value(self):
        # values can be set at creation.
        dat = vfl.Datum(y=3.125)
        self.assertEqual(dat.y, 3.125)

        # values are mutable.
        dat.y = -6.25
        self.assertEqual(dat.y, -6.25)

        # non-float values are not allowed.
        with self.assertRaises(TypeError):
            dat.y = 'foo'

        # non-float values are not allowed.
        with self.assertRaises(TypeError):
            dat = vfl.Datum(y='bar')
Ejemplo n.º 2
0
 def test_defaults(self):
     # Datum should initialize its properties to sane values.
     dat = vfl.Datum()
     self.assertEqual(dat.dims, 0)
     self.assertEqual(dat.output, 0)
     self.assertEqual(dat.y, 0.0)
     self.assertEqual(dat.x, [])
Ejemplo n.º 3
0
    def test_augment_from_datum(self):
        # Data accept single observations at creation.
        dat = vfl.Data(datum=vfl.Datum(x=[1, 0], y=-2, output=1))
        self.assertEqual(len(dat), 1)
        self.assertEqual(dat.dims, 2)
        self.assertEqual([d.y for d in dat], [-2])

        # Data accept single observations through augment().
        dat.augment(datum=vfl.Datum(x=[-1, 0], y=-3, output=2))
        self.assertEqual(len(dat), 2)
        self.assertEqual(dat.dims, 2)
        self.assertEqual([d.y for d in dat], [-2, -3])

        # dimensionalities of new observations must match the Data
        with self.assertRaises(RuntimeError):
            dat.augment(datum=vfl.Datum(x=[3]))
Ejemplo n.º 4
0
    def test_dims(self):
        # dimensionalities track the input location size.
        dat = vfl.Datum(x=[3])
        self.assertEqual(dat.dims, 1)

        # dimensionalities are immutable.
        with self.assertRaises(AttributeError):
            dat.dims = 10
Ejemplo n.º 5
0
    def test_output(self):
        # output indices can be set at creation.
        dat = vfl.Datum(output=2)
        self.assertEqual(dat.output, 2)

        # output indices are mutable.
        dat.output = 3
        self.assertEqual(dat.output, 3)

        # negative output indices are not allowed.
        with self.assertRaises(OverflowError):
            dat.output = -1

        # negative output indices are not allowed.
        with self.assertRaises(OverflowError):
            dat = vfl.Datum(output=-2)

        # non-integer output indices are not allowed.
        with self.assertRaises(TypeError):
            dat.output = 'foo'
Ejemplo n.º 6
0
    def test_sequence(self):
        # Datum is a sequence.
        dat = vfl.Datum(x=[1, -2, 3, -4])
        self.assertEqual(len(dat), 4)
        self.assertEqual(list(dat), [1, -2, 3, -4])

        # sequence elements are mutable.
        dat[1] = -222
        dat[2] = 333
        self.assertEqual(list(dat), [1, -222, 333, -4])

        # sequence elements must be floats.
        with self.assertRaises(TypeError):
            dat[3] = 'baz'
Ejemplo n.º 7
0
    def test_input(self):
        # input locations can be set at creation.
        dat = vfl.Datum(x=(1, 2, 3))
        self.assertEqual(dat.x, [1, 2, 3])

        # input locations are mutable.
        dat.x = [4, 5]
        self.assertEqual(len(dat), 2)
        self.assertEqual(dat.dims, 2)
        self.assertEqual(dat.x, [4, 5])

        # non-float-sequence input locations are not allowed.
        with self.assertRaises(TypeError):
            dat.x = 'foo'
Ejemplo n.º 8
0
    def test_sequence(self):
        # Data is a sequence.
        dat = vfl.Data(grid=[[1, 1, 3]], outputs=[0, 1])
        self.assertEqual(len(dat), 6)
        self.assertEqual([d.output for d in dat], [0, 0, 0, 1, 1, 1])
        self.assertEqual([d.x[0] for d in dat], [1, 2, 3, 1, 2, 3])

        # sequence elements are mutable, but data are re-sorted.
        dat[2] = vfl.Datum(output=2, x=[5], y=0)
        self.assertEqual([d.output for d in dat], [0, 0, 1, 1, 1, 2])
        self.assertEqual([d.x[0] for d in dat], [1, 2, 1, 2, 3, 5])

        # sequence elements must be Datum objects.
        with self.assertRaises(TypeError):
            dat[2] = 'baz'
Ejemplo n.º 9
0
 def test_empty_sequence(self):
     # Datum sequences default to empty (as does 'x')
     dat = vfl.Datum()
     self.assertEqual(len(dat), 0)
     with self.assertRaises(IndexError):
         z = dat[0]