Exemplo n.º 1
0
 def setUp(self):
     self.sampler = Sampler(DataUtil.createTestManager(), 'episodes')
     self.sampler.addSamplerPool(SamplerPool('testPool1', 3))
     self.sampler.addSamplerPool(SamplerPool('testPool2', 1))
     self.sampler.addSamplerPool(SamplerPool('testPool3', 523))
     self.sampler.addSamplerPool(SamplerPool('testPool4', 0))
     self.sampler2 = Sampler(DataUtil.createTestManager(), 'episodes')
     self.sampler2.addSamplerPool(SamplerPool('testPool5', 3))
Exemplo n.º 2
0
 def setUp(self):
     self.sampler = SequentialSampler(DataUtil.createTestManager(),
                                      'episodes', 'steps')
     self.sampler.addSamplerPool(SamplerPool('testPool1', 0))
     self.sampler.addSamplerPool(SamplerPool('testPool2', 7))
     self.sampler.addSamplerPool(SamplerPool('testPool3', 2))
     self.sampler.addSamplerPool(SamplerPool('testPool4', 11))
     self.sampler2 = SequentialSampler(DataUtil.createTestManager(),
                                       'episodes', 'steps')
     self.sampler2.addSamplerPool(SamplerPool('testPool5', 9))
Exemplo n.º 3
0
    def test_setgetDataEntryAlias(self):
        dataManager = DataUtil.createTestManager()
        dataManager.addDataAlias('statesAlias', [('states', ...)])
        dataManager.addDataAlias('pcAlias', [('parameters', ...),
                                             ('contexts', ...)])

        data = dataManager.createDataObject([10, 20, 30])

        self.assertTrue('statesAlias' in dataManager.getAliasNames()
                        and 'pcAlias' in dataManager.getAliasNames())

        data.setDataEntry('states', [], np.ones((200, 1)))

        self.assertTrue((data.getDataEntry('statesAlias', []) == np.ones(
            (200, 1))).all())

        data.setDataEntry('statesAlias', [], np.zeros((200, 1)))

        self.assertTrue((data.getDataEntry('states', []) == np.zeros(
            (200, 1))).all())

        data.setDataEntry('parameters', [], np.ones((10, 5)))
        data.setDataEntry('contexts', [], np.ones((10, 1)))

        self.assertTrue((data.getDataEntry('pcAlias', []) == np.ones(
            (10, 6))).all())

        data.setDataEntry('pcAlias', slice(0, 10), np.zeros((10, 6)))

        self.assertTrue((data.getDataEntry('parameters', []) == np.zeros(
            (10, 5))).all())
        self.assertTrue((data.getDataEntry('contexts', []) == np.zeros(
            (10, 1))).all())
Exemplo n.º 4
0
    def test_get_alias_alias_data(self):
        # reading and manipulating data from an alias that points to another
        # alias
        dataManager = DataUtil.createTestManager()
        dataManager.addDataAlias('alias1', [('parameters', slice(2, 5))])
        dataManager.addDataAlias('alias2', [('parameters', slice(0, 2)),
                                            ('alias1', ...)])

        myData = dataManager.createDataObject([3, 5, 10])

        alias1 = myData.getDataEntry('alias1')
        alias1[0] = [2, 3, 4]
        alias1[2] = [2, 3, 4]
        alias1[2][0] = 22
        myData.setDataEntry('alias1', [], alias1)

        self.assertEqual(myData.dataStructure['parameters'][0][0], 0)
        self.assertEqual(myData.dataStructure['parameters'][0][2], 2)
        self.assertEqual(myData.dataStructure['parameters'][0][4], 4)
        self.assertEqual(myData.dataStructure['parameters'][1][4], 0)
        self.assertEqual(myData.dataStructure['parameters'][2][4], 4)
        self.assertEqual(myData.dataStructure['parameters'][2][2], 22)

        alias2 = myData.getDataEntry('alias2')
        alias2[1] = [0, 1, 2, 3, 4]
        myData.setDataEntry('alias2', [], alias2)

        self.assertEqual(myData.dataStructure['parameters'][1][0], 0)
        self.assertEqual(myData.dataStructure['parameters'][1][3], 3)
        self.assertEqual(myData.dataStructure['parameters'][1][4], 4)
        self.assertEqual(myData.dataStructure['parameters'][0][0], 0)
        self.assertEqual(myData.dataStructure['parameters'][2][4], 4)
Exemplo n.º 5
0
    def test_resolveEntryPath(self):
        manager = DataUtil.createTestManager()
        data = manager.createDataObject([10, 20, 30])

        self.assertTrue(['parameters'] == data._resolveEntryPath('parameters'))
        self.assertTrue(['contexts'] == data._resolveEntryPath('contexts'))
        self.assertTrue(
            ['steps', 'states'] == data._resolveEntryPath('states'))
        self.assertTrue(['steps', 'subSteps', 'subActions'] ==
                        data._resolveEntryPath('subActions'))

        data.setDataEntry(['parameters'], [...], np.zeros((10, 5)))
        data.setDataEntry('parameters', [...], np.ones((10, 5)))
        self.assertTrue((data.getDataEntry(['parameters'], [...]) == np.ones(
            (10, 5))).all())
        self.assertTrue((data.getDataEntry('parameters', [...]) == np.ones(
            (10, 5))).all())

        data.setDataEntry(['steps', 'actions'], [0, ...], np.zeros((20, 2)))
        data.setDataEntry('actions', [0, ...], np.ones((20, 2)))
        self.assertTrue((data.getDataEntry(['steps', 'actions'],
                                           [0, ...]) == np.ones(
                                               (20, 2))).all())
        self.assertTrue((data.getDataEntry('actions', [0, ...]) == np.ones(
            (20, 2))).all())

        data.setDataEntry(['steps', 'subSteps', 'subStates'], [0, 0, ...],
                          np.zeros((30, 1)))
        data.setDataEntry('subStates', [0, 0, ...], np.ones((30, 1)))
        self.assertTrue((data.getDataEntry(['steps', 'subSteps', 'subStates'],
                                           [0, 0, ...]) == np.ones(
                                               (30, 1))).all())
        self.assertTrue((data.getDataEntry('subStates',
                                           [0, 0, ...]) == np.ones(
                                               (30, 1))).all())
Exemplo n.º 6
0
    def test_isDataEntry(self):
        dataManager = DataUtil.createTestManager()

        self.assertTrue(dataManager.isDataEntry('parameters'))
        self.assertTrue(dataManager.isDataEntry('contexts'))
        self.assertTrue(dataManager.isDataEntry('subActions'))
        self.assertFalse(dataManager.isDataEntry('notaparameter'))
Exemplo n.º 7
0
    def test_setRange(self):
        dataManager = DataUtil.createTestManager()
        dataManager.setRange('parameters', -1337 * np.ones(5),
                             1337 * np.ones(5))

        self.assertTrue(
            (dataManager.getMaxRange('parameters') == 1337 * np.ones(5)).all())

        self.assertTrue((dataManager.getMinRange('parameters') == -1337 *
                         np.ones(5)).all())

        self.assertRaises(ValueError, dataManager.setRange, 'parameters',
                          -1337 * np.ones(3), 1337 * np.ones(5))

        self.assertRaises(ValueError, dataManager.setRange, 'parameters',
                          -1337 * np.ones(5), 1337 * np.ones(3))

        self.assertRaises(ValueError, dataManager.setRange, 'parameters',
                          -1337 * np.ones(3), 1337 * np.ones(3))

        self.assertRaises(ValueError, dataManager.setRange, 'notaparameter',
                          -1337 * np.ones(5), 1337 * np.ones(5))

        dataManager.finalize()

        self.assertRaises(RuntimeError, dataManager.setRange, 'parameters',
                          -1337 * np.ones(5), 1337 * np.ones(5))
Exemplo n.º 8
0
    def test_getNumElements(self):
        manager = DataUtil.createTestManager()
        data = manager.createDataObject([3, 4, 5])

        self.assertEqual(data.getNumElements(), 3)
        self.assertEqual(data.getNumElements('states'), 12)
        self.assertEqual(data.getNumElements('subActions'), 60)
        self.assertEqual(data.getNumElements('contexts'), 3)
Exemplo n.º 9
0
    def test_getSubDataStructure(self):
        dataManager = DataUtil.createTestManager()
        data = dataManager.createDataObject([10, 20, 30])

        subData1 = data.dataStructure.getSubdataStructures(...)
        subData2 = data.dataStructure.getSubdataStructures([slice(0, 5), ...])
        self.assertRaises(ValueError, data.dataStructure.getSubdataStructures,
                          [..., 10, ...])

        self.assertEqual(len(subData1), 10)
        self.assertEqual(len(subData2), 100)
Exemplo n.º 10
0
    def testDataFromSettings(self):
        dataManager = DataUtil.createTestManager()
        dataManager.addOptionalDataEntry('optionalEntry', False, 2,
                                         np.array([[-1, -1]]),
                                         np.array([[5, 5]]))

        data = dataManager.createDataObject([10, 20, 30])
        dataManager.settings.setProperty('optionalEntry', np.array([[-2, -2]]))
        optionalEntry = data.getDataEntry('optionalEntry')
        optionalEntryTrue = np.tile(np.array([[-2, -2]]), (10, 1))
        self.assertTrue((optionalEntryTrue == optionalEntry).all())
Exemplo n.º 11
0
    def testNativeAccess(self):
        dataManager = DataUtil.createTestManager()

        data = dataManager.createDataObject([10, 20, 30])

        data.dataManager
        dummy = data[1].states

        data[2].states = dummy + 1

        self.assertTrue((data[2].states == dummy + 1).all())
Exemplo n.º 12
0
    def test_getElementNames(self):
        dataManager = DataUtil.createTestManager()
        self.assertEqual(sorted(dataManager.getEntryNames()), [
            'actions', 'contexts', 'parameters', 'states', 'subActions',
            'subStates'
        ])

        dataManager = DataManager('testDM')
        dataManager.addDataEntry('a', 5)
        dataManager.addDataEntry('b', 10)
        self.assertEqual(sorted(dataManager.getEntryNames()),
                         sorted(['a', 'b']))
Exemplo n.º 13
0
    def test_decorator_operator(self):
        dataManager = DataUtil.createTestManager()

        testDecorator = TestDecorator2(dataManager, 'contexts')
        testDecorator2 = TestDecorator2(dataManager, 'parameters')

        data = dataManager.createDataObject(10)
        context = np.array(range(0, 10))
        context.resize(10, 1)
        data.setDataEntry('contexts', ..., context)

        data >> testDecorator.sampleParameters >> data

        self.assertTrue((data.getDataEntry('parameters') == np.ones(
            (10, 5))).all())
        data[slice(0, 5)] >> testDecorator.sampleParameters2 >> data

        self.assertTrue((data.getDataEntry('parameters',
                                           slice(0, 5)) == np.ones(
                                               (5, 5)) * 2).all())
        self.assertTrue((data.getDataEntry('parameters',
                                           slice(5, 10)) == np.ones(
                                               (5, 5))).all())

        testArray = np.array([[3., 3., 3., 3., 3.], [4., 4., 4., 4., 4.],
                              [5., 5., 5., 5., 5.], [6., 6., 6., 6., 6.],
                              [7., 7., 7., 7., 7.]])

        testArray2 = data[slice(
            0, 5)] >> testDecorator.sampleParametersFromContext >= data
        self.assertTrue((testArray2 == testArray).all())

        data[slice(0, 5)] >> testDecorator.sampleParametersFromContext >> data
        self.assertTrue((data.getDataEntry('parameters',
                                           slice(0, 5)) == testArray).all())

        # Now test using a simpl function as data manipulation function
        @DataFunction(inputArguments='contexts', outputArguments='parameters')
        def dummyFunction(contexts):
            return np.ones((contexts.shape[0], 5)) * 4 + contexts

        data[...] >> dummyFunction >> data
        self.assertTrue((data.getDataEntry('parameters',
                                           slice(0,
                                                 5)) == testArray + 1).all())

        @DataFunction(inputArguments='contexts', outputArguments='parameters')
        def dummyFunction2(contexts):
            return np.ones((contexts.shape[0], 1)) * 4

        self.assertRaises(ValueError, data.__rshift__, dummyFunction2)
Exemplo n.º 14
0
    def test_getReserveStorage(self):
        dataManager = DataUtil.createTestManager()
        data = dataManager.createDataObject([10, 20, 30])

        self.assertEqual(data.getNumElementsForDepth(2), 6000)

        data.reserveStorage([5, 10, 10])
        self.assertEqual(data.getNumElementsForDepth(2), 500)

        data.reserveStorage([20, 30], ...)
        self.assertEqual(data.getNumElementsForDepth(2), 3000)

        data.reserveStorage([50], [1, 2])
        self.assertEqual(data.getNumElementsForDepth(2), 3000 + 20)
Exemplo n.º 15
0
    def test_getNumDimensions(self):
        dataManager = DataUtil.createTestManager()

        self.assertEqual(dataManager.getNumDimensions('parameters'), 5)
        self.assertEqual(dataManager.getNumDimensions('contexts'), 1)
        self.assertEqual(dataManager.getNumDimensions(['parameters']), 5)
        self.assertEqual(
            dataManager.getNumDimensions(['parameters', 'contexts']), 6)
        self.assertEqual(dataManager.getNumDimensions('states'), 1)
        self.assertEqual(dataManager.getNumDimensions('subStates'), 1)
        self.assertEqual(
            dataManager.getNumDimensions(
                ['parameters', 'contexts', 'states', 'subStates']), 8)

        self.assertRaises(ValueError, dataManager.getNumDimensions, 'none')
Exemplo n.º 16
0
    def test_getMaxRange(self):
        dataManager = DataUtil.createTestManager()
        self.assertTrue(
            (dataManager.getMaxRange('parameters') == 100 * np.ones(5)).all())

        self.assertTrue(
            (dataManager.getMaxRange(['parameters',
                                      'states']) == 100 * np.ones(6)).all())

        self.assertRaises(ValueError, dataManager.getMaxRange, 'none')

        brokenAlias = DataAlias('alias', [('none', ...)], 0)
        dataManager.dataAliases['alias'] = brokenAlias

        self.assertRaises(ValueError, dataManager.getMaxRange, 'alias')
Exemplo n.º 17
0
    def test_getNumElementsForIndex(self):
        manager = DataUtil.createTestManager()
        data = manager.createDataObject([3, 4, 5])

        self.assertEqual(data.getNumElementsForIndex(0), 3)
        self.assertEqual(data.getNumElementsForIndex(0, [...]), 3)
        self.assertEqual(data.getNumElementsForIndex(1), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [..., ...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [..., slice(0, 2)]), 6)
        self.assertEqual(
            data.getNumElementsForIndex(1,
                                        [slice(2, 3), slice(0, 2)]), 2)
        self.assertEqual(data.getNumElementsForIndex(2), 60)
        self.assertEqual(
            data.getNumElementsForIndex(2, ...),
            60)  # test implicit cast to array of second argument (deprecated)
        self.assertEqual(data.getNumElementsForIndex(2, [...]),
                         60)  # this should be the standart way
        self.assertEqual(data.getNumElementsForIndex(2, [..., ...]), 60)
        self.assertEqual(data.getNumElementsForIndex(2, [..., ..., ...]), 60)
        self.assertEqual(
            data.getNumElementsForIndex(2, [..., ..., slice(0, 2)]), 24)
        self.assertEqual(
            data.getNumElementsForIndex(
                2, [..., slice(1, 4), slice(0, 2)]), 18)
        self.assertEqual(
            data.getNumElementsForIndex(
                2, [slice(0, 1), slice(2, 3),
                    slice(0, 2)]), 2)

        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        dataManager.subDataManager = subDataManager
        dataManager.addDataEntry('parameters', 5)
        dataManager.addDataAlias('pAlias', [('parameters', ...)])
        dataManager.addDataEntry('contexts', 2)
        data = dataManager.createDataObject([3, 4, 5])

        self.assertEqual(data.getNumElementsForIndex(0), 3)
        self.assertEqual(data.getNumElementsForIndex(0, [...]), 3)
        self.assertEqual(data.getNumElementsForIndex(1), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [..., ...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [slice(2, 3)]), 4)
Exemplo n.º 18
0
    def test_completeLayerIndex(self):
        dataManager = DataUtil.createTestManager()
        data = dataManager.createDataObject([10, 20, 30])

        self.assertEqual(data.completeLayerIndex(0, ...), [slice(0, 10, None)])

        self.assertEqual(
            data.completeLayerIndex(2, [..., slice(0, 5)]),
            [slice(0, 10, None),
             slice(0, 5, None),
             slice(0, 30, None)])

        self.assertEqual(
            data.completeLayerIndex(2, ...),
            [slice(0, 10, None),
             slice(0, 20, None),
             slice(0, 30, None)])

        self.assertEqual(
            data.completeLayerIndex(1, [..., ..., ...]),
            [slice(0, 10, None), slice(0, 20, None)])
Exemplo n.º 19
0
 def test_getElementNamesLocal(self):
     dataManager = DataUtil.createTestManager()
     self.assertEqual(sorted(dataManager.getEntryNamesLocal()),
                      ['contexts', 'parameters'])
Exemplo n.º 20
0
    def test_assert(self):
        dataManager = DataUtil.createTestManager()
        data = dataManager.createDataObject([10, 20, 30])

        self.assertRaises(ValueError, data.setDataEntry, 'parameters', ...,
                          None)
Exemplo n.º 21
0
 def setUp(self):
     dataManager = DataUtil.createTestManager()
     self.ians = NumStepsTerminationFunction(dataManager, None, 40)
Exemplo n.º 22
0
 def setUp(self):
     self.dataManager = DataUtil.createTestManager()
     self.sampler = IndependentSampler(self.dataManager, 'episodes')
Exemplo n.º 23
0
 def test_createSamples(self):  #TODO
     data = DataUtil.createTestManager().createDataObject(1)
     self.assertRaises(NotImplementedError, self.sampler.createSamples,
                       data)
Exemplo n.º 24
0
 def test_getDataEntryDepth(self):
     dataManager = DataUtil.createTestManager()
     self.assertEqual(dataManager.getDataEntryLevel('contexts'), 0)
     self.assertEqual(dataManager.getDataEntryLevel('states'), 1)
     self.assertEqual(dataManager.getDataEntryLevel('subActions'), 2)
     self.assertRaises(ValueError, dataManager.getDataEntryLevel, 'none')
Exemplo n.º 25
0
 def test_getDataAlias_non_existing(self):
     dataManager = DataUtil.createTestManager()
     self.assertRaises(ValueError, dataManager.getDataAlias, 'none')
Exemplo n.º 26
0
 def test_set_data_entry_int(self):
     manager = DataUtil.createTestManager()
     data = manager.createDataObject([10, 20, 30])
     data.setDataEntry('parameters', 1, np.ndarray((5)))
Exemplo n.º 27
0
 def test_init(self):
     self.assertIsInstance(self.ians, NumStepsTerminationFunction)
     # TODO: check linkProperty
     self.assertEqual(self.ians.numTimeSteps, 40)
     datamngr = DataUtil.createTestManager()
Exemplo n.º 28
0
 def test_setParameterPolicy(self):
     datamngr = DataUtil.createTestManager()
     function = Function(datamngr, [], [])
     self.sampler.setParameterPolicy(function)
Exemplo n.º 29
0
 def test_setContextSampler(self):
     datamngr = DataUtil.createTestManager()
     function = Function(datamngr, [], [])
     self.sampler.setContextSampler(function)
Exemplo n.º 30
0
 def test_getDataObject_twice(self):
     dataManager = DataUtil.createTestManager()
     myData = dataManager.createDataObject([10, 5, 1])
     myData = dataManager.createDataObject([3, 1, 2])